Commit 8f1d8ccb authored by lode_leroy's avatar lode_leroy

support binary STL

git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@54 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 618caf6d
......@@ -163,18 +163,19 @@ class PolygonExtractor:
self.process_ver_scanline(self.curr_line)
def process_hor_scanline(self, scanline):
last = 0
inside = False
s = ""
for point in scanline:
next = point.x
if inside:
s += "*" * int(next-last)
else:
s += " " * int(next-last)
last = next
inside = not inside
if DEBUG_POLYGONEXTRACTOR: print s
if DEBUG_POLYGONEXTRACTOR:
last = 0
inside = False
s = ""
for point in scanline:
next = point.x
if inside:
s += "*" * int(next-last)
else:
s += " " * int(next-last)
last = next
inside = not inside
print s
if DEBUG_POLYGONEXTRACTOR: print "active paths: ",
for path in self.curr_path_list:
......
import re
from struct import unpack
from pycam.Geometry import *
from pycam.Geometry.PointKdtree import PointKdtree
......@@ -49,16 +50,14 @@ def ImportModel(filename, use_kdtree=True):
edges = 0
kdtree = None
f = file(filename,"r")
solid = re.compile("\s*solid\s+(\w+)\s+.*")
solid_AOI = re.compile("\s*solid\s+\"([\w\-]+)\"; Produced by Art of Illusion.*")
endsolid = re.compile("\s*endsolid\s+")
facet = re.compile("\s*facet\s+")
normal = re.compile("\s*facet\s+normal\s+(?P<x>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<y>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<z>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+")
endfacet = re.compile("\s*endfacet\s+")
loop = re.compile("\s*outer\s+loop\s+")
endloop = re.compile("\s*endloop\s+")
vertex = re.compile("\s*vertex\s+(?P<x>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<y>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<z>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+")
f = open(filename,"rb")
header = f.read(80)
binary = False
if header[0:5] == "solid":
binary = False
f.seek(0)
else:
binary = True
if use_kdtree:
kdtree = PointKdtree([],3,1,epsilon)
......@@ -68,65 +67,115 @@ def ImportModel(filename, use_kdtree=True):
p1 = None
p2 = None
p3 = None
AOI = False
for line in f:
m = solid_AOI.match(line)
if m:
model.name = m.group(1)
AOI = True
continue
m = solid.match(line)
if m:
model.name = m.group(1)
continue
m = facet.match(line)
if m:
m = normal.match(line)
if m:
if AOI:
n = Point(float(m.group('x')),float(m.group('z')),float(m.group('y')))
else:
n = Point(float(m.group('x')),float(m.group('y')),float(m.group('z')))
continue
m = loop.match(line)
if m:
continue
m = vertex.match(line)
if m:
if AOI:
p = UniqueVertex(float(m.group('x')),float(m.group('z')),float(m.group('y')))
else:
p = UniqueVertex(float(m.group('x')),float(m.group('y')),float(m.group('z')))
if p1 == None:
p1 = p
elif p2 == None:
p2 = p
elif p3 == None:
p3 = p
else:
print "ERROR: more then 3 points in facet"
continue
m = endloop.match(line)
if m:
continue
m = endfacet.match(line)
if m:
# make sure the points are in ClockWise order
if binary:
numfacets = unpack("<I",f.read(4))[0]
for i in range(1,numfacets):
a1 = unpack("<f",f.read(4))[0]
a2 = unpack("<f",f.read(4))[0]
a3 = unpack("<f",f.read(4))[0]
n = Point(float(a1),float(a2),float(a3))
v11 = unpack("<f",f.read(4))[0]
v12 = unpack("<f",f.read(4))[0]
v13 = unpack("<f",f.read(4))[0]
p1 = UniqueVertex(float(v11),float(v12),float(v13))
v21 = unpack("<f",f.read(4))[0]
v22 = unpack("<f",f.read(4))[0]
v23 = unpack("<f",f.read(4))[0]
p2 = UniqueVertex(float(v21),float(v22),float(v23))
v31 = unpack("<f",f.read(4))[0]
v32 = unpack("<f",f.read(4))[0]
v33 = unpack("<f",f.read(4))[0]
p3 = UniqueVertex(float(v31),float(v32),float(v33))
attribs = unpack("<H",f.read(2))
if n.dot(p3.sub(p1).cross(p2.sub(p1)))>0:
t = Triangle(p1, p2, p3, UniqueEdge(p1,p2), UniqueEdge(p2,p3), UniqueEdge(p3,p1))
else:
t = Triangle(p1, p3, p2, UniqueEdge(p1,p3), UniqueEdge(p3,p2), UniqueEdge(p2,p1))
t._normal = n
n=p1=p2=p3=None
if t.normal().z < 0:
continue
model.append(t)
continue
m = endsolid.match(line)
if m:
continue
else:
AOI = False
solid = re.compile("\s*solid\s+(\w+)\s+.*")
solid_AOI = re.compile("\s*solid\s+\"([\w\-]+)\"; Produced by Art of Illusion.*")
endsolid = re.compile("\s*endsolid\s+")
facet = re.compile("\s*facet\s+")
normal = re.compile("\s*facet\s+normal\s+(?P<x>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<y>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<z>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+")
endfacet = re.compile("\s*endfacet\s+")
loop = re.compile("\s*outer\s+loop\s+")
endloop = re.compile("\s*endloop\s+")
vertex = re.compile("\s*vertex\s+(?P<x>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<y>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+(?P<z>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+")
for line in f:
m = solid_AOI.match(line)
if m:
model.name = m.group(1)
AOI = True
continue
m = solid.match(line)
if m:
model.name = m.group(1)
continue
m = facet.match(line)
if m:
m = normal.match(line)
if m:
if AOI:
n = Point(float(m.group('x')),float(m.group('z')),float(m.group('y')))
else:
n = Point(float(m.group('x')),float(m.group('y')),float(m.group('z')))
continue
m = loop.match(line)
if m:
continue
m = vertex.match(line)
if m:
if AOI:
p = UniqueVertex(float(m.group('x')),float(m.group('z')),float(m.group('y')))
else:
p = UniqueVertex(float(m.group('x')),float(m.group('y')),float(m.group('z')))
if p1 == None:
p1 = p
elif p2 == None:
p2 = p
elif p3 == None:
p3 = p
else:
print "ERROR: more then 3 points in facet"
continue
m = endloop.match(line)
if m:
continue
m = endfacet.match(line)
if m:
# make sure the points are in ClockWise order
if n.dot(p3.sub(p1).cross(p2.sub(p1)))>0:
t = Triangle(p1, p2, p3, UniqueEdge(p1,p2), UniqueEdge(p2,p3), UniqueEdge(p3,p1))
else:
t = Triangle(p1, p3, p2, UniqueEdge(p1,p3), UniqueEdge(p3,p2), UniqueEdge(p2,p1))
t._normal = n
n=p1=p2=p3=None
if t.normal().z < 0:
continue
model.append(t)
continue
m = endsolid.match(line)
if m:
continue
if use_kdtree:
model.p_kdtree = kdtree
......
......@@ -101,7 +101,7 @@ class PushCutter:
y = miny
line = 0
while x<=maxx and y<=maxy:
self.pa.new_scanline()
if False:
......@@ -129,7 +129,7 @@ class PushCutter:
p = Point(minx, y, 10)
for i in range(0,100):
p.x = minx + float(i)/100*float(maxx-minx)
self.svg.AddDot(p.x, zmax)
self.svg.AddDot(p.x, z)
self.svg.fill('black')
......@@ -155,6 +155,7 @@ class PushCutter:
if DEBUG_PUSHCUTTER: print "< cl=",cl,",d=",-d,",t=",t.id,",t.n=",t.normal(),",n=",n
hits.append(Hit(cl,t,-d,backward))
hits.append(Hit(cl.sub(backward_small),t,-d+epsilon,backward))
hits.append(Hit(cl.add(backward_small),t,-d-epsilon,backward))
if DEBUG_PUSHCUTTER3: self.svg.AddDot(cl.x, cl.z)
else:
if DEBUG_PUSHCUTTER: print "< cl=",cl,",0",",t=",t.id
......@@ -163,6 +164,7 @@ class PushCutter:
if cl:
if DEBUG_PUSHCUTTER: print "> cl=",cl,",d=",d,",t=",t.id,",t.n=",t.normal(),",n=",n
hits.append(Hit(cl,t,d,forward))
hits.append(Hit(cl.add(forward_small),t,d+epsilon,forward))
hits.append(Hit(cl.sub(forward_small),t,d-epsilon,forward))
if DEBUG_PUSHCUTTER3: self.svg.AddDot(cl.x, cl.z)
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment