.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/modeling_features/004-advanced-selection-rules.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_004-advanced-selection-rules.py>`
        to download the full example code.

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

.. _sphx_glr_examples_modeling_features_004-advanced-selection-rules.py:


.. _advanced_selection_rules_example:

Advanced selection rules
========================

This example shows how to use advanced rules, including the geometrical,
cut-off, and variable offset rules. It also demonstrates how rules can be templated
and reused with different parameters. For more basic rules, see
:ref:`basic_selection_rules_example`.

This example only shows the PyACP part of the setup. For a complete composite analysis,
see :ref:`pymapdl_workflow_example`.

.. GENERATED FROM PYTHON SOURCE LINES 40-44

Import modules
--------------

Import the standard library and third-party dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 44-50

.. code-block:: Python

    import pathlib
    import tempfile

    import numpy as np
    import pyvista








.. GENERATED FROM PYTHON SOURCE LINES 51-52

Import the PyACP dependencies.

.. GENERATED FROM PYTHON SOURCE LINES 52-63

.. code-block:: Python

    from ansys.acp.core import (
        BooleanOperationType,
        EdgeSetType,
        LinkedSelectionRule,
        PhysicalDimension,
        launch_acp,
    )
    from ansys.acp.core.extras import ExampleKeys, get_example_file










.. GENERATED FROM PYTHON SOURCE LINES 65-67

Start ACP and load the model
----------------------------

.. GENERATED FROM PYTHON SOURCE LINES 69-70

Get the example file from the server.

.. GENERATED FROM PYTHON SOURCE LINES 70-74

.. code-block:: Python

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








.. GENERATED FROM PYTHON SOURCE LINES 75-76

Launch the PyACP server and connect to it.

.. GENERATED FROM PYTHON SOURCE LINES 76-78

.. code-block:: Python

    acp = launch_acp()








.. GENERATED FROM PYTHON SOURCE LINES 79-80

Load the model from the input file.

.. GENERATED FROM PYTHON SOURCE LINES 80-84

.. code-block:: Python


    model = acp.import_model(input_file)
    print(model.unit_system)





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

 .. code-block:: none

    mks




.. GENERATED FROM PYTHON SOURCE LINES 85-87

Add more layers to the modeling ply so that it is easier to see the effects of the selection rules.
Plot the thickness of all the plies without any rules.

.. GENERATED FROM PYTHON SOURCE LINES 87-95

.. code-block:: Python


    modeling_ply = model.modeling_groups["modeling_group"].modeling_plies["ply"]
    modeling_ply.number_of_layers = 10

    model.update()
    assert model.elemental_data.thickness is not None
    model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh).plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_001.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 96-98

Parametrized Parallel Rule
--------------------------

.. GENERATED FROM PYTHON SOURCE LINES 100-103

Rules can be parametrized. This is useful when a rule is used multiple times but with different
parameters.  The :class:`.LinkedSelectionRule` class shows what parameters are available for each rule.
This code modifies the extent of the parallel rule.

.. GENERATED FROM PYTHON SOURCE LINES 105-106

Create a parallel rule.

.. GENERATED FROM PYTHON SOURCE LINES 106-114

.. code-block:: Python

    parallel_rule = model.create_parallel_selection_rule(
        name="parallel_rule",
        origin=(0, 0, 0),
        direction=(1, 0, 0),
        lower_limit=0.005,
        upper_limit=1,
    )








.. GENERATED FROM PYTHON SOURCE LINES 115-116

Assign it the modeling ply.

.. GENERATED FROM PYTHON SOURCE LINES 116-120

.. code-block:: Python


    linked_parallel_rule = LinkedSelectionRule(parallel_rule)
    modeling_ply.selection_rules = [linked_parallel_rule]








.. GENERATED FROM PYTHON SOURCE LINES 121-122

Plot the thickness of the ply before the parametrization.

.. GENERATED FROM PYTHON SOURCE LINES 122-126

.. code-block:: Python

    model.update()
    assert model.elemental_data.thickness is not None
    model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh).plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_002.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 127-129

Modify the parallel rule by changing the parameters of the linked rule.
Parameters defined on the linked rule override the parameters of the original rule.

.. GENERATED FROM PYTHON SOURCE LINES 129-133

.. code-block:: Python

    linked_parallel_rule.template_rule = True
    linked_parallel_rule.parameter_1 = 0.002
    linked_parallel_rule.parameter_2 = 0.1








.. GENERATED FROM PYTHON SOURCE LINES 134-135

Plot the thickness of the ply with the modified rule.

.. GENERATED FROM PYTHON SOURCE LINES 135-140

.. code-block:: Python

    model.update()
    assert model.elemental_data.thickness is not None
    model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh).plot(show_edges=True)









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_003.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 141-143

Create a geometrical selection rule
-----------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 145-146

Add a CAD geometry to the model.

.. GENERATED FROM PYTHON SOURCE LINES 146-154

.. code-block:: Python

    triangle_path = get_example_file(ExampleKeys.RULE_GEOMETRY_TRIANGLE, WORKING_DIR)
    triangle = model.create_cad_geometry()
    triangle.refresh(triangle_path)

    # Note: It is important to update the model here, because the root_shapes of the
    # cad_geometry are not available until the model is updated.
    model.update()








.. GENERATED FROM PYTHON SOURCE LINES 155-156

Create a virtual geometry from the CAD geometry.

.. GENERATED FROM PYTHON SOURCE LINES 156-160

.. code-block:: Python

    triangle_virtual_geometry = model.create_virtual_geometry(
        name="triangle_virtual_geometry", cad_components=triangle.root_shapes.values()
    )








.. GENERATED FROM PYTHON SOURCE LINES 161-162

Create the geometrical selection rule.

.. GENERATED FROM PYTHON SOURCE LINES 162-167

.. code-block:: Python

    geometrical_selection_rule = model.create_geometrical_selection_rule(
        name="geometrical_rule",
        geometry=triangle_virtual_geometry,
    )








.. GENERATED FROM PYTHON SOURCE LINES 168-170

Assign the geometrical selection rule to the ply. Plot the ply extent with
the outline of the geometry.

.. GENERATED FROM PYTHON SOURCE LINES 170-181

.. code-block:: Python

    modeling_ply.selection_rules = [LinkedSelectionRule(geometrical_selection_rule)]
    model.update()
    assert model.elemental_data.thickness is not None

    plotter = pyvista.Plotter()
    plotter.add_mesh(
        triangle.visualization_mesh.to_pyvista(), style="wireframe", line_width=4, color="white"
    )
    plotter.add_mesh(model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh), show_edges=True)
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_004.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_004.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 182-184

Create a cut-off selection rule
-------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 186-187

Add the cut off CAD geometry to the model.

.. GENERATED FROM PYTHON SOURCE LINES 187-195

.. code-block:: Python

    cut_off_plane_path = get_example_file(ExampleKeys.CUT_OFF_GEOMETRY, WORKING_DIR)
    cut_off_plane = model.create_cad_geometry()
    cut_off_plane.refresh(cut_off_plane_path)

    # Note: It is important to update the model here, because the root_shapes of the
    # cad_geometry are not available until the model is updated.
    model.update()








.. GENERATED FROM PYTHON SOURCE LINES 196-197

Create a virtual geometry from the CAD geometry.

.. GENERATED FROM PYTHON SOURCE LINES 197-201

.. code-block:: Python

    cut_off_virtual_geometry = model.create_virtual_geometry(
        name="cut_off_virtual_geometry", cad_components=cut_off_plane.root_shapes.values()
    )








.. GENERATED FROM PYTHON SOURCE LINES 202-203

Create the cut_off selection rule.

.. GENERATED FROM PYTHON SOURCE LINES 203-209

.. code-block:: Python

    cut_off_selection_rule = model.create_cut_off_selection_rule(
        name="cut_off_rule",
        cut_off_geometry=cut_off_virtual_geometry,
    )









.. GENERATED FROM PYTHON SOURCE LINES 210-212

Assign the cut_off selection rule to the ply. Plot the ply extent with
the outline of the geometry.

.. GENERATED FROM PYTHON SOURCE LINES 212-223

.. code-block:: Python

    modeling_ply.selection_rules = [LinkedSelectionRule(cut_off_selection_rule)]

    model.update()
    assert model.elemental_data.thickness is not None
    plotter = pyvista.Plotter()
    plotter.add_mesh(cut_off_plane.visualization_mesh.to_pyvista(), color="white")
    plotter.add_mesh(model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh))
    plotter.camera_position = [(-0.05, 0.01, 0), (0.005, 0.005, 0.005), (0, 1, 0)]

    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_005.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_005.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 224-226

Create a variable offset selection rule
---------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 228-229

Create the lookup table.

.. GENERATED FROM PYTHON SOURCE LINES 229-235

.. code-block:: Python

    lookup_table = model.create_lookup_table_1d(
        name="lookup_table",
        origin=(0, 0, 0),
        direction=(0, 0, 1),
    )








.. GENERATED FROM PYTHON SOURCE LINES 236-238

Add the location data. The "Location" column of the lookup table
is always created by default.

.. GENERATED FROM PYTHON SOURCE LINES 238-240

.. code-block:: Python

    lookup_table.columns["Location"].data = np.array([0, 0.005, 0.01])








.. GENERATED FROM PYTHON SOURCE LINES 241-242

Create the offset column that defines the offsets from the edge.

.. GENERATED FROM PYTHON SOURCE LINES 242-248

.. code-block:: Python

    offsets_column = lookup_table.create_column(
        name="offset",
        physical_dimension=PhysicalDimension.LENGTH,
        data=np.array([0.00, 0.004, 0]),
    )








.. GENERATED FROM PYTHON SOURCE LINES 249-251

Create the edge set from the "All_Elements" element set. Because you
assigned 30 degrees to the limit angle, only one edge at x=0 is selected.

.. GENERATED FROM PYTHON SOURCE LINES 251-259

.. code-block:: Python

    edge_set = model.create_edge_set(
        name="edge_set",
        edge_set_type=EdgeSetType.BY_REFERENCE,
        limit_angle=30,
        element_set=model.element_sets["All_Elements"],
        origin=(0, 0, 0),
    )








.. GENERATED FROM PYTHON SOURCE LINES 260-261

Create the variable offset rule and assign it to the ply.

.. GENERATED FROM PYTHON SOURCE LINES 261-267

.. code-block:: Python

    variable_offset_rule = model.create_variable_offset_selection_rule(
        name="variable_offset_rule", edge_set=edge_set, offsets=offsets_column, distance_along_edge=True
    )

    modeling_ply.selection_rules = [LinkedSelectionRule(variable_offset_rule)]








.. GENERATED FROM PYTHON SOURCE LINES 268-269

Plot the ply extent.

.. GENERATED FROM PYTHON SOURCE LINES 269-273

.. code-block:: Python

    model.update()
    assert model.elemental_data.thickness is not None
    model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh).plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_006.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_006.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 274-276

Create a boolean selection rule
-------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 278-283

Creating a Boolean selection rule and assigning it to a ply has the same
effect as linking the individual rules directly to the ply. Boolean rules are still useful
because they can help organize rules and make more complex ones.

Create a cylindrical selection rule to combine with the parallel rule.

.. GENERATED FROM PYTHON SOURCE LINES 283-308

.. code-block:: Python

    cylindrical_rule_boolean = model.create_cylindrical_selection_rule(
        name="cylindrical_rule",
        origin=(0.005, 0, 0.005),
        direction=(0, 1, 0),
        radius=0.002,
    )

    parallel_rule_boolean = model.create_parallel_selection_rule(
        name="parallel_rule",
        origin=(0, 0, 0),
        direction=(1, 0, 0),
        lower_limit=0.005,
        upper_limit=1,
    )

    linked_cylindrical_rule_boolean = LinkedSelectionRule(cylindrical_rule_boolean)
    linked_parallel_rule_boolean = LinkedSelectionRule(parallel_rule_boolean)

    boolean_selection_rule = model.create_boolean_selection_rule(
        name="boolean_rule",
        selection_rules=[linked_parallel_rule_boolean, linked_cylindrical_rule_boolean],
    )

    modeling_ply.selection_rules = [LinkedSelectionRule(boolean_selection_rule)]








.. GENERATED FROM PYTHON SOURCE LINES 309-310

Plot the ply extent.

.. GENERATED FROM PYTHON SOURCE LINES 310-314

.. code-block:: Python

    model.update()
    assert model.elemental_data.thickness is not None
    model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh).plot(show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_007.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_007.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 315-316

Modify the operation type of the Boolean selection rule so that the two rules are added.

.. GENERATED FROM PYTHON SOURCE LINES 316-319

.. code-block:: Python

    linked_parallel_rule_boolean.operation_type = BooleanOperationType.INTERSECT
    linked_cylindrical_rule_boolean.operation_type = BooleanOperationType.ADD








.. GENERATED FROM PYTHON SOURCE LINES 320-321

Plot the ply extent.

.. GENERATED FROM PYTHON SOURCE LINES 321-324

.. code-block:: Python

    model.update()
    assert model.elemental_data.thickness is not None
    model.elemental_data.thickness.get_pyvista_mesh(mesh=model.mesh).plot(show_edges=True)







.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_008.png
        :alt: 004 advanced selection rules
        :srcset: /examples/modeling_features/images/sphx_glr_004-advanced-selection-rules_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_004-advanced-selection-rules_008.vtksz







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

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


.. _sphx_glr_download_examples_modeling_features_004-advanced-selection-rules.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 004-advanced-selection-rules.ipynb <004-advanced-selection-rules.ipynb>`

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

      :download:`Download Python source code: 004-advanced-selection-rules.py <004-advanced-selection-rules.py>`

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

      :download:`Download zipped: 004-advanced-selection-rules.zip <004-advanced-selection-rules.zip>`


.. only:: html

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

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