def getReplacedSVGTemplate(self, fileName, procedureName, rotatedLoopLayers, xmlElement=None): 'Get the lines of text from the layer_template.svg file.' self.extent = self.cornerMaximum - self.cornerMinimum svgTemplateText = archive.getFileTextInFileDirectory( __file__, os.path.join('templates', 'layer_template.svg') ) self.xmlParser = XMLSimpleReader( fileName, None, svgTemplateText ) self.svgElement = self.xmlParser.getRoot() svgElementDictionary = self.svgElement.attributeDictionary self.sliceDictionary = getSliceDictionary(self.svgElement) self.controlBoxHeight = float(self.sliceDictionary['controlBoxHeight']) self.controlBoxWidth = float(self.sliceDictionary['controlBoxWidth']) self.margin = float(self.sliceDictionary['margin']) self.marginTop = float(self.sliceDictionary['marginTop']) self.textHeight = float(self.sliceDictionary['textHeight']) self.unitScale = float(self.sliceDictionary['unitScale']) svgMinWidth = float(self.sliceDictionary['svgMinWidth']) self.controlBoxHeightMargin = self.controlBoxHeight + self.marginTop if not self.addLayerTemplateToSVG: self.svgElement.getXMLElementByID('layerTextTemplate').removeFromIDNameParent() del self.svgElement.getXMLElementByID('sliceElementTemplate').attributeDictionary['transform'] self.graphicsXMLElement = self.svgElement.getXMLElementByID('sliceElementTemplate') self.graphicsXMLElement.attributeDictionary['id'] = 'z:' self.addRotatedLoopLayersToOutput(rotatedLoopLayers) self.setMetadataNoscriptElement('layerThickness', 'Layer Thickness: ', self.layerThickness) self.setMetadataNoscriptElement('maxX', 'X: ', self.cornerMaximum.x) self.setMetadataNoscriptElement('minX', 'X: ', self.cornerMinimum.x) self.setMetadataNoscriptElement('maxY', 'Y: ', self.cornerMaximum.y) self.setMetadataNoscriptElement('minY', 'Y: ', self.cornerMinimum.y) self.setMetadataNoscriptElement('maxZ', 'Z: ', self.cornerMaximum.z) self.setMetadataNoscriptElement('minZ', 'Z: ', self.cornerMinimum.z) self.textHeight = float( self.sliceDictionary['textHeight'] ) controlTop = len(rotatedLoopLayers) * (self.margin + self.extent.y * self.unitScale + self.textHeight) + self.marginTop + self.textHeight self.svgElement.getFirstChildWithClassName('title').text = os.path.basename(fileName) + ' - Slice Layers' svgElementDictionary['height'] = '%spx' % self.getRounded(max(controlTop, self.controlBoxHeightMargin)) width = max(self.extent.x * self.unitScale, svgMinWidth) svgElementDictionary['width'] = '%spx' % self.getRounded( width ) self.sliceDictionary['decimalPlacesCarried'] = str( self.decimalPlacesCarried ) if self.perimeterWidth != None: self.sliceDictionary['perimeterWidth'] = self.getRounded( self.perimeterWidth ) self.sliceDictionary['yAxisPointingUpward'] = 'true' self.sliceDictionary['procedureName'] = procedureName self.setDimensionTexts('dimX', 'X: ' + self.getRounded(self.extent.x)) self.setDimensionTexts('dimY', 'Y: ' + self.getRounded(self.extent.y)) self.setDimensionTexts('dimZ', 'Z: ' + self.getRounded(self.extent.z)) self.setTexts('numberOfLayers', 'Number of Layers: %s' % len(rotatedLoopLayers)) volume = 0.0 for rotatedLoopLayer in rotatedLoopLayers: volume += euclidean.getAreaLoops(rotatedLoopLayer.loops) volume *= 0.001 self.setTexts('volume', 'Volume: %s cm3' % self.getRounded(volume)) if not self.addLayerTemplateToSVG: self.svgElement.getFirstChildWithClassName('script').removeFromIDNameParent() self.svgElement.getXMLElementByID('isoControlBox').removeFromIDNameParent() self.svgElement.getXMLElementByID('layerControlBox').removeFromIDNameParent() self.svgElement.getXMLElementByID('scrollControlBox').removeFromIDNameParent() self.graphicsXMLElement.removeFromIDNameParent() self.addOriginalAsComment(xmlElement) output = cStringIO.StringIO() output.write(self.xmlParser.beforeRoot) self.svgElement.addXML(0, output) return xml_simple_writer.getBeforeRootOutput(self.xmlParser)
def getReplacedSVGTemplate(self, fileName, procedureName, rotatedBoundaryLayers, xmlElement=None): "Get the lines of text from the layer_template.svg file." # ( layers.length + 1 ) * (margin + sliceDimY * unitScale + txtHeight) + margin + txtHeight + margin + 110 cornerMaximum = self.carving.getCarveCornerMaximum() cornerMinimum = self.carving.getCarveCornerMinimum() self.extent = cornerMaximum - cornerMinimum svgTemplateText = archive.getFileTextInFileDirectory( __file__, os.path.join('templates', 'layer_template.svg') ) self.xmlParser = XMLSimpleReader( fileName, None, svgTemplateText ) self.svgElement = self.xmlParser.getRoot() if not self.addLayerTemplateToSVG: self.svgElement.getXMLElementByID('layerTextTemplate').removeFromIDNameParent() del self.svgElement.getXMLElementByID('sliceElementTemplate').attributeDictionary['transform'] svgElementDictionary = self.svgElement.attributeDictionary self.graphicsXMLElement = self.svgElement.getXMLElementByID('sliceElementTemplate') self.graphicsXMLElement.attributeDictionary['id'] = 'z:' self.addRotatedLoopLayersToOutput( rotatedBoundaryLayers ) self.sliceDictionary = getSliceDictionary( self.svgElement ) self.setMetadataNoscriptElement('layerThickness', self.carving.getCarveLayerThickness() ) self.setMetadataNoscriptElement('maxX', cornerMaximum.x ) self.setMetadataNoscriptElement('minX', cornerMinimum.x ) self.setMetadataNoscriptElement('maxY', cornerMaximum.y ) self.setMetadataNoscriptElement('minY', cornerMinimum.y ) self.setMetadataNoscriptElement('maxZ', cornerMaximum.z ) self.setMetadataNoscriptElement('minZ', cornerMinimum.z ) self.margin = float( self.sliceDictionary['margin'] ) self.textHeight = float( self.sliceDictionary['textHeight'] ) javascriptControlBoxWidth = float( self.sliceDictionary['javascriptControlBoxWidth'] ) noJavascriptControlBoxHeight = float( self.sliceDictionary['noJavascriptControlBoxHeight'] ) controlTop = len( rotatedBoundaryLayers ) * ( self.margin + self.extent.y * self.unitScale + self.textHeight ) + 2.0 * self.margin + self.textHeight self.svgElement.getFirstChildWithClassName('title').text = os.path.basename(fileName) + ' - Slice Layers' svgElementDictionary['height'] = '%spx' % self.getRounded( controlTop + noJavascriptControlBoxHeight + self.margin ) # width = margin + (sliceDimX * unitScale) + margin; width = 2.0 * self.margin + max( self.extent.x * self.unitScale, javascriptControlBoxWidth ) svgElementDictionary['width'] = '%spx' % self.getRounded( width ) self.sliceDictionary['decimalPlacesCarried'] = str( self.decimalPlacesCarried ) if self.perimeterWidth != None: self.sliceDictionary['perimeterWidth'] = self.getRounded( self.perimeterWidth ) self.sliceDictionary['yAxisPointingUpward'] = 'true' self.sliceDictionary['procedureDone'] = procedureName noJavascriptDictionary = self.svgElement.getXMLElementByID('noJavascriptControls').attributeDictionary noJavascriptDictionary['transform'] = 'translate(%s, %s)' % ( self.getRounded(self.margin), self.getRounded( controlTop ) ) self.svgElement.getXMLElementByID('dimXNoJavascript').text = self.getRounded( self.extent.x ) self.svgElement.getXMLElementByID('dimYNoJavascript').text = self.getRounded( self.extent.y ) self.svgElement.getXMLElementByID('dimZNoJavascript').text = self.getRounded( self.extent.z ) if not self.addLayerTemplateToSVG: self.svgElement.getFirstChildWithClassName('script').removeFromIDNameParent() self.svgElement.getXMLElementByID('beginningOfControlSection').removeFromIDNameParent() self.svgElement.getXMLElementByID('noJavascriptControls').removeFromIDNameParent() self.graphicsXMLElement.removeFromIDNameParent() self.addOriginalAsComment(xmlElement) output = cStringIO.StringIO() output.write(self.xmlParser.beforeRoot) self.svgElement.addXML(0, output) return output.getvalue()
def execute(self): "Export the canvas as an svg file." svgFileName = archive.getFilePathWithUnderscoredBasename( self.fileName, self.suffix ) boundingBox = self.canvas.bbox( settings.Tkinter.ALL ) # tuple (w, n, e, s) self.boxW = boundingBox[0] self.boxN = boundingBox[1] boxWidth = boundingBox[2] - self.boxW boxHeight = boundingBox[3] - self.boxN print('Exported svg file saved as ' + svgFileName ) svgTemplateText = archive.getFileTextInFileDirectory( settings.__file__, os.path.join('templates', 'canvas_template.svg') ) output = cStringIO.StringIO() lines = archive.getTextLines( svgTemplateText ) firstWordTable = {} firstWordTable['height="999px"'] = ' height="%spx"' % int( round( boxHeight ) ) firstWordTable['<!--replaceLineWith_coloredLines-->'] = self.getCanvasLinesOutput() firstWordTable['replaceLineWithTitle'] = archive.getSummarizedFileName( self.fileName ) firstWordTable['width="999px"'] = ' width="%spx"' % int( round( boxWidth ) ) for line in lines: parseLineReplace( firstWordTable, line, output ) archive.writeFileText( svgFileName, output.getvalue() ) fileExtension = self.fileExtension.value svgViewer = self.svgViewer.value if svgViewer == '': return if svgViewer == 'webbrowser': settings.openWebPage( svgFileName ) return svgFilePath = '"' + os.path.normpath( svgFileName ) + '"' # " to send in file name with spaces shellCommand = svgViewer + ' ' + svgFilePath print('') if fileExtension == '': print('Sending the shell command:') print( shellCommand ) commandResult = os.system( shellCommand ) if commandResult != 0: print('It may be that the system could not find the %s program.' % svgViewer ) print('If so, try installing the %s program or look for another svg viewer, like Netscape which can be found at:' % svgViewer ) print('http://www.netscape.org/') return convertedFileName = archive.getFilePathWithUnderscoredBasename( svgFilePath, '.' + fileExtension + '"') shellCommand += ' ' + convertedFileName print('Sending the shell command:') print( shellCommand ) commandResult = os.system( shellCommand ) if commandResult != 0: print('The %s program could not convert the svg to the %s file format.' % ( svgViewer, fileExtension ) ) print('Try installing the %s program or look for another one, like Image Magick which can be found at:' % svgViewer ) print('http://www.imagemagick.org/script/index.php')
def getReplacedSVGTemplate(self, fileName, procedureName, rotatedBoundaryLayers, xmlElement=None): "Get the lines of text from the layer_template.svg file." # ( layers.length + 1 ) * (margin + sliceDimY * unitScale + txtHeight) + margin + txtHeight + margin + 110 cornerMaximum = self.carving.getCarveCornerMaximum() cornerMinimum = self.carving.getCarveCornerMinimum() self.extent = cornerMaximum - cornerMinimum svgTemplateText = archive.getFileTextInFileDirectory( __file__, os.path.join('templates', 'layer_template.svg')) self.xmlParser = XMLSimpleReader(fileName, None, svgTemplateText) self.svgElement = self.xmlParser.getRoot() if not self.addLayerTemplateToSVG: self.svgElement.getXMLElementByID( 'layerTextTemplate').removeFromIDNameParent() del self.svgElement.getXMLElementByID( 'sliceElementTemplate').attributeDictionary['transform'] svgElementDictionary = self.svgElement.attributeDictionary self.graphicsXMLElement = self.svgElement.getXMLElementByID( 'sliceElementTemplate') self.graphicsXMLElement.attributeDictionary['id'] = 'z:' self.addRotatedLoopLayersToOutput(rotatedBoundaryLayers) self.sliceDictionary = getSliceDictionary(self.svgElement) self.setMetadataNoscriptElement('layerThickness', self.carving.getCarveLayerThickness()) self.setMetadataNoscriptElement('maxX', cornerMaximum.x) self.setMetadataNoscriptElement('minX', cornerMinimum.x) self.setMetadataNoscriptElement('maxY', cornerMaximum.y) self.setMetadataNoscriptElement('minY', cornerMinimum.y) self.setMetadataNoscriptElement('maxZ', cornerMaximum.z) self.setMetadataNoscriptElement('minZ', cornerMinimum.z) self.margin = float(self.sliceDictionary['margin']) self.textHeight = float(self.sliceDictionary['textHeight']) javascriptControlBoxWidth = float( self.sliceDictionary['javascriptControlBoxWidth']) noJavascriptControlBoxHeight = float( self.sliceDictionary['noJavascriptControlBoxHeight']) controlTop = len(rotatedBoundaryLayers) * ( self.margin + self.extent.y * self.unitScale + self.textHeight) + 2.0 * self.margin + self.textHeight self.svgElement.getFirstChildWithClassName( 'title').text = os.path.basename(fileName) + ' - Slice Layers' svgElementDictionary['height'] = '%spx' % self.getRounded( controlTop + noJavascriptControlBoxHeight + self.margin) # width = margin + (sliceDimX * unitScale) + margin; width = 2.0 * self.margin + max(self.extent.x * self.unitScale, javascriptControlBoxWidth) svgElementDictionary['width'] = '%spx' % self.getRounded(width) self.sliceDictionary['decimalPlacesCarried'] = str( self.decimalPlacesCarried) if self.perimeterWidth != None: self.sliceDictionary['perimeterWidth'] = self.getRounded( self.perimeterWidth) self.sliceDictionary['yAxisPointingUpward'] = 'true' self.sliceDictionary['procedureDone'] = procedureName noJavascriptDictionary = self.svgElement.getXMLElementByID( 'noJavascriptControls').attributeDictionary noJavascriptDictionary['transform'] = 'translate(%s, %s)' % ( self.getRounded(self.margin), self.getRounded(controlTop)) self.svgElement.getXMLElementByID( 'dimXNoJavascript').text = self.getRounded(self.extent.x) self.svgElement.getXMLElementByID( 'dimYNoJavascript').text = self.getRounded(self.extent.y) self.svgElement.getXMLElementByID( 'dimZNoJavascript').text = self.getRounded(self.extent.z) if not self.addLayerTemplateToSVG: self.svgElement.getFirstChildWithClassName( 'script').removeFromIDNameParent() self.svgElement.getXMLElementByID( 'beginningOfControlSection').removeFromIDNameParent() self.svgElement.getXMLElementByID( 'noJavascriptControls').removeFromIDNameParent() self.graphicsXMLElement.removeFromIDNameParent() self.addOriginalAsComment(xmlElement) output = cStringIO.StringIO() output.write(self.xmlParser.beforeRoot) self.svgElement.addXML(0, output) return output.getvalue()
def getReplacedSVGTemplate(self, fileName, procedureName, rotatedLoopLayers, xmlElement=None): 'Get the lines of text from the layer_template.svg file.' self.extent = self.cornerMaximum - self.cornerMinimum svgTemplateText = archive.getFileTextInFileDirectory( __file__, os.path.join('templates', 'layer_template.svg')) self.xmlParser = XMLSimpleReader(fileName, None, svgTemplateText) self.svgElement = self.xmlParser.getRoot() svgElementDictionary = self.svgElement.attributeDictionary self.sliceDictionary = getSliceDictionary(self.svgElement) self.controlBoxHeight = float(self.sliceDictionary['controlBoxHeight']) self.controlBoxWidth = float(self.sliceDictionary['controlBoxWidth']) self.margin = float(self.sliceDictionary['margin']) self.marginTop = float(self.sliceDictionary['marginTop']) self.textHeight = float(self.sliceDictionary['textHeight']) self.unitScale = float(self.sliceDictionary['unitScale']) svgMinWidth = float(self.sliceDictionary['svgMinWidth']) self.controlBoxHeightMargin = self.controlBoxHeight + self.marginTop if not self.addLayerTemplateToSVG: self.svgElement.getXMLElementByID( 'layerTextTemplate').removeFromIDNameParent() del self.svgElement.getXMLElementByID( 'sliceElementTemplate').attributeDictionary['transform'] self.graphicsXMLElement = self.svgElement.getXMLElementByID( 'sliceElementTemplate') self.graphicsXMLElement.attributeDictionary['id'] = 'z:' self.addRotatedLoopLayersToOutput(rotatedLoopLayers) self.setMetadataNoscriptElement('layerThickness', 'Layer Thickness: ', self.layerThickness) self.setMetadataNoscriptElement('maxX', 'X: ', self.cornerMaximum.x) self.setMetadataNoscriptElement('minX', 'X: ', self.cornerMinimum.x) self.setMetadataNoscriptElement('maxY', 'Y: ', self.cornerMaximum.y) self.setMetadataNoscriptElement('minY', 'Y: ', self.cornerMinimum.y) self.setMetadataNoscriptElement('maxZ', 'Z: ', self.cornerMaximum.z) self.setMetadataNoscriptElement('minZ', 'Z: ', self.cornerMinimum.z) self.textHeight = float(self.sliceDictionary['textHeight']) controlTop = len(rotatedLoopLayers) * ( self.margin + self.extent.y * self.unitScale + self.textHeight) + self.marginTop + self.textHeight self.svgElement.getFirstChildWithClassName( 'title').text = os.path.basename(fileName) + ' - Slice Layers' svgElementDictionary['height'] = '%spx' % self.getRounded( max(controlTop, self.controlBoxHeightMargin)) width = max(self.extent.x * self.unitScale, svgMinWidth) svgElementDictionary['width'] = '%spx' % self.getRounded(width) self.sliceDictionary['decimalPlacesCarried'] = str( self.decimalPlacesCarried) if self.perimeterWidth != None: self.sliceDictionary['perimeterWidth'] = self.getRounded( self.perimeterWidth) self.sliceDictionary['yAxisPointingUpward'] = 'true' self.sliceDictionary['procedureName'] = procedureName self.setDimensionTexts('dimX', 'X: ' + self.getRounded(self.extent.x)) self.setDimensionTexts('dimY', 'Y: ' + self.getRounded(self.extent.y)) self.setDimensionTexts('dimZ', 'Z: ' + self.getRounded(self.extent.z)) self.setTexts('numberOfLayers', 'Number of Layers: %s' % len(rotatedLoopLayers)) volume = 0.0 for rotatedLoopLayer in rotatedLoopLayers: volume += euclidean.getAreaLoops(rotatedLoopLayer.loops) volume *= 0.001 self.setTexts('volume', 'Volume: %s cm3' % self.getRounded(volume)) if not self.addLayerTemplateToSVG: self.svgElement.getFirstChildWithClassName( 'script').removeFromIDNameParent() self.svgElement.getXMLElementByID( 'isoControlBox').removeFromIDNameParent() self.svgElement.getXMLElementByID( 'layerControlBox').removeFromIDNameParent() self.svgElement.getXMLElementByID( 'scrollControlBox').removeFromIDNameParent() self.graphicsXMLElement.removeFromIDNameParent() self.addOriginalAsComment(xmlElement) output = cStringIO.StringIO() output.write(self.xmlParser.beforeRoot) self.svgElement.addXML(0, output) return xml_simple_writer.getBeforeRootOutput(self.xmlParser)