Skip to content

abzicht/svgocode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SVGOCODE Logo

SVGOCODE - Yet Another SVG to GCODE Converter

SVGOCODE - /æz wi ˈɡoʊkoʊd/ - converts SVG to GCODE, allowing digital imagery to be brought to life via pen-plotters, cutters, etc. First and foremost, it is intended to be used for modified 3D printers.

SVGOCODE can deal with everything* between complete SVG files and single SVG elements. The support for custom plotter profiles enables adoption of various devices and quick file conversion, once a suitable profile is found.

* The SVG standard is only partially implemented. For the supported feature set, see below.

The pen-plotted result of a SVG plane that was converted via SVGOCODE

Features

  • Supports the following SVG elements: svg, g, a, defs, use, path, line, rect, circle, ellipse, polygon, polyline.
    • use resolves to its referenced element.
    • path commands are fully covered.
  • Supports the transform attribute and all of its functions (matrix, translate, translateX, translateY, scale, scaleX, scaleY, skew, skewX, skewY, rotate).
  • Supports SVG user units mm, cm, and in. Supports GCODE units mm and in. Values are being converted, if SVG and GCODE units don't match.
  • Translates to GCODE based on customizable plotter / printer profiles.
  • Offers algorithms for minimizing travel distance in-between draw operations.
  • Defines interfaces for easily extending SVGOCODE with custom converters, ordering algorithms, etc.

Installation

This will install the binary in GOBIN:

# a)
make install
# b)
go install github.com/abzicht/svgocode

Use

SVGOCODE expects SVG-formatted input from STDIN (or from files provided with -s). It writes GCODE to STDOUT (or to files provided with -g), all auxiliary information is written to STDERR.

To convert SVG files to GCODE files, use svgocode as follows:

# a) STDIN / STDOUT:
cat drawing.svg | svgocode > drawing.gcode
# b) via flags:
svgocode -s drawing.svg -g drawing.gcode

By default, the travel distance between gcode segments is minimized via 2-Opt. 2-Opt is an optimal algorithm for the travelling salesman problem (TSP). The implementation of this algorithm is not optimized and may be computationally expensive when ordering too many elements. Using svgocode --ordering-algorithm=, the algorithm can be customized.

The following algorithms are available:

  • 2opt: Optimal, but not suitable for ordering thousands of elements.
  • greedy: Fast at finding a good, but not optimal solution.
  • none: No ordering is performed. The gcode segments are ordered in the order of their associated SVG elements.
  • reverse: Reverses the segment order - the last SVG element will be drawn first.

Development

  • Use make run to build and run svgocode.
  • Use make dev to build and run svgocode with debugging information.
  • Use make gdb to build and run svgocode inside of gdb.

Configuration

SVGOCODE is configured with a YAML-encoded plotter profile that supplies information such as the plotter's dimensions, pen offset, drawing speed, etc.

The default profile can be obtained via svgocode --plotter-config-template. It provides an exemplary configuration for the Longer LK5 Pro 3D printer.

Custom profiles are supplied via svgocode --plotter-config=file.yml.

Here is a description of the default configuration parameters:

gprefix: "gcode that is placed at the start of the output"
gsuffix: "gcode that is placed at the end   of the output"
length-unit: "mm" # Can be "mm" or "in". The unit is applied on gcode and all distance/speed variables in the plotter profile.
plate:
    center: # Center coordinates of the plotter's base plate
        "x": 150
        "y": 150
    min:    # Minimum value of each axis
        "x": 0
        "y": 0
        "z": 0
    max:    # Maximum value of each axis
        "x": 300
        "y": 300
        "z": 200
drawing-height: 20  # Absolute position on the Z-axis where drawing takes place
retract-height: 23  # Absolute position on the Z-axis where movement without drawing takes place
draw-speed: 2000    # Speed with that drawing is performed
retract-speed: 4000 # Speed with that movement without drawing is performed
remove-comments: false # Strip all comments (starting with ";") from GCODE.
mirror-x-axis: false # Mirror all X values around the plate's center X-axis.
mirror-y-axis: true  # Mirror all Y values around the plate's center Y-axis.
pen-offset:          # Offset with that the pen is mounted on the printer/plotter.
    "x": 47
    "y": 30

Library

SVGOCODE can be easily used as library, both for parsing SVG and for GCODE conversion. main.go may give you a hint on how to use the library in your own projects:

	var parsed_svg svg.SVG
	decoder := svg.NewDecoder(READER)
	if err := decoder.Decode(&parsed_svg); err != nil {
        panic(err)
	}
	gCode := svgocode.Svg2Gcode( # The all-in-one converter
            &parsed_svg,
            plotter.PlotterConfigLongerLK5ProDefault(), # Plotter profile
            convs.NewDirect(plotterConfig), # Conversion implementation
            ordering.NewGreedy() # Ordering method
        )
    // to string
	fmt.Println(gCode.String())
    // or encode to writer
	encoder := gcode.NewEncoder(WRITER)
	if err := encoder.Encode(gCode); err != nil {
        panic(err)
	}
	if err := encoder.Close(); err != nil {
        panic(err)
	}

Troubleshoot & Disclaimer

So far, SVGOCODE is the creation of one person (@abzicht). The SVG parser was written by one person, just as the conversion methods were.

As such, SVGOCODE can do a whole lot, but it is also quite limited. E.g., SVGOCODE cannot yet

  • convert text/tspan elements,
  • account for transform-origin/transform-box,
  • guarantee correctness of complex transform hierarchies,
  • work with embedded svg elements,
  • convert sodipodi / Inkscape attributes,
  • resolve non-local hyperlinks (href that do not point to elements inside the provided svg structure), or
  • work with strange length units (pt, px).

It is, therefore, recommended to

  • convert text/tspan elements to path elements manually (e.g., via Inkscape),
  • consider mirror-x-axis/mirror-y-axis (cf. Configuration), if GCODE appears flipped,
  • switch to faster algorithms (svgocode --ordering-algorithm=greedy), if input is large and processing takes too long,
  • double-check the SVG's units (must be mm, cm, or in), and
  • in general, have a good look at the SVG, e.g., via Inkscape's XML Editor.

Finally, it is crucial to assess the produced GCODE before use. E.g., open the GCODE in CURA and activate the Travels checkbox in Color Scheme > Line Type.

Disclaimer: Running GCODE on expensive machines can create expensive noises. The authors of SVGOCODE are not responsible for any damage caused by GCODE-based mishaps! The produced GCODE is in ASCII and can be easily assessed before its use. Do so!

Contributing

Some SVG feature is not yet supported? Please

  • create an issue or
  • DIY and submit a pull request.

Demo

In doc/assets, a demo SVG file is presented alongside with its GCODE equivalent and the physical drawing that was obtained with said GCODE:

A SVG blueprint with text, shapes, and an aircraft The pen-plotted equivalent of the digital blueprint

Releases

No releases published

Packages

No packages published