make plycutter working correctly

This commit is contained in:
Mario Voigt 2021-10-31 21:31:13 +01:00
parent 33098180b4
commit bc6966c91b
4 changed files with 34 additions and 44 deletions

View File

@ -1,10 +1,10 @@
[
{
"name": "PlyCutter (Unstable)",
"name": "PlyCutter",
"id": "fablabchemnitz.de.plycutter",
"path": "plycutter",
"dependent_extensions": null,
"original_name": "PlyCutter (Unstable)",
"original_name": "PlyCutter",
"original_id": "fablabchemnitz.de.plycutter",
"license": "GNU AGPL v3",
"license_url": "https://github.com/tjltjl/plycutter/blob/master/LICENSE-agpl-3.0.txt",

@ -1 +1 @@
Subproject commit 92b545edd5f12d2e88e0b440e498fac55f0046e2
Subproject commit 7d07e897af9707c87743326108823f48a0022b7d

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>PlyCutter (Unstable)</name>
<name>PlyCutter</name>
<id>fablabchemnitz.de.plycutter</id>
<param name="tab" type="notebook">
<page name="tab_settings" gui-text="PlyCutter">

View File

@ -25,10 +25,6 @@ class PlyCutter(inkex.EffectExtension):
pars.add_argument("--final_dilation", default=0.05, type=float, help="Final dilation (laser cutter kerf compensation)")
pars.add_argument("--random_seed", type=int, default=42, help="Random seed for pseudo-random heuristics")
#-o OUTPUT_FILE, --output_file OUTPUT_FILE
# File to write the DXF output in (default: None)
def effect(self):
stl_input = self.options.infile
if not os.path.exists(stl_input):
@ -36,13 +32,13 @@ class PlyCutter(inkex.EffectExtension):
exit(1)
# Prepare output
dxf_output = os.path.join(tempfile.gettempdir(), os.path.splitext(os.path.basename(stl_input))[0] + ".dxf")
svg_output = os.path.join(tempfile.gettempdir(), os.path.splitext(os.path.basename(stl_input))[0] + ".svg")
basename = os.path.splitext(os.path.basename(stl_input))[0]
svg_output = os.path.join(tempfile.gettempdir(), basename + ".svg")
# Clean up possibly previously generated output file from plycutter
if os.path.exists(dxf_output):
if os.path.exists(svg_output):
try:
os.remove(dxf_output)
os.remove(svg_output)
except OSError as e:
inkex.utils.debug("Error while deleting previously generated output file " + stl_input)
@ -55,22 +51,24 @@ class PlyCutter(inkex.EffectExtension):
plycutter_cmd += "--support_radius " + str(self.options.support_radius) + " "
plycutter_cmd += "--final_dilation " + str(self.options.final_dilation) + " "
plycutter_cmd += "--random_seed " + str(self.options.random_seed) + " "
plycutter_cmd += "-o \"" + dxf_output + "\" "
plycutter_cmd += "--format svg " #static
plycutter_cmd += "-o \"" + svg_output + "\" "
plycutter_cmd += "\"" + stl_input + "\""
#print command
inkex.utils.debug(plycutter_cmd)
#inkex.utils.debug(plycutter_cmd)
#os.system(plycutter_cmd) #does not work
#create a new env for subprocess which does not contain extensions dir because there's a collision with "rtree.py"
pypath = ''
for d in sys.path:
if d != '/usr/share/inkscape/extensions':
pypath = pypath + d + ';'
neutral_env = os.environ.copy()
neutral_env['PYTHONPATH'] = pypath
#pid=os.fork()
#if pid==0: # new process
# os.system("nohup " + plycutter_cmd + " &") #does not work too
# exit()
# parent process continues
p = Popen(plycutter_cmd, shell=True, stdout=PIPE, stderr=PIPE)
p = Popen(plycutter_cmd, shell=True, stdout=PIPE, stderr=PIPE, env=neutral_env)
stdout, stderr = p.communicate()
p.wait()
if p.returncode != 0:
inkex.utils.debug("PlyCutter failed: %d %s %s" % (p.returncode,
@ -78,39 +76,31 @@ class PlyCutter(inkex.EffectExtension):
str(stderr).replace('\\n', '\n').replace('\\t', '\t'))
)
exit(1)
if not os.path.exists(dxf_output):
inkex.utils.debug("There was no DXF output generated by PlyCutter. Please check your model file.")
exit(1)
# Convert the DXF output to SVG
wd = os.path.join(os.getcwd(), "kabeja")
proc = subprocess.Popen("java -jar launcher.jar -nogui -pipeline svg " + dxf_output + " " + svg_output, cwd=wd, shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
inkex.errormsg("kabeja failed: %d %s %s" % (proc.returncode, stdout, stderr))
elif self.options.debug is True:
inkex.utils.debug("PlyCutter debug output: %d %s %s" % (p.returncode,
str(stdout).replace('\\n', '\n').replace('\\t', '\t'),
str(stderr).replace('\\n', '\n').replace('\\t', '\t'))
)
# Write the generated SVG into InkScape's canvas
try:
stream = open(svg_output, 'r')
except FileNotFoundError as e:
inkex.utils.debug("There was no SVG output generated by kabeja. Cannot continue")
inkex.utils.debug("There was no SVG output generated by PlyCutter. Please check your model file.")
exit(1)
p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True)).getroot()
stream.close()
dxfGroup = inkex.Group(id=self.svg.get_unique_id("plycutter-"))
for element in doc.iter("{http://www.w3.org/2000/svg}g"):
dxfGroup.append(element)
self.document.getroot().add(dxfGroup)
#apply scale factor
translation_matrix = [[self.options.scalefactor, 0.0, 0.0], [0.0, self.options.scalefactor, 0.0]]
dxfGroup.transform = Transform(translation_matrix) * dxfGroup.transform
g = inkex.Group(id=self.svg.get_unique_id("plycutter-"))
g.insert(0, inkex.Desc("Imported file: {}".format(self.options.infile)))
self.svg.get_current_layer().add(g)
for element in doc.iter("{http://www.w3.org/2000/svg}path"):
g.append(element)
#Adjust viewport and width/height to have the import at the center of the canvas
if self.options.resizetoimport:
bbox = dxfGroup.bounding_box() #does not work. why?
bbox = g.bounding_box() #does not work correctly if only 2 objects are inside (strangely). need at least 3 (e.g. one svg:desc and 2 svg:path elements)
if bbox is not None:
root = self.svg.getElement('//svg:svg');
offset = self.svg.unittouu(str(self.options.extraborder) + self.options.extraborder_units)