Skip to content

Instantly share code, notes, and snippets.

@iamargentum
Last active September 15, 2021 21:02
Show Gist options
  • Save iamargentum/9b0507d32e3d81d5e7f7c34ac221207c to your computer and use it in GitHub Desktop.
Save iamargentum/9b0507d32e3d81d5e7f7c34ac221207c to your computer and use it in GitHub Desktop.
solution for the stl slicing problem with program names corresponding to the question numbers
import trimesh
from time import sleep
from savePlot import saveSection
myMesh = trimesh.load_mesh('assignment.stl')
height = int(input('please input height - '))
# hard coded base plane (bottom)
saveSection(2, height, myMesh)
sleep(1)
print('saving sections with height 10mm, 30mm and 50mm')
saveSection(2, 10, myMesh)
sleep(1)
saveSection(2, 30, myMesh)
sleep(1)
saveSection(2, 50, myMesh)
sleep(1)
from datetime import datetime
import matplotlib.pyplot as plt
def saveSection(basePlane, height, mesh):
rightNow = datetime.now()
myMesh = mesh
if (basePlane == 1):
height = height + 25
planeNormal = [0, 0, 1]
if (basePlane == 2):
planeNormal = [0, 1, 0]
if (basePlane == 3):
planeNormal = [1, 0, 0]
planeOrigin = [0, 0, 0]
for i in range(len(planeNormal)):
planeOrigin[i] = planeNormal[i]*height
try:
slicedMesh = myMesh.section(plane_origin=planeOrigin, plane_normal=planeNormal)
sliced2D, to_3d = slicedMesh.to_planar()
plt.axes().set_aspect('equal', 'datalim')
eformat = {'Line0': {'color': 'g', 'linewidth': 1},
'Line1': {'color': 'y', 'linewidth': 1},
'Arc0': {'color': 'r', 'linewidth': 1},
'Arc1': {'color': 'b', 'linewidth': 1},
'Bezier0': {'color': 'k', 'linewidth': 1},
'Bezier1': {'color': 'k', 'linewidth': 1},
'BSpline0': {'color': 'm', 'linewidth': 1},
'BSpline1': {'color': 'm', 'linewidth': 1}
}
for entity in sliced2D.entities:
if hasattr(entity, 'plot'):
entity.plot(sliced2D.vertices)
continue
discrete = entity.discrete(sliced2D.vertices)
eKey = entity.__class__.__name__ + str(int(entity.closed))
fmt = eformat[eKey].copy()
if hasattr(entity, 'color'):
fmt['color'] = entity.color
plt.plot(*discrete.T, **fmt)
fileName = datetime.now().strftime('section%d%b%Y%H%M%S') + '.png'
plt.savefig(fileName)
print('section saved with file name ', fileName)
except AttributeError:
print('geometry does not exist in the slicing plane')
import trimesh
from time import sleep
from savePlot import saveSection
myMesh = trimesh.load_mesh('assignment.stl')
height = int(input('please input height - '))
basePlane = int(input('please input base plane -\n1. front\n2. bottom\n3. left'))
# hard coded base plane (bottom)
saveSection(basePlane, height, myMesh)
sleep(1)
print('saving sections with height 10mm, 30mm and 50mm')
saveSection(1, 10, myMesh)
sleep(1)
saveSection(1, 30, myMesh)
sleep(1)
saveSection(1, 50, myMesh)
sleep(1)
import trimesh
from time import sleep
from savePlot import saveSection
stepHeight = int(input('input the step height - '))
myMesh = trimesh.load_mesh('assignment.stl')
heightSufficient = True
height = 0
while heightSufficient:
saveSection(2, height, myMesh)
sleep(2)
height = height + stepHeight
if height >= 60:
heightSufficient = False
print('done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment