This repository has been archived on 2023-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
mightyscape-1.1-deprecated/extensions/fablabchemnitz/cutting_optimizer/cutting_optimizer.py

101 lines
3.5 KiB
Python
Raw Normal View History

2020-08-31 22:57:34 +02:00
#!/usr/bin/env python3
"""
Extension for InkScape 1.0
CutOptim OS Wrapper script to make CutOptim work on Windows and Linux systems without duplicating .inx files
Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org
Date: 31.08.2020
2021-01-14 15:25:38 +01:00
Last patch: 14.01.2021
2020-08-31 22:57:34 +02:00
License: GNU GPL v3
"""
import inkex
import sys
import os
from lxml import etree
2021-04-28 13:05:17 +02:00
import subprocess
2020-08-31 22:57:34 +02:00
class CuttingOptimizer(inkex.EffectExtension):
2021-04-28 13:05:17 +02:00
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):
2020-08-31 22:57:34 +02:00
args = sys.argv[1:]
for arg in args:
key=arg.split("=")[0]
if len(arg.split("=")) == 2:
value=arg.split("=")[1]
try:
2021-04-28 13:05:17 +02:00
if key != "--id":
pars.add_argument(key, default=key)
2020-08-31 22:57:34 +02:00
except:
pass #ignore duplicate id arg
def effect(self):
cmd=""
if os.name == "nt":
cutoptim="CutOptim.exe"
else:
cutoptim="./CutOptim"
cmd += cutoptim
2021-04-28 13:05:17 +02:00
2020-08-31 22:57:34 +02:00
for arg in vars(self.options):
if arg != "output" and arg != "ids" and arg != "selected_nodes":
#inkex.utils.debug(str(arg) + " = " + str(getattr(self.options, arg)))
#fix behaviour of "original" arg which does not correctly gets interpreted if set to false
if arg == "original" and str(getattr(self.options, arg)) == "false":
continue
if arg == "input_file":
cmd += " --file " + str(getattr(self.options, arg))
else:
cmd += " --" + arg + " " + str(getattr(self.options, arg))
2021-01-14 15:25:38 +01:00
output_file = None
2020-08-31 22:57:34 +02:00
if os.name == "nt":
2021-01-14 15:25:38 +01:00
output_file = "cutoptim.svg"
2020-08-31 22:57:34 +02:00
else:
2021-01-14 15:25:38 +01:00
output_file = "/tmp/cutoptim.svg"
cmd += " --output " + output_file
2020-08-31 22:57:34 +02:00
# run CutOptim with the parameters provided
with os.popen(cmd, "r") as cutoptim:
result = cutoptim.read()
# check output existence
try:
2021-01-14 15:25:38 +01:00
stream = open(output_file, 'r')
2020-08-31 22:57:34 +02:00
except FileNotFoundError as e:
2021-04-28 13:05:17 +02:00
inkex.utils.debug("There was no SVG output generated. Cannot continue. Command was:")
inkex.utils.debug(cmd)
2020-08-31 22:57:34 +02:00
exit(1)
2021-04-28 13:05:17 +02:00
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
2020-08-31 22:57:34 +02:00
p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
stream.close()
2021-04-28 13:05:17 +02:00
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)
2020-08-31 22:57:34 +02:00
if __name__ == '__main__':
CuttingOptimizer().run()