From b7c9e074bb208e8cdb9e19a68299e714df68025e Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Tue, 4 Jan 2022 21:50:30 +0100 Subject: [PATCH] added normalize drawing scale extension --- .../normalize_drawing_scale/meta.json | 20 +++++++ .../normalize_drawing_scale.inx | 17 ++++++ .../normalize_drawing_scale.py | 55 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 extensions/fablabchemnitz/normalize_drawing_scale/meta.json create mode 100644 extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.inx create mode 100644 extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.py diff --git a/extensions/fablabchemnitz/normalize_drawing_scale/meta.json b/extensions/fablabchemnitz/normalize_drawing_scale/meta.json new file mode 100644 index 00000000..7cce306a --- /dev/null +++ b/extensions/fablabchemnitz/normalize_drawing_scale/meta.json @@ -0,0 +1,20 @@ +[ + { + "name": "Normalize Drawing Scale", + "id": "fablabchemnitz.de.normalize_drawing_scale", + "path": "laser_check", + "dependent_extensions": null, + "original_name": "Normalize Drawing Scale", + "original_id": "fablabchemnitz.de.normalize_drawing_scale", + "license": "GNU GPL v3", + "license_url": "", + "comment": "", + "source_url": "https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X/src/branch/master/extensions/fablabchemnitz/normalize_drawing_scale", + "fork_url": null, + "documentation_url": "https://stadtfabrikanten.org/display/IFM/Normale+Drawing+Scale", + "inkscape_gallery_url": null, + "main_authors": [ + "github.com/vmario89" + ] + } +] \ No newline at end of file diff --git a/extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.inx b/extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.inx new file mode 100644 index 00000000..576aab2a --- /dev/null +++ b/extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.inx @@ -0,0 +1,17 @@ + + + Normalize Drawing Scale + fablabchemnitz.de.normalize_drawing_scale + false + + all + + + + + + + + \ No newline at end of file diff --git a/extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.py b/extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.py new file mode 100644 index 00000000..e4a78a47 --- /dev/null +++ b/extensions/fablabchemnitz/normalize_drawing_scale/normalize_drawing_scale.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +import inkex +from inkex import Transform +from lxml import etree + +class NormalizeDrawingScale(inkex.EffectExtension): + + def add_arguments(self, pars): + pars.add_argument('--remove_viewbox', type=inkex.Boolean, default=True) + + def effect(self): + namedView = self.document.getroot().find(inkex.addNS('namedview', 'sodipodi')) + doc_units = namedView.get(inkex.addNS('document-units', 'inkscape')) + docScale = self.svg.scale + docWidth = self.svg.uutounit(self.svg.width, doc_units) + docHeight = self.svg.uutounit(self.svg.height, doc_units) + vxMin, vyMin, vxMax, vyMax = self.svg.get_viewbox() + vxTotal = vxMax - vxMin + vScaleX = self.svg.unittouu(str(vxTotal / self.svg.width) + doc_units) + if round(vScaleX, 5) != 1.0 or self.options.remove_viewbox is True: + + #set scale to 100% (we adjust viewBox) + if self.options.remove_viewbox is False: + self.svg.set('viewBox', '0 0 {} {}'.format(docWidth, docHeight)) + else: + self.svg.pop('viewBox') + self.document.getroot().set('inkscape:document-units', 'px') + self.svg.set('width', docWidth) + self.svg.set('height', docHeight) + + namedView.attrib[inkex.addNS('document-units', 'inkscape')] = 'px' + translation_matrix = [[1/vScaleX, 0.0, 0.0], [0.0, 1/vScaleX, 0.0]] + + #select each top layer and apply the transformation to scale + processed = [] + for element in self.document.getroot().iter(tag=etree.Element): + if element != self.document.getroot(): + if element.tag == inkex.addNS('g','svg'): + parent = element.getparent() + if parent.get('inkscape:groupmode') != 'layer' and element.get('inkscape:groupmode') == 'layer': + element.transform = Transform(translation_matrix) * element.composed_transform() + processed.append(element) + + #do the same for all elements which lay on first level and which are not a layer + for element in self.document.getroot().getchildren(): + if isinstance(element, inkex.ShapeElement) and element not in processed: + element.transform = Transform(translation_matrix) * element.composed_transform() + + else: + inkex.utils.debug("Nothing to do. Scale is already 100%") + + +if __name__ == '__main__': + NormalizeDrawingScale().run() \ No newline at end of file