Skip to content

Instantly share code, notes, and snippets.

@nmz787
Last active July 3, 2020 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmz787/be850c63e7cab3739c79821bfac0c6f4 to your computer and use it in GitHub Desktop.
Save nmz787/be850c63e7cab3739c79821bfac0c6f4 to your computer and use it in GitHub Desktop.
# most of the following code is from here: https://github.com/WoLpH/numpy-stl#combining-multiple-stl-files
# but instead of combining STLs, I'm just copying and replicating the same one
import math
import stl
from stl import mesh
import numpy
# find the max dimensions, so we can know the bounding box, getting the height, width, length (because these are the step size)...
def find_mins_maxs(obj):
minx = maxx = miny = maxy = minz = maxz = None
for p in obj.points:
# p contains (x, y, z)
if minx == None:
minx = p[0]
maxx = p[0]
miny = p[1]
maxy = p[1]
minz = p[2]
maxz = p[2]
else:
maxx = max(p[0], maxx)
minx = min(p[0], minx)
maxy = max(p[1], maxy)
miny = min(p[1], miny)
maxz = max(p[2], maxz)
minz = min(p[2], minz)
return minx, maxx, miny, maxy, minz, maxz
def translate(_solid, step, padding, multiplier, axis):
if axis=='x':
items = [0,3,6]
elif axis == 'y':
items = [1,4,7]
elif axis == 'z':
items = [2,5,8]
for p in _solid.points:
# point items are ((x, y, z), (x, y, z), (x, y, z))
p[items[0]] = p[items[0]] + (step * multiplier) + (padding * multiplier)
p[items[1]] = p[items[1]] + (step * multiplier) + (padding * multiplier)
p[items[2]] = p[items[2]] + (step * multiplier) + (padding * multiplier)
def copy_obj(obj, dims, num_rows, num_cols, num_layers):
w,l,h = dims
copies = []
for layer in xrange(num_layers):
for row in xrange(num_rows):
for col in xrange(num_cols):
# skip the position where original being copied is
if row==0 and col==0 and layer==0:
continue
_copy = mesh.Mesh(obj.data.copy())
# pad the space between objects by 10% of the dimension being translated
if col !=0:
translate(_copy, w, w/10., col, 'x')
if row !=0:
translate(_copy, l, l/10., row, 'y')
if layer !=0:
translate(_copy, h, h/10., layer, 'z')
copies.append(_copy)
return copies
# Using an existing stl file:
your_mesh = mesh.Mesh.from_file(r'ball_and_socket_simplified_-_main_body.stl')
# rotate along Y
your_mesh.rotate([0.0, 0.5, 0.0], math.radians(90))
minx,maxx, miny, maxy, minz,maxz = find_mins_maxs(your_mesh)
w1 = maxx-minx
l1 = maxy-miny
h1 = maxz-minz
copies = copy_obj(your_mesh, (w1,l1,h1), 2, 2, 1)
# I wanted to add another related STL to the final STL
# your_mesh2 = mesh.Mesh.from_file(r'ball_and_socket_simplified_-_twist_lock.stl')
# minx,maxx, miny, maxy, minz,maxz = find_mins_maxs(your_mesh2)
# w2 = maxx-minx
# l2 = maxy-miny
# h2 = maxz-minz
# translate(your_mesh2, w1, w1/10., 3, 'x')
# copies2 = copy_obj(your_mesh2, (w2,l2,h2), 2, 2, 1)
# combined = mesh.Mesh(numpy.concatenate([your_mesh.data, your_mesh2.data] + [copy.data for copy in copies] + [copy.data for copy in copies2]))
combined = mesh.Mesh(numpy.concatenate([your_mesh.data] + [copy.data for copy in copies]))
combined.save('combined.stl', mode=1) # save as ASCII
@Occupied-Observer
Copy link

copied code from numpy-stl ...!!!! please acknowledge the source......

@nmz787
Copy link
Author

nmz787 commented Jul 3, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment