refactored and rename modify path start node

This commit is contained in:
Mario Voigt 2021-05-19 14:02:49 +02:00
parent 275d8d657d
commit 90fd706551
2 changed files with 35 additions and 32 deletions

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Modify Path Start Node</name>
<id>fablabchemnitz.de.modify_path_start</id>
<name>Move Path Node</name>
<id>fablabchemnitz.de.move_path_node</id>
<param name="tab" type="notebook">
<page name="tab_settings" gui-text="Modify Path Start Node">
<param name="closed_only" type="bool" gui-text="Handle closed paths only" gui-description="If disabled we also apply on open (sub)paths. Warning: This REMOVES segments!">true</param>
<param name="movenode" type="int" min="-99999" max="99999" gui-text="Move starting node n nodes further">0</param>
<param name="closed_only" type="bool" gui-text="Handle closed paths only" gui-description="If disabled we also apply on open (sub)paths. Warning: This REMOVES segments!">true</param>
<param name="movenode" type="int" min="-99999" max="99999" gui-text="Move node n nodes further">0</param>
<param name="visualize_result" type="bool" gui-text="Visualize first two nodes" gui-description="If enabled first two nodes get a number and a dot">false</param>
<param name="fontsize" type="string" gui-text="Font size:">10px</param>
<param name="dotsize" type="string" gui-text="Dot size:">10px</param>
@ -15,12 +15,12 @@
<label>Use extension "Chain Paths" to make closed paths out of segments.</label>
</page>
<page name="tab_about" gui-text="About">
<label appearance="header">Modify Path Start Node</label>
<label>Extension to change starting node of a path and visualize it by dots and numbers</label>
<label appearance="header">Move Path Node</label>
<label>Extension to change starting / end node of a path and visualize it by dots and numbers. You can also use this extension as a trimmer for open paths.</label>
<label>2021 / written by Mario Voigt (Stadtfabrikanten e.V. / FabLab Chemnitz)</label>
<spacer/>
<label appearance="header">Online Documentation</label>
<label appearance="url">https://y.stadtfabrikanten.org/modifypathstartnode</label>
<label appearance="url">https://y.stadtfabrikanten.org/movepathnode</label>
<spacer/>
<label appearance="header">Contributing</label>
<label appearance="url">https://gitea.fablabchemnitz.de/MarioVoigt/mightyscape-1.X</label>
@ -50,6 +50,6 @@
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">modify_path_start.py</command>
<command location="inx" interpreter="python">move_path_node.py</command>
</script>
</inkscape-extension>

View File

@ -1,22 +1,18 @@
#!/usr/bin/env python3
'''
Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org
Date: 19.05.2021
Last patch: 19.05.2021
License: GNU GPL v3
ToDo:
link for usage: plotting/laser cutting, path unwinding plugin > startpunkt für anwicklung
'''
import copy
import inkex
from inkex import Circle, TextElement, Path, PathElement, CubicSuperPath
class ModifyStartDirection(inkex.EffectExtension):
class MovePathNode(inkex.EffectExtension):
def modify(self, element):
raw = element.path.to_arrays()
@ -27,7 +23,10 @@ class ModifyStartDirection(inkex.EffectExtension):
prev = i
subpaths.append(raw[prev:])
if self.options.debug is True:
self.msg("Element {} has {} subpath(s)".format(element.get('id'), len(subpaths)))
if len(subpaths) == 0:
self.msg("{} has no subpaths").format(element.get('id'))
else:
self.msg("{} has {} subpath(s)".format(element.get('id'), len(subpaths)))
subpathNr = 0
for path in subpaths:
@ -43,9 +42,13 @@ class ModifyStartDirection(inkex.EffectExtension):
if self.options.debug is True:
self.msg("pathIsClosed = " + str(pathIsClosed))
self.msg("nodes = " + str(len(path)))
if self.options.closed_only is True and pathIsClosed is False:
self.msg("Path {}/subpath {} is not closed!".format(element.get('id'), subpathNr))
if len(subpaths) == 0:
self.msg("{}/subpath {} is not closed! Skipping ...".format(element.get('id'), subpathNr))
else:
self.msg("{} is not closed! Skipping ...".format(element.get('id')))
continue #skip this open path
if path[-1][0] == 'Z': #replace Z with another L command (which moves to the coordinates of the first M command in path) to have better overview
@ -53,32 +56,32 @@ class ModifyStartDirection(inkex.EffectExtension):
path[-1][1] = path[0][1]
#adjust if entered move number is higher than actual node count. We handle as infinite looping
moves = self.options.movenode % len(path)
moves = (self.options.movenode - 1) % len(path)
if pathIsClosed is True: #if closed start and end collapse and "duplicate"
moves = self.options.movenode % (len(path) - 1)
moves = (self.options.movenode - 1) % (len(path) - 1)
if self.options.movenode == 0: #special handling for 0 is required
moves = 0
if self.options.debug is True:
self.msg("moves to perform = " + str(moves))
self.msg("root path:")
self.msg(path)
self.msg("-"*25)
if self.options.debug is True:
self.msg("moves = " + str(moves))
for i in range(moves):
if len(path) > 2: #the path needs at least more than two nodes
#we move the first node to the end of the list
if len(path) > 2: #the path needs at least more than two segments
#we move the first segment to the end of the list
move = path[0]
del path[0]
path.append(move)
oldseg = copy.deepcopy(path[0]) #if we assign like "oldseg = path[0]", it will get overwritten. So we need copy
if self.options.debug is True:
self.msg("moved path:")
self.msg("moved path (move no. {}):".format(i+1))
self.msg(path)
self.msg("-"*25)
oldseg = copy.deepcopy(path[0]) #if we assign like "oldseg = path[0]", it will get overwritten. So we need copy
#Now we messed the integrity of the path. It does not begin with 'M' now. But we need an 'M'.
#It now either starts with L or C. H, V, Z cannot occure here.
if path[0][0] == 'C': #and path[-1][0] == 'M':
@ -108,9 +111,9 @@ class ModifyStartDirection(inkex.EffectExtension):
self.msg("-"*25)
newSubpaths[subpathNr - 1] = path
#else:
# inkex.utils.debug("More moves entered than possible to apply")
else:
inkex.utils.debug("More moves entered than possible to apply")
composedPath = inkex.Path()
for newSubpath in newSubpaths:
composedPath.extend(newSubpath)
@ -173,4 +176,4 @@ class ModifyStartDirection(inkex.EffectExtension):
return
if __name__ == '__main__':
ModifyStartDirection().run()
MovePathNode().run()