.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/gallery_examples/gallery/00_lucid_file_IO.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_gallery_examples_gallery_00_lucid_file_IO.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_gallery_examples_gallery_00_lucid_file_IO.py:


.. _ref_file_io:

==============================================================
Convert data when importing and exporting mesh and CAD formats
==============================================================

**Summary**: This example shows how mesh and geometry formats are converted
during import and export.

Objective
~~~~~~~~~~

The objective is to illustrate how data is converted and passed during import
and export of mesh and geometry.


.. image:: ../../../images/part_type_new.png
    :align: center
    :width: 800
    :alt: Part Structure


Procedure
~~~~~~~~~~
#. Launch an Ansys Prime Server instance.
#. Instantiate the meshing utilities from the ``lucid`` class.
#. Import CAD geometry and review the imported entities.
#. Generate surface mesh with a constant mesh size of 2mm.
#. Generate volume mesh using tetrahedral elements and default settings .
#. Review the entities to be exported to solvers.
#. Export the mesh file as pmdat, cdb and cas format.
#. Import the created solver files to review the entities as they are coming from the solvers.
#. Exit the PyPrimeMesh session.

.. GENERATED FROM PYTHON SOURCE LINES 61-67

Launch Ansys Prime Server
~~~~~~~~~~~~~~~~~~~~~~~~~
Import all necessary modules and
launch an instance of Ansys Prime Server.
Connect the PyPrimeMesh client and get the model.
Instantiate meshing utilities from the ``lucid`` class.

.. GENERATED FROM PYTHON SOURCE LINES 67-78

.. code-block:: Python


    import os
    import tempfile

    import ansys.meshing.prime as prime
    from ansys.meshing.prime.graphics.plotter import PrimePlotter

    prime_client = prime.launch_prime()
    model = prime_client.model
    mesh_util = prime.lucid.Mesh(model=model)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Using Ansys Prime Server from container ansys-prime-server-1




.. GENERATED FROM PYTHON SOURCE LINES 79-117

Import geometry
~~~~~~~~~~~~~~~~~~~
Download the CAD file “pyprime_block_import.fmd” and import its geometry
into PyPrimeMesh.

Display part details by printing the model. The TopoPart’s
name from model details is **pyprime_block_import**.

After import of CAD model, within the topopart the facets from the CAD
exists in the form of geom data. This can be seen in the image below.

.. image:: ../../../images/00_after_cad_import.png
  :align: center
  :width: 800
  :alt: TopoPart after import of CAD file “pyprime_block_import.fmd”.

The topology consists of the following TopoEntities , they are TopoEdges, TopoFaces
and TopoVolumes.

* TopoEdge represent the curves/edges present in the CAD.
  In this case there are **17 edges** present in SpaceClaim are imported
  as **17 TopoEdges**.

* TopoFace represent the surfaces/faces present in the CAD.
  The **8 CAD Faces** present in SpaceClaim are imported as **8 Topofaces** in
  PyPrimeMesh.

* TopoVolume represent the solid volumes present in the CAD.
  Since there is only **one solid body** in SpaceClaim, this is imported as **one Topovolume**
  in PyPrimeMesh.

Named selections or groups in the CAD become labels after import. In this example ,
the Named Selection / Group named **my_group** in Spaceclaim is imported as a label
in PyPrimeMesh.

After CAD import the solid body, surface body or an edge body  present in SCDM would be defined
as Volume Zones, Face Zones and Edge Zones in PyPrimeMesh. In the CAD model , there exist a
**single solid body named “solid”** which after import becomes as a **Volume Zone named solid**.

.. GENERATED FROM PYTHON SOURCE LINES 117-125

.. code-block:: Python


    mesh_util.read(file_name=prime.examples.download_block_model_fmd())
    # mesh_util.read(file_name=prime.examples.download_block_model_scdoc())
    print(model)
    display = PrimePlotter()
    display.plot(model)
    display.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_001.png
        :alt: 00 lucid file IO
        :srcset: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyprimemesh/pyprimemesh/doc/source/examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_001.vtksz



.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Part Summary:

    Part Name: pyprime_block_import
    Part ID: 2
        17 Topo Edges
        8 Topo Faces
        1 Topo Volumes

        0 Edge Zones
            Edge Zone Name(s) : []
        0 Face Zones
            Face Zone Name(s) : []
        1 Volume Zones
            Volume Zone Name(s) : [solid]

        1 Label(s)
            Names: [my_group]

        Bounding box (-16 0 -17)
                     (12 28 11)





.. GENERATED FROM PYTHON SOURCE LINES 126-145

Generate Mesh
~~~~~~~~~~~~~~~
The topo part currently has no mesh associated with it and contains only
geometry.

Using the Lucid API ``surface_mesh``, users can generate a conformal mesh on the topofaces.
A conformal mesh with a constant mesh size of 2mm is generated. After mesh generation, the
mesh data is available within the TopoPart.

This can be seen in the image below

.. image:: ../../../images/00_after_mesh_generation.png
  :align: center
  :width: 800
  :alt: TopoPart after mesh generation.

The mesh for a group of topo faces labeled “my_group” is displayed by defining the
label expression in the display scope. The Volume Mesh is generated keeping the volume fill
as the default meshing algorithms.

.. GENERATED FROM PYTHON SOURCE LINES 145-159

.. code-block:: Python


    mesh_util.surface_mesh(min_size=2.0)
    display = PrimePlotter()
    display.plot(model, update=True)
    display.show()

    part = model.get_part_by_name("pyprime_block_import")

    display = PrimePlotter()
    display.plot(model, prime.ScopeDefinition(model, label_expression="my_group"))
    display.show()

    mesh_util.volume_mesh()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_002.png
        :alt: 00 lucid file IO
        :srcset: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyprimemesh/pyprimemesh/doc/source/examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_002.vtksz






.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_003.png
        :alt: 00 lucid file IO
        :srcset: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyprimemesh/pyprimemesh/doc/source/examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 160-165

Export mesh as PyPrimeMesh (.pmdat) native format mesh file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PyPrimeMesh allows user to export mesh in its native format name pmdat.
This configuration allows retaining the topology data along with mesh data.


.. GENERATED FROM PYTHON SOURCE LINES 165-171

.. code-block:: Python

    temp_folder = tempfile.TemporaryDirectory()
    mesh_file_pmdat = os.path.join(temp_folder.name, "pyprime_block_mesh.pmdat")
    mesh_util.write(mesh_file_pmdat)
    assert os.path.exists(mesh_file_pmdat)
    print("\nExported file:\n", mesh_file_pmdat)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Exported file:
     /tmp/tmpa_psj9wj/pyprime_block_mesh.pmdat




.. GENERATED FROM PYTHON SOURCE LINES 172-178

Export mesh as Ansys MAPDL (.cdb) format mesh file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PyPrimeMesh allows export of mesh as Ansys MAPDL (.cdb) format mesh file. While exporting
the mesh to Ansys MAPDL, the labels present in session are converted to components
containing nodes.


.. GENERATED FROM PYTHON SOURCE LINES 178-185

.. code-block:: Python


    mesh_file_cdb = os.path.join(temp_folder.name, "pyprime_block_mesh.cdb")
    mesh_util.write(mesh_file_cdb)
    assert os.path.exists(mesh_file_cdb)
    print("\nExported file:\n", mesh_file_cdb)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    This get_abaqus_simulation_data is a beta API. The behavior and implementation may change in future.

    Exported file:
     /tmp/tmpa_psj9wj/pyprime_block_mesh.cdb




.. GENERATED FROM PYTHON SOURCE LINES 186-203

Export mesh Ansys Fluent (CAS) format mesh file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Zones in PyPrimeMesh can be defined as a collection of either topo or zonelet entities that
we can assign properties to in a solver when exported as mesh , for example "if the user
wishes to assign a material to a region of the model they can define a volume zone for
multiple topo volumes or cell zonelets so they can apply the property.

Hence while exporting the mesh as (MSH or CAS) file to the Fluent solver, the
boundary conditions for the zones needs to be defined. For this reason the topo
entities / zonelets associated with a labels are converted to volume/face/edge zones
respectively.

The property of a zone is that a zonelet or TopoEntity can only be present in a single zone.
The topo entities / zonelets that are not associated with their respective zones types are
merged together during export to Fluent formats. The topology data present is removed
automatically when export to Fluent(MSH or CAS) formats.


.. GENERATED FROM PYTHON SOURCE LINES 203-213

.. code-block:: Python


    mesh_util.create_zones_from_labels("my_group")
    print(model)

    # Export as Fluent (*.cas) format mesh file
    mesh_file_cas = os.path.join(temp_folder.name, "pyprime_block_mesh.cas")
    mesh_util.write(mesh_file_cas)
    assert os.path.exists(mesh_file_cas)
    print("\nExported file:\n", mesh_file_cas)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Part Summary:

    Part Name: pyprime_block_import
    Part ID: 2
        17 Topo Edges
        8 Topo Faces
        1 Topo Volumes

        0 Edge Zones
            Edge Zone Name(s) : []
        1 Face Zones
            Face Zone Name(s) : [my_group]
        1 Volume Zones
            Volume Zone Name(s) : [solid]

        1 Label(s)
            Names: [my_group]

        Bounding box (-16 0 -17)
                     (12 28 11)


    Exported file:
     /tmp/tmpa_psj9wj/pyprime_block_mesh.cas




.. GENERATED FROM PYTHON SOURCE LINES 214-242

Reading Ansys PyPrimeMesh native mesh file (pmdat)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Read the exported PyPrimeMesh(pmdat) native mesh format file, it is observed that
part topology contains both geom data as well as mesh data. This is seen in the image below

.. image:: ../../../images/00_import_pmdat.png
  :align: center
  :width: 800
  :alt: TopoPart after reading mesh part.

Meshed zonelets (that contain the mesh data) are only created once the topo part
is converted to a mesh part by deleting the topo entities. Here , while deleting the topology
we are deleting the geom data (face) and retaining the mesh data for solve purpose.
When deleting the topoogy , the TopoPart is converted to MeshPart and the topo entities
are converted to their respective zonelet type in MeshPart, this is shown as follows;

* **01 TopoVolumes ->  01 Cell Zonelets**
* **08 TopoFaces ->  08 Face Zonelets**
* **17 TopoEdges -> 17 Edge Zonelets**

The zones association with topoentities would change to their
corresponding equivalent zonelet type in MeshParts.

.. image:: ../../../images/00_after_delete_topology.png
  :align: center
  :width: 800
  :alt: MeshPart after deleting topology.


.. GENERATED FROM PYTHON SOURCE LINES 242-256

.. code-block:: Python


    mesh_util.read(mesh_file_pmdat, append=False)

    print(model)
    for part in model.parts:
        if len(part.get_topo_faces()) > 0:
            part.delete_topo_entities(
                prime.DeleteTopoEntitiesParams(
                    model, delete_geom_zonelets=True, delete_mesh_zonelets=False
                )
            )

    print(model)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Part Summary:

    Part Name: pyprime_block_import
    Part ID: 2
        17 Topo Edges
        8 Topo Faces
        1 Topo Volumes

        0 Edge Zones
            Edge Zone Name(s) : []
        0 Face Zones
            Face Zone Name(s) : []
        1 Volume Zones
            Volume Zone Name(s) : [solid]

        1 Label(s)
            Names: [my_group]

        Bounding box (-16 0 -17)
                     (12 28 11)

    Part Summary:

    Part Name: pyprime_block_import
    Part ID: 2
        17 Edge Zonelets
        8 Face Zonelets
        1 Cell Zonelets

        0 Edge Zones
            Edge Zone Name(s) : []
        0 Face Zones
            Face Zone Name(s) : []
        1 Volume Zones
            Volume Zone Name(s) : [solid]

        1 Label(s)
            Names: [my_group]

        Bounding box (-16 0 -17)
                     (12 28 11)





.. GENERATED FROM PYTHON SOURCE LINES 257-270

Reading Ansys Fluent (.cas) format mesh file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Read the exported Fluent format mesh file.

.. image:: ../../../images/00_import_cas.png
  :align: center
  :width: 800
  :alt: After import of cas mesh file.

It would be observed that
the zone name **my_group** is retained and the remaining face zonelets that are not
associated with a face zone(s) are merged to create a new zone named **wall**.
There are no labels present in the mesh file.

.. GENERATED FROM PYTHON SOURCE LINES 270-285

.. code-block:: Python


    mesh_util.read(mesh_file_cas, append=False)
    print(model)
    part = model.parts[0]
    for zone in part.get_face_zones():
        print(model.get_zone_name(zone))
        scope = prime.ScopeDefinition(
            model,
            evaluation_type=prime.ScopeEvaluationType.ZONES,
            zone_expression=model.get_zone_name(zone),
        )
        display = PrimePlotter()
        display.plot(model, scope, update=True)
        display.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_004.png
        :alt: 00 lucid file IO
        :srcset: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_004.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyprimemesh/pyprimemesh/doc/source/examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_004.vtksz






.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_005.png
        :alt: 00 lucid file IO
        :srcset: /examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_005.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyprimemesh/pyprimemesh/doc/source/examples/gallery_examples/gallery/images/sphx_glr_00_lucid_file_IO_005.vtksz



.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Part Summary:

    Part Name: pyprime_block_mesh
    Part ID: 2
        0 Edge Zonelets
        2 Face Zonelets
        1 Cell Zonelets

        0 Edge Zones
            Edge Zone Name(s) : []
        2 Face Zones
            Face Zone Name(s) : [wall, my_group]
        1 Volume Zones
            Volume Zone Name(s) : [solid]

        0 Label(s)
            Names: []

        Bounding box (-16 0 -17)
                     (12 28 11)

    wall
    my_group




.. GENERATED FROM PYTHON SOURCE LINES 286-288

Exit PyPrimeMesh
~~~~~~~~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 288-290

.. code-block:: Python


    prime_client.exit()








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 18.721 seconds)


.. _sphx_glr_download_examples_gallery_examples_gallery_00_lucid_file_IO.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 00_lucid_file_IO.ipynb <00_lucid_file_IO.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 00_lucid_file_IO.py <00_lucid_file_IO.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 00_lucid_file_IO.zip <00_lucid_file_IO.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_