mirror of
git://projects.qi-hardware.com/cae-tools.git
synced 2024-12-22 23:26:27 +02:00
sfc/slicer.py: cleanup and debugging; add various alignment and transformation options
This commit is contained in:
parent
79a821052f
commit
ecb7667c7c
148
sfc/slicer.py
148
sfc/slicer.py
@ -25,8 +25,14 @@ from math import hypot
|
|||||||
|
|
||||||
epsilon = 0.0001 # acceptable math rounding error and slicing offset
|
epsilon = 0.0001 # acceptable math rounding error and slicing offset
|
||||||
mech_eps = 0.01 # acceptable mechanical deviation
|
mech_eps = 0.01 # acceptable mechanical deviation
|
||||||
margin = None # draw a workpiece at the specified xy distance around
|
margin = None # draw a rectangular workpiece at the specified xy
|
||||||
# the model (default: none)
|
# 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):
|
def dist(a, b):
|
||||||
@ -35,13 +41,58 @@ def dist(a, b):
|
|||||||
return hypot(pa[0] - pb[0], pa[1] - pb[1])
|
return hypot(pa[0] - pb[0], pa[1] - pb[1])
|
||||||
|
|
||||||
|
|
||||||
def print_vec(v):
|
def print_vec(v, z):
|
||||||
p = v.Point
|
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():
|
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)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
@ -54,10 +105,23 @@ stdout = os.dup(1)
|
|||||||
os.dup2(2, 1)
|
os.dup2(2, 1)
|
||||||
sys.stdout = os.fdopen(stdout, "w")
|
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:
|
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)
|
margin = float(arg)
|
||||||
|
elif opt == "-s":
|
||||||
|
z_step = float(arg)
|
||||||
else:
|
else:
|
||||||
assert False
|
assert False
|
||||||
|
|
||||||
@ -65,10 +129,11 @@ if len(args) != 1:
|
|||||||
usage()
|
usage()
|
||||||
|
|
||||||
#
|
#
|
||||||
# Read the STL mesh
|
# Read the STL mesh and determine its bounding box
|
||||||
#
|
#
|
||||||
|
|
||||||
mesh = Mesh.Mesh(args[0])
|
mesh = Mesh.Mesh(args[0])
|
||||||
|
bb = mesh.BoundBox
|
||||||
|
|
||||||
#
|
#
|
||||||
# The 2.5D model consists of "plateaus" (facets parallel to the xy plane) and
|
# 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:
|
if nz > epsilon:
|
||||||
inclined += 1
|
inclined += 1
|
||||||
max_nz = max(max_nz, nz)
|
max_nz = max(max_nz, nz)
|
||||||
v1 = Base.Vector(facet.Points[0])
|
vert.addFacet(vec(facet.Points[0]), vec(facet.Points[1]),
|
||||||
v2 = Base.Vector(facet.Points[1])
|
vec(facet.Points[2]))
|
||||||
v3 = Base.Vector(facet.Points[1])
|
|
||||||
vert.addFacet(v1, v2, v3)
|
|
||||||
|
|
||||||
if inclined:
|
if inclined:
|
||||||
print >>sys.stderr # FreeCAD progress reporting messes up newlines
|
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 = Part.Shape()
|
||||||
shape.makeShapeFromMesh(mesh.Topology, mech_eps)
|
shape.makeShapeFromMesh(vert.Topology, mech_eps)
|
||||||
bb = shape.BoundBox
|
|
||||||
|
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.
|
# 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
|
# For this, we add a small offset to the z position so that we intersect above
|
||||||
# the plateau.
|
# the plateau.
|
||||||
#
|
#
|
||||||
|
# We advance by at most z_step and insert intermediate layers if needed.
|
||||||
|
#
|
||||||
|
|
||||||
for z in z_levels:
|
if height is not None and height > 0:
|
||||||
print "# level z = ", z
|
last_z = height
|
||||||
|
else:
|
||||||
if margin is not None:
|
last_z = None
|
||||||
print bb.XMin - margin, " ", bb.YMin - margin, " ", z
|
if height is not None and height < 0 and z_levels[-1] > height:
|
||||||
print bb.XMax + margin, " ", bb.YMin - margin, " ", z
|
z_levels.append(height)
|
||||||
print bb.XMax + margin, " ", bb.YMax + margin, " ", z
|
|
||||||
print bb.XMin - margin, " ", bb.YMax + margin, " ", z
|
for next_z in z_levels:
|
||||||
print bb.XMin - margin, " ", bb.YMin - margin, " ", z
|
wires = shape.slice(Base.Vector(0, 0, 1), next_z + epsilon)
|
||||||
print
|
if z_step is None or last_z is None or last_z + z_step >= next_z:
|
||||||
|
dump_level(wires, next_z + z_off)
|
||||||
for wire in shape.slice(Base.Vector(0, 0, 1), z + epsilon):
|
else:
|
||||||
print "# wire = ", wire
|
d = next_z - last_z
|
||||||
first = None
|
n = (d // z_step) + 1
|
||||||
last = None
|
for i in range(0, n):
|
||||||
for e in wire.Edges:
|
dump_level(wires, last_z + (i + 1) * (d / n) + z_off)
|
||||||
v = e.Vertexes[0]
|
last_z = next_z
|
||||||
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
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# That's all, folks !
|
# That's all, folks !
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user