Use the OpenCascade kernel

Use the pychrono.cascade module to create a shape with the OpenCascade kernel, then let it fall on the ground. Uses PyChrono.

Learn how to:

  • use the python.cascade module
  • create collisions with concave meshes
  • control collision tolerances (envelope, margin)
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 print ("Example: create OpenCascade shapes and use them as rigid bodies");
12 
13 
14 import pychrono.core as chrono
15 import pychrono.irrlicht as chronoirr
16 import pychrono.cascade as cascade
17 try:
18  from OCC.Core import BRepPrimAPI
19  from OCC.Core import BRepAlgoAPI
20 except:
21  from OCC import BRepPrimAPI, BRepAlgoAPI
22 # Change this path to asset path, if running from other working dir.
23 # It must point to the data folder, containing GUI assets (textures, fonts, meshes, etc.)
24 chrono.SetChronoDataPath("../../../data/")
25 
26 
27 # ---------------------------------------------------------------------
28 #
29 # Create the simulation system and add items
30 #
31 
32 
33 mysystem = chrono.ChSystemNSC()
34 
35 
36 # Set the global collision margins. This is expecially important for very large or
37 # very small objects. Set this before creating shapes. Not before creating mysystem.
38 chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.001);
39 chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.001);
40 
41 
42 # create a 3dCAD shape using the OCC OpenCascade API (a torus cut by a cylinder)
43 my_torus = BRepPrimAPI.BRepPrimAPI_MakeTorus(0.1,0.02).Shape()
44 my_cylinder = BRepPrimAPI.BRepPrimAPI_MakeCylinder(0.09,0.1).Shape()
45 my_shape = BRepAlgoAPI.BRepAlgoAPI_Cut(my_torus, my_cylinder).Shape()
46 
47 # use it to make a body with proper center of mass and inertia tensor,
48 # given the CAD shape. Also visualize it.
49 my_body = cascade.ChBodyEasyCascade(my_shape,# the CAD shape
50  1000, # the density
51  True, # must collide using the triangle mesh geometry?
52  True) # must be visualized?
53 mysystem.Add(my_body)
54 
55 
56 # Create a large cube as a floor.
57 
58 my_floor = chrono.ChBodyEasyBox(1, 0.2, 1, 1000, True)
59 my_floor.SetPos(chrono.ChVectorD(0,-0.3,0))
60 my_floor.SetBodyFixed(True)
61 mysystem.Add(my_floor)
62 
63 my_color = chrono.ChColorAsset(0.2, 0.2, 0.5)
64 my_floor.AddAsset(my_color)
65 
66 
67 
68 # ---------------------------------------------------------------------
69 #
70 # Create an Irrlicht application to visualize the system
71 #
72 
73 myapplication = chronoirr.ChIrrApp(mysystem, 'Use OpenCascade shapes', chronoirr.dimension2du(1024,768))
74 
75 myapplication.AddTypicalSky(chrono.GetChronoDataPath() + 'skybox/')
76 myapplication.AddTypicalLogo(chrono.GetChronoDataPath() + 'logo_pychrono_alpha.png')
77 myapplication.AddTypicalCamera(chronoirr.vector3df(0.2,0.2,-0.2))
78 myapplication.AddTypicalLights()
79 
80  # ==IMPORTANT!== Use this function for adding a ChIrrNodeAsset to all items
81  # in the system. These ChIrrNodeAsset assets are 'proxies' to the Irrlicht meshes.
82  # If you need a finer control on which item really needs a visualization proxy in
83  # Irrlicht, just use application.AssetBind(myitem); on a per-item basis.
84 
85 myapplication.AssetBindAll();
86 
87  # ==IMPORTANT!== Use this function for 'converting' into Irrlicht meshes the assets
88  # that you added to the bodies into 3D shapes, they can be visualized by Irrlicht!
89 
90 myapplication.AssetUpdateAll();
91 
92 
93 # ---------------------------------------------------------------------
94 #
95 # Run the simulation
96 #
97 
98 mysystem.SetSolverType(chrono.ChSolver.Type_SOR)
99 
100 myapplication.SetTimestep(0.005)
101 
102 
103 while(myapplication.GetDevice().run()):
104  myapplication.BeginScene()
105  myapplication.DrawAll()
106  myapplication.DoStep()
107  myapplication.EndScene()
108 
109 
110 
111 
112