stlFileImport.py

You can view and download this file on Github: stlFileImport.py

  1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2# This is an EXUDYN example
  3#
  4# Details:  demo showing import of simple STL file using specialized interface functions
  5#
  6# Author:   Johannes Gerstmayr
  7# Date:     2022-07-03
  8#
  9# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
 10#
 11#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 12
 13import exudyn as exu
 14from exudyn.itemInterface import *
 15from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
 16import exudyn.graphics as graphics #only import if it does not conflict #includes graphics and rigid body utilities
 17import numpy as np
 18
 19SC = exu.SystemContainer()
 20mbs = SC.AddSystem()
 21
 22
 23#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 24#physical parameters
 25g =     [0,-9.81,0] #gravity
 26L = 1               #length
 27w = 0.1             #width
 28bodyDim=[L,w,w] #body dimensions
 29p0 =    [0,0,0]     #origin of pendulum
 30pMid0 = np.array([L*0.5,0,0]) #center of mass, body0
 31
 32#ground body
 33oGround = mbs.AddObject(ObjectGround())
 34
 35#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 36#first link:
 37iCube0 = InertiaCuboid(density=5000, sideLengths=bodyDim)
 38iCube0 = iCube0.Translated([-0.25*L,0,0]) #transform COM, COM not at reference point!
 39
 40#graphics for body
 41fileName = 'solution/stlImport.stl'
 42if True: #True=create STL file; False=load STL file
 43    graphicsBody0 = graphics.Brick([0,0,0], bodyDim, graphics.color.dodgerblue)
 44    graphics.ExportSTL(graphicsBody0, fileName)
 45
 46graphicsBody0 = graphics.FromSTLfileASCII(fileName, graphics.color.dodgerblue) #color not stored in STL file
 47#faster version (for large STL files):
 48#use binary files and install numpy-stl library: [allow options like scale, offset, ...]
 49# graphicsBody0 = graphics.FromSTLfile(fileName, graphics.color.dodgerblue, scale=1., Aoff=np.eye(3), pOff=[0,0,0])
 50
 51#+++++++++++++++++++++++
 52graphicsBody0 = AddEdgesAndSmoothenNormals(graphicsBody0, edgeAngle = 0.25*pi, addEdges=True, smoothNormals=True)
 53
 54
 55graphicsCOM0 = graphics.Basis(origin=iCube0.com, length=2*w)
 56
 57dictCube0 = mbs.CreateRigidBody(
 58              inertia=iCube0,
 59              referencePosition=pMid0,
 60              referenceRotationMatrix=np.diag([1,1,1]),
 61              gravity=g,
 62              graphicsDataList=[graphicsCOM0, graphicsBody0],
 63              returnDict=True)
 64[n0, b0] = [dictCube0['nodeNumber'], dictCube0['bodyNumber']]
 65
 66
 67#%%++++++++++++++++++++++++++
 68#revolute joint (free z-axis)
 69
 70#revolute joint option 3:
 71AddRevoluteJoint(mbs, body0=oGround, body1=b0, point=[0,0,0],
 72                  axis=[0,0,1], useGlobalFrame=True, showJoint=True,
 73                  axisRadius=0.2*w, axisLength=1.4*w)
 74
 75#assemble system before solving
 76mbs.Assemble()
 77simulationSettings = exu.SimulationSettings() #takes currently set values or default values
 78
 79tEnd = 4 #simulation time
 80h = 1e-3 #step size
 81simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
 82simulationSettings.timeIntegration.endTime = tEnd
 83simulationSettings.timeIntegration.verboseMode = 1
 84#simulationSettings.timeIntegration.simulateInRealtime = True
 85simulationSettings.solutionSettings.solutionWritePeriod = 0.005 #store every 5 ms
 86
 87SC.visualizationSettings.window.renderWindowSize=[1600,1200]
 88SC.visualizationSettings.openGL.multiSampling = 4
 89SC.visualizationSettings.openGL.lineWidth = 3
 90SC.visualizationSettings.general.autoFitScene = False
 91
 92SC.visualizationSettings.nodes.drawNodesAsPoint=False
 93SC.visualizationSettings.nodes.showBasis=True
 94
 95exu.StartRenderer()
 96if 'renderState' in exu.sys: #reload old view
 97    SC.SetRenderState(exu.sys['renderState'])
 98
 99mbs.WaitForUserToContinue() #stop before simulating
100
101mbs.SolveDynamic(simulationSettings = simulationSettings,
102                 solverType=exu.DynamicSolverType.TrapezoidalIndex2)
103
104SC.WaitForRenderEngineStopFlag() #stop before closing
105exu.StopRenderer() #safely close rendering window!