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/>.
class SVGExporter:
def __init__(self, filename):
self.file = file(filename,"w")
self.file.write("""<?xml version='1.0'?>
def __init__(self, output):
if isinstance(output, basestring):
# 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'>
<g transform='translate(320,320) scale(50)' stroke-width='0.01' font-size='0.2'>
""")
self._fill = 'none'
self._stroke = 'black'
def close(self):
self.file.write("""</g>
def close(self, close_stream=True):
self.output.write("""</g>
</svg>
""")
self.file.close()
if close_stream:
self.output.close()
def stroke(self, stroke):
self._stroke = stroke
......@@ -51,12 +56,12 @@ class SVGExporter:
y = -7
l = "<circle fill='" + self._fill +"'" + (" cx='%g'" % x) \
+ (" cy='%g'" % -y) + " r='0.04'/>\n"
self.file.write(l)
self.output.write(l)
def AddText(self, x, y, text):
l = "<text fill='" + self._fill +"'" + (" x='%g'" % x) \
+ (" y='%g'" % -y) + " dx='0.07'>" + text + "</text>\n"
self.file.write(l)
self.output.write(l)
def AddLine(self, x1, y1, x2, y2):
......@@ -67,24 +72,40 @@ class SVGExporter:
l = "<line fill='" + self._fill +"' stroke='" + self._stroke + "'" \
+ (" x1='%g'" % x1) + (" y1='%g'" % -y1) + (" x2='%g'" % x2) \
+ (" y2='%g'" % -y2) + " />\n"
self.file.write(l)
self.output.write(l)
def AddPoint(self, p):
self.AddDot(p.x, p.y)
def AddPath(self, path):
self.AddLines(path.points)
def AddLines(self, points):
l = "<path fill='" + self._fill +"' stroke='" + self._stroke + "' d='"
for i in range(0, len(path.points)):
p = path.points[i]
for i in range(0, len(points)):
p = points[i]
if i == 0:
l += "M "
else:
l += " L "
l += "%g %g" % (p.x, -p.y-5)
l += "'/>\n"
self.file.write(l)
self.output.write(l)
def AddPathList(self, pathlist):
for path in pathlist:
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/>.
"""
import pycam.Exporters.STLExporter
import pycam.Exporters.SVGExporter
from pycam.Geometry.Triangle import Triangle
from pycam.Geometry.Line import Line
from pycam.Geometry.Plane import Plane
......@@ -312,6 +313,7 @@ class ContourModel(BaseModel):
self._plane_groups = [self._plane]
self._item_groups.append(self._plane_groups)
self._cached_offset_models = {}
self._export_function = pycam.Exporters.SVGExporter.SVGExporterContourModel
def reset_cache(self):
super(ContourModel, self).reset_cache()
......@@ -365,6 +367,8 @@ class ContourModel(BaseModel):
elif isinstance(item, Polygon):
if not unify_overlaps or (len(self._line_groups) == 0):
self._line_groups.append(item)
for subitem in item.next():
self._update_limits(subitem)
else:
# go through all polygons and check if they can be combined
is_outer = item.is_outer()
......@@ -409,6 +413,7 @@ class ContourModel(BaseModel):
print "New queue: %s" % str([len(p.get_lines()) for p in new_queue])
for processed_polygon in processed_polygons + new_queue:
self._line_groups.append(processed_polygon)
# TODO: this is quite expensive - can we do it differently?
self.reset_cache()
else:
# ignore any non-supported items (they are probably handled by a
......
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