better bbox resizing for dxf2papercraft, dxfdwgimporter and

papercraft_unfold
This commit is contained in:
Mario Voigt 2021-04-05 22:16:06 +02:00
parent 93a1009130
commit 4bc3b24572
3 changed files with 9 additions and 70 deletions

View File

@ -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 <svg:g id="draft"> child within dxf2papercraft-<id> 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.
#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(node)
if bbox is not None:
root = self.svg.getElement('//svg:svg');
offset = self.svg.unittouu(str(self.options.extraborder) + self.options.extraborder_units)

View File

@ -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)

View File

@ -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.
#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.add(element.bounding_box())
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)
if bbox is not None:
root = self.svg.getElement('//svg:svg');
offset = self.svg.unittouu(str(self.options.extraborder) + self.options.extraborder_units)