Skip to content

Commit

Permalink
Update py demos to Chrono 9
Browse files Browse the repository at this point in the history
  • Loading branch information
dariofusai93 committed Nov 25, 2024
1 parent e8e3208 commit 5847155
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 689 deletions.
22 changes: 12 additions & 10 deletions to_put_in_app_dir/examples/addin_tester/addin_tester_simulation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'''
Test simulation to check integrity of Chrono::Solidworks Addin exporter
with respect to:
1) bodies
2) links
3) visual shapes, colors, transparencies
4) collision shapes
5) markers
6) rigid sub-assemblies, flexible sub-assemblies
'''
#-------------------------------------------------------------------------------
#
# Test simulation to check integrity of Chrono::Solidworks Addin exporter
# with respect to:
# 1) bodies
# 2) links
# 3) visual shapes, colors, transparencies
# 4) collision shapes
# 5) markers
# 6) rigid sub-assemblies, flexible sub-assemblies
#
#-------------------------------------------------------------------------------

import pychrono as chrono
import pychrono.irrlicht as chronoirr
Expand Down
236 changes: 53 additions & 183 deletions to_put_in_app_dir/examples/collisions/demo_SW_collisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,8 @@
# - PyChrono must be installed in your Python environment
#-------------------------------------------------------------------------------

def main():
pass

if __name__ == '__main__':
main()


import os
import math
import time
import sys, getopt
import sys
import pychrono as chrono
import pychrono.postprocess as postprocess
import pychrono.irrlicht as chronoirr

print("Demo program that shows how to use the SolidWorks add-in.")
Expand All @@ -31,214 +20,95 @@ def main():
print(" 3) modify ImportSolidWorksSystem(..) to match what you exported")
print(" 4) move this .py file in directory X and execute it.")

# Create Chrono physical system, non-smooth contact method
my_system = chrono.ChSystemNSC()

m_timestep = 0.001
m_length = 4.0
m_visualization = "irrlicht" # use "irrlicht" (realtime 3D) or "pov" (raytracing postprocess)
m_datapath = "C:/Program Files/ChronoSolidworks/data/"

# For irrlicht fonts & background. Adjust to your path
chrono.SetChronoDataPath(m_datapath)

# Set collision detection engine
my_system.SetCollisionSystemType(chrono.ChCollisionSystem.Type_BULLET)

# ***TRICK***
# Set the default outward/inward shape margins for collision detection,
# this is epecially important for very large or very small objects.
# This is a global setting to be put BEFORE creating objects/systems
chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.005)
chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.005)


m_timestep = 0.001



# ---------------------------------------------------------------------
#
# load the file generated by the SolidWorks CAD plugin
# and add it to the ChSystem.
#
# This is usually done as
# from my_module import exported_items
# but here is 'my_module' is a string, so there's a workaround with 'exec'


print ("Loading C::E scene...");
# Load the file generated by the SolidWorks CAD plugin and add it to the ChSystem.
# NB: modify the argument of ImportSolidWorksSystem() to match what you exported.
print ("Loading C::E scene...")

exported_items = chrono.ImportSolidWorksSystem('./portal.py')

print ("...loading done!");

print ("...loading done!")

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

# Add items to the physical system
my_system = chrono.ChSystemNSC()
for my_item in exported_items:
my_system.Add(my_item)



# ***TRICK***
# Create a contact material (surface property)to share between all objects.

brick_material = chrono.ChMaterialSurfaceNSC()
# Create a contact material (surface property) to share between all objects.
brick_material = chrono.ChContactMaterialNSC()
brick_material.SetFriction(0.6)
#brick_material.SetRestitution(0)
#brick_material.SetDampingF(0.05)
#brick_material.SetCompliance (0.000000003)
#brick_material.SetComplianceT(0.000000001)

for my_body in my_system.GetBodies():
if my_body.GetCollisionModel():
my_body.GetCollisionModel().SetAllShapesMaterial(brick_material)

for my_body in my_system.Get_bodylist():
my_body.GetCollisionModel().SetAllShapesMaterial(brick_material)
# Customize bodies color
for my_body in my_system.GetBodies():
if my_body.GetVisualModel():
my_body.GetVisualShape(0).SetColor(chrono.ChColor(0.2, 0.2, 0.2))


# ***TRICK***
# Create motion between table and ground:

my_floor = my_system.SearchBody('floor^portal-1')
if not my_floor :
if not my_floor:
sys.exit('Error: cannot find floor from its name in the C::E system!')

my_ground = my_system.SearchBody('ground')
if not my_ground :
my_ground = my_system.SearchBody('SLDW_GROUND')
if not my_ground:
sys.exit('Error: cannot find ground from its name in the C::E system!')

# make the shaking motion
my_floor.SetBodyFixed(False)
# Make the shaking motion
my_floor.SetFixed(False)
link_shaker = chrono.ChLinkLockLock()
link_shaker.Initialize(my_floor, my_ground, chrono.CSYSNORM)
link_shaker.Initialize(my_floor, my_ground, chrono.ChFramed())
my_system.Add(link_shaker)


my_functA = chrono.ChFunction_Sine(0,1.4,0.06)
my_functA.thisown = 0
my_functB = chrono.ChFunction_Sine(0,0.1,1)
my_functB.thisown = 0
my_funct = chrono.ChFunction_Operation()
my_funct.Set_fa(my_functA)
my_funct.Set_fb(my_functB)
my_funct.Set_optype(chrono.ChFunction_Operation.ChOP_MUL)
my_funct.thisown = 0
link_shaker.SetMotion_X(my_funct)


if m_visualization == "pov":
# ---------------------------------------------------------------------
#
# Render a short animation by generating scripts
# to be used with POV-Ray
#

pov_exporter = postprocess.ChPovRay(my_system)

# Set the path where it will save all .pov, .ini, .asset and .dat files,
# this directory will be created if not existing. For example:
pov_exporter.SetBasePath("povray_pychrono_generated")
pov_exporter.SetTemplateFile("_template_POV.pov")

# Sets the viewpoint, aimed point, lens angle
pov_exporter.SetCamera(chrono.ChVectorD(3.2,1.3,3.5), chrono.ChVectorD(0.6,0.5,0), 32)

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

# Sets other settings
pov_exporter.SetPictureSize(640,480)
pov_exporter.SetAmbientLight(chrono.ChColor(2,2,2))

# Turn on the rendering of xyz axes for the centers of gravity or reference frames:
#pov_exporter.SetShowCOGs (1, 0.05)
#pov_exporter.SetShowFrames(1, 0.02)
#pov_exporter.SetShowLinks(1, 0.03)
if (False):
pov_exporter.SetShowContacts(1,
postprocess.ChPovRay.ContactSymbol_VECTOR_SCALELENGTH,
0.2, # 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.
pov_exporter.SetCustomPOVcommandsScript(
'''
#include "stones.inc"
light_source{ <1,3,1.5> color rgb<1,1,1> }
''')

# Tell which physical items you want to render
pov_exporter.AddAll()


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

# Assign custom textures to some objects
for my_body in my_system.Get_bodylist():
pov_exporter.SetCustomCommands(my_body,'''
texture{T_Stone8}
''')

pov_exporter.SetCustomCommands(my_floor, '''
texture{T_Stone9}
''')


# Optionally set some solver parameters.
#my_system.SetSolverType(chrono.ChSolver.Type_BARZILAIBORWEIN) # precise, more slow
my_system.SetSolverType(chrono.ChSolver.Type_SOR)
my_system.SetMaxItersSolverSpeed(70)
my_system.Set_G_acc(chrono.ChVectorD(0,-9.8,0))

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

my_system.DoStepDynamics(m_timestep)

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

# 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)
pov_exporter.ExportData()

nstep = nstep +1

print ("\n\nOk, Simulation done!");
time.sleep(2)


if m_visualization == "irrlicht":

# ---------------------------------------------------------------------
#
# Create an Irrlicht application to visualize the system
#

# Create the Irrlicht visualization
vis = chronoirr.ChVisualSystemIrrlicht()
vis.AttachSystem(my_system)
vis.SetWindowSize(1024,768)
vis.SetWindowTitle('Test')
vis.Initialize()
vis.AddLogo(chrono.GetChronoDataPath() + 'logo_pychrono_alpha.png')
vis.AddSkyBox()
vis.AddCamera(chrono.ChVectorD(1, 1, 1))
vis.AddTypicalLights()

# Simulation loop
while vis.Run():
vis.BeginScene()
vis.Render()
vis.EndScene()
my_system.DoStepDynamics(m_timestep)

my_functA = chrono.ChFunctionSine(1.4, 0.06, 0)
my_functB = chrono.ChFunctionSine(0.1, 1, 0)
my_funct = chrono.ChFunctionOperator()
my_funct.SetFirstOperandFunction(my_functA)
my_funct.SetSecondOperandFunction(my_functB)
my_funct.SetOperationType(chrono.ChFunctionOperator.MUL)
link_shaker.SetMotionX(my_funct)

# Create the Irrlicht visualization
vis = chronoirr.ChVisualSystemIrrlicht()
vis.AttachSystem(my_system)
vis.SetWindowSize(1024, 768)
vis.SetWindowTitle('Test')
vis.Initialize()
vis.AddLogo(chrono.GetChronoDataPath() + 'logo_pychrono_alpha.png')
vis.AddSkyBox()
vis.AddCamera(chrono.ChVector3d(0, 1, 2))
vis.AddTypicalLights()

vis.EnableCollisionShapeDrawing(True)

timestep = 0.001

# Simulation loop
while vis.Run():
vis.BeginScene()
vis.Render()
vis.EndScene()
my_system.DoStepDynamics(timestep)
Loading

0 comments on commit 5847155

Please sign in to comment.