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 graphics and rigid body utilities
 16import numpy as np
 17
 18SC = exu.SystemContainer()
 19mbs = SC.AddSystem()
 20
 21
 22#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 23#physical parameters
 24g =     [0,-9.81,0] #gravity
 25L = 1               #length
 26w = 0.1             #width
 27bodyDim=[L,w,w] #body dimensions
 28p0 =    [0,0,0]     #origin of pendulum
 29pMid0 = np.array([L*0.5,0,0]) #center of mass, body0
 30
 31#ground body
 32oGround = mbs.AddObject(ObjectGround())
 33
 34#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 35#first link:
 36iCube0 = InertiaCuboid(density=5000, sideLengths=bodyDim)
 37iCube0 = iCube0.Translated([-0.25*L,0,0]) #transform COM, COM not at reference point!
 38
 39#graphics for body
 40fileName = 'solution/stlImport.stl'
 41if True: #True=create STL file; False=load STL file
 42    graphicsBody0 = GraphicsDataOrthoCubePoint([0,0,0], bodyDim, color4dodgerblue)
 43    ExportGraphicsData2STL(graphicsBody0, fileName)
 44
 45graphicsBody0 = GraphicsDataFromSTLfileTxt(fileName, color4dodgerblue) #color not stored in STL file
 46#faster version (for large STL files):
 47#use binary files and install numpy-stl library: [allow options like scale, offset, ...]
 48# graphicsBody0 = GraphicsDataFromSTLfile(fileName, color4dodgerblue, scale=1., Aoff=np.eye(3), pOff=[0,0,0])
 49
 50#+++++++++++++++++++++++
 51graphicsBody0 = AddEdgesAndSmoothenNormals(graphicsBody0, edgeAngle = 0.25*pi, addEdges=True, smoothNormals=True)
 52
 53
 54graphicsCOM0 = GraphicsDataBasis(origin=iCube0.com, length=2*w)
 55
 56[n0,b0]=AddRigidBody(mainSys = mbs,
 57                     inertia = iCube0, #includes COM
 58                     nodeType = exu.NodeType.RotationEulerParameters,
 59                     position = pMid0,
 60                     rotationMatrix = np.diag([1,1,1]),
 61                     gravity = g,
 62                     graphicsDataList = [graphicsCOM0, graphicsBody0])
 63
 64
 65#%%++++++++++++++++++++++++++
 66#revolute joint (free z-axis)
 67
 68#revolute joint option 3:
 69AddRevoluteJoint(mbs, body0=oGround, body1=b0, point=[0,0,0],
 70                  axis=[0,0,1], useGlobalFrame=True, showJoint=True,
 71                  axisRadius=0.2*w, axisLength=1.4*w)
 72
 73#assemble system before solving
 74mbs.Assemble()
 75simulationSettings = exu.SimulationSettings() #takes currently set values or default values
 76
 77tEnd = 4 #simulation time
 78h = 1e-3 #step size
 79simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
 80simulationSettings.timeIntegration.endTime = tEnd
 81simulationSettings.timeIntegration.verboseMode = 1
 82#simulationSettings.timeIntegration.simulateInRealtime = True
 83simulationSettings.solutionSettings.solutionWritePeriod = 0.005 #store every 5 ms
 84
 85SC.visualizationSettings.window.renderWindowSize=[1600,1200]
 86SC.visualizationSettings.openGL.multiSampling = 4
 87SC.visualizationSettings.openGL.lineWidth = 3
 88SC.visualizationSettings.general.autoFitScene = False
 89
 90SC.visualizationSettings.nodes.drawNodesAsPoint=False
 91SC.visualizationSettings.nodes.showBasis=True
 92
 93exu.StartRenderer()
 94if 'renderState' in exu.sys: #reload old view
 95    SC.SetRenderState(exu.sys['renderState'])
 96
 97mbs.WaitForUserToContinue() #stop before simulating
 98
 99mbs.SolveDynamic(simulationSettings = simulationSettings,
100                 solverType=exu.DynamicSolverType.TrapezoidalIndex2)
101
102SC.WaitForRenderEngineStopFlag() #stop before closing
103exu.StopRenderer() #safely close rendering window!