Load from SolidWorks and raytrace with POVray

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

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 with POVray scripts for rendering a 3D animation
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 
13 import os
14 import pychrono as chrono
15 import pychrono.postprocess as postprocess
16 
17 
18 # Change this path to asset path, if running from other working dir.
19 # It must point to the data folder, containing GUI assets (textures, fonts, meshes, etc.)
20 chrono.SetChronoDataPath("../../../data/")
21 
22 
23 # ---------------------------------------------------------------------
24 #
25 # Create the simulation system.
26 # (Do not create parts and constraints programmatically here, we will
27 # load a mechanism from file)
28 
29 my_system = chrono.ChSystemNSC()
30 
31 
32 # Set the collision margins. This is expecially important for very large or
33 # very small objects (as in this example)! Do this before creating shapes.
34 chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.001);
35 chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.001);
36 
37 
38 # ---------------------------------------------------------------------
39 #
40 # load the file generated by the SolidWorks CAD plugin
41 # and add it to the system
42 #
43 
44 print ("Loading Chrono scene...");
45 
46 # Note that the ImportSolidWorksSystem() function requires the name of the
47 # .py file generated by the Chrono::SolidWorks plugin, but without the ".py"
48 # suffix. Here we use an example mechanism that we stored in the data/ directory,
49 # but we could also use an absolute path as ..("C:/my_path/swiss_escapement")
50 exported_items = chrono.ImportSolidWorksSystem(chrono.GetChronoDataPath() + "solid_works/swiss_escapement")
51 
52 print ("...done!");
53 
54 # Print exported items
55 for my_item in exported_items:
56  print (my_item.GetName())
57 
58 # Add items to the physical system
59 for my_item in exported_items:
60  my_system.Add(my_item)
61 
62 
63 # ---------------------------------------------------------------------
64 #
65 # Render a short animation by generating scripts
66 # to be used with POV-Ray
67 #
68 
69 pov_exporter = postprocess.ChPovRay(my_system)
70 
71  # Sets some file names for in-out processes.
72 pov_exporter.SetTemplateFile (chrono.GetChronoDataPath() + "_template_POV.pov")
73 pov_exporter.SetOutputScriptFile ("rendering_frames.pov")
74 if not os.path.exists("output"):
75  os.mkdir("output")
76 if not os.path.exists("anim"):
77  os.mkdir("anim")
78 pov_exporter.SetOutputDataFilebase("output/my_state")
79 pov_exporter.SetPictureFilebase("anim/picture")
80 
81  # Sets the viewpoint, aimed point, lens angle
82 pov_exporter.SetCamera(chrono.ChVectorD(0.2,0.3,0.5), chrono.ChVectorD(0,0,0), 35)
83 
84  # Sets the default ambient light and default light lamp
85 pov_exporter.SetAmbientLight(chrono.ChColor(1,1,0.9))
86 pov_exporter.SetLight(chrono.ChVectorD(-2,2,-1), chrono.ChColor(0.9,0.9,1.1), True)
87 
88  # Sets other settings
89 pov_exporter.SetPictureSize(640,480)
90 pov_exporter.SetAmbientLight(chrono.ChColor(2,2,2))
91 
92  # If wanted, turn on the rendering of COGs, reference frames, contacts:
93 #pov_exporter.SetShowCOGs (1, 0.05)
94 #pov_exporter.SetShowFrames(1, 0.02)
95 #pov_exporter.SetShowLinks(1, 0.03)
96 #pov_exporter.SetShowContacts(1,
97 # postprocess.ChPovRay.SYMBOL_VECTOR_SCALELENGTH,
98 # 0.01, # scale
99 # 0.0007, # width
100 # 0.1, # max size
101 # 1,0,0.5 ) # colormap on, blue at 0, red at 0.5
102 
103  # Add additional POV objects/lights/materials in the following way, entering
104  # an optional text using the POV scene description laguage. This will be
105  # appended to the generated .pov file.
106  # For multi-line strings, use the python ''' easy string delimiter.
107 pov_exporter.SetCustomPOVcommandsScript(
108 '''
109 light_source{ <1,3,1.5> color rgb<0.9,0.9,0.8> }
110 ''')
111 
112  # Tell which physical items you want to render
113 pov_exporter.AddAll()
114 
115 
116  # 1) Create the two .pov and .ini files for POV-Ray (this must be done
117  # only once at the beginning of the simulation).
118 pov_exporter.ExportScript()
119 
120  # Configure the solver, if needed
121 my_system.SetSolverType(chrono.ChSolver.Type_BARZILAIBORWEIN)
122 my_system.SetMaxItersSolverSpeed(40)
123 my_system.SetMaxPenetrationRecoverySpeed(0.002)
124 my_system.Set_G_acc(chrono.ChVectorD(0,-9.8,-9.80))
125 
126  # Perform a short simulation
127 nstep =0
128 while (my_system.GetChTime() < 1.2) :
129 
130  my_system.DoStepDynamics(0.002)
131 
132  #if math.fmod(nstep,10) ==0 :
133  print ('time=', my_system.GetChTime() )
134 
135  # 2) Create the incremental nnnn.dat and nnnn.pov files that will be load
136  # by the pov .ini script in POV-Ray (do this at each simulation timestep)
137  pov_exporter.ExportData()
138 
139  nstep = nstep +1
140 
141 
142