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