fix multiple issues in bbox adjust

This commit is contained in:
Mario Voigt 2023-08-21 15:17:54 +02:00
parent c2b0593fab
commit 50253d51ee
2 changed files with 10 additions and 11 deletions

View File

@ -4,6 +4,8 @@
<id>fablabchemnitz.de.epilog_dashboard_bbox_adjust</id> <id>fablabchemnitz.de.epilog_dashboard_bbox_adjust</id>
<param name="tab" type="notebook"> <param name="tab" type="notebook">
<page name="tab_settings" gui-text="Settings"> <page name="tab_settings" gui-text="Settings">
<label appearance="header">Note on selection</label>
<label>The extension result is different depending on item selection. If selection is empty, whole document is processed!</label>
<param name="apply_transformations" type="bool" gui-text="Apply transformations (requires separate extension)" gui-description="This will call the extension 'Apply Transformations'. Helps avoiding geometry shifting">false</param> <param name="apply_transformations" type="bool" gui-text="Apply transformations (requires separate extension)" gui-description="This will call the extension 'Apply Transformations'. Helps avoiding geometry shifting">false</param>
<param name="offset" type="float" min="0.0" max="1000.0" precision="3" gui-text="XY Offset (mm) from top left corner">1.0</param> <param name="offset" type="float" min="0.0" max="1000.0" precision="3" gui-text="XY Offset (mm) from top left corner">1.0</param>
<param name="removal" gui-text="Element removal" type="optiongroup" appearance="combo" gui-description="Remove all elements outside the bounding box or selection. PObjects partially outside the canvas will be dropped too in case you selected 'outside of canvas'"> <param name="removal" gui-text="Element removal" type="optiongroup" appearance="combo" gui-description="Remove all elements outside the bounding box or selection. PObjects partially outside the canvas will be dropped too in case you selected 'outside of canvas'">

View File

@ -118,12 +118,6 @@ class EpilogDashboardBboxAdjust(inkex.EffectExtension):
self.document.getroot().attrib['viewBox'] = f'{viewBoxXmin} {viewBoxYmin} {viewBoxXmax} {viewBoxYmax}' self.document.getroot().attrib['viewBox'] = f'{viewBoxXmin} {viewBoxYmin} {viewBoxXmax} {viewBoxYmax}'
self.document.getroot().attrib['height'] = height self.document.getroot().attrib['height'] = height
# translate all elements to fit the adjusted viewBox
mat = Transform("translate(%f, %f)" % (-bbox.left,-bbox.top))
for element in self.document.getroot().iter("*"):
if isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('g', 'svg'):
element.transform = Transform(mat) @ element.composed_transform()
if self.options.removal == "outside_canvas": if self.options.removal == "outside_canvas":
for element in self.document.getroot().iter("*"): for element in self.document.getroot().iter("*"):
if isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('g', 'svg'): if isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('g', 'svg'):
@ -132,7 +126,6 @@ class EpilogDashboardBboxAdjust(inkex.EffectExtension):
#inkex.utils.debug("{:02f} > {:02f} {}".format(ebbox.left, viewBoxXmax, ebbox.left > viewBoxXmax)) #inkex.utils.debug("{:02f} > {:02f} {}".format(ebbox.left, viewBoxXmax, ebbox.left > viewBoxXmax))
#inkex.utils.debug("{:02f} < {:02f} {}".format(ebbox.top, viewBoxYmin, ebbox.top < viewBoxYmin)) #inkex.utils.debug("{:02f} < {:02f} {}".format(ebbox.top, viewBoxYmin, ebbox.top < viewBoxYmin))
#inkex.utils.debug("{:02f} > {:02f} {}".format(ebbox.bottom, viewBoxYmax, ebbox.bottom > viewBoxYmax)) #inkex.utils.debug("{:02f} > {:02f} {}".format(ebbox.bottom, viewBoxYmax, ebbox.bottom > viewBoxYmax))
#self.msg("{} | bbox: left = {:0.3f} right = {:0.3f} top = {:0.3f} bottom = {:0.3f}".format(element.get('id'), ebbox.left, ebbox.right, ebbox.top, ebbox.bottom)) #self.msg("{} | bbox: left = {:0.3f} right = {:0.3f} top = {:0.3f} bottom = {:0.3f}".format(element.get('id'), ebbox.left, ebbox.right, ebbox.top, ebbox.bottom))
#check if the element's bbox is inside the view canvas. If not: delete it! #check if the element's bbox is inside the view canvas. If not: delete it!
if ebbox.right < viewBoxXmin or \ if ebbox.right < viewBoxXmin or \
@ -142,21 +135,25 @@ class EpilogDashboardBboxAdjust(inkex.EffectExtension):
if self.options.debug is True: if self.options.debug is True:
self.msg("Removing {} {}".format(element.get('id'), ebbox)) self.msg("Removing {} {}".format(element.get('id'), ebbox))
element.delete() element.delete()
if self.options.removal == "outside_selection":
elif self.options.removal == "outside_selection":
if len(self.svg.selected) == 0: if len(self.svg.selected) == 0:
inkex.utils.debug("Your selection is empty but you have chosen the option to remove all elements outside selection!") inkex.utils.debug("Your selection is empty but you have chosen the option to remove all elements outside selection!")
return return
allElements = [] allElements = []
for selected in self.svg.selection: for selected in self.svg.selection:
allElements = self.getElementChildren(selected, allElements) allElements = self.getElementChildren(selected, allElements)
for element in self.document.getroot().iter("*"): for element in self.document.getroot().iter("*"):
if element not in allElements and isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('g', 'svg'): if element not in allElements and isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('g', 'svg'):
if self.options.debug is True: if self.options.debug is True:
self.msg("Removing {}".format(element.get('id'))) self.msg("Removing {}".format(element.get('id')))
element.delete() element.delete()
# translate all remaining elements to fit the adjusted viewBox
mat = Transform("translate(%f, %f)" % (-bbox.left,-bbox.top))
for element in self.document.getroot().iter("*"):
if isinstance (element, inkex.ShapeElement) and element.tag != inkex.addNS('g', 'svg'):
element.transform = Transform(mat) @ element.composed_transform()
if __name__ == '__main__': if __name__ == '__main__':
EpilogDashboardBboxAdjust().run() EpilogDashboardBboxAdjust().run()