Commit 30fa4082 authored by sumpfralle's avatar sumpfralle

fixed code-style issues


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@478 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 5e5750ed
...@@ -90,8 +90,10 @@ class DXFParser: ...@@ -90,8 +90,10 @@ class DXFParser:
current_group = ordered_groups[-1] current_group = ordered_groups[-1]
closest_distance = None closest_distance = None
for cmp_group in remaining_groups: for cmp_group in remaining_groups:
cmp_distance = get_distance_between_groups(current_group, cmp_group) cmp_distance = get_distance_between_groups(current_group,
if (closest_distance is None) or (cmp_distance < closest_distance): cmp_group)
if (closest_distance is None) \
or (cmp_distance < closest_distance):
closest_distance = cmp_distance closest_distance = cmp_distance
closest_group = cmp_group closest_group = cmp_group
ordered_groups.append(closest_group) ordered_groups.append(closest_group)
...@@ -140,13 +142,13 @@ class DXFParser: ...@@ -140,13 +142,13 @@ class DXFParser:
line2 = None line2 = None
else: else:
line2 = line2.upper() line2 = line2.upper()
pass
self.line_number += 2 self.line_number += 2
return line1, line2 return line1, line2
def parse_content(self): def parse_content(self):
key, value = self._read_key_value() key, value = self._read_key_value()
while (not key is None) and not ((key == self.KEYS["MARKER"]) and (value == "EOF")): while (not key is None) \
and not ((key == self.KEYS["MARKER"]) and (value == "EOF")):
if key == self.KEYS["MARKER"]: if key == self.KEYS["MARKER"]:
if value in ("SECTION", "TABLE", "LAYER", "ENDTAB", "ENDSEC"): if value in ("SECTION", "TABLE", "LAYER", "ENDTAB", "ENDSEC"):
# we don't handle these meta-information # we don't handle these meta-information
...@@ -181,32 +183,35 @@ class DXFParser: ...@@ -181,32 +183,35 @@ class DXFParser:
pass pass
key, value = self._read_key_value() key, value = self._read_key_value()
end_line = self.line_number end_line = self.line_number
# the last lines were not used - they are just the marker for the next item # The last lines were not used - they are just the marker for the next
# item.
if not key is None: if not key is None:
self._push_on_stack(key, value) self._push_on_stack(key, value)
if (None in p1) or (None in p2): if (None in p1) or (None in p2):
log.warn("DXFImporter: Incomplete LINE definition between line " \ log.warn("DXFImporter: Incomplete LINE definition between line " \
+ "%d and %d" % (start_line, end_line)) + "%d and %d" % (start_line, end_line))
else: else:
self.lines.append(Line(Point(p1[0], p1[1], p1[2]), Point(p2[0], p2[1], p2[2]))) self.lines.append(Line(Point(p1[0], p1[1], p1[2]),
Point(p2[0], p2[1], p2[2])))
def check_header(self): def check_header(self):
# TODO: this function is not used? # TODO: this function is not used?
# we expect "0" in the first line and "SECTION" in the second one # we expect "0" in the first line and "SECTION" in the second one
key, value = self._read_key_value() key, value = self._read_key_value()
if (key != KEYS["MARKER"]) or (value and (value != "SECTION")): if (key != self.KEYS["MARKER"]) or (value and (value != "SECTION")):
log.error("DXFImporter: DXF file header not recognized") log.error("DXFImporter: DXF file header not recognized")
return None return None
def import_model(filename): def import_model(filename):
try: try:
f = open(filename,"rb") infile = open(filename,"rb")
except IOError, err_msg: except IOError, err_msg:
log.error("DXFImporter: Failed to read file (%s): %s" % (filename, err_msg)) log.error("DXFImporter: Failed to read file (%s): %s" \
% (filename, err_msg))
return None return None
result = DXFParser(f) result = DXFParser(infile)
lines = result.get_model()["lines"] lines = result.get_model()["lines"]
......
...@@ -39,34 +39,33 @@ edges = 0 ...@@ -39,34 +39,33 @@ edges = 0
epsilon = 0.0001 epsilon = 0.0001
kdtree = None kdtree = None
def UniqueVertex(x,y,z): def UniqueVertex(x, y, z):
global vertices global vertices
if kdtree: if kdtree:
last = Point.id last = Point.id
p = kdtree.Point(x,y,z) p = kdtree.Point(x, y, z)
if p.id == last: if p.id == last:
# print p
vertices += 1 vertices += 1
return p return p
else: else:
vertices += 1 vertices += 1
return Point(x,y,z) return Point(x, y, z)
def UniqueEdge(p1, p2): def UniqueEdge(p1, p2):
global edges global edges
if hasattr(p1,"edges"): if hasattr(p1, "edges"):
for e in p1.edges: for e in p1.edges:
if e.p1 == p1 and e.p2 == p2: if e.p1 == p1 and e.p2 == p2:
return e return e
if e.p2 == p1 and e.p1 == p2: if e.p2 == p1 and e.p1 == p2:
return e return e
edges += 1 edges += 1
e = Line(p1,p2) e = Line(p1, p2)
if not hasattr(p1,"edges"): if not hasattr(p1, "edges"):
p1.edges = [e] p1.edges = [e]
else: else:
p1.edges.append(e) p1.edges.append(e)
if not hasattr(p2,"edges"): if not hasattr(p2, "edges"):
p2.edges = [e] p2.edges = [e]
else: else:
p2.edges.append(e) p2.edges.append(e)
...@@ -80,11 +79,13 @@ def ImportModel(filename, use_kdtree=True): ...@@ -80,11 +79,13 @@ def ImportModel(filename, use_kdtree=True):
kdtree = None kdtree = None
try: try:
f = open(filename,"rb") f = open(filename, "rb")
except IOError, err_msg: except IOError, err_msg:
log.error("STLImporter: Failed to read file (%s): %s" % (filename, err_msg)) log.error("STLImporter: Failed to read file (%s): %s" \
% (filename, err_msg))
return None return None
# read the first two lines of (potentially non-binary) input - they should contain "solid" and "facet" # Read the first two lines of (potentially non-binary) input - they should
# contain "solid" and "facet".
header_lines = [] header_lines = []
while len(header_lines) < 2: while len(header_lines) < 2:
current_line = f.readline(200) current_line = f.readline(200)
...@@ -99,7 +100,7 @@ def ImportModel(filename, use_kdtree=True): ...@@ -99,7 +100,7 @@ def ImportModel(filename, use_kdtree=True):
header = "".join(header_lines) header = "".join(header_lines)
# read byte 80 to 83 - they contain the "numfacets" value in binary format # read byte 80 to 83 - they contain the "numfacets" value in binary format
f.seek(80) f.seek(80)
numfacets = unpack("<I",f.read(4))[0] numfacets = unpack("<I", f.read(4))[0]
binary = False binary = False
if os.path.getsize(filename) == (84 + 50*numfacets): if os.path.getsize(filename) == (84 + 50*numfacets):
...@@ -108,11 +109,11 @@ def ImportModel(filename, use_kdtree=True): ...@@ -108,11 +109,11 @@ def ImportModel(filename, use_kdtree=True):
binary = False binary = False
f.seek(0) f.seek(0)
else: else:
print "STL binary/ascii detection failed" log.error("STLImporter: STL binary/ascii detection failed")
return None return None
if use_kdtree: if use_kdtree:
kdtree = PointKdtree([],3,1,epsilon) kdtree = PointKdtree([], 3, 1, epsilon)
model = Model() model = Model()
t = None t = None
...@@ -121,63 +122,77 @@ def ImportModel(filename, use_kdtree=True): ...@@ -121,63 +122,77 @@ def ImportModel(filename, use_kdtree=True):
p3 = None p3 = None
if binary: if binary:
for i in range(1,numfacets+1): for i in range(1, numfacets + 1):
a1 = unpack("<f",f.read(4))[0] a1 = unpack("<f", f.read(4))[0]
a2 = unpack("<f",f.read(4))[0] a2 = unpack("<f", f.read(4))[0]
a3 = unpack("<f",f.read(4))[0] a3 = unpack("<f", f.read(4))[0]
n = Point(float(a1),float(a2),float(a3)) n = Point(float(a1), float(a2), float(a3))
v11 = unpack("<f",f.read(4))[0] v11 = unpack("<f", f.read(4))[0]
v12 = unpack("<f",f.read(4))[0] v12 = unpack("<f", f.read(4))[0]
v13 = unpack("<f",f.read(4))[0] v13 = unpack("<f", f.read(4))[0]
p1 = UniqueVertex(float(v11),float(v12),float(v13)) p1 = UniqueVertex(float(v11), float(v12), float(v13))
v21 = unpack("<f",f.read(4))[0] v21 = unpack("<f", f.read(4))[0]
v22 = unpack("<f",f.read(4))[0] v22 = unpack("<f", f.read(4))[0]
v23 = unpack("<f",f.read(4))[0] v23 = unpack("<f", f.read(4))[0]
p2 = UniqueVertex(float(v21),float(v22),float(v23)) p2 = UniqueVertex(float(v21), float(v22), float(v23))
v31 = unpack("<f",f.read(4))[0] v31 = unpack("<f", f.read(4))[0]
v32 = unpack("<f",f.read(4))[0] v32 = unpack("<f", f.read(4))[0]
v33 = unpack("<f",f.read(4))[0] v33 = unpack("<f", f.read(4))[0]
p3 = UniqueVertex(float(v31),float(v32),float(v33)) p3 = UniqueVertex(float(v31), float(v32), float(v33))
attribs = unpack("<H",f.read(2)) # not used
attribs = unpack("<H", f.read(2))
dotcross = n.dot(p3.sub(p1).cross(p2.sub(p1))) dotcross = n.dot(p3.sub(p1).cross(p2.sub(p1)))
if a1==0 and a2==0 and a3==0: if a1 == a2 == a3 == 0:
dotcross = p3.sub(p1).cross(p2.sub(p1)).z dotcross = p3.sub(p1).cross(p2.sub(p1)).z
n = None n = None
if dotcross > 0: if dotcross > 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))
elif dotcross < 0: elif dotcross < 0:
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))
else: else:
# the three points are in a line - or two points are identical # the three points are in a line - or two points are identical
# usually this is caused by points, that are too close together # usually this is caused by points, that are too close together
# check the tolerance value in pycam/Geometry/PointKdtree.py # check the tolerance value in pycam/Geometry/PointKdtree.py
print "ERROR: skipping invalid triangle: %s / %s / %s" % (p1, p2, p3) log.warn("Skipping invalid triangle: %s / %s / %s" \
% (p1, p2, p3) + " (maybe the resolution of the model" \
+ " is too high?)")
continue continue
if n: if n:
t._normal = n t._normal = n
model.append(t) model.append(t)
else: else:
solid = re.compile("\s*solid\s+(\w+)\s+.*") solid = re.compile(r"\s*solid\s+(\w+)\s+.*")
endsolid = re.compile("\s*endsolid\s*") endsolid = re.compile(r"\s*endsolid\s*")
facet = re.compile("\s*facet\s*") facet = re.compile(r"\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+") normal = re.compile(r"\s*facet\s+normal" \
endfacet = re.compile("\s*endfacet\s+") + r"\s+(?P<x>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)" \
loop = re.compile("\s*outer\s+loop\s+") + r"\s+(?P<y>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)" \
endloop = re.compile("\s*endloop\s+") + r"\s+(?P<z>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\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+") endfacet = re.compile(r"\s*endfacet\s+")
loop = re.compile(r"\s*outer\s+loop\s+")
endloop = re.compile(r"\s*endloop\s+")
vertex = re.compile(r"\s*vertex" \
+ r"\s+(?P<x>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)" \
+ r"\s+(?P<y>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)" \
+ r"\s+(?P<z>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\s+")
current_line = 0
for line in f: for line in f:
current_line += 1
m = solid.match(line) m = solid.match(line)
if m: if m:
model.name = m.group(1) model.name = m.group(1)
...@@ -187,7 +202,8 @@ def ImportModel(filename, use_kdtree=True): ...@@ -187,7 +202,8 @@ def ImportModel(filename, use_kdtree=True):
if m: if m:
m = normal.match(line) m = normal.match(line)
if m: if m:
n = Point(float(m.group('x')),float(m.group('y')),float(m.group('z'))) n = Point(float(m.group('x')), float(m.group('y')),
float(m.group('z')))
else: else:
n = None n = None
continue continue
...@@ -196,7 +212,8 @@ def ImportModel(filename, use_kdtree=True): ...@@ -196,7 +212,8 @@ def ImportModel(filename, use_kdtree=True):
continue continue
m = vertex.match(line) m = vertex.match(line)
if m: if m:
p = UniqueVertex(float(m.group('x')),float(m.group('y')),float(m.group('z'))) p = UniqueVertex(float(m.group('x')), float(m.group('y')),
float(m.group('z')))
if p1 is None: if p1 is None:
p1 = p p1 = p
elif p2 is None: elif p2 is None:
...@@ -204,7 +221,8 @@ def ImportModel(filename, use_kdtree=True): ...@@ -204,7 +221,8 @@ def ImportModel(filename, use_kdtree=True):
elif p3 is None: elif p3 is None:
p3 = p p3 = p
else: else:
print "ERROR: more then 3 points in facet" log.error("STLImporter: ERROR: more then 3 points in " \
+ "facet (line %d)" % current_line)
continue continue
m = endloop.match(line) m = endloop.match(line)
if m: if m:
...@@ -218,17 +236,21 @@ def ImportModel(filename, use_kdtree=True): ...@@ -218,17 +236,21 @@ def ImportModel(filename, use_kdtree=True):
# make sure the points are in ClockWise order # make sure the points are in ClockWise order
dotcross = n.dot(p3.sub(p1).cross(p2.sub(p1))) dotcross = n.dot(p3.sub(p1).cross(p2.sub(p1)))
if dotcross > 0: if dotcross > 0:
t = Triangle(p1, p2, p3, UniqueEdge(p1,p2), UniqueEdge(p2,p3), UniqueEdge(p3,p1), n) t = Triangle(p1, p2, p3, UniqueEdge(p1, p2),
UniqueEdge(p2, p3), UniqueEdge(p3, p1), n)
elif dotcross < 0: elif dotcross < 0:
t = Triangle(p1, p3, p2, UniqueEdge(p1,p3), UniqueEdge(p3,p2), UniqueEdge(p2,p1), n) t = Triangle(p1, p3, p2, UniqueEdge(p1, p3),
UniqueEdge(p3, p2), UniqueEdge(p2, p1), n)
else: else:
# the three points are in a line - or two points are identical # The three points are in a line - or two points are
# usually this is caused by points, that are too close together # identical. Usually this is caused by points, that are too
# check the tolerance value in pycam/Geometry/PointKdtree.py # close together. Check the tolerance value in
print "ERROR: skipping invalid triangle: %s / %s / %s" % (p1, p2, p3) # pycam/Geometry/PointKdtree.py.
n=p1=p2=p3=None print "ERROR: skipping invalid triangle: %s / %s / %s" \
% (p1, p2, p3)
n, p1, p2, p3 = (None, None, None, None)
continue continue
n=p1=p2=p3=None n, p1, p2, p3 = (None, None, None, None)
model.append(t) model.append(t)
continue continue
m = endsolid.match(line) m = endsolid.match(line)
...@@ -239,7 +261,8 @@ def ImportModel(filename, use_kdtree=True): ...@@ -239,7 +261,8 @@ def ImportModel(filename, use_kdtree=True):
model.p_kdtree = kdtree model.p_kdtree = kdtree
model.t_kdtree = TriangleKdtree(model.triangles()) model.t_kdtree = TriangleKdtree(model.triangles())
print "Imported STL model: ", vertices, "vertices,", edges, "edges,", len(model.triangles()), "triangles" log.info("Imported STL model: %d vertices, %d edges, %d triangles" \
% (vertices, edges, len(model.triangles())))
vertices = 0 vertices = 0
edges = 0 edges = 0
kdtree = None kdtree = None
......
...@@ -23,52 +23,55 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -23,52 +23,55 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
from pycam.Geometry import Triangle, Line, Point from pycam.Geometry import Triangle, Line, Point
from pycam.Geometry.Model import Model from pycam.Geometry.Model import Model
def TestModel(): def get_test_model():
points = [] points = []
points.append(Point(-2,1,4)) points.append(Point(-2, 1, 4))
points.append(Point(2,1,4)) points.append(Point(2, 1, 4))
points.append(Point(0,-2,4)) points.append(Point(0, -2, 4))
points.append(Point(-5,2,2)) points.append(Point(-5, 2, 2))
points.append(Point(-1,3,2)) points.append(Point(-1, 3, 2))
points.append(Point(5,2,2)) points.append(Point(5, 2, 2))
points.append(Point(4,-1,2)) points.append(Point(4, -1, 2))
points.append(Point(2,-4,2)) points.append(Point(2, -4, 2))
points.append(Point(-2,-4,2)) points.append(Point(-2, -4, 2))
points.append(Point(-3,-2,2)) points.append(Point(-3, -2, 2))
lines = [] lines = []
lines.append(Line(points[0],points[1])) lines.append(Line(points[0], points[1]))
lines.append(Line(points[1],points[2])) lines.append(Line(points[1], points[2]))
lines.append(Line(points[2],points[0])) lines.append(Line(points[2], points[0]))
lines.append(Line(points[0],points[3])) lines.append(Line(points[0], points[3]))
lines.append(Line(points[3],points[4])) lines.append(Line(points[3], points[4]))
lines.append(Line(points[4],points[0])) lines.append(Line(points[4], points[0]))
lines.append(Line(points[4],points[1])) lines.append(Line(points[4], points[1]))
lines.append(Line(points[4],points[5])) lines.append(Line(points[4], points[5]))
lines.append(Line(points[5],points[1])) lines.append(Line(points[5], points[1]))
lines.append(Line(points[5],points[6])) lines.append(Line(points[5], points[6]))
lines.append(Line(points[6],points[1])) lines.append(Line(points[6], points[1]))
lines.append(Line(points[6],points[2])) lines.append(Line(points[6], points[2]))
lines.append(Line(points[6],points[7])) lines.append(Line(points[6], points[7]))
lines.append(Line(points[7],points[2])) lines.append(Line(points[7], points[2]))
lines.append(Line(points[7],points[8])) lines.append(Line(points[7], points[8]))
lines.append(Line(points[8],points[2])) lines.append(Line(points[8], points[2]))
lines.append(Line(points[8],points[9])) lines.append(Line(points[8], points[9]))
lines.append(Line(points[9],points[2])) lines.append(Line(points[9], points[2]))
lines.append(Line(points[9],points[0])) lines.append(Line(points[9], points[0]))
lines.append(Line(points[9],points[3])) lines.append(Line(points[9], points[3]))
model = Model() model = Model()
model.append(Triangle(points[0],points[1],points[2],lines[0],lines[1],lines[2])) for p1, p2, p3, l1, l2, l3 in (
model.append(Triangle(points[0],points[3],points[4],lines[3],lines[4],lines[5])) (0, 1, 2, 0, 1, 2),
model.append(Triangle(points[0],points[4],points[1],lines[5],lines[6],lines[0])) (0, 3, 4, 3, 4, 5),
model.append(Triangle(points[1],points[4],points[5],lines[6],lines[7],lines[8])) (0, 4, 1, 5, 6, 0),
model.append(Triangle(points[1],points[5],points[6],lines[8],lines[9],lines[10])) (1, 4, 5, 6, 7, 8),
model.append(Triangle(points[1],points[6],points[2],lines[10],lines[11],lines[1])) (1, 5, 6, 8, 9, 10),
model.append(Triangle(points[2],points[6],points[7],lines[11],lines[12],lines[13])) (1, 6, 2, 10, 11, 1),
model.append(Triangle(points[2],points[7],points[8],lines[13],lines[14],lines[15])) (2, 6, 7, 11, 12, 13),
model.append(Triangle(points[2],points[8],points[9],lines[15],lines[16],lines[17])) (2, 7, 8, 13, 14, 15),
model.append(Triangle(points[2],points[9],points[0],lines[17],lines[18],lines[2])) (2, 8, 9, 15, 16, 17),
model.append(Triangle(points[0],points[9],points[3],lines[18],lines[19],lines[3])) (2, 9, 0, 17, 18, 2),
(0, 9, 3, 18, 19, 3)):
model.append(Triangle(points[p1], points[p2], points[p3],
lines[l1], lines[l2], lines[l3]))
return model return model
...@@ -24,8 +24,8 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -24,8 +24,8 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
__all__ = ["STLImporter", "DXFImporter", "TestModel"] __all__ = ["STLImporter", "DXFImporter", "TestModel"]
import pycam.Utils.log import pycam.Utils.log
import DXFImporter import pycam.Importers.DXFImporter
import STLImporter import pycam.Importers.STLImporter
import os import os
...@@ -40,9 +40,9 @@ def detect_file_type(filename): ...@@ -40,9 +40,9 @@ def detect_file_type(filename):
# check all listed importers # check all listed importers
# TODO: this should be done by evaluating the header of the file # TODO: this should be done by evaluating the header of the file
if filename.endswith(".stl"): if filename.endswith(".stl"):
return ("stl", STLImporter.ImportModel) return ("stl", pycam.Importers.STLImporter.ImportModel)
elif filename.endswith(".dxf"): elif filename.endswith(".dxf"):
return ("dxf", DXFImporter.import_model) return ("dxf", pycam.Importers.DXFImporter.import_model)
else: else:
log.error("Importers: Failed to detect the model type of '%s'." \ log.error("Importers: Failed to detect the model type of '%s'." \
% filename + " Is the file extension (.stl/.dxf) correct?") % filename + " Is the file extension (.stl/.dxf) correct?")
......
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