Examples#

Example 01 - Using hm and hm.entities module#

Model setup using HyperMesh Python API#
 1# ---Imports-----------
 2import hm
 3import hm.entities as ent
 4
 5
 6def main():
 7    # Path to the model file
 8    modelFile = "C:/DemoModel.hm"
 9
10    # Starting a New session and loading model
11    session = hm.Session()
12    model = hm.Model(session.get_all_models()[0])
13    model.readfile(modelFile, 0)
14
15    # Material Creation
16    mat1 = ent.Material(model)
17    mat1.name = "Steel"
18    mat1.cardimage = "MAT1"
19    mat1.E = 21e04
20    mat1.Nu = 0.3
21    mat1.Rho = 7850
22
23    # Property Creation and Material Assignment to Property
24    prop1 = ent.Property(model)
25    prop1.name = "Prop"
26    prop1.cardimage = "PSOLID"
27    prop1.materialid = mat1
28
29    # Assign Properties to Component with id=1
30    compID = 1
31    comp1 = ent.Component(model, compID)
32    comp1.property = prop1
33
34    # Create Tetramesh
35    node_col = hm.Collection(model, ent.Node, populate=False)
36    str_array1 = hm.hwStringList(
37        [
38            "pars: upd_shell fix_comp_bdr post_cln elem_order = 2 delaunay el2comp=3 fill_void=1 tet_clps='0.100000,0.300000, 0.500000, 1.000000, 0.380000, 0.100000'",
39            "tet: 35 1.3 -1 0.014 0.8 0 0 1",
40            "2d: 1 0 4 0.01 0.001 30 1",
41        ]
42    )
43    solids_col = hm.Collection(model, ent.Solid)
44    model.tetmesh(solids_col, 1, node_col, 5, str_array1)
45
46    # Apply constraints to specific nodes
47    indNode = ent.Node(model, 1462)  # Independent Node for rigidlink
48    nodelist = (
49        list(range(1420, indNode.id))
50        + list(range(indNode.id + 1, 1642))
51        + list(range(74051, 74064))
52        + list(range(74214, 74300))
53        + list(range(74356, 74369))
54        + list(range(74980, 75066))
55    )
56
57    # Dependent Node for rigidlink
58    filtNode = hm.FilterByEnumeration(ent.Node, nodelist)
59    depnodes = hm.Collection(model, filtNode)
60    model.rigidlink(indNode, depnodes, 123456)  # rigid link creation
61    indNode_col = hm.Collection(model, ent.Node, f"id={indNode.id}")
62    model.loadcreateonentity_curve(indNode_col, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
63
64    # Creating a Load collection with the name "Pressure" and we apply it on selected elements
65    loadcol2 = ent.Loadcol(model)
66    loadcol2.name = "Pressure"
67
68    elemes = hm.CollectionByInteractiveSelection(model, ent.Element)
69    filt = hm.FilterByCollection(ent.Node, ent.Element, [""])
70    nele = hm.Collection(model, filt, elemes)
71
72    model.pressuresonentity_curve(elemes, nele, 0, 0, -1, 1, 30, 1, 0, 0, 0, 0, 0)
73
74    # We customise the load step
75    loadStep1 = ent.Loadstep(model)
76    loadStep1.OS_TYPE = 1  # Linear Static
77    loadStep1.OS_SPCID = model.get(hm.Entity(ent.Loadcol, 1))
78    loadStep1.OS_LOADID = loadcol2
79
80    # Two options for our geometry. Translation of elements or rotation
81    opt = int(
82        input("Enter Option: 1 for Translate Elements, 2 for Component Rotation: ")
83    )
84    if opt == 1:
85        # Translate Element
86        tranElem = hm.CollectionByInteractiveSelection(model, ent.Element)
87        model.translatemark(tranElem, [0.0, 1.0, 0.0], 0.5)
88    elif opt == 2:
89        # Rotate Component
90        comp1 = hm.Collection(model, ent.Component, "id=1")
91        RotAx = [0.0, 0.0, 1.0]
92        PlaneOr = [0.0, 0.0, 0.0]
93        model.rotatemark(comp1, RotAx, PlaneOr, 30)
94
95
96if __name__ == "__main__":
97    main()

Example 02 - Querying the properties of a model#

Query the property entities and for selected elements query their property, material, and jacobian#
  1import hm
  2import hm.entities as ent
  3import prettytable as pt
  4
  5
  6def main():
  7
  8    model = hm.Model()
  9
 10    # Create a collection of all properties in the model
 11    prop_data = list()
 12    props = hm.Collection(model, ent.Property)
 13
 14    # Printing a table of properties and their respective material if exist
 15    for p in props:
 16        prop_name = p.name
 17        prop_id = p.id
 18        prop_cardimage = p.cardimage
 19        if prop_cardimage == "PSHELL":
 20            prop_thickness = p.PSHELL_T
 21        else:
 22            prop_thickness = ""
 23        if p.materialid:
 24            mat = p.materialid
 25            mat_name = mat.name
 26            mat_id = mat.id
 27            mat_cardimage = mat.cardimage
 28        else:
 29            mat_name = "-"
 30            mat_id = "-"
 31            mat_cardimage = "-"
 32        prop_data.append(
 33            [
 34                prop_name,
 35                prop_id,
 36                prop_cardimage,
 37                prop_thickness,
 38                mat_name,
 39                mat_id,
 40                mat_cardimage,
 41            ]
 42        )
 43
 44    tab = pt.PrettyTable(
 45        [
 46            "Prop Name",
 47            "Prop ID",
 48            "Prop Cardimage",
 49            "Prop Thickness",
 50            "Mat Name",
 51            "Mat ID",
 52            "Mat Cardimage",
 53        ]
 54    )
 55
 56    tab.add_rows(prop_data)
 57    print(tab)
 58
 59    # Create a collection of elements by interactive selection
 60    elems = hm.CollectionByInteractiveSelection(model, ent.Element)
 61
 62    # Create a table of elements along with some attributes (id, configuration number, property_name, material_name, No of nodes and jacobian)
 63    elem_data = list()
 64
 65    for e in elems:
 66        el_id = e.id
 67        el_conf = e.config
 68        el_ncount = e.nodecount
 69        if e.propertyid:
 70            el_prop_name = e.propertyid
 71        else:
 72            el_prop_name = "-"
 73        if e.materialid:
 74            el_mat_name = e.materialid
 75        else:
 76            el_mat_name = "-"
 77        if e.jacobian:
 78            el_jac = e.jacobian
 79        else:
 80            el_jac = "-"
 81        elem_data.append(
 82        [
 83            el_id,
 84            el_conf,
 85            el_ncount,
 86            el_prop_name,
 87            el_mat_name,
 88            el_jac
 89        ]
 90    )
 91
 92    tab2 = pt.PrettyTable(
 93        [
 94            "Elem ID",
 95            "Elem Config",
 96            "Elem NodeCount",
 97            "Elem Prop",
 98            "Elem Mat",
 99            "Jacobian",
100        ]
101    )
102    tab2.add_rows(elem_data)
103    print(tab2)
104
105
106if __name__ == "__main__":
107    main()

Example 03 - Creating a node at CoG of every component#

For each component, assign a property and a material, and create node at the center of gravity (CoG).#
 1import hm
 2import hm.entities as ent
 3
 4
 5def main():
 6
 7    model = hm.Model()
 8
 9    # Create a material with name 'Al' and properties of Aluminum
10    mat1 = ent.Material(model)
11    mat1.name = "Al"
12    mat1.cardimage = "MAT1"
13    mat1.E = 7e04
14    mat1.Nu = 0.3
15    mat1.Rho = 2700
16
17    # Create a property with the name 'central_prop' and assign 'Al' material to it
18    prop1 = ent.Property(model)
19    prop1.name = "central_prop"
20    prop1.cardimage = "PSHELL"
21    prop1.materialid = mat1
22    prop1.PSHELL_T = 1.0
23
24    # Assign the property to all components, compute for each component the center of gravity (CoG) and a create a node to cog
25    comp_col = hm.Collection(
26        model, ent.Component
27    )
28
29    # Create a collection of all components
30    for comp in comp_col:
31        comp.propertyid = prop1
32        ccol = hm.Collection(model, ent.Component, f"id={comp.id}")
33        status, result = model.hm_getcog(ccol)
34        nodeNew = ent.Node(model)  # Create a new node
35        nodeNew.localcoordinates = result.coord  # Place the node at CoG
36
37
38if __name__ == "__main__":
39    main()

Example 04 - Creating a new component per solid (includes “performance” API)#

Create a new component for each solid while boosting the performance using the corresponding setoption options.#
 1import hm
 2import hm.entities as ent
 3
 4
 5def main():
 6
 7    model = hm.Model()
 8
 9    solidcol = hm.Collection(model, ent.Solid)
10
11    # Enable performance boost
12    setoption(model, "enabled")
13
14    # Create a component for each solid
15    for solid in solidcol:
16        comp = ent.Component(model)
17        comp.name = f"solid_{solid.id}"
18        # Organize (move) the solid to the new component
19        filterBen = hm.FilterByEnumeration(ent.Solid, list([solid.id]))
20        solid_col = hm.Collection(model, filterBen)
21        model.movemark(solid_col, name=f"solid_{solid.id}")
22
23    # Disable performance boost
24    setoption(model, "disabled")
25
26
27def setoption(model, string):
28    if string == "enabled":
29        model.setoption(
30            block_redraw=1,
31            block_messages=1,
32            block_error_messages=1,
33            command_file_state=0,
34        )
35        model.hm_blockbrowserupdate(1)
36    else:
37        model.setoption(
38            block_redraw=0,
39            block_messages=0,
40            block_error_messages=0,
41            command_file_state=1,
42        )
43        model.hm_blockbrowserupdate(0)
44
45
46if __name__ == "__main__":
47    main()