Commit dd3b6aa0 authored by Arnout Engelen's avatar Arnout Engelen

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.
parent e6b171ab
......@@ -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]]
......
......@@ -32,6 +32,7 @@
<param name="precut" type="boolean" _gui-text="Precut" _gui-description="Check this to cut a small line before the real drawing starts to correctly align the tool orientation. (Default: Checked)">true</param>
<param name="flat" type="float" min="0.1" max="10.0" precision="1" _gui-text="Curve flatness:" _gui-description="Curves are divided into lines, this number controls how fine the curves will be reproduced, the smaller the finer (Default: '1.2')">1.2</param>
<param name="autoAlign" type="boolean" _gui-text="Auto align" _gui-description="Check this to auto align the drawing to the zero point (Plus the tool offset if used). If unchecked you have to make sure that all parts of your drawing are within the document border! (Default: Checked)">true</param>
<param name="convertObjects" type="boolean" _gui-text="Convert objects to paths" _gui-description="Check this to automatically (nondestructively) convert all objects to paths before plotting (Default: Checked)">true</param>
</page>
</param>
<_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.</_param>
......
......@@ -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
......
......@@ -87,6 +87,7 @@
<param name="autoAlign" type="boolean" _gui-text="Auto align" _gui-description="Check this to auto align the drawing to the zero point (Plus the tool offset if used). If unchecked you have to make sure that all parts of your drawing are within the document border! (Default: Checked)">true</param>
<param name="space" type="description">&#xa0;</param>
<param name="debug" type="boolean" _gui-text="Show debug information" _gui-description="Check this to get verbose information about the plot without actually sending something to the plotter (A.k.a. data dump) (Default: Unchecked)">false</param>
<param name="convertObjects" type="boolean" _gui-text="Convert objects to paths" _gui-description="Check this to automatically (nondestructively) convert all objects to paths before plotting (Default: Checked)">true</param>
</page>
</param>
<_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.</_param>
......
......@@ -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
......
  • I just tried this with a document containing only a rectangle. The error message I get is:

    "No paths where found. Please convert all objects you want to plot into paths."

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