rewrite outline extension

pull/2881/head
Kaalleen 2024-03-31 20:03:43 +02:00
rodzic d32a8fd466
commit eab19fcb45
2 zmienionych plików z 54 dodań i 22 usunięć

Wyświetl plik

@ -3,26 +3,37 @@
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
import inkex
from shapely import concave_hull
from shapely.geometry import LineString, MultiPolygon
from inkex import Boolean, Path, errormsg
from shapely import offset_curve
from shapely.geometry import LineString, MultiPolygon, Polygon
from ..i18n import _
from ..svg import PIXELS_PER_MM
from ..svg.tags import SVG_PATH_TAG
from ..utils.geometry import ensure_multi_line_string
from ..utils.smoothing import smooth_path
from .base import InkstitchExtension
class Outline(InkstitchExtension):
def __init__(self, *args, **kwargs):
InkstitchExtension.__init__(self, *args, **kwargs)
self.arg_parser.add_argument("-r", "--ratio", type=float, default=0.0, dest="ratio")
self.arg_parser.add_argument("-a", "--allow-holes", type=inkex.Boolean, default=False, dest="allow_holes")
self.arg_parser.add_argument("-k", "--keep-original", type=Boolean, default=False, dest="keep_original")
self.arg_parser.add_argument("-b", "--buffer", type=float, default=0.001, dest="buffer")
self.arg_parser.add_argument("-s", "--smoothness", type=float, default=0.3, dest="smoothness")
self.arg_parser.add_argument("-t", "--threshold", type=float, default=10.0, dest="threshold")
self.arg_parser.add_argument("-i", "--inset", type=float, default=0.001, dest="inset")
def effect(self):
if not self.svg.selection:
inkex.errormsg(_("Please select one or more shapes to convert to their outline."))
errormsg(_("Please select one or more shapes to convert to their outline."))
return
self.threshold = self.options.threshold * PIXELS_PER_MM
self.shape_buffer = max(self.options.buffer * PIXELS_PER_MM, 0.001)
self.smoothness = self.options.smoothness * PIXELS_PER_MM
self.inset = self.options.inset * PIXELS_PER_MM
for element in self.svg.selection:
self.element_to_outline(element)
@ -32,19 +43,27 @@ class Outline(InkstitchExtension):
self.element_to_outline(element)
return
path = element.get_path()
path = path.end_points
hull = concave_hull(LineString(path), ratio=self.options.ratio, allow_holes=self.options.allow_holes)
if isinstance(hull, LineString):
return
if not isinstance(hull, MultiPolygon):
hull = MultiPolygon([hull])
d = ''
for geom in hull.geoms:
d += 'M '
for x, y in geom.exterior.coords:
d += f'{x}, {y} '
d += "Z"
transform = element.composed_transform()
path = element.get_path().transform(transform).break_apart()
for subpath in path:
points = subpath.end_points
shape = LineString(points).buffer(self.shape_buffer)
outline = ensure_multi_line_string(offset_curve(shape, -self.inset))
element.set('d', d)
interiors = []
for interior in outline.geoms:
if Polygon(interior).area < self.threshold:
continue
interior_path = smooth_path(interior.coords, self.smoothness)
if len(interior_path) > 2:
interiors.append(Polygon(interior_path))
outline = MultiPolygon(interiors)
for geom in outline.geoms:
d += str(Path(geom.exterior.coords).transform(-transform))
if self.options.keep_original:
element.duplicate()
element.set('d', d)
else:
element.set('d', d)

Wyświetl plik

@ -11,8 +11,21 @@
</submenu>
</effects-menu>
</effect>
<param name="ratio" type="float" appearance="full" gui-text="Ratio" precision="2" min="0" max="1">0</param>
<param name="allow-holes" type="boolean" gui-text="Allow Holes">false</param>
<param name="keep-original" type="boolean" gui-text="Keep original element(s)">false</param>
<spacer />
<separator />
<spacer />
<param name="threshold" type="float" appearance="full" precision="2" min="0" max="100"
gui-text="Threshold (mm)" gui-description="Removes smaller shape areas than that">10</param>
<param name="buffer" type="float" appearance="full" precision="3" min="0.001" max="1"
gui-text="Buffer (mm)" gui-description="Enlarge strokes by this amount">0.001</param>
<param name="smoothness" type="float" appearance="full" precision="2" min="0.01" max="5"
gui-text="Smoothness (mm)" gui-description="Smooth path by this amount">0.3</param>
<spacer />
<separator />
<spacer />
<param name="inset" type="float" appearance="full" precision="2" min="0.01" max="5"
gui-text="Inset (mm)" gui-description="Counteract the buffer effect">0.001</param>
<script>
{{ command_tag | safe }}
</script>