sfc/slicer.py: cleanup and debugging; add various alignment and transformation options

This commit is contained in:
Werner Almesberger 2015-01-18 22:52:57 -03:00
parent 79a821052f
commit ecb7667c7c
1 changed files with 107 additions and 41 deletions

View File

@ -25,8 +25,14 @@ from math import hypot
epsilon = 0.0001 # acceptable math rounding error and slicing offset
mech_eps = 0.01 # acceptable mechanical deviation
margin = None # draw a workpiece at the specified xy distance around
# the model (default: none)
margin = None # draw a rectangular workpiece at the specified xy
# distance around the model (default: none)
z_step = None # maximum Z step (default: unlimited)
flip = False # flip around X center (default: don't)
height = None # height of the workpiece above the Z plane (can be
# negative). Default: use model dimensions.
align_top = None # align the Z position of the model to the workpiece
align_bottom = None
def dist(a, b):
@ -35,13 +41,58 @@ def dist(a, b):
return hypot(pa[0] - pb[0], pa[1] - pb[1])
def print_vec(v):
def print_vec(v, z):
p = v.Point
print p[0], " ", p[1], " ", p[2] - epsilon
print p[0], " ", p[1], " ", z
# Make a vector from a point. While we're at it, also apply flipping (if
# requested).
def vec(p):
if flip:
return Base.Vector(p[0],
bb.YMax - p[1] + bb.YMin, bb.ZMax - p[2] + bb.ZMin)
else:
return Base.Vector(p[0], p[1], p[2])
#
# Dump the current Z level (plateau or intermediate level).
#
def dump_level(wires, z):
print "# level z = ", z
if margin is not None:
print bb.XMin - margin, " ", bb.YMin - margin, " ", z
print bb.XMax + margin, " ", bb.YMin - margin, " ", z
print bb.XMax + margin, " ", bb.YMax + margin, " ", z
print bb.XMin - margin, " ", bb.YMax + margin, " ", z
print bb.XMin - margin, " ", bb.YMin - margin, " ", z
print
for wire in wires:
print "# wire = ", wire
first = None
last = None
for e in wire.Edges:
v = e.Vertexes[0]
if first is None:
first = v
if last is None or dist(v, last) >= mech_eps:
print_vec(v, z)
last = v
if first is not None:
print_vec(first, z)
print
print
def usage():
print >>sys.stderr, "usage:", sys.argv[0], "file.stl"
print >>sys.stderr, "usage:", sys.argv[0], \
"[-a (top|bottom)(+|-)offset] [-f] [-h height]" + \
"\t[-b piece_distance] [-s max_step] file.stl"
sys.exit(1)
@ -54,10 +105,23 @@ stdout = os.dup(1)
os.dup2(2, 1)
sys.stdout = os.fdopen(stdout, "w")
opts, args = getopt.getopt(sys.argv[1:], "b:")
opts, args = getopt.getopt(sys.argv[1:], "a:fh:p:s:")
for opt, arg in opts:
if opt == "-b":
if opt == "-a":
if arg[0:3] == "top":
align_top = float(arg[3:])
elif arg[0:6] == "bottom":
align_bottom = float(arg[6:])
else:
usage()
elif opt == "-f":
flip = True
elif opt == "-h":
height = float(arg)
elif opt == "-p":
margin = float(arg)
elif opt == "-s":
z_step = float(arg)
else:
assert False
@ -65,10 +129,11 @@ if len(args) != 1:
usage()
#
# Read the STL mesh
# Read the STL mesh and determine its bounding box
#
mesh = Mesh.Mesh(args[0])
bb = mesh.BoundBox
#
# The 2.5D model consists of "plateaus" (facets parallel to the xy plane) and
@ -91,10 +156,8 @@ for facet in mesh.Facets:
if nz > epsilon:
inclined += 1
max_nz = max(max_nz, nz)
v1 = Base.Vector(facet.Points[0])
v2 = Base.Vector(facet.Points[1])
v3 = Base.Vector(facet.Points[1])
vert.addFacet(v1, v2, v3)
vert.addFacet(vec(facet.Points[0]), vec(facet.Points[1]),
vec(facet.Points[2]))
if inclined:
print >>sys.stderr # FreeCAD progress reporting messes up newlines
@ -120,42 +183,45 @@ for z in sorted(z_raw.keys(), reverse = True):
#
shape = Part.Shape()
shape.makeShapeFromMesh(mesh.Topology, mech_eps)
bb = shape.BoundBox
shape.makeShapeFromMesh(vert.Topology, mech_eps)
z_off = 0
if height is not None:
if align_top is not None:
z_off = align_top - bb.ZMax
if height > 0:
z_off += height
if align_bottom is not None:
z_off = align_bottom - bb.ZMin
if height < 0:
z_off += height
#
# Iterate over all plateaus and determine how they intersect with the walls.
# For this, we add a small offset to the z position so that we intersect above
# the plateau.
#
# We advance by at most z_step and insert intermediate layers if needed.
#
for z in z_levels:
print "# level z = ", z
if margin is not None:
print bb.XMin - margin, " ", bb.YMin - margin, " ", z
print bb.XMax + margin, " ", bb.YMin - margin, " ", z
print bb.XMax + margin, " ", bb.YMax + margin, " ", z
print bb.XMin - margin, " ", bb.YMax + margin, " ", z
print bb.XMin - margin, " ", bb.YMin - margin, " ", z
print
for wire in shape.slice(Base.Vector(0, 0, 1), z + epsilon):
print "# wire = ", wire
first = None
last = None
for e in wire.Edges:
v = e.Vertexes[0]
if first is None:
first = v
if last is None or dist(v, last) >= mech_eps:
print_vec(v)
last = v
if first is not None:
print_vec(first)
print
print
if height is not None and height > 0:
last_z = height
else:
last_z = None
if height is not None and height < 0 and z_levels[-1] > height:
z_levels.append(height)
for next_z in z_levels:
wires = shape.slice(Base.Vector(0, 0, 1), next_z + epsilon)
if z_step is None or last_z is None or last_z + z_step >= next_z:
dump_level(wires, next_z + z_off)
else:
d = next_z - last_z
n = (d // z_step) + 1
for i in range(0, n):
dump_level(wires, last_z + (i + 1) * (d / n) + z_off)
last_z = next_z
#
# That's all, folks !
#