updated contour scanner

This commit is contained in:
leyghisbb 2021-04-14 20:54:10 +02:00
parent edc8313bbf
commit 23cdd26c4f
2 changed files with 63 additions and 26 deletions

View File

@ -4,26 +4,36 @@
<id>fablabchemnitz.de.contour_scanner</id> <id>fablabchemnitz.de.contour_scanner</id>
<param name="main_tabs" type="notebook"> <param name="main_tabs" type="notebook">
<page name="tab_active" gui-text="Active"> <page name="tab_active" gui-text="Active">
<label appearance="header">General</label> <hbox>
<param name="breakapart" type="bool" gui-text="Break apart selection into single contours" gui-description="(with ignoring the group hirarchy by taking all children elements)">false</param> <vbox>
<param name="removefillsetstroke" type="bool" gui-text="Remove fill and define stroke">false</param> <label appearance="header">Highlight paths</label>
<param name="strokewidth" min="0.0" max="10000.0" gui-text="Stroke width (px)" type="float">1.0</param> <param name="highlight_opened" type="bool" gui-text="Highlight opened contours">true</param>
<label appearance="header">Highlight paths</label> <param name="color_opened" type="color" appearance="colorbutton" gui-text="Color opened contours">4012452351</param>
<param name="highlight_opened" type="bool" gui-text="Highlight opened contours">true</param> <param name="highlight_closed" type="bool" gui-text="Highlight closed contours">true</param>
<param name="color_opened" type="color" appearance="colorbutton" gui-text="Color opened contours">4012452351</param> <param name="color_closed" type="color" appearance="colorbutton" gui-text="Color closed contours">2330080511</param>
<param name="highlight_closed" type="bool" gui-text="Highlight closed contours">true</param> <param name="highlight_selfintersecting" type="bool" gui-text="Highlight self-intersecting contours" gui-description="Due to the nature of the algorithm this might detect non-closed contours too.">true</param>
<param name="color_closed" type="color" appearance="colorbutton" gui-text="Color closed contours">2330080511</param> <param name="color_selfintersecting" type="color" appearance="colorbutton" gui-text="Color self-intersecting contours">1923076095</param>
<param name="highlight_selfintersecting" type="bool" gui-text="Highlight self-intersecting contours" gui-description="Due to the nature of the algorithm this might detect non-closed contours too.">true</param> <param name="highlight_intersectionpoints" type="bool" gui-text="Highlight self-intersecting points">true</param>
<param name="color_selfintersecting" type="color" appearance="colorbutton" gui-text="Color self-intersecting contours">1923076095</param> <param name="color_intersectionpoints" type="color" appearance="colorbutton" gui-text="Color self-intersecting points">4239343359</param>
<param name="highlight_intersectionpoints" type="bool" gui-text="Highlight self-intersecting points">true</param> <param name="dotsize" type="int" min="0" max="10000" gui-text="Dot size (px) for self-intersecting points">10</param>
<param name="color_intersectionpoints" type="color" appearance="colorbutton" gui-text="Color self-intersecting points">4239343359</param> <param name="addlines" type="bool" gui-text="Add closing lines for open self-crossing contours" gui-description="They will have the same color as the intersection points and help to better visualize possible virtual crossings. The algorithm can only detect intersections for closed contours by it's nature, but we handle open contours like they were closed. This may put put too much intersection points.">true</param>
<param name="dotsize" type="int" min="0" max="10000" gui-text="Dot size (px) for self-intersecting points">10</param> <param name="polypaths" type="bool" gui-text="Add polypath outline for self-crossing contours" gui-description="This makes only sense if your path is actually a curve. If it's already a polyline you just get a duplicate line (but with reduced nodes)">true</param>
<param name="addlines" type="bool" gui-text="Add closing lines for open self-crossing contours" gui-description="They will have the same color as the intersection points and help to better visualize possible virtual crossings. The algorithm can only detect intersections for closed contours by it's nature, but we handle open contours like they were closed. This may put put too much intersection points.">true</param> </vbox>
<param name="polypaths" type="bool" gui-text="Add polypath outline for self-crossing contours" gui-description="This makes only sense if your path is actually a curve. If it's already a polyline you just get a duplicate line (but with reduced nodes)">true</param> <separator/>
<label appearance="header">Remove paths</label> <vbox>
<param name="remove_opened" type="bool" gui-text="Remove opened contours">false</param> <label appearance="header">Remove paths</label>
<param name="remove_closed" type="bool" gui-text="Remove closed contours">false</param> <param name="remove_opened" type="bool" gui-text="Remove opened contours">false</param>
<param name="remove_selfintersecting" type="bool" gui-text="Remove self-intersecting contours">false</param> <param name="remove_closed" type="bool" gui-text="Remove closed contours">false</param>
<param name="remove_selfintersecting" type="bool" gui-text="Remove self-intersecting contours">false</param>
<separator/>
<label appearance="header">General</label>
<param name="apply_transformations" type="bool" gui-text="Use 'Apply Transformations' extension" gui-description="Run 'Apply Transformations' extension before running to avoid IndexErrors in calculation.">false</param>
<param name="breakapart" type="bool" gui-text="Break apart selection into single contours" gui-description="(with ignoring the group hirarchy by taking all children elements)">false</param>
<param name="removefillsetstroke" type="bool" gui-text="Remove fill and define stroke">false</param>
<param name="strokewidth" min="0.0" max="10000.0" gui-text="Stroke width (px)" type="float">1.0</param>
<param name="show_debug" type="bool" gui-text="Show debug info">false</param>
</vbox>
</hbox>
</page> </page>
<page name="tab_info" gui-text="About"> <page name="tab_info" gui-text="About">
<label appearance="header">About</label> <label appearance="header">About</label>

View File

@ -17,19 +17,22 @@ Last patch: 05.09.2020
License: GNU GPL v3 License: GNU GPL v3
""" """
import sys
from math import * from math import *
import inkex
from inkex.paths import Path, CubicSuperPath
from inkex import Style, Color, Circle
from lxml import etree from lxml import etree
import poly_point_isect import poly_point_isect
import copy import copy
import inkex
from inkex.paths import Path, CubicSuperPath
from inkex import Style, Color, Circle
class ContourScanner(inkex.Effect): class ContourScanner(inkex.Effect):
def __init__(self): def __init__(self):
inkex.Effect.__init__(self) inkex.Effect.__init__(self)
self.arg_parser.add_argument("--main_tabs")
self.arg_parser.add_argument("--breakapart", type=inkex.Boolean, default=False, help="Break apart selection into single contours") self.arg_parser.add_argument("--breakapart", type=inkex.Boolean, default=False, help="Break apart selection into single contours")
self.arg_parser.add_argument("--apply_transformations", type=inkex.Boolean, default=False, help="Run 'Apply Transformations' extension before running to avoid IndexErrors in calculation.")
self.arg_parser.add_argument("--removefillsetstroke", type=inkex.Boolean, default=False, help="Remove fill and define stroke") self.arg_parser.add_argument("--removefillsetstroke", type=inkex.Boolean, default=False, help="Remove fill and define stroke")
self.arg_parser.add_argument("--strokewidth", type=float, default=1.0, help="Stroke width (px)") self.arg_parser.add_argument("--strokewidth", type=float, default=1.0, help="Stroke width (px)")
self.arg_parser.add_argument("--highlight_opened", type=inkex.Boolean, default=True, help="Highlight opened contours") self.arg_parser.add_argument("--highlight_opened", type=inkex.Boolean, default=True, help="Highlight opened contours")
@ -46,7 +49,7 @@ class ContourScanner(inkex.Effect):
self.arg_parser.add_argument("--remove_opened", type=inkex.Boolean, default=False, help="Remove opened contours") self.arg_parser.add_argument("--remove_opened", type=inkex.Boolean, default=False, help="Remove opened contours")
self.arg_parser.add_argument("--remove_closed", type=inkex.Boolean, default=False, help="Remove closed contours") self.arg_parser.add_argument("--remove_closed", type=inkex.Boolean, default=False, help="Remove closed contours")
self.arg_parser.add_argument("--remove_selfintersecting", type=inkex.Boolean, default=False, help="Remove self-intersecting contours") self.arg_parser.add_argument("--remove_selfintersecting", type=inkex.Boolean, default=False, help="Remove self-intersecting contours")
self.arg_parser.add_argument("--main_tabs") self.arg_parser.add_argument("--show_debug", type=inkex.Boolean, default=False, help="Show debug info")
#function to refine the style of the lines #function to refine the style of the lines
def adjustStyle(self, node): def adjustStyle(self, node):
@ -241,7 +244,12 @@ class ContourScanner(inkex.Effect):
# polySegsNode.attrib['style'] = closingLineStyle # polySegsNode.attrib['style'] = closingLineStyle
except AssertionError as e: # we skip AssertionError except AssertionError as e: # we skip AssertionError
#inkex.utils.debug("Error: " + str(e)) if self.options.show_debug is True:
inkex.utils.debug("AssertionError at " + node.get('id'))
continue
except IndexError as i: # we skip IndexError
if self.options.show_debug is True:
inkex.utils.debug("IndexError at " + node.get('id'))
continue continue
#if the intersectionGroup was created but nothing attached we delete it again to prevent messing the SVG XML tree #if the intersectionGroup was created but nothing attached we delete it again to prevent messing the SVG XML tree
if len(intersectionGroup.getchildren()) == 0: if len(intersectionGroup.getchildren()) == 0:
@ -257,6 +265,25 @@ class ContourScanner(inkex.Effect):
self.scanContours(child) self.scanContours(child)
def effect(self): def effect(self):
applyTransformAvailable = False
# at first we apply external extension
try:
sys.path.append("..") # add parent directory to path to allow importing applytransform (vpype extension is encapsulated in sub directory)
import applytransform
applyTransformAvailable = True
except Exception as e:
#inkex.utils.debug(e)
inkex.utils.debug("Calling 'Apply Transformations' extension failed. Maybe the extension is not installed. You can download it from official InkScape Gallery. Skipping this step")
'''
we need to apply transfoms to the complete document even if there are only some single paths selected.
If we apply it to selected nodes only the parent groups still might contain transforms.
This messes with the coordinates and creates hardly controllable behaviour
'''
if self.options.apply_transformations is True and applyTransformAvailable is True:
applytransform.ApplyTransform().recursiveFuseTransform(self.document.getroot())
if self.options.breakapart: if self.options.breakapart:
if len(self.svg.selected) == 0: if len(self.svg.selected) == 0:
self.breakContours(self.document.getroot()) self.breakContours(self.document.getroot())