Lines

This example shows how you can use PyMAPDL to create basic geometry using Pythonic PREP7 line commands.

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)

Out:

Product:             Ansys Mechanical Enterprise
MAPDL Version:       21.2
ansys.mapdl Version: 0.59.dev0

APDL Command: L

Create a line between the two keypoints (0, 0, 0) and (1, 0, 0)

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 1, 0, 0)
lnum = mapdl.l(k0, k1)
lnum

Out:

1

APDL Command: LARC

Create a circular arc that travels between (0, 0, 0) and (1, 1, 0) with a radius of curvature of 2. Plot the result.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 1, 1, 0)
k2 = mapdl.k("", 0, 1, 0)
lnum = mapdl.larc(k0, k1, k2, 2)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.5, 0.5, 2.7320508075688776),
 (0.5, 0.5, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: L2ANG

Create two circles and join them with a line. Plot the result.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 0, 0, 1)
k2 = mapdl.k("", 0, 0, 0.5)
carc0 = mapdl.circle(k0, 1, k1)
carc1 = mapdl.circle(k2, 1, k1)
lnum = mapdl.l2ang(carc0[0], carc1[0], 90, 90)
mapdl.lplot(background='w', color='k', line_width=5)
01 lines

Out:

[(3.203613541114848, 3.203613541114848, 3.453613541114848),
 (0.0, 0.0, 0.25),
 (0.0, 0.0, 1.0)]

APDL Command: L2TAN

Create two circular arcs and connect them with a spline. Plot the result.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 0, 0, 1)
k2 = mapdl.k("", -1.5, 1.5, 0)
k3 = mapdl.k("", -1.5, 1.5, 1)
carc0 = mapdl.circle(k0, 1, k1, arc=90)
carc1 = mapdl.circle(k2, 1, k3, arc=90)
lnum = mapdl.l2tan(1, 2)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(-0.25, 1.25, 6.830127018922194),
 (-0.25, 1.25, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: LANG

Create a line from a line from (0, 0, 0) to (1, 0, 0) to a keypoint at (1, 1, 1) at an angle of 60 degrees. Plot the result.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 1, 0, 0)
lnum = mapdl.l(k0, k1)
k2 = mapdl.k("", 1, 1, 0)
lnum = mapdl.lang(lnum, k2, 60)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.5, 0.5, 2.7320508075688776),
 (0.5, 0.5, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: LAREA

Generate a line on a square between its two corners.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 1, 0, 0)
k2 = mapdl.k("", 1, 1, 0)
k3 = mapdl.k("", 0, 1, 0)
a0 = mapdl.a(k0, k1, k2, k3)
lnum = mapdl.larea(k0, k2, a0)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.50000000015, 0.50000000015, 2.7320508083884927),
 (0.50000000015, 0.50000000015, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: LCOMB

Create two lines and combine them.

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 1, 0, 0)
k2 = mapdl.k("", 2, 0, 0)
l0 = mapdl.l(k0, k1)
l1 = mapdl.l(k0, k2)
lout = mapdl.lcomb(l0, l1)

APDL Command: LDIV

Create a single line and divide it into 5 pieces.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 1, 0, 0)
l0 = mapdl.l(k0, k1)
mapdl.ldiv(l0, ndiv=5)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.5, 0.0, 1.9318516525781368),
 (0.5, 0.0, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: LEXTND

Create a circular arc and extend it at one of its keypoints.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 0, 0, 1)
carcs = mapdl.circle(k0, 1, k1, arc=90)
lnum = mapdl.lextnd(carcs[0], 3, 1)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.5, 0.0, 4.319751617610021),
 (0.5, 0.0, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: LFILLT

Create two intersecting lines at a right angle and add a fillet between them.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 0, 1, 0)
k2 = mapdl.k("", 1, 0, 0)
l0 = mapdl.l(k0, k1)
l1 = mapdl.l(k0, k2)
lnum = mapdl.lfillt(l0, l1, 0.25)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.5, 0.5, 2.7320508075688776),
 (0.5, 0.5, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: LTAN

Create a circular arc and generate a tangent spline at the end of it directed to a new keypoint.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 0, 0, 1)
k2 = mapdl.k("", -1, 1.5, 0)
carc = mapdl.circle(k0, 1, k1, arc=90)
lnum = mapdl.ltan(carc[0], k2)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.0, 0.75, 4.8296291314453415),
 (0.0, 0.75, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: SPLINE

Generate a segmented spline through 5 keypoints.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k('', 0, 0, 0)
k1 = mapdl.k('', 0.2, 0.2, 0)
k2 = mapdl.k('', 0.4, 0.3, 0)
k3 = mapdl.k('', 0.6, 0.5, 0)
k4 = mapdl.k('', 0.8, 0.3, 0)
mapdl.spline(k0, k1, k2, k3, k4)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.4, 0.25208733064211125, 1.8267923549699552),
 (0.4, 0.25208733064211125, 0.0),
 (0.0, 1.0, 0.0)]

APDL Command: BSPLIN

Generate a single line from a spline fit to a series of keypoints. Generate through (0, 0, 0), (0, 1, 0) and (1, 2, 0)

This is different than the spline method as this creates a single line rather than multiple lines.

mapdl.clear(); mapdl.prep7()

k0 = mapdl.k("", 0, 0, 0)
k1 = mapdl.k("", 0, 1, 0)
k2 = mapdl.k("", 1, 2, 0)
lnum = mapdl.bsplin(k0, k1, k2)
mapdl.lplot(background='w', color='k', line_width=5, cpos='xy')
01 lines

Out:

[(0.45194711247094027, 1.0, 4.405912813957279),
 (0.45194711247094027, 1.0, 0.0),
 (0.0, 1.0, 0.0)]

Line IDs

Return an array of the line IDs

lnum = mapdl.geometry.lnum
lnum

Out:

array([1], dtype=int32)

Line Geometry

Get the VTK PolyData containing lines. This VTK mesh can be saved or plotted. For more details, visit https://docs.pyvista.com

lines = mapdl.geometry.lines
lines
HeaderData Arrays
PolyDataInformation
N Cells1
N Points100
X Bounds-9.611e-02, 1.000e+00
Y Bounds0.000e+00, 2.000e+00
Z Bounds0.000e+00, 0.000e+00
N Arrays1
NameFieldTypeN CompMinMax
entity_numCellsint3211.000e+001.000e+00


Line Selection

There are two approaches for selecting lines, 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 lines in a pythonic manner.

This example generates a series of random lines and selects them

mapdl.clear(); mapdl.prep7()


def generate_random_line():
    k0 = mapdl.k('', *np.random.random(3))
    k1 = mapdl.k('', *np.random.random(3))
    mapdl.l(k0, k1)

# create 20 random lines
for _ in range(20):
    generate_random_line()

# Print the line numbers
print(mapdl.geometry.lnum)

Out:

[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Select every other line with the old style command.

mapdl.ksel('S', 'KP', '', 1, 20, 2)
print(mapdl.geometry.lnum)

Out:

[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Select every other line with the new style command.

Note that the item IDs are 1 based in MAPDL, while Python ranges are 0 based.

mapdl.geometry.line_select(range(1, 21, 2))
print(mapdl.geometry.lnum)

Out:

[ 1  3  5  7  9 11 13 15 17 19]

Select lines 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.line_select([1, 5, 10, 20], return_selected=True)
print(items)

Out:

[ 1  5 10 20]

APDL Command: LPLOT

Plot colored lines while displaying the keypoint numbers.

There are a variety of plotting options available for all the common plotting methods.

mapdl.lplot(show_keypoint_numbering=True,
            color_lines=True,
            show_line_numbering=False,
            background='black',
            show_bounds=True,
            line_width=5,
            cpos='xy',
            font_size=26)
01 lines

Out:

[(0.4807801619172096, 0.526522058993578, 2.8344084684086055),
 (0.4807801619172096, 0.526522058993578, 0.3730733646079898),
 (0.0, 1.0, 0.0)]

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

Gallery generated by Sphinx-Gallery