.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/modeling_features/031-imported-solid-model.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_modeling_features_031-imported-solid-model.py>`
        to download the full example code.

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

.. _sphx_glr_examples_modeling_features_031-imported-solid-model.py:


.. _imported_solid_model_example:

Imported solid model
====================

This example guides you through the definition of an :class:`.ImportedSolidModel`
which allows to map the layup onto an external solid mesh.
In contrast to the :class:`.SolidModel`, the raw solid mesh of
:class:`.ImportedSolidModel` is loaded from an external source, such as a CDB file.
In this example, the layup is applied onto a t-joint which consists of different
parts such as shell, stringer, and bonding skins.
The example only shows the PyACP part of the setup. For a complete composite analysis,
see :ref:`pymapdl_workflow_example`.

This example starts from an ACP model with layup. It shows how to:

- Create an :class:`.ImportedSolidModel` from an external mesh.
- Define the :class:`.LayupMappingObject` to apply the layup onto the solid mesh.
- Scope plies to specific parts of the solid mesh.
- Visualize the mapped layup.

It is recommended to look at the Ansys help for all the details. This example shows the
basic setup only.

.. GENERATED FROM PYTHON SOURCE LINES 50-51

Import the standard library and third-party dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 51-56

.. code-block:: Python

    import pathlib
    import tempfile

    import pyvista








.. GENERATED FROM PYTHON SOURCE LINES 57-58

Import the PyACP dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 58-63

.. code-block:: Python

    from ansys.acp.core import ElementTechnology, LayupMappingRosetteSelectionMethod, launch_acp
    from ansys.acp.core.extras import ExampleKeys, get_example_file










.. GENERATED FROM PYTHON SOURCE LINES 65-69

Start ACP and load the model
----------------------------
%%
Get the example file from the server.

.. GENERATED FROM PYTHON SOURCE LINES 69-73

.. code-block:: Python

    tempdir = tempfile.TemporaryDirectory()
    WORKING_DIR = pathlib.Path(tempdir.name)
    input_file = get_example_file(ExampleKeys.IMPORTED_SOLID_MODEL_ACPH5, WORKING_DIR)








.. GENERATED FROM PYTHON SOURCE LINES 74-75

Launch the PyACP server and connect to it.

.. GENERATED FROM PYTHON SOURCE LINES 75-77

.. code-block:: Python

    acp = launch_acp()








.. GENERATED FROM PYTHON SOURCE LINES 78-79

Load the model from an acph5 file

.. GENERATED FROM PYTHON SOURCE LINES 79-81

.. code-block:: Python

    model = acp.import_model(input_file)








.. GENERATED FROM PYTHON SOURCE LINES 82-87

Import external solid model
---------------------------

Get the solid mesh file and create an ImportedSolidModel,
load the initial mesh and show the raw mesh without any mapping.

.. GENERATED FROM PYTHON SOURCE LINES 87-95

.. code-block:: Python

    solid_mesh_file = get_example_file(ExampleKeys.IMPORTED_SOLID_MODEL_SOLID_MESH, WORKING_DIR)
    imported_solid_model = model.create_imported_solid_model(
        name="Imported Solid Model",
    )
    imported_solid_model.refresh(path=solid_mesh_file, format="ansys:h5")
    imported_solid_model.import_initial_mesh()
    model.solid_mesh.to_pyvista().plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_001.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 96-98

The solid element sets are used as target for the mapping later.
Here is the full list and one is visualized.

.. GENERATED FROM PYTHON SOURCE LINES 98-108

.. code-block:: Python

    imported_solid_model.solid_element_sets.keys()

    solid_eset_mesh = imported_solid_model.solid_element_sets[
        "mapping_target bonding skin right"
    ].solid_mesh
    plotter = pyvista.Plotter()
    plotter.add_mesh(solid_eset_mesh.to_pyvista())
    plotter.add_mesh(model.solid_mesh.to_pyvista(), opacity=0.2, show_edges=False)
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_002.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 109-115

Add mapping objects
-------------------

Link the layup (plies) of the top skin of the sandwich
with the corresponding named selections of the solid mesh
and show the updated solid model.

.. GENERATED FROM PYTHON SOURCE LINES 115-128

.. code-block:: Python

    solid_esets = imported_solid_model.solid_element_sets

    imported_solid_model.create_layup_mapping_object(
        name="sandwich skin top",
        element_technology=ElementTechnology.LAYERED_ELEMENT,
        shell_element_sets=[model.element_sets["els_sandwich_skin_top"]],
        entire_solid_mesh=False,
        solid_element_sets=[solid_esets["mapping_target sandwich skin top"]],
    )

    model.update()
    model.solid_mesh.to_pyvista().plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_003.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 129-130

Show the mass of the solid model elements

.. GENERATED FROM PYTHON SOURCE LINES 130-134

.. code-block:: Python

    mass_data = model.elemental_data.mass
    assert mass_data is not None
    mass_data.get_pyvista_mesh(mesh=model.solid_mesh).plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_004.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_004.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_004.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 135-136

Add other mapping objects

.. GENERATED FROM PYTHON SOURCE LINES 136-174

.. code-block:: Python

    imported_solid_model.create_layup_mapping_object(
        name="sandwich skin bottom",
        element_technology=ElementTechnology.LAYERED_ELEMENT,
        shell_element_sets=[model.element_sets["els_sandwich_skin_bottom"]],
        entire_solid_mesh=False,
        solid_element_sets=[
            imported_solid_model.solid_element_sets["mapping_target sandwich skin bottom"]
        ],
    )

    imported_solid_model.create_layup_mapping_object(
        name="stringer",
        element_technology=ElementTechnology.LAYERED_ELEMENT,
        shell_element_sets=[model.element_sets["els_stringer_skin_left"]],
        entire_solid_mesh=False,
        solid_element_sets=[
            solid_esets[v]
            for v in [
                "mapping_target stringer honeycomb",
                "mapping_target stringer skin left",
                "mapping_target stringer skin right",
            ]
        ],
    )

    imported_solid_model.create_layup_mapping_object(
        name="bonding skin",
        element_technology=ElementTechnology.LAYERED_ELEMENT,
        shell_element_sets=[
            model.element_sets[v] for v in ["els_bonding_skin_left", "els_bonding_skin_right"]
        ],
        entire_solid_mesh=False,
        solid_element_sets=[
            solid_esets[v]
            for v in ["mapping_target bonding skin left", "mapping_target bonding skin right"]
        ],
    )





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

 .. code-block:: none


    <LayupMappingObject with id 'bonding skin'>



.. GENERATED FROM PYTHON SOURCE LINES 175-176

Show intermediate result

.. GENERATED FROM PYTHON SOURCE LINES 176-179

.. code-block:: Python

    model.update()
    model.solid_mesh.to_pyvista().plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_005.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_005.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_005.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 180-182

The mapping can also be done for specific plies
as shown for the core materials.

.. GENERATED FROM PYTHON SOURCE LINES 182-216

.. code-block:: Python

    imported_solid_model.create_layup_mapping_object(
        name="foam",
        element_technology=ElementTechnology.LAYERED_ELEMENT,
        shell_element_sets=[
            model.element_sets[v] for v in ["els_foam_core_left", "els_foam_core_right"]
        ],
        select_all_plies=False,
        sequences=[model.modeling_groups["MG foam_core"]],
        entire_solid_mesh=False,
        solid_element_sets=[solid_esets["mapping_target foam core"]],
        delete_lost_elements=False,
        filler_material=model.materials["SAN Foam (81 kg m^-3)"],
        rosettes=[model.rosettes["Global Coordinate System"]],
        rosette_selection_method=LayupMappingRosetteSelectionMethod.MINIMUM_DISTANCE,
    )

    imported_solid_model.create_layup_mapping_object(
        name="honeycomb",
        element_technology=ElementTechnology.LAYERED_ELEMENT,
        shell_element_sets=[
            model.element_sets[v] for v in ["els_honeycomb_left", "els_honeycomb_right"]
        ],
        select_all_plies=False,
        sequences=[model.modeling_groups["MG honeycomb_core"]],
        entire_solid_mesh=False,
        solid_element_sets=[solid_esets["mapping_target sandwich honeycomb"]],
        delete_lost_elements=False,
        filler_material=model.materials["Honeycomb"],
        rosettes=[model.rosettes["Global Coordinate System"]],
        rosette_selection_method=LayupMappingRosetteSelectionMethod.MINIMUM_DISTANCE,
    )
    model.update()
    model.solid_mesh.to_pyvista().plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_006.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_006.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_006.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 217-219

Add filler mapping objects where the solid mesh is "filled"
with a single material. No plies from the layup are used here.

.. GENERATED FROM PYTHON SOURCE LINES 219-234

.. code-block:: Python


    imported_solid_model.create_layup_mapping_object(
        name="resin",
        element_technology=ElementTechnology.LAYERED_ELEMENT,
        shell_element_sets=[],
        entire_solid_mesh=False,
        solid_element_sets=[
            solid_esets[v] for v in ["mapping_target adhesive", "mapping_target adhesive stringer root"]
        ],
        delete_lost_elements=False,
        filler_material=model.materials["Resin Epoxy"],
        rosettes=[model.rosettes["Global Coordinate System"]],
        rosette_selection_method=LayupMappingRosetteSelectionMethod.MINIMUM_DISTANCE,
    )





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

 .. code-block:: none


    <LayupMappingObject with id 'resin'>



.. GENERATED FROM PYTHON SOURCE LINES 235-236

Show final solid mesh with mapped layup

.. GENERATED FROM PYTHON SOURCE LINES 236-240

.. code-block:: Python

    model.update()
    model.solid_mesh.to_pyvista().plot(show_edges=True)









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_007.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_007.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_007.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 241-249

Show extent and thickness of mapped plies
-----------------------------------------

Use :func:`.print_model` to get the list of plies. After identifying the ply
of interest, for example the thickness can be visualized. Note that
only ply-wise data of :class:`.AnalysisPly` can be visualized on the
solid mesh. :class:`.ProductionPly` and :class:`.ModelingPly` cannot
be visualized on the solid mesh.

.. GENERATED FROM PYTHON SOURCE LINES 249-263

.. code-block:: Python

    ap = (
        model.modeling_groups["MG bonding_skin_right"]
        .modeling_plies["ModelingPly.26"]
        .production_plies["ProductionPly.33"]
        .analysis_plies["P1L1__ModelingPly.26"]
    )
    thickness_data = ap.elemental_data.thickness
    thickness_pyvista_mesh = thickness_data.get_pyvista_mesh(mesh=ap.solid_mesh)  # type: ignore
    plotter = pyvista.Plotter()
    plotter.add_mesh(thickness_pyvista_mesh)
    plotter.add_mesh(model.solid_mesh.to_pyvista(), opacity=0.2, show_edges=False)
    plotter.show()









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_008.png
        :alt: 031 imported solid model
        :srcset: /examples/modeling_features/images/sphx_glr_031-imported-solid-model_008.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyacp/pyacp/doc/source/examples/modeling_features/images/sphx_glr_031-imported-solid-model_008.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 264-277

Other features
--------------

The :class:`.CutOffGeometry` can be used in combination witt the :class:`.ImportedSolidModel`
as well. See example :ref:`solid_model_example` for more details.
More plotting capabilities are shown in the example :ref:`solid_model_example` as well.

An example of an :class:`.ImportedSolidModel` in combination with :class:`.ImportedModelingPly`
is shown in :ref:`imported_plies_example`.

The solid mesh can be exported as CDB for MAPDL or to PyMechanical for further analysis.
These workflows are shown in :ref:`pymapdl_workflow_example` and
:ref:`pymechanical_solid_example`.


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

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


.. _sphx_glr_download_examples_modeling_features_031-imported-solid-model.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 031-imported-solid-model.ipynb <031-imported-solid-model.ipynb>`

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

      :download:`Download Python source code: 031-imported-solid-model.py <031-imported-solid-model.py>`

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

      :download:`Download zipped: 031-imported-solid-model.zip <031-imported-solid-model.zip>`


.. only:: html

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

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