some fixes in cutoptim

This commit is contained in:
leyghisbb 2021-04-28 13:05:17 +02:00
parent 47894c0fce
commit b5b2179c8c
4 changed files with 43 additions and 89 deletions

View File

@ -34,8 +34,11 @@ class boxesPyWrapper(inkex.GenerateExtension):
box_file = "box.svg" box_file = "box.svg"
if os.path.exists(box_file): if os.path.exists(box_file):
os.remove(box_file) #remove previously generated box file at the beginning os.remove(box_file) #remove previously generated box file at the beginning
cmd = "boxes" if os.name == "nt":
cmd = "boxes.exe"
else:
cmd = "./boxes"
for arg in vars(self.options): for arg in vars(self.options):
if arg != "output" and arg != "ids" and arg != "selected_nodes": if arg != "output" and arg != "ids" and arg != "selected_nodes":
#inkex.utils.debug(str(arg) + " = " + str(getattr(self.options, arg))) #inkex.utils.debug(str(arg) + " = " + str(getattr(self.options, arg)))
@ -61,18 +64,16 @@ class boxesPyWrapper(inkex.GenerateExtension):
inkex.utils.debug(str(cmd)) inkex.utils.debug(str(cmd))
exit(1) exit(1)
# write the generated SVG into InkScape's canvas # write the generated SVG into Inkscape's canvas
p = etree.XMLParser(huge_tree=True) p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True)) doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
stream.close() stream.close()
if os.path.exists(box_file): if os.path.exists(box_file):
os.remove(box_file) #remove previously generated box file at the end too os.remove(box_file) #remove previously generated box file at the end too
group = inkex.Group(id="boxes.py") group = inkex.Group(id="boxes.py")
for element in doc.getroot(): for element in doc.getroot():
group.append(element) group.append(element)
return group return group
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -4,7 +4,7 @@
<id>fablabchemnitz.de.cutoptim</id> <id>fablabchemnitz.de.cutoptim</id>
<param name="unit" type="optiongroup" appearance="combo" gui-text="Unit"> <param name="unit" type="optiongroup" appearance="combo" gui-text="Unit">
<option value="mm">mm</option> <option value="mm">mm</option>
<option value="cm">cm</option> <!--<option value="cm">cm</option>
<option value="m">m</option> <option value="m">m</option>
<option value="km">km</option> <option value="km">km</option>
<option value="in">in</option> <option value="in">in</option>
@ -12,11 +12,11 @@
<option value="yd">yd</option> <option value="yd">yd</option>
<option value="pt">pt</option> <option value="pt">pt</option>
<option value="px">px</option> <option value="px">px</option>
<option value="pc">pc</option> <option value="pc">pc</option>-->
</param> </param>
<param name="distance" type="float" min="0" max="10.0" gui-text="Min distance between objects">2.0</param> <param name="distance" type="float" min="0.0" max="10.0" precision="2" gui-text="Min distance between objects">2.0</param>
<param name="max_length" type="float" min="0" max="1000" gui-text="Max length of single segment">1000</param> <param name="max_length" type="float" min="0" max="1000" gui-text="Max length of single segment">1000</param>
<param name="optimizing_level" type="int" min="1" max="10" gui-text="Optimizing level">1</param> <param name="optimizing_level" type="int" min="1" max="3" gui-text="Optimizing level">1</param>
<param name="original" type="bool" gui-text="Keep original layer in output">false</param> <param name="original" type="bool" gui-text="Keep original layer in output">false</param>
<param name="firstpos" type="optiongroup" appearance="combo" gui-text="Select option for largest element placement: "> <param name="firstpos" type="optiongroup" appearance="combo" gui-text="Select option for largest element placement: ">
<option value="TL">Top Left</option> <option value="TL">Top Left</option>
@ -32,7 +32,7 @@
<param name="free_rot" type="bool" gui-text="Allow free rotation of paths, angle parameter not used">true</param> <param name="free_rot" type="bool" gui-text="Allow free rotation of paths, angle parameter not used">true</param>
<param name="angle" type="float" min="0" max="180" gui-text="Try rotation by (0 no rotation allowed)">0</param> <param name="angle" type="float" min="0" max="180" gui-text="Try rotation by (0 no rotation allowed)">0</param>
<param name="nested" type="bool" gui-text="Attach nested path to the bigger one">true</param> <param name="nested" type="bool" gui-text="Attach nested path to the bigger one">true</param>
<param name="debug_file" type="bool" gui-text="Debug file generation">true</param> <param name="debug_file" type="bool" gui-text="Generate and open debug file">true</param>
<effect> <effect>
<object-type>all</object-type> <object-type>all</object-type>
<effects-menu> <effects-menu>

View File

@ -17,17 +17,26 @@ import inkex
import sys import sys
import os import os
from lxml import etree from lxml import etree
import subprocess
class CutOptimWrapper(inkex.EffectExtension): class CutOptimWrapper(inkex.EffectExtension):
def __init__(self):
inkex.Effect.__init__(self) def openDebugFile(self, file):
DETACHED_PROCESS = 0x00000008
if os.name == 'nt':
subprocess.Popen(["explorer", file], close_fds=True, creationflags=DETACHED_PROCESS).wait()
else:
subprocess.Popen(["xdg-open", file], close_fds=True, start_new_session=True).wait()
def add_arguments(self, pars):
args = sys.argv[1:] args = sys.argv[1:]
for arg in args: for arg in args:
key=arg.split("=")[0] key=arg.split("=")[0]
if len(arg.split("=")) == 2: if len(arg.split("=")) == 2:
value=arg.split("=")[1] value=arg.split("=")[1]
try: try:
self.arg_parser.add_argument(key, default=key) if key != "--id":
pars.add_argument(key, default=key)
except: except:
pass #ignore duplicate id arg pass #ignore duplicate id arg
@ -37,8 +46,8 @@ class CutOptimWrapper(inkex.EffectExtension):
cutoptim="CutOptim.exe" cutoptim="CutOptim.exe"
else: else:
cutoptim="./CutOptim" cutoptim="./CutOptim"
#inkex.utils.debug(cmd)
cmd += cutoptim cmd += cutoptim
for arg in vars(self.options): for arg in vars(self.options):
if arg != "output" and arg != "ids" and arg != "selected_nodes": if arg != "output" and arg != "ids" and arg != "selected_nodes":
#inkex.utils.debug(str(arg) + " = " + str(getattr(self.options, arg))) #inkex.utils.debug(str(arg) + " = " + str(getattr(self.options, arg)))
@ -57,7 +66,6 @@ class CutOptimWrapper(inkex.EffectExtension):
output_file = "/tmp/cutoptim.svg" output_file = "/tmp/cutoptim.svg"
cmd += " --output " + output_file cmd += " --output " + output_file
#inkex.utils.debug(str(cmd))
# run CutOptim with the parameters provided # run CutOptim with the parameters provided
with os.popen(cmd, "r") as cutoptim: with os.popen(cmd, "r") as cutoptim:
@ -67,14 +75,27 @@ class CutOptimWrapper(inkex.EffectExtension):
try: try:
stream = open(output_file, 'r') stream = open(output_file, 'r')
except FileNotFoundError as e: except FileNotFoundError as e:
inkex.utils.debug("There was no SVG output generated. Cannot continue") inkex.utils.debug("There was no SVG output generated. Cannot continue. Command was:")
inkex.utils.debug(cmd)
exit(1) exit(1)
# write the generated SVG into InkScape's canvas if self.options.original == "false": #we need to use string representation of bool
for element in self.document.getroot():
if isinstance(element, inkex.ShapeElement):
element.delete()
if self.options.debug_file == "true": #we need to use string representation of bool
self.openDebugFile("Debug_CutOptim.txt")
# write the generated SVG into Inkscape's canvas
p = etree.XMLParser(huge_tree=True) p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True)) doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
stream.close() stream.close()
doc.write(sys.stdout.buffer) group = inkex.Group(id="CutOptim")
targetLayer = doc.xpath('//svg:g[@inkscape:label="Placed_Layer"]', namespaces=inkex.NSS)[0]#.getchildren()[0]
for element in targetLayer.getchildren():
group.append(element)
self.document.getroot().append(group)
if __name__ == '__main__': if __name__ == '__main__':
CutOptimWrapper().run() CutOptimWrapper().run()

View File

@ -1,68 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with CutOptim (http://www.fablab-lannion.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="210mm"
height="297mm"
viewBox="0 0 210 297"
version="1.1"
id="svg8"
sodipodi:docname="C:\Users\tomate\AppData\Local\Temp\ink_ext_XXXXXX.svgQEHBX0">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:document-units="mm"
inkscape:current-layer="Placed_Layer"
showgrid="false"
inkscape:window-maximized="1" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Placed_Layer"
inkscape:groupmode="layer"
id="Placed_Layer1">
<path
style="fill:none;stroke:#000000;stroke-width:0.264583"
d="M 49.059378,37.701578 L 93.660563,73.987295 L 46.035558,121.612283 L 32.428421,77.767051 L 49.059378,37.701578 z"
id="Placed_path1141_0"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583"
d="M 3.353129,0.703400 L 47.954333,36.989106 L 0.329333,84.614106 L -13.277807,40.768861 L 3.353129,0.703400 z"
id="Placed_path1141-8_0"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583"
d="M 45.633913,121.534615 L 1.032732,85.248901 L 48.657732,37.623901 L 62.264872,81.469147 L 45.633913,121.534615 z"
id="Placed_path1141-9_0"
inkscape:connector-curvature="0" />
<path
style="fill:none;stroke:#000000;stroke-width:0.264583"
d="M 91.340129,158.532802 L 46.738955,122.247081 L 94.363963,74.622090 L 107.971103,118.467338 L 91.340129,158.532802 z"
id="Placed_path1141-0_0"
inkscape:connector-curvature="0" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 2.5 KiB