chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / cura_sf / fabmetheus_utilities / fabmetheus_tools / interpret_plugins / svg.py
1 """
2 This page is in the table of contents.
3 The svg.py script is an import translator plugin to get a carving from an svg file.  This script will read an svg file made by skeinforge or by inkscape.
4
5 An example inkscape svg file is inkscape_star.svg in the models folder.
6
7 An import plugin is a script in the interpret_plugins folder which has the function getCarving.  It is meant to be run from the interpret tool.  To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
8
9 The getCarving function takes the file name of an svg file and returns the carving.
10
11 """
12
13
14 from __future__ import absolute_import
15 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
16 import __init__
17
18 from fabmetheus_utilities.svg_reader import SVGReader
19 from fabmetheus_utilities.vector3 import Vector3
20 from fabmetheus_utilities import archive
21 from fabmetheus_utilities import euclidean
22 from fabmetheus_utilities import gcodec
23 from fabmetheus_utilities import svg_writer
24 from fabmetheus_utilities import xml_simple_writer
25 import math
26
27
28 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
29 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
30 __date__ = '$Date: 2008/21/04 $'
31 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
32
33
34 def getCarving(fileName=''):
35         'Get the triangle mesh for the gts file.'
36         carving = SVGCarving()
37         carving.parseSVG(fileName, archive.getFileText(fileName))
38         return carving
39
40
41 class SVGCarving:
42         'An svg carving.'
43         def __init__(self):
44                 'Add empty lists.'
45                 self.layerHeight = 1.0
46                 self.maximumZ = - 987654321.0
47                 self.minimumZ = 987654321.0
48                 self.svgReader = SVGReader()
49
50         def __repr__(self):
51                 'Get the string representation of this carving.'
52                 return self.getCarvedSVG()
53
54         def addXML(self, depth, output):
55                 'Add xml for this object.'
56                 xml_simple_writer.addXMLFromObjects(depth, self.svgReader.loopLayers, output)
57
58         def getCarveBoundaryLayers(self):
59                 'Get the  boundary layers.'
60                 return self.svgReader.loopLayers
61
62         def getCarveCornerMaximum(self):
63                 'Get the corner maximum of the vertexes.'
64                 return self.cornerMaximum
65
66         def getCarveCornerMinimum(self):
67                 'Get the corner minimum of the vertexes.'
68                 return self.cornerMinimum
69
70         def getCarvedSVG(self):
71                 'Get the carved svg text.'
72                 return svg_writer.getSVGByLoopLayers(True, self, self.svgReader.loopLayers)
73
74         def getCarveLayerHeight(self):
75                 'Get the layer height.'
76                 return self.layerHeight
77
78         def getFabmetheusXML(self):
79                 'Return the fabmetheus XML.'
80                 return None
81
82         def getInterpretationSuffix(self):
83                 'Return the suffix for a carving.'
84                 return 'svg'
85
86         def parseSVG(self, fileName, svgText):
87                 'Parse SVG text and store the layers.'
88                 if svgText == '':
89                         return
90                 self.fileName = fileName
91                 self.svgReader.parseSVG(fileName, svgText)
92                 self.layerHeight = euclidean.getFloatDefaultByDictionary(
93                         self.layerHeight, self.svgReader.sliceDictionary, 'layerHeight')
94                 self.cornerMaximum = Vector3(-987654321.0, -987654321.0, self.maximumZ)
95                 self.cornerMinimum = Vector3(987654321.0, 987654321.0, self.minimumZ)
96                 svg_writer.setSVGCarvingCorners(
97                         self.cornerMaximum, self.cornerMinimum, self.layerHeight, self.svgReader.loopLayers)
98
99         def setCarveImportRadius(self, importRadius):
100                 'Set the import radius.'
101                 pass
102
103         def setCarveIsCorrectMesh(self, isCorrectMesh):
104                 'Set the is correct mesh flag.'
105                 pass
106
107         def setCarveLayerHeight(self, layerHeight):
108                 'Set the layer height.'
109                 self.layerHeight = layerHeight