Source provided is an SVG within xml.etree.ElementTree root, as used widely.
import xml.etree.ElementTree as XML
svgXml = XML.parse('file.svg')
import GGen
ggObject = GGen.GGen( svgXml.getroot() )
#notice default xform is only Y-inverted matrix
ggObject.set(
xform = [[1.0, 0.0, 0.0], [0.0, -1.0, 0.0]],
smoothness = 0.02,
precision = 4,
shapePre = '',
shapeIn = '',
shapeOut = ''
)
for gShape, gList in ggObject.generate(
xform = None,
smoothness = None,
precision = None
):
do_something_with( gList )
#or
gString = ggObject.str(
xform = None,
smoothness = None,
precision = None
)where gList will be per-shape G-commands lists, and gShape is bound to SVG shape. gShape can be accessed iterating .tree(). .setData() and .data() can be used to store arbitrary data within shape.
In addition to being strings, shapePre, shapeIn and shapeOut passed can be hook functions to generate inline:
-
shapePre(currentSvgTag)
Called once, inlined before starting point of each sub-shape. May return False to skip shape entirely. -
shapeIn(currentSvgTag, pointZero)
Called for each sub-shape, inlined after starting point -
shapeOut(currentSvgTag, shapePointsList)
Called for each sub-shape and inlined after last point.
All hook functions should return either value or list of values.
def shapePreHook(_tag):
print( f"(pre for {_tag})" )
def shapeInHook(_tag, _point):
print( f"(in for {_tag}, starting at {_point})" )
def shapeOutHook(_tag, _shape):
print( f"(out for {_tag}, {len(_shape)}")
ggObject.set(
shapePre = shapePreHook,
shapeIn = shapeInHook,
shapeOut = shapeOutHook
)Typical static config for laser engraver can be:
ggObject.set(
shapePre = 'G0', #Fast position
shapeIn = 'S1000 G1', #Set 100% laser bightness and start feed move
shapeOut = 'S0', #Set 0% laser bightness
)Forked from vishpat/svg2gcode without Inkscape branch.
Most notable changes: