def getReplacedSVGTemplate(self, fileName, procedureName, rotatedBoundaryLayers, xmlElement): "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 = gcodec.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() if xmlElement != None: xmlElement.setParentAddToChildren(self.svgElement) 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 = gcodec.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 = gcodec.getFileTextInFileDirectory( settings.__file__, os.path.join('templates', 'canvas_template.svg') ) output = cStringIO.StringIO() lines = gcodec.getTextLines( svgTemplateText ) firstWordTable = {} firstWordTable['height="999px"'] = ' height="%spx"' % int( round( boxHeight ) ) firstWordTable['<!--replaceLineWith_coloredLines-->'] = self.getCanvasLinesOutput() firstWordTable['replaceLineWithTitle'] = gcodec.getSummarizedFileName( self.fileName ) firstWordTable['width="999px"'] = ' width="%spx"' % int( round( boxWidth ) ) for line in lines: parseLineReplace( firstWordTable, line, output ) gcodec.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 = gcodec.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 execute( self ): "Export the canvas as an svg file." svgFileName = gcodec.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 = gcodec.getFileTextInFileDirectory( settings.__file__, os.path.join( 'templates', 'canvas_template.svg' ) ) output = cStringIO.StringIO() lines = gcodec.getTextLines( svgTemplateText ) firstWordTable = {} firstWordTable[ 'height="999px"' ] = ' height="%spx"' % int( round( boxHeight ) ) firstWordTable[ '<!--replaceLineWith_coloredLines-->' ] = self.getCanvasLinesOutput() firstWordTable[ 'replaceLineWithTitle' ] = gcodec.getSummarizedFileName( self.fileName ) firstWordTable[ 'width="999px"' ] = ' width="%spx"' % int( round( boxWidth ) ) for line in lines: parseLineReplace( firstWordTable, line, output ) gcodec.writeFileText( svgFileName, output.getvalue() ) fileExtension = self.fileExtension.value svgProgram = self.svgProgram.value if svgProgram == '': return if svgProgram == 'webbrowser': settings.openWebPage( svgFileName ) return svgFilePath = '"' + os.path.normpath( svgFileName ) + '"' # " to send in file name with spaces shellCommand = svgProgram + ' ' + 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.' % svgProgram ) print( 'If so, try installing the %s program or look for another one, like the Gnu Image Manipulation Program (Gimp) which can be found at:' % svgProgram ) print( 'http://www.gimp.org/' ) return convertedFileName = gcodec.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.' % ( svgProgram, fileExtension ) ) print( 'Try installing the %s program or look for another one, like Image Magick which can be found at:' % svgProgram ) print( 'http://www.imagemagick.org/script/index.php' )
def getReplacedSVGTemplate( self, fileName, procedureName, rotatedBoundaryLayers ): "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 self.addRotatedLoopLayersToOutput( rotatedBoundaryLayers ) svgTemplateText = gcodec.getFileTextInFileDirectory( __file__, os.path.join( 'templates', 'layer_template.svg' ) ) svgTemplateText = getReplacedWordAndInQuotes( 'layerThickness', self.getRounded( self.carving.getCarveLayerThickness() ), svgTemplateText ) svgTemplateText = getReplacedWordAndInQuotes( 'maxX', self.getRounded( cornerMaximum.x ), svgTemplateText ) svgTemplateText = getReplacedWordAndInQuotes( 'minX', self.getRounded( cornerMinimum.x ), svgTemplateText ) svgTemplateText = getReplacedWordAndInQuotes( 'maxY', self.getRounded( cornerMaximum.y ), svgTemplateText ) svgTemplateText = getReplacedWordAndInQuotes( 'minY', self.getRounded( cornerMinimum.y ), svgTemplateText ) svgTemplateText = getReplacedWordAndInQuotes( 'maxZ', self.getRounded( cornerMaximum.z ), svgTemplateText ) svgTemplateText = getReplacedWordAndInQuotes( 'minZ', self.getRounded( cornerMinimum.z ), svgTemplateText ) lines = gcodec.getTextLines( svgTemplateText ) self.margin = getParameterFromJavascript( lines, 'margin', self.margin ) self.textHeight = getParameterFromJavascript( lines, 'textHeight', self.textHeight ) javascriptControlsWidth = getParameterFromJavascript( lines, 'javascripControlBoxX', 510.0 ) noJavascriptControlsHeight = getParameterFromJavascript( lines, 'noJavascriptControlBoxY', 110.0 ) controlTop = len( rotatedBoundaryLayers ) * ( self.margin + self.extent.y * self.unitScale + self.textHeight ) + 2.0 * self.margin + self.textHeight # width = margin + (sliceDimX * unitScale) + margin; width = 2.0 * self.margin + max( self.extent.x * self.unitScale, javascriptControlsWidth ) summarizedFileName = gcodec.getSummarizedFileName( fileName ) + ' SVG Slice File' noJavascriptControlsTagString = ' <g id="noJavascriptControls" fill="#000" transform="translate(%s, %s)">' % ( self.getRounded( self.margin ), self.getRounded( controlTop ) ) firstWordTable = {} firstWordTable[ 'height="999px"' ] = ' height="%spx"' % self.getRounded( controlTop + noJavascriptControlsHeight + self.margin ) firstWordTable[ 'width="999px"' ] = ' width="%spx"' % self.getRounded( width ) firstWordTable[ '<!--replaceLineWith_boundaryLayerLines-->' ] = self.output.getvalue() firstWordTable[ '<!--replaceLineWith_emptyString-->' ] = '' firstWordTable[ '<!--replaceLineWith_noJavascriptControls-->' ] = noJavascriptControlsTagString firstWordTable[ '<!--replaceLineWith_sliceVariableLines-->' ] = self.getInitializationForOutputSVG( procedureName ) replaceWithTable = {} replaceWithTable[ 'replaceWith_Title' ] = summarizedFileName replaceWithTable[ 'replaceWith_dimX' ] = self.getRounded( self.extent.x ) replaceWithTable[ 'replaceWith_dimY' ] = self.getRounded( self.extent.y ) replaceWithTable[ 'replaceWith_dimZ' ] = self.getRounded( self.extent.z ) output = cStringIO.StringIO() for line in lines: parseLineReplaceWithTable( firstWordTable, line, output, replaceWithTable ) return output.getvalue()
def getReplacedSVGTemplate(self, fileName, procedureName, rotatedBoundaryLayers, xmlElement): "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 = gcodec.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() if xmlElement != None: xmlElement.setParentAddToChildren(self.svgElement) output = cStringIO.StringIO() output.write(self.xmlParser.beforeRoot) self.svgElement.addXML(0, output) return output.getvalue()