Commit c742e361 authored by sumpfralle's avatar sumpfralle

added basic support for exporting polygon models as SVG


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@823 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 5e266ade
...@@ -22,21 +22,26 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -22,21 +22,26 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
class SVGExporter: class SVGExporter:
def __init__(self, filename): def __init__(self, output):
self.file = file(filename,"w") if isinstance(output, basestring):
self.file.write("""<?xml version='1.0'?> # a filename was given
self.output = file(filename,"w")
else:
# a stream was given
self.output = output
self.output.write("""<?xml version='1.0'?>
<svg xmlns='http://www.w3.org/2000/svg' width='640' height='800'> <svg xmlns='http://www.w3.org/2000/svg' width='640' height='800'>
<g transform='translate(320,320) scale(50)' stroke-width='0.01' font-size='0.2'> <g transform='translate(320,320) scale(50)' stroke-width='0.01' font-size='0.2'>
""") """)
self._fill = 'none' self._fill = 'none'
self._stroke = 'black' self._stroke = 'black'
def close(self): def close(self, close_stream=True):
self.file.write("""</g> self.output.write("""</g>
</svg> </svg>
""") """)
if close_stream:
self.file.close() self.output.close()
def stroke(self, stroke): def stroke(self, stroke):
self._stroke = stroke self._stroke = stroke
...@@ -51,12 +56,12 @@ class SVGExporter: ...@@ -51,12 +56,12 @@ class SVGExporter:
y = -7 y = -7
l = "<circle fill='" + self._fill +"'" + (" cx='%g'" % x) \ l = "<circle fill='" + self._fill +"'" + (" cx='%g'" % x) \
+ (" cy='%g'" % -y) + " r='0.04'/>\n" + (" cy='%g'" % -y) + " r='0.04'/>\n"
self.file.write(l) self.output.write(l)
def AddText(self, x, y, text): def AddText(self, x, y, text):
l = "<text fill='" + self._fill +"'" + (" x='%g'" % x) \ l = "<text fill='" + self._fill +"'" + (" x='%g'" % x) \
+ (" y='%g'" % -y) + " dx='0.07'>" + text + "</text>\n" + (" y='%g'" % -y) + " dx='0.07'>" + text + "</text>\n"
self.file.write(l) self.output.write(l)
def AddLine(self, x1, y1, x2, y2): def AddLine(self, x1, y1, x2, y2):
...@@ -67,24 +72,40 @@ class SVGExporter: ...@@ -67,24 +72,40 @@ class SVGExporter:
l = "<line fill='" + self._fill +"' stroke='" + self._stroke + "'" \ l = "<line fill='" + self._fill +"' stroke='" + self._stroke + "'" \
+ (" x1='%g'" % x1) + (" y1='%g'" % -y1) + (" x2='%g'" % x2) \ + (" x1='%g'" % x1) + (" y1='%g'" % -y1) + (" x2='%g'" % x2) \
+ (" y2='%g'" % -y2) + " />\n" + (" y2='%g'" % -y2) + " />\n"
self.file.write(l) self.output.write(l)
def AddPoint(self, p): def AddPoint(self, p):
self.AddDot(p.x, p.y) self.AddDot(p.x, p.y)
def AddPath(self, path): def AddPath(self, path):
self.AddLines(path.points)
def AddLines(self, points):
l = "<path fill='" + self._fill +"' stroke='" + self._stroke + "' d='" l = "<path fill='" + self._fill +"' stroke='" + self._stroke + "' d='"
for i in range(0, len(path.points)): for i in range(0, len(points)):
p = path.points[i] p = points[i]
if i == 0: if i == 0:
l += "M " l += "M "
else: else:
l += " L " l += " L "
l += "%g %g" % (p.x, -p.y-5) l += "%g %g" % (p.x, -p.y-5)
l += "'/>\n" l += "'/>\n"
self.file.write(l) self.output.write(l)
def AddPathList(self, pathlist): def AddPathList(self, pathlist):
for path in pathlist: for path in pathlist:
self.AddPath(path) self.AddPath(path)
#TODO: we need to create a unified "Exporter" interface and base class
class SVGExporterContourModel(object):
def __init__(self, model, comment=None):
self.model = model
def write(self, stream):
writer = SVGExporter(stream)
for polygon in self.model.get_polygons():
writer.AddLines(polygon.get_points())
writer.close(close_stream=False)
...@@ -22,6 +22,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -22,6 +22,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
import pycam.Exporters.STLExporter import pycam.Exporters.STLExporter
import pycam.Exporters.SVGExporter
from pycam.Geometry.Triangle import Triangle from pycam.Geometry.Triangle import Triangle
from pycam.Geometry.Line import Line from pycam.Geometry.Line import Line
from pycam.Geometry.Plane import Plane from pycam.Geometry.Plane import Plane
...@@ -312,6 +313,7 @@ class ContourModel(BaseModel): ...@@ -312,6 +313,7 @@ class ContourModel(BaseModel):
self._plane_groups = [self._plane] self._plane_groups = [self._plane]
self._item_groups.append(self._plane_groups) self._item_groups.append(self._plane_groups)
self._cached_offset_models = {} self._cached_offset_models = {}
self._export_function = pycam.Exporters.SVGExporter.SVGExporterContourModel
def reset_cache(self): def reset_cache(self):
super(ContourModel, self).reset_cache() super(ContourModel, self).reset_cache()
...@@ -365,6 +367,8 @@ class ContourModel(BaseModel): ...@@ -365,6 +367,8 @@ class ContourModel(BaseModel):
elif isinstance(item, Polygon): elif isinstance(item, Polygon):
if not unify_overlaps or (len(self._line_groups) == 0): if not unify_overlaps or (len(self._line_groups) == 0):
self._line_groups.append(item) self._line_groups.append(item)
for subitem in item.next():
self._update_limits(subitem)
else: else:
# go through all polygons and check if they can be combined # go through all polygons and check if they can be combined
is_outer = item.is_outer() is_outer = item.is_outer()
...@@ -409,7 +413,8 @@ class ContourModel(BaseModel): ...@@ -409,7 +413,8 @@ class ContourModel(BaseModel):
print "New queue: %s" % str([len(p.get_lines()) for p in new_queue]) print "New queue: %s" % str([len(p.get_lines()) for p in new_queue])
for processed_polygon in processed_polygons + new_queue: for processed_polygon in processed_polygons + new_queue:
self._line_groups.append(processed_polygon) self._line_groups.append(processed_polygon)
self.reset_cache() # TODO: this is quite expensive - can we do it differently?
self.reset_cache()
else: else:
# ignore any non-supported items (they are probably handled by a # ignore any non-supported items (they are probably handled by a
# parent class) # parent class)
......
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