Load a STEP file

Use the pychrono.cascade module to load a STEP file saved from a CAD. We provide the .step file in the data/ directory for this example.

Uses PyChrono.

Learn how to:

  • load a STEP file, saved from a 3D CAD.
  • fetch parts from the STEP document and conver into Chrono bodies.
1 #------------------------------------------------------------------------------
2 # Name: pychrono example
3 # Purpose:
4 #
5 # Author: Alessandro Tasora
6 #
7 # Created: 1/01/2019
8 # Copyright: (c) ProjectChrono 2019
9 #------------------------------------------------------------------------------
10 
11 import pychrono.core as chrono
12 import pychrono.irrlicht as chronoirr
13 import pychrono.cascade as cascade
14 try:
15  from OCC.Core import TopoDS
16 except:
17  from OCC import TopoDS
18 print ("Example: Load a STEP file, generated by a CAD");
19 
20 # Change this path to asset path, if running from other working dir.
21 # It must point to the data folder, containing GUI assets (textures, fonts, meshes, etc.)
22 chrono.SetChronoDataPath("../../../data/")
23 
24 
25 # ---------------------------------------------------------------------
26 #
27 # Create the simulation system and add items
28 #
29 
30 mysystem = chrono.ChSystemNSC()
31 
32 # Load a STEP file, containing a mechanism. The demo STEP file has been
33 # created using a 3D CAD (in this case, SolidEdge v.18).
34 #
35 
36 # Create the ChCascadeDoc, a container that loads the STEP model
37 # and manages its subassembles
38 mydoc = cascade.ChCascadeDoc()
39 
40 
41 # load the STEP model using this command:
42 load_ok = mydoc.Load_STEP(chrono.GetChronoDataPath() + "cascade/assembly.stp"); # or specify abs.path: ("C:\\data\\cascade\\assembly.stp");
43 
44 # print the contained shapes
45 #mydoc.Dump(chrono.GetLog())
46 
47 
48 # In most CADs the Y axis is horizontal, but we want it vertical.
49 # So define a root transformation for rotating all the imported objects.
50 rotation1 = chrono.ChQuaternionD()
51 rotation1.Q_from_AngAxis(-chrono.CH_C_PI / 2, chrono.ChVectorD(1, 0, 0)); # 1: rotate 90° on X axis
52 rotation2 = chrono.ChQuaternionD()
53 rotation2.Q_from_AngAxis(chrono.CH_C_PI, chrono.ChVectorD(0, 1, 0)); # 2: rotate 180° on vertical Y axis
54 tot_rotation = chrono.ChQuaternionD()
55 tot_rotation = rotation2 % rotation1 # rotate on 1 then on 2, using quaternion product
56 root_frame = chrono.ChFrameMovingD(chrono.ChVectorD(0, 0, 0), tot_rotation);
57 
58 # Retrieve some sub shapes from the loaded model, using
59 # the GetNamedShape() function, that can use path/subpath/subsubpath/part
60 # syntax and * or ? wildcards, etc.
61 
62 mrigidBody1 = 0
63 mrigidBody2 = 0
64 
65 if load_ok:
66 
67  shape1 = TopoDS.TopoDS_Shape()
68  if (mydoc.GetNamedShape(shape1, "Assem1/body1")):
69 
70  mbody1 = mydoc.CreateBodyFromShape(shape1,1000, False, True)
71  # or: mbody1 = cascade.ChBodyEasyCascade(shape1, 1000, False, True)
72  mysystem.Add(mbody1)
73 
74  mbody1.SetBodyFixed(True)
75 
76  # Move the body as for global displacement/rotation (also mbody1 %= root_frame; )
77  mbody1.ConcatenatePreTransformation(root_frame);
78 
79  mrigidBody1= mbody1;
80 
81  else:
82  print("Warning. Desired object not found in document \n")
83 
84  shape2 = TopoDS.TopoDS_Shape()
85  if (mydoc.GetNamedShape(shape2, "Assem1/body2")):
86 
87  mbody2 = mydoc.CreateBodyFromShape(shape2,1000, False, True)
88  # or: mbody2 = cascade.ChBodyEasyCascade(shape2, 1000, False, True)
89  mysystem.Add(mbody2)
90 
91  # Move the body as for global displacement/rotation (also mbody2 %= root_frame; )
92  mbody2.ConcatenatePreTransformation(root_frame);
93 
94  mrigidBody2= mbody2;
95 
96  else:
97  print("Warning. Desired object not found in document \n")
98 
99 else:
100  print("Warning. Desired STEP file could not be opened/parsed \n")
101 
102 # Create a revolute joint between the two parts
103 # as in a pendulum. We assume we already know in advance
104 # the aboslute position of the joint (ex. we used measuring tools in the 3D CAD)
105 
106 measured_joint_pos_mm = chrono.ChVectorD(0, 48, 120);
107 
108 scale = 1. / 1000. # because we use meters instead of mm
109 
110 joint_pos = chrono.ChVectorD(root_frame.TransformPointLocalToParent(measured_joint_pos_mm * scale))
111  # transform because we rotated everything
112 
113 if (mrigidBody1 and mrigidBody2):
114  my_link = chrono.ChLinkLockRevolute()
115  my_link.Initialize(mrigidBody1, mrigidBody2, chrono.ChCoordsysD(joint_pos));
116  mysystem.Add(my_link);
117 
118 
119 # Create a large cube as a floor.
120 
121 mfloor = chrono.ChBodyEasyBox(1, 0.2, 1, 1000)
122 mfloor.SetPos(chrono.ChVectorD(0,-0.3,0))
123 mfloor.SetBodyFixed(True)
124 mysystem.Add(mfloor)
125 
126 mcolor = chrono.ChColorAsset(0.3, 0.3, 0.8)
127 mfloor.AddAsset(mcolor)
128 
129 
130 
131 # ---------------------------------------------------------------------
132 #
133 # Create an Irrlicht application to visualize the system
134 #
135 
136 myapplication = chronoirr.ChIrrApp(mysystem, 'Test', chronoirr.dimension2du(1024,768))
137 
138 myapplication.AddTypicalSky()
139 myapplication.AddTypicalLogo(chrono.GetChronoDataPath() + 'logo_pychrono_alpha.png')
140 myapplication.AddTypicalCamera(chronoirr.vector3df(0.2,0.2,-0.2))
141 myapplication.AddTypicalLights()
142 
143  # ==IMPORTANT!== Use this function for adding a ChIrrNodeAsset to all items
144  # in the system. These ChIrrNodeAsset assets are 'proxies' to the Irrlicht meshes.
145  # If you need a finer control on which item really needs a visualization proxy in
146  # Irrlicht, just use application.AssetBind(myitem); on a per-item basis.
147 
148 myapplication.AssetBindAll();
149 
150  # ==IMPORTANT!== Use this function for 'converting' into Irrlicht meshes the assets
151  # that you added to the bodies into 3D shapes, they can be visualized by Irrlicht!
152 
153 myapplication.AssetUpdateAll();
154 
155 
156 # ---------------------------------------------------------------------
157 #
158 # Run the simulation
159 #
160 
161 
162 myapplication.SetTimestep(0.01)
163 
164 
165 while(myapplication.GetDevice().run()):
166  myapplication.BeginScene()
167  myapplication.DrawAll()
168  myapplication.DoStep()
169  myapplication.EndScene()
170 
171 
172 
173 
174