ChronoPyEngine:Demo solidworks

From ChronoWiki

Jump to: navigation, search

In this tutorial one can see that it is easy to load a mechanical system that has been exported from SolidWorks using the Chrono::SolidWorks Add-In. In this case, a simulation is run without realtime visualization, but the postprocessing features are used to export the .ini file that can be load in POVray and rendered, to give you the rendered animation:

First, you need to install the Chrono::SolidWorks add-in for SolidWorks. Of course this requires that you have a license of SolidWorks, but If you do not have SolidWorks, you still can try this demo, because we provide also the .py files that we already exported for you, in the directory ../bin/data/solid_works/swiss_escapement.

You can load the exported .py files by providing the name of the exported directory with the function chrono.ImportSolidWorksSystem , as shown in the Python code:

import os
import math
import ChronoEngine_PYTHON_core as chrono
import ChronoEngine_PYTHON_postprocess as postprocess

# ---------------------------------------------------------------------
#  Create the simulation system.
#  (Do not create parts and constraints programmatically here, we will
#  load a mechanism from file)

my_system = chrono.ChSystem()

# Set the collision margins. This is expecially important for very large or
# very small objects (as in this example)! Do this before creating shapes.

# ---------------------------------------------------------------------
#  load the file generated by the SolidWorks CAD plugin
#  and add it to the ChSystem

print ("Loading C::E scene...");

exported_items = chrono.ImportSolidWorksSystem('../bin/data/solid_works/swiss_escapement')

print ("...done!");

# Print exported items
for my_item in exported_items:
    print (my_item.GetName())

# Add items to the physical system
for my_item in exported_items:

# ---------------------------------------------------------------------
#  Render a short animation by generating scripts
#  to be used with POV-Ray

pov_exporter = postprocess.ChPovRay(my_system)

 # Sets some file names for in-out processes.
pov_exporter.SetTemplateFile        ("../bin/data/_template_POV.pov")
pov_exporter.SetOutputScriptFile    ("rendering_frames.pov")
if not os.path.exists("output"):
if not os.path.exists("anim"):

 # Sets the viewpoint, aimed point, lens angle
pov_exporter.SetCamera(chrono.ChVectorD(0.2,0.3,0.5), chrono.ChVectorD(0,0,0), 35)

 # Sets the default ambient light and default light lamp
pov_exporter.SetLight(chrono.ChVectorD(-2,2,-1), chrono.ChColor(0.9,0.9,1.1), 1)

 # Sets other settings

 # If wanted, turn on the rendering of COGs, reference frames, contacts:
#pov_exporter.SetShowCOGs  (1, 0.05)
#pov_exporter.SetShowFrames(1, 0.02)
#pov_exporter.SetShowLinks(1, 0.03)
#                            postprocess.ChPovRay.SYMBOL_VECTOR_SCALELENGTH,
#                            0.01,   # scale
#                            0.0007, # width
#                            0.1,    # max size
#                            1,0,0.5 ) # colormap on, blue at 0, red at 0.5

 # Add additional POV objects/lights/materials in the following way, entering
 # an optional text using the POV scene description laguage. This will be
 # appended to the generated .pov file.
 # For multi-line strings, use the python ''' easy string delimiter.
light_source{ <1,3,1.5> color rgb<0.9,0.9,0.8> }

 # Tell which physical items you want to render

 # 1) Create the two .pov and .ini files for POV-Ray (this must be done
 #    only once at the beginning of the simulation).

 # Configure the solver, if needed

 # Perform a short simulation
nstep =0
while (my_system.GetChTime() < 1.2) :


    #if math.fmod(nstep,10) ==0 :
    print ('time=', my_system.GetChTime() )

        # 2) Create the incremental nnnn.dat and nnnn.pov files that will be load
        #    by the pov .ini script in POV-Ray (do this at each simulation timestep)

    nstep = nstep +1

This demo must be run in this way:

  • execute the in your Python IDE or by double clicking on it
  • on the console you will see a time counter showing that the system is load and it is being simulated
  • when the program ends, you must open POVray and open the rendering_frames.pov.ini file, using the Open menu or button, or drag&drop (you can find this .ini file and other POVray as they are saved in the same directory of the file)


  • press the Run button in POVray to execute the .ini file , and you should see that POVray generates lot of frames like this:


Personal tools