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
 57[n0,b0]=AddRigidBody(mainSys = mbs,
 58                     inertia = iCube0, #includes COM
 59                     nodeType = exu.NodeType.RotationEulerParameters,
 60                     position = pMid0,
 61                     rotationMatrix = np.diag([1,1,1]),
 62                     gravity = g,
 63                     graphicsDataList = [graphicsCOM0, graphicsBody0])
 64
 65
 66#%%++++++++++++++++++++++++++
 67#revolute joint (free z-axis)
 68
 69#revolute joint option 3:
 70AddRevoluteJoint(mbs, body0=oGround, body1=b0, point=[0,0,0],
 71                  axis=[0,0,1], useGlobalFrame=True, showJoint=True,
 72                  axisRadius=0.2*w, axisLength=1.4*w)
 73
 74#assemble system before solving
 75mbs.Assemble()
 76simulationSettings = exu.SimulationSettings() #takes currently set values or default values
 77
 78tEnd = 4 #simulation time
 79h = 1e-3 #step size
 80simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
 81simulationSettings.timeIntegration.endTime = tEnd
 82simulationSettings.timeIntegration.verboseMode = 1
 83#simulationSettings.timeIntegration.simulateInRealtime = True
 84simulationSettings.solutionSettings.solutionWritePeriod = 0.005 #store every 5 ms
 85
 86SC.visualizationSettings.window.renderWindowSize=[1600,1200]
 87SC.visualizationSettings.openGL.multiSampling = 4
 88SC.visualizationSettings.openGL.lineWidth = 3
 89SC.visualizationSettings.general.autoFitScene = False
 90
 91SC.visualizationSettings.nodes.drawNodesAsPoint=False
 92SC.visualizationSettings.nodes.showBasis=True
 93
 94exu.StartRenderer()
 95if 'renderState' in exu.sys: #reload old view
 96    SC.SetRenderState(exu.sys['renderState'])
 97
 98mbs.WaitForUserToContinue() #stop before simulating
 99
100mbs.SolveDynamic(simulationSettings = simulationSettings,
101                 solverType=exu.DynamicSolverType.TrapezoidalIndex2)
102
103SC.WaitForRenderEngineStopFlag() #stop before closing
104exu.StopRenderer() #safely close rendering window!