From 4bc3b2457264031cdf4fc57dd0f48b118e4667f3 Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Mon, 5 Apr 2021 22:16:06 +0200 Subject: [PATCH] better bbox resizing for dxf2papercraft, dxfdwgimporter and papercraft_unfold --- .../dxf2papercraft/dxf2papercraft.py | 28 +++---------------- .../dxfdwgimporter/dxfdwgimporter.py | 24 ++-------------- .../papercraft_unfold/papercraft_unfold.py | 27 ++---------------- 3 files changed, 9 insertions(+), 70 deletions(-) diff --git a/extensions/fablabchemnitz/dxf2papercraft/dxf2papercraft.py b/extensions/fablabchemnitz/dxf2papercraft/dxf2papercraft.py index 7f9bc4eb..3d8d8ec4 100644 --- a/extensions/fablabchemnitz/dxf2papercraft/dxf2papercraft.py +++ b/extensions/fablabchemnitz/dxf2papercraft/dxf2papercraft.py @@ -127,36 +127,16 @@ class PapercraftUnfold(inkex.Effect): doc.set('width','') doc.set('height','') doc.set('viewBox','') - doc.getchildren()[0].set('transform','') + doc.getchildren()[1].set('transform','') #this removes the "transform:scale(1, -1)" from child within dxf2papercraft- group #apply scale factor node = doc.getchildren()[1] translation_matrix = [[self.options.scalefactor, 0.0, 0.0], [0.0, self.options.scalefactor, 0.0]] node.transform = Transform(translation_matrix) * node.transform - #Adjust viewport and width/height to have the import at the center of the canvas - unstable at the moment. - if self.options.resizetoimport: - elements = [] - for child in doc.getchildren(): - #if child.tag == inkex.addNS('g','svg'): - elements.append(child) - - #build sum of bounding boxes and ignore errors for faulty elements (sum function often fails for that usecase!) - bbox = None - try: - bbox = elements[0].bounding_box() #init with the first bounding box of the tree (and hope that it is not a faulty one) - except Exception as e: - #inkex.utils.debug(str(e)) - pass - count = 0 - for element in elements: - if count != 0: #skip the first - try: - bbox += element.bounding_box() - except Exception as e: - #inkex.utils.debug(str(e)) - pass - count += 1 #some stupid counter + #Adjust viewport and width/height to have the import at the center of the canvas + if self.options.resizetoimport: + bbox = inkex.elements._selected.ElementList.bounding_box(node) if bbox is not None: root = self.svg.getElement('//svg:svg'); offset = self.svg.unittouu(str(self.options.extraborder) + self.options.extraborder_units) diff --git a/extensions/fablabchemnitz/dxfdwgimporter/dxfdwgimporter.py b/extensions/fablabchemnitz/dxfdwgimporter/dxfdwgimporter.py index 94c8b031..41409693 100644 --- a/extensions/fablabchemnitz/dxfdwgimporter/dxfdwgimporter.py +++ b/extensions/fablabchemnitz/dxfdwgimporter/dxfdwgimporter.py @@ -427,29 +427,9 @@ class DXFDWGImport(inkex.EffectExtension): doc.set('viewBox','') doc.getchildren()[0].set('transform','') - #adjust viewport and width/height to have the import at the center of the canvas - unstable at the moment. + #adjust viewport and width/height to have the import at the center of the canvas if self.options.resizetoimport: - elements = [] - for child in doc.getchildren(): - #if child.tag == inkex.addNS('g','svg'): - elements.append(child) - - #build sum of bounding boxes and ignore errors for faulty elements (sum function often fails for that usecase!) - bbox = None - try: - bbox = elements[0].bounding_box() #init with the first bounding box of the tree (and hope that it is not a faulty one) - except Exception as e: - #inkex.utils.debug(str(e)) - pass - count = 0 - for element in elements: - if count != 0: #skip the first - try: - bbox += element.bounding_box() - except Exception as e: - #inkex.utils.debug(str(e)) - pass - count += 1 #some stupid counter + bbox = inkex.elements._selected.ElementList.bounding_box(doc.getchildren()[0]) if bbox is not None: root = self.svg.getElement('//svg:svg'); offset = self.svg.unittouu(str(self.options.extraborder) + self.options.extraborder_units) diff --git a/extensions/fablabchemnitz/papercraft_unfold/papercraft_unfold.py b/extensions/fablabchemnitz/papercraft_unfold/papercraft_unfold.py index bf6901ac..4452b070 100644 --- a/extensions/fablabchemnitz/papercraft_unfold/papercraft_unfold.py +++ b/extensions/fablabchemnitz/papercraft_unfold/papercraft_unfold.py @@ -182,30 +182,9 @@ class Unfold(inkex.Effect): doc.set('id', self.svg.get_unique_id('papercraft_unfold')) self.document.getroot().append(doc) - #adjust viewport and width/height to have the import at the center of the canvas - unstable at the moment. - if self.options.resizetoimport: - elements = [] - for child in doc.getchildren(): - #if child.tag == inkex.addNS('g','svg'): - elements.append(child) - - #build sum of bounding boxes and ignore errors for faulty elements (sum function often fails for that usecase!) - bbox = None - try: - bbox = elements[0].bounding_box() #init with the first bounding box of the tree (and hope that it is not a faulty one) - except Exception as e: - #inkex.utils.debug(str(e)) - pass - count = 0 - for element in elements: - if count != 0: #skip the first - try: - #bbox.add(element.bounding_box()) - bbox += element.bounding_box() - except Exception as e: - #inkex.utils.debug(str(e)) - pass - count += 1 #some stupid counter + #adjust viewport and width/height to have the import at the center of the canvas + if self.options.resizetoimport: + bbox = inkex.elements._selected.ElementList.bounding_box(doc) if bbox is not None: root = self.svg.getElement('//svg:svg'); offset = self.svg.unittouu(str(self.options.extraborder) + self.options.extraborder_units)