Commit 6e315d9b authored by sumpfralle's avatar sumpfralle

added support for multi-layered contour models imported from SVG


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@930 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 46516f8e
...@@ -28,7 +28,7 @@ import pycam.Utils.log ...@@ -28,7 +28,7 @@ import pycam.Utils.log
log = pycam.Utils.log.get_logger() log = pycam.Utils.log.get_logger()
class DXFParser: class DXFParser(object):
# see http://www.autodesk.com/techpubs/autocad/acad2000/dxf/group_code_value_types_dxf_01.htm # see http://www.autodesk.com/techpubs/autocad/acad2000/dxf/group_code_value_types_dxf_01.htm
MAX_CHARS_PER_LINE = 2049 MAX_CHARS_PER_LINE = 2049
...@@ -44,11 +44,12 @@ class DXFParser: ...@@ -44,11 +44,12 @@ class DXFParser:
"COLOR": 62, "COLOR": 62,
} }
def __init__(self, inputstream, callback=None): def __init__(self, inputstream, color_as_height=False, callback=None):
self.inputstream = inputstream self.inputstream = inputstream
self.line_number = 0 self.line_number = 0
self.lines = [] self.lines = []
self._input_stack = [] self._input_stack = []
self._color_as_height = color_as_height
self.callback = callback self.callback = callback
self.parse_content() self.parse_content()
self.optimize_line_order() self.optimize_line_order()
...@@ -141,7 +142,7 @@ class DXFParser: ...@@ -141,7 +142,7 @@ class DXFParser:
line2 = int(line2) line2 = int(line2)
except ValueError: except ValueError:
log.warn("DXFImporter: Invalid input in line " \ log.warn("DXFImporter: Invalid input in line " \
+ "%d (float expected): %s" % (self.line_number, line2)) + "%d (int expected): %s" % (self.line_number, line2))
line1 = None line1 = None
line2 = None line2 = None
else: else:
...@@ -188,8 +189,12 @@ class DXFParser: ...@@ -188,8 +189,12 @@ class DXFParser:
axis = 0 axis = 0
elif key == self.KEYS["START_Y"]: elif key == self.KEYS["START_Y"]:
axis = 1 axis = 1
elif key == self.KEYS["START_Z"]: elif not self._color_as_height and (key == self.KEYS["START_Z"]):
axis = 2
elif self._color_as_height and (key == self.KEYS["COLOR"]):
# interpret the color as the height
axis = 2 axis = 2
value = float(value) / 255
else: else:
axis = None axis = None
if not axis is None: if not axis is None:
...@@ -230,6 +235,7 @@ class DXFParser: ...@@ -230,6 +235,7 @@ class DXFParser:
# the z-level defaults to zero (for 2D models) # the z-level defaults to zero (for 2D models)
p1 = [None, None, 0] p1 = [None, None, 0]
p2 = [None, None, 0] p2 = [None, None, 0]
color = None
key, value = self._read_key_value() key, value = self._read_key_value()
while (not key is None) and (key != self.KEYS["MARKER"]): while (not key is None) and (key != self.KEYS["MARKER"]):
if key == self.KEYS["START_X"]: if key == self.KEYS["START_X"]:
...@@ -244,6 +250,8 @@ class DXFParser: ...@@ -244,6 +250,8 @@ class DXFParser:
p2[1] = value p2[1] = value
elif key == self.KEYS["END_Z"]: elif key == self.KEYS["END_Z"]:
p2[2] = value p2[2] = value
elif key == self.KEYS["COLOR"]:
color = value
else: else:
pass pass
key, value = self._read_key_value() key, value = self._read_key_value()
...@@ -256,6 +264,10 @@ class DXFParser: ...@@ -256,6 +264,10 @@ class DXFParser:
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:
if self._color_as_height and (not color is None):
# use the color code as the z coordinate
p1[2] = float(color) / 255
p2[2] = float(color) / 255
line = Line(Point(p1[0], p1[1], p1[2]), Point(p2[0], p2[1], p2[2])) line = Line(Point(p1[0], p1[1], p1[2]), Point(p2[0], p2[1], p2[2]))
if line.len > 0: if line.len > 0:
self.lines.append(line) self.lines.append(line)
...@@ -273,7 +285,8 @@ class DXFParser: ...@@ -273,7 +285,8 @@ class DXFParser:
return None return None
def import_model(filename, program_locations=None, unit=None, callback=None): def import_model(filename, program_locations=None, unit=None,
color_as_height=False, callback=None):
try: try:
infile = open(filename,"rb") infile = open(filename,"rb")
except IOError, err_msg: except IOError, err_msg:
...@@ -281,7 +294,8 @@ def import_model(filename, program_locations=None, unit=None, callback=None): ...@@ -281,7 +294,8 @@ def import_model(filename, program_locations=None, unit=None, callback=None):
% (filename, err_msg)) % (filename, err_msg))
return None return None
result = DXFParser(infile, callback=callback) result = DXFParser(infile, color_as_height=color_as_height,
callback=callback)
lines = result.get_model()["lines"] lines = result.get_model()["lines"]
......
...@@ -131,7 +131,7 @@ def import_model(filename, program_locations=None, unit=None, callback=None): ...@@ -131,7 +131,7 @@ def import_model(filename, program_locations=None, unit=None, callback=None):
else: else:
log.info("Successfully converted EPS file to DXF file") log.info("Successfully converted EPS file to DXF file")
result = pycam.Importers.DXFImporter.import_model(dxf_file_name, result = pycam.Importers.DXFImporter.import_model(dxf_file_name,
unit=unit, callback=callback) unit=unit, color_as_height=True, callback=callback)
# always remove the dxf file # always remove the dxf file
remove_temp_file(dxf_file_name) remove_temp_file(dxf_file_name)
return result return result
......
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