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: ...@@ -163,18 +163,19 @@ class PolygonExtractor:
self.process_ver_scanline(self.curr_line) self.process_ver_scanline(self.curr_line)
def process_hor_scanline(self, scanline): def process_hor_scanline(self, scanline):
last = 0 if DEBUG_POLYGONEXTRACTOR:
inside = False last = 0
s = "" inside = False
for point in scanline: s = ""
next = point.x for point in scanline:
if inside: next = point.x
s += "*" * int(next-last) if inside:
else: s += "*" * int(next-last)
s += " " * int(next-last) else:
last = next s += " " * int(next-last)
inside = not inside last = next
if DEBUG_POLYGONEXTRACTOR: print s inside = not inside
print s
if DEBUG_POLYGONEXTRACTOR: print "active paths: ", if DEBUG_POLYGONEXTRACTOR: print "active paths: ",
for path in self.curr_path_list: for path in self.curr_path_list:
......
import re import re
from struct import unpack
from pycam.Geometry import * from pycam.Geometry import *
from pycam.Geometry.PointKdtree import PointKdtree from pycam.Geometry.PointKdtree import PointKdtree
...@@ -49,16 +50,14 @@ def ImportModel(filename, use_kdtree=True): ...@@ -49,16 +50,14 @@ def ImportModel(filename, use_kdtree=True):
edges = 0 edges = 0
kdtree = None kdtree = None
f = file(filename,"r") f = open(filename,"rb")
solid = re.compile("\s*solid\s+(\w+)\s+.*") header = f.read(80)
solid_AOI = re.compile("\s*solid\s+\"([\w\-]+)\"; Produced by Art of Illusion.*") binary = False
endsolid = re.compile("\s*endsolid\s+") if header[0:5] == "solid":
facet = re.compile("\s*facet\s+") binary = False
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+") f.seek(0)
endfacet = re.compile("\s*endfacet\s+") else:
loop = re.compile("\s*outer\s+loop\s+") binary = True
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+")
if use_kdtree: if use_kdtree:
kdtree = PointKdtree([],3,1,epsilon) kdtree = PointKdtree([],3,1,epsilon)
...@@ -68,65 +67,115 @@ def ImportModel(filename, use_kdtree=True): ...@@ -68,65 +67,115 @@ def ImportModel(filename, use_kdtree=True):
p1 = None p1 = None
p2 = None p2 = None
p3 = None p3 = None
AOI = False
if binary:
for line in f: numfacets = unpack("<I",f.read(4))[0]
m = solid_AOI.match(line)
if m: for i in range(1,numfacets):
model.name = m.group(1) a1 = unpack("<f",f.read(4))[0]
AOI = True a2 = unpack("<f",f.read(4))[0]
continue a3 = unpack("<f",f.read(4))[0]
m = solid.match(line)
if m: n = Point(float(a1),float(a2),float(a3))
model.name = m.group(1)
continue v11 = unpack("<f",f.read(4))[0]
v12 = unpack("<f",f.read(4))[0]
m = facet.match(line) v13 = unpack("<f",f.read(4))[0]
if m:
m = normal.match(line) p1 = UniqueVertex(float(v11),float(v12),float(v13))
if m:
if AOI: v21 = unpack("<f",f.read(4))[0]
n = Point(float(m.group('x')),float(m.group('z')),float(m.group('y'))) v22 = unpack("<f",f.read(4))[0]
else: v23 = unpack("<f",f.read(4))[0]
n = Point(float(m.group('x')),float(m.group('y')),float(m.group('z')))
continue p2 = UniqueVertex(float(v21),float(v22),float(v23))
m = loop.match(line)
if m: v31 = unpack("<f",f.read(4))[0]
continue v32 = unpack("<f",f.read(4))[0]
m = vertex.match(line) v33 = unpack("<f",f.read(4))[0]
if m:
if AOI: p3 = UniqueVertex(float(v31),float(v32),float(v33))
p = UniqueVertex(float(m.group('x')),float(m.group('z')),float(m.group('y')))
else: attribs = unpack("<H",f.read(2))
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: 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)) t = Triangle(p1, p2, p3, UniqueEdge(p1,p2), UniqueEdge(p2,p3), UniqueEdge(p3,p1))
else: else:
t = Triangle(p1, p3, p2, UniqueEdge(p1,p3), UniqueEdge(p3,p2), UniqueEdge(p2,p1)) t = Triangle(p1, p3, p2, UniqueEdge(p1,p3), UniqueEdge(p3,p2), UniqueEdge(p2,p1))
t._normal = n t._normal = n
n=p1=p2=p3=None
if t.normal().z < 0: if t.normal().z < 0:
continue continue
model.append(t) model.append(t)
continue else:
m = endsolid.match(line) AOI = False
if m:
continue 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: if use_kdtree:
model.p_kdtree = kdtree model.p_kdtree = kdtree
......
...@@ -101,7 +101,7 @@ class PushCutter: ...@@ -101,7 +101,7 @@ class PushCutter:
y = miny y = miny
line = 0 line = 0
while x<=maxx and y<=maxy: while x<=maxx and y<=maxy:
self.pa.new_scanline() self.pa.new_scanline()
if False: if False:
...@@ -129,7 +129,7 @@ class PushCutter: ...@@ -129,7 +129,7 @@ class PushCutter:
p = Point(minx, y, 10) p = Point(minx, y, 10)
for i in range(0,100): for i in range(0,100):
p.x = minx + float(i)/100*float(maxx-minx) 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') self.svg.fill('black')
...@@ -155,6 +155,7 @@ class PushCutter: ...@@ -155,6 +155,7 @@ class PushCutter:
if DEBUG_PUSHCUTTER: print "< cl=",cl,",d=",-d,",t=",t.id,",t.n=",t.normal(),",n=",n 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,t,-d,backward))
hits.append(Hit(cl.sub(backward_small),t,-d+epsilon,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) if DEBUG_PUSHCUTTER3: self.svg.AddDot(cl.x, cl.z)
else: else:
if DEBUG_PUSHCUTTER: print "< cl=",cl,",0",",t=",t.id if DEBUG_PUSHCUTTER: print "< cl=",cl,",0",",t=",t.id
...@@ -163,6 +164,7 @@ class PushCutter: ...@@ -163,6 +164,7 @@ class PushCutter:
if cl: if cl:
if DEBUG_PUSHCUTTER: print "> cl=",cl,",d=",d,",t=",t.id,",t.n=",t.normal(),",n=",n 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,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)) hits.append(Hit(cl.sub(forward_small),t,d-epsilon,forward))
if DEBUG_PUSHCUTTER3: self.svg.AddDot(cl.x, cl.z) if DEBUG_PUSHCUTTER3: self.svg.AddDot(cl.x, cl.z)
else: 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