.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/gallery_examples/02-geometry/00-keypoints.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_02-geometry_00-keypoints.py>`
to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_examples_gallery_examples_02-geometry_00-keypoints.py:
.. _ref_basic-geometry-keypoints:
Keypoints
---------
This example shows how to create basic geometry
using keypoints commands.
This section is focused on creating keypoints.
.. GENERATED FROM PYTHON SOURCE LINES 13-25
.. code-block:: default
import numpy as np
from ansys.mapdl.core import launch_mapdl
# start MAPDL and enter the pre-processing routine
mapdl = launch_mapdl()
mapdl.clear()
mapdl.prep7()
print(mapdl)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
Product: Ansys Mechanical Enterprise
MAPDL Version: 22.2
ansys.mapdl Version: 0.64.1
.. GENERATED FROM PYTHON SOURCE LINES 26-30
APDL Command: K
~~~~~~~~~~~~~~~
Create a single keypoint at ``[0, 0, 0]``. Note that the first
value is an empty string to allow MAPDL to pick the keypoint number.
.. GENERATED FROM PYTHON SOURCE LINES 30-34
.. code-block:: default
k0 = mapdl.k("", 0, 0, 0)
print(k0)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
1
.. GENERATED FROM PYTHON SOURCE LINES 35-36
Create keypoint at (10, 11, 12) while specifying the keypoint number.
.. GENERATED FROM PYTHON SOURCE LINES 36-40
.. code-block:: default
k1 = mapdl.k(2, 1, 0, 0)
print(k1)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
2
.. GENERATED FROM PYTHON SOURCE LINES 41-44
APDL Command: KBETW
~~~~~~~~~~~~~~~~~~~
Create keypoint between two keypoints
.. GENERATED FROM PYTHON SOURCE LINES 44-48
.. code-block:: default
k2 = mapdl.kbetw(kp1=k0, kp2=k1)
print(k2)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
3
.. GENERATED FROM PYTHON SOURCE LINES 49-53
APDL Command: KCENTER
~~~~~~~~~~~~~~~~~~~~~
Create keypoint at the center of a circular arc defined by three locations.
Note that we first clear mapdl before generating this geometry
.. GENERATED FROM PYTHON SOURCE LINES 53-62
.. code-block:: default
mapdl.clear()
mapdl.prep7()
k0 = mapdl.k("", 0, 1, 0)
k1 = mapdl.k("", 1, 0, 0)
k2 = mapdl.k("", 0, -1, 0)
k3 = mapdl.kcenter("KP", k0, k1, k2)
print([k0, k1, k2, k3])
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[1, 2, 3, 4]
.. GENERATED FROM PYTHON SOURCE LINES 63-67
Keypoint IDs
~~~~~~~~~~~~
Return an array of the keypoint IDs
Note that this matches the array ``[k0, k1, k2, k3]`` (due to sorting)
.. GENERATED FROM PYTHON SOURCE LINES 67-70
.. code-block:: default
knum = mapdl.geometry.knum
knum
.. rst-class:: sphx-glr-script-out
.. code-block:: none
array([1, 2, 3, 4], dtype=int32)
.. GENERATED FROM PYTHON SOURCE LINES 71-74
Keypoint Coordinates
~~~~~~~~~~~~~~~~~~~~
Return an array of the keypoint locations
.. GENERATED FROM PYTHON SOURCE LINES 74-78
.. code-block:: default
kloc = mapdl.geometry.keypoints
kloc
.. rst-class:: sphx-glr-script-out
.. code-block:: none
array([[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, -1.00000000e+00, 0.00000000e+00],
[ 2.23711432e-17, 2.22044605e-16, 0.00000000e+00]])
.. GENERATED FROM PYTHON SOURCE LINES 79-83
APDL Command: KDIST
~~~~~~~~~~~~~~~~~~~
Calculate the distance between two keypoints. Note that you could
compute this yourself from ``kloc``
.. GENERATED FROM PYTHON SOURCE LINES 83-87
.. code-block:: default
dist = mapdl.kdist(k0, k1)
dist
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[1.414213562, 1.0, -1.0, 0.0]
.. GENERATED FROM PYTHON SOURCE LINES 88-96
Keypoint Selection
~~~~~~~~~~~~~~~~~~
There are two approaches for selecting keypoints, the old "legacy"
style and the new style. The old style is valuable for those who
are comfortable with the existing MAPDL commands, and new style is
useful for selecting keypoints in a pythonic manner.
This example generates a series of random keypoints and selects them
.. GENERATED FROM PYTHON SOURCE LINES 96-107
.. code-block:: default
mapdl.clear()
mapdl.prep7()
# create 20 random keypoints
for _ in range(20):
mapdl.k("", *np.random.random(3))
# Print the keypoint numbers
print(mapdl.geometry.knum)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
.. GENERATED FROM PYTHON SOURCE LINES 108-109
Select every other keypoint with the old style command.
.. GENERATED FROM PYTHON SOURCE LINES 109-113
.. code-block:: default
mapdl.ksel("S", "KP", "", 1, 20, 2)
print(mapdl.geometry.knum)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[ 1 3 5 7 9 11 13 15 17 19]
.. GENERATED FROM PYTHON SOURCE LINES 114-118
Select every other keypoint with the new style command.
Note that the item IDs are 1 based in MAPDL, while Python ranges are
0 based.
.. GENERATED FROM PYTHON SOURCE LINES 118-122
.. code-block:: default
mapdl.geometry.keypoint_select(range(1, 21, 2))
print(mapdl.geometry.knum)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[ 1 3 5 7 9 11 13 15 17 19]
.. GENERATED FROM PYTHON SOURCE LINES 123-130
Select keypoints from a list
Note that you can ``return_selected`` if you want to see what you
have selected. This is helpful when reselecting from existing
areas.
Note that you could use a numpy array here as well.
.. GENERATED FROM PYTHON SOURCE LINES 130-134
.. code-block:: default
items = mapdl.geometry.keypoint_select([1, 5, 10, 20], return_selected=True)
print(items)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[ 1 5 10 20]
.. GENERATED FROM PYTHON SOURCE LINES 135-141
APDL Command: KPLOT
~~~~~~~~~~~~~~~~~~~
Plot the keypoints while displaying the keypoint numbers
There are a variety of plotting options available for all the common
plotting methods.
.. GENERATED FROM PYTHON SOURCE LINES 141-148
.. code-block:: default
mapdl.kplot(
show_keypoint_numbering=True,
background="black",
show_bounds=True,
font_size=26,
)
.. image-sg:: /examples/gallery_examples/02-geometry/images/sphx_glr_00-keypoints_001.png
:alt: 00 keypoints
:srcset: /examples/gallery_examples/02-geometry/images/sphx_glr_00-keypoints_001.png
:class: sphx-glr-single-img
.. GENERATED FROM PYTHON SOURCE LINES 149-152
Stop mapdl
~~~~~~~~~~
.. GENERATED FROM PYTHON SOURCE LINES 152-153
.. code-block:: default
mapdl.exit()
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.866 seconds)
.. _sphx_glr_download_examples_gallery_examples_02-geometry_00-keypoints.py:
.. only:: html
.. container:: sphx-glr-footer sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: 00-keypoints.py <00-keypoints.py>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: 00-keypoints.ipynb <00-keypoints.ipynb>`
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_