discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Export two layers to DXF

K
kitwallace
Sun, Apr 16, 2017 8:49 AM

In case it is of interest, I wrote up this work in my blog

http://kitwallace.tumblr.com/post/159631931839/laser-cut-nets-with-openscad

These OpenSCAD functions generate SVG from multiple 'paths'  as list of line
segments.  No attempt is made to join up connected lines since laser cutter
software seems to do this anyway and it doest matter when displaying SVG.
Paths are enclosed in a g element so that transformations can be added to
the SVG later.

/*  path object:
path[0] : lines
path[1] : color
path[2] : width

*/

function rstr(list,i=0) =  // string join a list of strings
i < len(list)
? str(list[i],rstr(list,i+1))
: "";

function line_to_svg(line) =
str(" M ", line[0].x,",",line[0].y," L ",  line[1].x,",",line[1].y);

function path_to_svg(path, colour, width) =
str("<path d='",rstr([for (line  = path[0]) line_to_svg(line)]) ," ' ",

"style='fill:#ffffff;stroke:",path[1],";stroke-width:",path[2],"'"
," />");

function bounding_box(vertices) =
[ [ min([for (v = vertices) v.x]),
min([for (v = vertices) v.y]),
min([for (v = vertices) v.z])],
[ max([for (v = vertices) v.x]),
max([for (v = vertices) v.y]),
max([for (v= vertices) v.z])]
];

function dash_line(line,dash_length) =
let (v = line[1]- line[0])
let(line_length=norm(v))
let(n =  floor(line_length/dash_length))
[for (i=[0:n-1])
let(a = v *  i / n)
let(b = v *(i + 0.5) /n)
[line[0]+a,line[0]+b]
];

function dash_lines(lines, dash_length) =
flatten( [for (l = lines)
dash_line(l,dash_length)
]);

function paths_to_svg(paths, name) =
let (v= flatten([for (path=paths) flatten(path[0])]))
let (box=bounding_box(v))
let (bx =  ceil(2box[1].x-box[0].x))
let (by =  ceil(2
box[1].y-box[0].x))
str("<svg  xmlns='http://www.w3.org/2000/svg' version='1.1' ",
" width='",bx,"mm'", " height='",by,
"mm' ",
" viewBox='", floor(box[0].x),"
",floor(box[0].y)," ", bx, " ",by,"'",
">",
"<title>",name,"</title>","<g>",
rstr(
[for (path=paths)
path_to_svg(path)
])
,"</g>","</svg>")
;

Hopefully I'll get to do some actual cutting with this code soon. If anyone
wants to try, my polyhedra generator
http://kitwallace.co.uk/3d/solid-index.xq  now uses this code when
generating a net.

--
View this message in context: http://forum.openscad.org/Export-two-layers-to-DXF-tp21157p21237.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

In case it is of interest, I wrote up this work in my blog http://kitwallace.tumblr.com/post/159631931839/laser-cut-nets-with-openscad These OpenSCAD functions generate SVG from multiple 'paths' as list of line segments. No attempt is made to join up connected lines since laser cutter software seems to do this anyway and it doest matter when displaying SVG. Paths are enclosed in a g element so that transformations can be added to the SVG later. /* path object: path[0] : lines path[1] : color path[2] : width */ function rstr(list,i=0) = // string join a list of strings i < len(list) ? str(list[i],rstr(list,i+1)) : ""; function line_to_svg(line) = str(" M ", line[0].x,",",line[0].y," L ", line[1].x,",",line[1].y); function path_to_svg(path, colour, width) = str("<path d='",rstr([for (line = path[0]) line_to_svg(line)]) ," ' ", "style='fill:#ffffff;stroke:",path[1],";stroke-width:",path[2],"'" ," />"); function bounding_box(vertices) = [ [ min([for (v = vertices) v.x]), min([for (v = vertices) v.y]), min([for (v = vertices) v.z])], [ max([for (v = vertices) v.x]), max([for (v = vertices) v.y]), max([for (v= vertices) v.z])] ]; function dash_line(line,dash_length) = let (v = line[1]- line[0]) let(line_length=norm(v)) let(n = floor(line_length/dash_length)) [for (i=[0:n-1]) let(a = v * i / n) let(b = v *(i + 0.5) /n) [line[0]+a,line[0]+b] ]; function dash_lines(lines, dash_length) = flatten( [for (l = lines) dash_line(l,dash_length) ]); function paths_to_svg(paths, name) = let (v= flatten([for (path=paths) flatten(path[0])])) let (box=bounding_box(v)) let (bx = ceil(2*box[1].x-box[0].x)) let (by = ceil(2*box[1].y-box[0].x)) str("<svg xmlns='http://www.w3.org/2000/svg' version='1.1' &quot;, &quot; width='&quot;,bx,&quot;mm'&quot;, &quot; height='&quot;,by, &quot;mm' &quot;, &quot; viewBox='&quot;, floor(box[0].x),&quot; &quot;,floor(box[0].y),&quot; &quot;, bx, &quot; &quot;,by,&quot;'&quot;, &quot;>", "<title>",name,"</title>","<g>", rstr( [for (path=paths) path_to_svg(path) ]) ,"</g>","</svg>") ; Hopefully I'll get to do some actual cutting with this code soon. If anyone wants to try, my polyhedra generator http://kitwallace.co.uk/3d/solid-index.xq now uses this code when generating a net. -- View this message in context: http://forum.openscad.org/Export-two-layers-to-DXF-tp21157p21237.html Sent from the OpenSCAD mailing list archive at Nabble.com.