OpenSCAD User Manual/Other 2D formats

From Wikibooks, open books for an open world
Jump to: navigation, search


Currently, OpenSCAD only supports DXF as a graphics format for 2D graphics. Other common formats are PS/EPS and SVG.

PS/EPS[edit]

The pstoedit program can convert between various vector graphics formats. OpenSCAD needs the -polyaslines option passed to the dxf output plugin to understand the file. The -mm option sets one mm to be one unit in the dxf; include this if you use one unit in OpenSCAD as equal to one millimeter. The -dt options instructs pstoedit to render texts, which is usually what you want if you include text. (If the rendered text's resolution in terms of polygon count is too low, the easiest solution is to scape up the eps before converting; if you know a more elegant solution, please add it to the example.)

pstoedit -dt -f "dxf: -polyaslines -mm" infile.eps outfile.dxf

SVG[edit]

pstoedit does not understand SVG, but EPS can be converted from an SVG. inkscape, an SVG editor, can be used for conversion.

inkscape -E intermediate.eps infile.svg
pstoedit -dt -f dxf:-polyaslines\ -mm intermediate.eps outfile.dxf

Makefile automation[edit]

The conversion can be automated using the make system; put the following lines in your Makefile:

 all: my_first_file.dxf my_second_file.dxf another_file.dxf
 
 %.eps: %.svg
 	inkscape -E $@ $<
 
 %.dxf: %.eps
 	pstoedit -dt -f dxf:-polyaslines\ -mm $< $@

The first line specifies which dxf files are to be generated when make is called in the current directory. The second paragraph specifies how to convert a file ending in .svg to a file ending in .eps, and the third from .eps to .dxf.

A more complete makefile could autogenerate dxf files from the any svg in the folder. In which case, put the following lines into your Makefile:

SVG := $(wildcard *.svg)
DXF := $(SVG:%.svg=%.dxf)
EPS := $(SVG:%.svg=%.eps)
 
.PHONY: all clean clean-eps clean-dxf
 
all: $(DXF)
 
%.eps: %.svg
	inkscape -E $*.eps $*.svg
 
%.dxf: %.eps
	pstoedit -dt -f "dxf: -polyaslines -mm" $*.eps $*.dxf
 
clean: clean-dxf clean-eps
 
clean-dxf: 
	rm -f $(DXF)
 
clean-eps:
	rm -f $(EPS)

It's still possible to call make filename.dxf to build a particular file, but this code also allows for (re)building of all dxf files in a folder just by calling make or make all.

This code is also universal enough that it's possible to put the code in a single file and symlink every makefile in any directory that has svg files for dxf conversion by running:

ls -s /path/to/this/svg_to_dxf_makefile makefile

in each respective directory.