Examples#
Example 01 - Using hm and hm.entities module#
Using the attached file “pedHM.hm” you can run the script commands that is following:
1#---Imports-----------
2import hm
3import hw
4import hm.mdi.entities as ent
5import time
6import os
7from hwcont import hwStringList
8
9
10
11
12def main():
13 # Names of the the model, output file and working directory
14 scriptDir = os.path.dirname(os.path.abspath(__file__))
15 modelFile = os.path.join(scriptDir,"pedHM.hm")
16
17 # Starting a New session and loading model
18 seshW = hw.Session()
19 seshW.new()
20 session = hm.Session()
21 model = hm.Model(session.get_all_models()[0])
22 model.readfile(modelFile,0)
23 model.hm_answernext('yes')
24 model.writefile("C:/Users/dtolis/OneDrive - Altair Engineering, Inc/Documents/Jobs/Open_Mini_Project_on_HM/pedHMPy.hm",0)
25
26 # Material Creation
27 mat1 = ent.Material(model)
28 mat1.name = "Steel"
29 mat1.cardimage = "MAT1"
30 mat1.E = 21e+04
31 mat1.Nu = 0.3
32 mat1.Rho = 7850
33
34 # Property Creation and Material Assignemment to Property
35 prop1 = ent.Property(model)
36 prop1.name = "DumProp"
37 prop1.cardimage = 'PSOLID'
38 prop1.materialid = mat1
39
40 # Assign Properties to Component id=1
41 compID = 1
42 comp1 = ent.Component(model,compID)
43 comp1.property = prop1
44
45 # Create Tetramesh
46 node_col = hm.Collection(model,ent.Node,populate=False)
47 str_array1 = hwStringList(["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'" ,
48"tet: 35 1.3 -1 0.014 0.8 0 0 1", "2d: 1 0 4 0.01 0.001 30 1"])
49 solids_col = hm.Collection(model,ent.Solid)
50 model.tetmesh(solids_col,1,node_col,5,str_array1)
51
52 # Apply constraints to specific nodes
53
54 indNode=ent.Node(model,1462) # Independent Node for rigidlink
55 nodelist = list(range(1420,indNode.id))+list(range(indNode.id+1,1642))+list(range(74051,74064))+list(range(74214,74300))+list(range(74356,74369))+list(range(74980,75066)) # Dependent Node for rigidlink
56 filtNode = hm.FilterByEnumeration(ent.Node,nodelist)
57 depnodes = hm.Collection(model,filtNode)
58 model.rigidlink(indNode,depnodes,123456) # rigid link creation
59 indNode_col = hm.Collection(model,ent.Node,f'id={indNode.id}')
60 model.loadcreateonentity_curve(indNode_col,3,1,0,0,0,0,0,0,0,0,0,0,0)
61
62
63 # Creating a Load collection with the name "Pressure" and we apply it on selected elements
64 loadcol2 = ent.Loadcol(model)
65 loadcol2.name = 'Pressure'
66
67 elemes=model.CreateCollectionByInteractiveSelection(ent.Element)
68 filt = hm.FilterByCollection(ent.Node,ent.Element,[''])
69 nele = hm.Collection(model,filt,elemes)
70
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. Choice is done via Python API.
81 opt = int(input('Enter Option: 1 for Translate Elements, 2 for Component Rotation: '))
82 if opt == 1:
83 # Translate Element
84 tranElem=model.CreateCollectionByInteractiveSelection(ent.Element)
85 model.translatemark(tranElem,[0.0,1.0,0.0],0.5)
86 elif opt == 2:
87 # Rotate Component
88 comp1 = hm.Collection(model,ent.Component,'id=1')
89 RotAx = [0.0,0.0,1.0]
90 PlaneOr = [0.0, 0.0, 0.0]
91 model.rotatemark(comp1,RotAx,PlaneOr,30)
92
93
94if __name__=='__main__':
95 start_time = time.time()
96 main()
97 print('---Finished at %s seconds ---' % (time.time() - start_time))
Example 02 - Querying the properties of a model#
In this example we query the properties of a model and then for the selected element we querry their properties , jacobian and materials if exist
1#---Imports-----------
2import hm
3import hw
4import hm.entities as ent
5import time
6import os
7import prettytable as pt
8
9
10
11def main():
12 # Reading the model file (.hm) and getting the handle of model
13 modelFile = r"C:\Users\dtolis\OneDrive - Altair Engineering, Inc\Documents\Jobs\Tickets\Test_Models\bumper_setvalue.hm"
14 model = hm.Model()
15 model.readfile(modelFile,0)
16
17 # Create a collection of all properties in the model
18 prop_data = list()
19 props = hm.Collection(model,ent.Property)
20
21 # Printing a table of properties and their respective material if exist
22 for p in props:
23 prop_name = p.name
24 prop_id = p.id
25 prop_cardimage = p.cardimage
26 if prop_cardimage == 'PSHELL':
27 prop_thickness = p.PSHELL_T
28 else:
29 prop_thickness = ""
30 if p.materialid:
31 mat = p.materialid
32 mat_name = mat.name
33 mat_id = mat.id
34 mat_cardimage = mat.cardimage
35 else:
36 mat_name = '-'
37 mat_id = '-'
38 mat_cardimage = '-'
39 prop_data.append([prop_name,
40 prop_id,
41 prop_cardimage,
42 prop_thickness,
43 mat_name,
44 mat_id,
45 mat_cardimage])
46 tab = pt.PrettyTable(['Prop Name','Prop ID','Prop Cardimage','Prop Thickness','Mat Name','Mat ID','Mat Cardimage'])
47 tab.add_rows(prop_data)
48 print(tab)
49
50 # Create a collection of materials by interactive selection
51 elemes=model.CreateCollectionByInteractiveSelection(ent.Element)
52 # Create a table of elements along with some attributes (id, configuration number, property_name, material_name, No of nodes and jacobian)
53 elem_data=list()
54 for e in elemes:
55 el_id = e.id
56 el_conf = e.config
57 el_ncount = e.nodecount
58 if e.propertyid:
59 el_prop_name=e.propertyid
60 else:
61 el_prop_name='-'
62 if e.materialid:
63 el_mat_name=e.materialid
64 else:
65 el_mat_name='-'
66 if e.jacobian:
67 el_jac=e.jacobian
68 else:
69 el_jac='-'
70 elem_data.append([el_id,el_conf,el_ncount,el_prop_name,el_mat_name,el_jac])
71 tab2 = pt.PrettyTable(['Elem ID','Elem Config','Elem NodeCount','Elem Prop','Elem Mat','Jacobian'])
72 tab2.add_rows(elem_data)
73 print(tab2)
74
75
76
77
78
79if __name__=='__main__':
80 start_time = time.time()
81 main()
82 print('---Finished at %s seconds ---' % (time.time() - start_time))
Example 03 - Creating Nodes and assigning them to the CoG of each component#
In this example we want to create nodes at the center of gravity (CoG) of each component. However first we need to assign to these components a property with a material in order to have a mass.
1#---Imports-----------
2import hm
3import hw
4import hm.entities as ent
5import time
6import os
7
8
9
10
11def main():
12
13 # Reading the model file (.hm) and getting the handle of model
14 modelFile = r"C:\Program Files\Altair\2023\hwdesktop\demos\hm\bumper.hm"
15 model = hm.Model()
16 model.readfile(modelFile,0)
17
18 # Create a material with name 'Al' and properties of Aluminum
19 mat1 = ent.Material(model)
20 mat1.name = "Al"
21 mat1.cardimage = "MAT1"
22 mat1.E = 7e+04
23 mat1.Nu = 0.3
24 mat1.Rho = 2700
25
26 # Create a property with the name 'central_prop' and assign 'Al' to that
27 prop1 = ent.Property(model)
28 prop1.name = "central_prop"
29 prop1.cardimage = 'PSHELL'
30 prop1.materialid = mat1
31 prop1.PSHELL_T = 1.0
32
33 # Assign the property to all components, compute for each componenet the center of gravity (cog) and a assign an node to cog
34 comp_col = hm.Collection(model,ent.Component) # Create a collection of all components
35 for comp in comp_col:
36 comp.propertyid = prop1
37 ccol = hm.Collection(model,ent.Component,f'id={comp.id}')
38 status, result = model.hm_getcog(ccol)
39 # The Pythonic way
40 nodeNew = ent.Node(model) # Create a new node
41 nodeNew.localcoordinates = result.coord # Assign node to cog
42 # Using Tcl in Python Interface
43 # hw.evalTcl(f'*createentity nodes x={result.coord[0]} y={result.coord[1]} z={result.coord[2]}')
44
45
46
47if __name__=='__main__':
48 start_time = time.time()
49 main()
50 print('---Finished at %s seconds ---' % (time.time() - start_time))
Example 04 - Creating new components from solids and affect the performance of API#
In this example we want to create components from a collection of solids. In this example using the setoption from Tcl we can boost the performance of the script.
1#---Imports-----------
2import hm
3import hw
4import hm.entities as ent
5import time
6import os
7
8
9def main():
10
11 modelFile = r"C:\Program Files\Altair\2023\hwdesktop\demos\hm\solids.hm"
12 ses=hm.Session()
13 model = hm.Model(ses.get_all_models()[0])
14 model.hm_answernext('Yes')
15 model.readfile(modelFile,0)
16
17
18 solidcol = hm.Collection(model,ent.Solid)
19
20 # A set of options for runnig the script without updating graphics.
21 setoption(model,'Enabled')
22
23 # For each solid create a component
24 for solid in solidcol:
25 comp = ent.Component(model)
26 comp.name=f'solid_{solid.id}'
27 # Make a collection of each solid
28 filterBen = hm.FilterByEnumeration(ent.Solid,list([solid.id]))
29 solid_col = hm.Collection(model,filterBen)
30 # Move solid to collector with name "solid_solid.id"
31 model.movemark(solid_col,name=f'solid_{solid.id}')
32
33 setoption(model,'Disabled')
34
35
36def setoption(model,string):
37 if string=='Enabled':
38 model.setoption(block_redraw=1,block_messages=1,block_error_messages=1,command_file_state=0)
39 # hw.evalTcl('*setoption block_redraw=1')
40 # hw.evalTcl('*setoption block_messages=1')
41 # hw.evalTcl('*setoption block_error_messages=1')
42 # hw.evalTcl('*setoption command_file_state=0')
43 model.hm_blockbrowserupdate(1)
44 else:
45 model.setoption(block_redraw=0,block_messages=0,block_error_messages=0,command_file_state=1)
46 # hw.evalTcl('*setoption block_redraw=0')
47 # hw.evalTcl('*setoption block_messages=0')
48 # hw.evalTcl('*setoption block_error_messages=0')
49 # hw.evalTcl('*setoption command_file_state=1')
50 model.hm_blockbrowserupdate(0)
51
52
53
54if __name__=='__main__':
55 start_time = time.time()
56 main()
57 print('---Finished at %s seconds ---' % (time.time() - start_time))