Skip to content

NikolayRag/svg2gcode_ggen

 
 

Repository files navigation

SVG to GCode converter

Source provided is an SVG within xml.etree.ElementTree root, as used widely.

Quick useage, also showing defaults:

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
)

Reference:

Forked from vishpat/svg2gcode without Inkscape branch.

Most notable changes:

  • Python 3 compatable
  • Is importable module
  • GGen class as interface
  • Used as generator
  • Gather transform all over hierarchy for node
  • Fix: curved shapes collected with wrong points #2733998
  • Fix: proccess multishapes per-shape #43d4dba
  • Fix: complex curves out or recursion limits #33737f2

About

Python3 module to generate gcode out of svg

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%