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    # Grabbing a model instance by name
  8    session = hm.Session()
  9    model = hm.Model(session.get_all_models()[0])
 10
 11    # Material Creation
 12    mat1 = ent.Material(model)
 13    mat1.name = "Steel"
 14    mat1.cardimage = "MAT1"
 15    mat1.E = 21e04
 16    mat1.Nu = 0.3
 17    mat1.Rho = 7850
 18
 19    # Property Creation and Material Assignment to Property
 20    prop1 = ent.Property(model)
 21    prop1.name = "Prop"
 22    prop1.cardimage = "PSOLID"
 23    prop1.materialid = mat1
 24
 25    # Assign Properties to Component with id=1
 26    compID = 1
 27    comp1 = ent.Component(model, compID)
 28    comp1.property = prop1
 29
 30    # Create Tetramesh
 31    node_col = hm.Collection(model, ent.Node, populate=False)
 32    str_array1 = hm.hwStringList(
 33        [
 34            "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'",
 35            "tet: 35 1.3 -1 0.014 0.8 0 0 1",
 36            "2d: 1 0 4 0.01 0.001 30 1",
 37        ]
 38    )
 39    solids_col = hm.Collection(model, ent.Solid)
 40    model.tetmesh(
 41        collection1=solids_col,
 42        mode1=1,
 43        collection2=node_col,
 44        mode2=5,
 45        string_array=str_array1,
 46    )
 47
 48    # Apply constraints to specific nodes
 49    indNode = ent.Node(model, 1462)  # Independent Node for rigidlink
 50    nodelist = (
 51        list(range(1420, indNode.id))
 52        + list(range(indNode.id + 1, 1642))
 53        + list(range(74051, 74064))
 54        + list(range(74214, 74300))
 55        + list(range(74356, 74369))
 56        + list(range(74980, 75066))
 57    )
 58
 59    # Dependent Node for rigidlink
 60    filtNode = hm.FilterByEnumeration(ent.Node, nodelist)
 61    depnodes = hm.Collection(model, filtNode)
 62    model.rigidlink(
 63        independent=indNode, collection=depnodes, dofs=123456
 64    )  # rigid link creation
 65    indNode_col = hm.Collection(model, ent.Node, f"id={indNode.id}")
 66    model.loadcreateonentity_curve(
 67        collection=indNode_col,
 68        config=3,
 69        type=1,
 70        comp1=0,
 71        comp2=0,
 72        comp3=0,
 73        comp4=0,
 74        comp5=0,
 75        comp6=0,
 76        x_loc=0,
 77        y_loc=0,
 78        z_loc=0,
 79        curve_id=0,
 80        x_scale=0,
 81    )
 82
 83    # Creating a Load collection with the name "Pressure" and we apply it on selected elements
 84    loadcol2 = ent.Loadcol(model)
 85    loadcol2.name = "Pressure"
 86
 87    elemes = hm.CollectionByInteractiveSelection(model, ent.Element)
 88    filt = hm.FilterByCollection(ent.Node, ent.Element, [""])
 89    nele = hm.Collection(model, filt, elemes)
 90
 91    model.pressuresonentity_curve(
 92        collection=elemes,
 93        facenodes=nele,
 94        x_comp=0,
 95        y_comp=0,
 96        z_comp=-1,
 97        magnitude=1,
 98        breakangle=30,
 99        onface=1,
100        xlocation=0,
101        ylocation=0,
102        zlocation=0,
103        curve_id=0,
104        x_scale=0,
105    )
106
107    # Setting up the loadstep
108    loadStep1 = ent.Loadstep(model)
109    loadStep1.OS_TYPE = 1  # Linear Static
110    loadStep1.OS_SPCID = model.get(hm.Entity(ent.Loadcol, 1))
111    loadStep1.OS_LOADID = loadcol2
112
113    # Two options for our geometry. Translation of elements or rotation
114    opt = int(
115        input("Enter Option: 1 for Translate Elements, 2 for Component Rotation: ")
116    )
117    if opt == 1:
118        # Translate elements
119        tranElem = hm.CollectionByInteractiveSelection(model, ent.Element)
120        model.translatemark(collection=tranElem, vector=[0.0, 1.0, 0.0], distance=0.5)
121    elif opt == 2:
122        # Rotate component
123        comp1 = hm.Collection(model, ent.Component, "id=1")
124        RotAx = [0.0, 0.0, 1.0]
125        PlaneOr = [0.0, 0.0, 0.0]
126        model.rotatemark(
127            collection=comp1, plane_normal=RotAx, plane_normal_base=PlaneOr, angle=30
128        )
129
130
131if __name__ == "__main__":
132    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    performance(model, True)
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(collection=solid_col, name=f"solid_{solid.id}")
22
23    # Disable performance boost
24    performance(model, False)
25
26
27def performance(model, switch):
28    if switch == True:
29        hm.setoption(
30            block_redraw=1,
31            command_file_state=0,
32            entity_highlighting=0,
33        )
34        model.hm_blockbrowserupdate(mode=1)
35    else:
36        hm.setoption(
37            block_redraw=0,
38            command_file_state=1,
39            entity_highlighting=1,
40        )
41        model.hm_blockbrowserupdate(mode=0)
42
43
44if __name__ == "__main__":
45    main()