Load a system saved from SolidWorks

Import a SolidWorks scene into your PyChrono program, and simulate it.

The Chrono::Solidworks add-in can be used in SolidWorks to export an assembly into a .py file that can be later load and simulated.

Uses PyChrono.

Learn how to:

  • use the Chrono::SolidWorks Add-In for exporting a mechanical system
  • load the system in PyChrono and simulate it
  • visualize it in Irrlicht
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 
12 import pychrono as chrono
13 import pychrono.irrlicht as chronoirr
14 
15 # Change this path to asset path, if running from other working dir.
16 # It must point to the data folder, containing GUI assets (textures, fonts, meshes, etc.)
17 chrono.SetChronoDataPath("../../../data/")
18 
19 
20 # ---------------------------------------------------------------------
21 #
22 # Create the simulation system.
23 # (Do not create parts and constraints programmatically here, we will
24 # load a mechanism from file)
25 
26 my_system = chrono.ChSystemNSC()
27 
28 
29 # Set the collision margins. This is expecially important for very large or
30 # very small objects (as in this example)! Do this before creating shapes.
31 chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.001);
32 chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.001);
33 
34 
35 # ---------------------------------------------------------------------
36 #
37 # load the file generated by the SolidWorks CAD plugin
38 # and add it to the system
39 #
40 
41 print ("Loading C::E scene...");
42 
43 exported_items = chrono.ImportSolidWorksSystem(chrono.GetChronoDataPath() + "solid_works/swiss_escapement")
44 
45 print ("...done!");
46 
47 # Print exported items
48 for my_item in exported_items:
49  print (my_item.GetName())
50 
51 # Add items to the physical system
52 for my_item in exported_items:
53  my_system.Add(my_item)
54 
55 
56 # ---------------------------------------------------------------------
57 #
58 # Create an Irrlicht application to visualize the system
59 #
60 print (chrono.GetChronoDataFile("skybox/"))
61 
62 myapplication = chronoirr.ChIrrApp(my_system, 'Test: using data exported by Chrono::Solidworks', chronoirr.dimension2du(1024,768));
63 
64 myapplication.AddTypicalSky()
65 myapplication.AddTypicalLogo(chrono.GetChronoDataPath() + 'logo_pychrono_alpha.png')
66 myapplication.AddTypicalCamera(chronoirr.vector3df(0.3,0.3,0.4))
67 myapplication.AddTypicalLights()
68 
69  # ==IMPORTANT!== Use this function for adding a ChIrrNodeAsset to all items
70  # in the system. These ChIrrNodeAsset assets are 'proxies' to the Irrlicht meshes.
71  # If you need a finer control on which item really needs a visualization proxy in
72  # Irrlicht, just use application.AssetBind(myitem); on a per-item basis.
73 
74 myapplication.AssetBindAll();
75 
76  # ==IMPORTANT!== Use this function for 'converting' into Irrlicht meshes the assets
77  # that you added to the bodies into 3D shapes, they can be visualized by Irrlicht!
78 
79 myapplication.AssetUpdateAll();
80 
81 
82 # ---------------------------------------------------------------------
83 #
84 # Run the simulation
85 #
86 
87 
88 myapplication.GetSystem().SetMaxPenetrationRecoverySpeed(0.002);
89 myapplication.SetTimestep(0.002)
90 
91 
92 while(myapplication.GetDevice().run()):
93  myapplication.BeginScene()
94  myapplication.DrawAll()
95  myapplication.DoStep()
96  myapplication.EndScene()
97 
98 
99 
100 
101