From dd3b6aa099175e2244e1e04dde45bf21a966425e Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Thu, 28 Dec 2017 16:37:33 +0100 Subject: Add option to convert objects to paths before plotting Added an option to extensions->export->plot and the HPGL export feature to nondestructively convert objects to paths before conversion. --- share/extensions/hpgl_encoder.py | 26 +++++++++++++++++++++++++- share/extensions/hpgl_output.inx | 1 + share/extensions/hpgl_output.py | 1 + share/extensions/plotter.inx | 1 + share/extensions/plotter.py | 1 + 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/share/extensions/hpgl_encoder.py b/share/extensions/hpgl_encoder.py index 04387a91b..353b8aa0a 100644 --- a/share/extensions/hpgl_encoder.py +++ b/share/extensions/hpgl_encoder.py @@ -22,6 +22,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import math import re import string +from subprocess import Popen, PIPE +from shutil import copy2 # local libraries import bezmisc import cspsubdiv @@ -52,9 +54,13 @@ class hpglEncoder: "precut":bool "autoAlign":bool "debug":bool + "convertObjects":bool ''' self.options = effect.options - self.doc = effect.document.getroot() + if self.options.convertObjects: + self.doc = self.convertObjectsToPaths(effect.args[-1], effect.document) + else: + self.doc = effect.document.getroot() self.docWidth = effect.unittouu(self.doc.get('width')) self.docHeight = effect.unittouu(self.doc.get('height')) self.hpgl = '' @@ -104,6 +110,24 @@ class hpglEncoder: self.viewBoxTransformX = self.docWidth / effect.unittouu(effect.addDocumentUnit(viewBox2[2])) self.viewBoxTransformY = self.docHeight / effect.unittouu(effect.addDocumentUnit(viewBox2[3])) + def convertObjectsToPaths(self, file, document): + tempfile = inkex.os.path.splitext(file)[0] + "-prepare.svg" + # tempfile is needed here only because we want to force the extension to be .svg + # so that we can open and close it silently + copy2(file, tempfile) + + # Unfortunately this briefly pops up the GUI and cannot be done with -z, see https://bugs.launchpad.net/inkscape/+bug/843260 + p = Popen('inkscape --verb=EditSelectAllInAllLayers --verb=EditUnlinkClone --verb=ObjectToPath --verb=FileSave --verb=FileQuit ' + tempfile, shell=True, stdout=PIPE, stderr=PIPE) + (out, err) = p.communicate() + + if p.returncode != 0: + inkex.errormsg(_("Failed to convert objects to paths. Continued without converting.")) + inkex.errormsg(out) + inkex.errormsg(err) + return document.getroot() + else: + return inkex.etree.parse(tempfile).getroot() + def getHpgl(self): # dryRun to find edges groupmat = [[self.mirrorX * self.scaleX * self.viewBoxTransformX, 0.0, 0.0], [0.0, self.mirrorY * self.scaleY * self.viewBoxTransformY, 0.0]] diff --git a/share/extensions/hpgl_output.inx b/share/extensions/hpgl_output.inx index 673be29bb..609fc03d4 100644 --- a/share/extensions/hpgl_output.inx +++ b/share/extensions/hpgl_output.inx @@ -32,6 +32,7 @@ true 1.2 true + true <_param name="settingsHelp" type="description">All these settings depend on the plotter you use, for more information please consult the manual or homepage for your plotter. diff --git a/share/extensions/hpgl_output.py b/share/extensions/hpgl_output.py index d4a23743f..fc212b449 100755 --- a/share/extensions/hpgl_output.py +++ b/share/extensions/hpgl_output.py @@ -44,6 +44,7 @@ class HpglOutput(inkex.Effect): self.OptionParser.add_option('--precut', action='store', type='inkbool', dest='precut', default='TRUE', help='Use precut') self.OptionParser.add_option('--flat', action='store', type='float', dest='flat', default=1.2, help='Curve flatness') self.OptionParser.add_option('--autoAlign', action='store', type='inkbool', dest='autoAlign', default='TRUE', help='Auto align') + self.OptionParser.add_option('--convertObjects',action='store', type='inkbool', dest='convertObjects',default='TRUE', help='Convert objects to paths') def effect(self): self.options.debug = False diff --git a/share/extensions/plotter.inx b/share/extensions/plotter.inx index c91857b05..9b112938e 100644 --- a/share/extensions/plotter.inx +++ b/share/extensions/plotter.inx @@ -87,6 +87,7 @@ true   false + true <_param name="settingsHelp" type="description">All these settings depend on the plotter you use, for more information please consult the manual or homepage for your plotter. diff --git a/share/extensions/plotter.py b/share/extensions/plotter.py index 1d8e8f79e..43d70eebc 100755 --- a/share/extensions/plotter.py +++ b/share/extensions/plotter.py @@ -56,6 +56,7 @@ class Plot(inkex.Effect): self.OptionParser.add_option('--flat', action='store', type='float', dest='flat', default=1.2, help='Curve flatness') self.OptionParser.add_option('--autoAlign', action='store', type='inkbool', dest='autoAlign', default='TRUE', help='Auto align') self.OptionParser.add_option('--debug', action='store', type='inkbool', dest='debug', default='FALSE', help='Show debug information') + self.OptionParser.add_option('--convertObjects', action='store', type='inkbool', dest='convertObjects', default='TRUE', help='Convert objects to paths') def effect(self): # get hpgl data -- cgit v1.2.3