Keypoints#

This example shows how to create basic geometry using keypoints commands.

This section is focused on creating keypoints.

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)
Product:             Ansys Mechanical Enterprise Academic Student
MAPDL Version:       24.2
ansys.mapdl Version: 0.69.dev0

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.

k0 = mapdl.k("", 0, 0, 0)
print(k0)
1

Create keypoint at (10, 11, 12) while specifying the keypoint number.

k1 = mapdl.k(2, 1, 0, 0)
print(k1)
2

APDL Command: KBETW#

Create keypoint between two keypoints

k2 = mapdl.kbetw(kp1=k0, kp2=k1)
print(k2)
3

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

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])
[1, 2, 3, 4]

Keypoint IDs#

Return an array of the keypoint IDs Note that this matches the array [k0, k1, k2, k3] (due to sorting)

knum = mapdl.geometry.knum
knum
array([1, 2, 3, 4], dtype=int32)

Keypoints geometry#

Get the VTK MultiBlock containing keypoints. This VTK mesh can be saved or plotted. For more information, visit PyVista documentation.

keypoints = mapdl.geometry.keypoints
keypoints
InformationBlocks
MultiblockValues
N Blocks4
X Bounds0.000, 1.000
Y Bounds-1.000, 1.000
Z Bounds0.000, 0.000
IndexNameType
0kp 1PolyData
1kp 2PolyData
2kp 3PolyData
3kp 4PolyData


Keypoint Coordinates#

Return an array of the keypoint locations

kloc = mapdl.geometry.get_keypoints()
kloc
HeaderData Arrays
PolyDataInformation
N Cells4
N Points4
N Strips0
X Bounds0.000e+00, 1.000e+00
Y Bounds-1.000e+00, 1.000e+00
Z Bounds0.000e+00, 0.000e+00
N Arrays1
NameFieldTypeN CompMinMax
entity_numPointsint6411.000e+004.000e+00


APDL Command: KDIST#

Calculate the distance between two keypoints. Note that you could compute this yourself from kloc

dist = mapdl.kdist(k0, k1)
dist
[1.414213562, 1.0, -1.0, 0.0]

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

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)
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Select every other keypoint with the old style command.

mapdl.ksel("S", "KP", "", 1, 20, 2)
print(mapdl.geometry.knum)
[ 1  3  5  7  9 11 13 15 17 19]

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.

mapdl.geometry.keypoint_select(range(1, 21, 2))
print(mapdl.geometry.knum)
[ 1  3  5  7  9 11 13 15 17 19]

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.

items = mapdl.geometry.keypoint_select([1, 5, 10, 20], return_selected=True)
print(items)
[ 1  5 10 20]

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.

mapdl.kplot(
    show_keypoint_numbering=True,
    background="white",
    show_bounds=True,
    font_size=26,
)
00 keypoints

Stop mapdl#

mapdl.exit()

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