From ee33ea1f675eaa683d26f042c7554e0b41bf92dd Mon Sep 17 00:00:00 2001 From: hadron137 Date: Tue, 30 May 2017 12:41:04 -0700 Subject: [PATCH] svg2poly.ulp Added ability to handle H and V commands. Also added ability to parse the attribute "transform="scale(z.zzzzzz)"" . Finally, included a GUI frontend. --- scripts/svg2poly.ulp | 509 +++++++++++++++++++++++++++++-------------- 1 file changed, 341 insertions(+), 168 deletions(-) diff --git a/scripts/svg2poly.ulp b/scripts/svg2poly.ulp index c968db9..ef770af 100644 --- a/scripts/svg2poly.ulp +++ b/scripts/svg2poly.ulp @@ -1,9 +1,9 @@ /* * svg2poly.ulp -* Feb 2012, By Cruz Monrreal II (Cruz.Monrreal@gmail.com) +* Jan 2013, By Cruz Monrreal II (Cruz.Monrreal@gmail.com) * -* Imports select .svg file and will draw it as a set of polygons -* For best results, follow Inkscape instructions as closely as +* Imports Plain .svg file and will draw it as a set of polygons +* For best results, follow GitHub instructions as closely as * possible. * * @@ -13,24 +13,40 @@ * */ -#usage "Convert a modified SVG file to a Polygon

\n" -" run svg2poly -ratio num\n" -" run svg2poly -ratiox numx -ratioy numy -layer layername -outline thickness\n

" -" The polygons will automatically be centered about the current 'mark'

\n" -" For instructions on how to prepare the SVG file, go to www.thinkmaketest.com\\nn" -"Author: Cruz Monrreal II\n(cruz.monrreal@gmail.com)\n1-Feb-2011" +#usage "Import a Plain SVG file to a Polygon\n" +"

NOTE: Polygons will automatically be centered about the current 'mark'

" +"

Usage: run svg2poly [ -ratio num ] [ -layer name ] [ -outline thickness ]" +"

Options:
" +"" +"" +"" +"" +"
-ratio numscale SVG with the given ratio, default 1
-layer namechange layer that SVG is drawn on, default tDocu
-outline thickness draw an outline of the given size, default 0
" +"

Example:" +"

run svg2poly -ratio 2 -layer tDocu" +"

This will import a Plain SVG file, scale it 2x, and draw it on the tDocu layer with no outline" +"

" +"" +"" +"" +"" +"
Cruz Monrreal
cruz.monrreal@gmail.com
http://anomalousmaker.com
" + + /* Inputs, Defaults */ -string layer = "tDocu"; -int outline = 0; -real ratioX = 1.0, ratioY = 1.0; +string layer = "tNames"; +real outline = 0.0005; +real ratioX = 0.01, ratioY = 0.01; /* Globals */ string tmp[], master_xml; -int poly_start[], num_pts=0, num_polys=0; +int poly_start[], global_pts=0, num_polys=0; real pts_x[], pts_y[]; real tmp_coords[]; +real scaleFactor = 1.0; // SVG defined +string file; // filename of SVG /* Output */ string cmd; @@ -45,7 +61,7 @@ void push(string s){ stack = (stack == "") ? s : stack + "|" + s; } string pull() { string tmp; - + if (stack == "") // Nothing in stack return ""; @@ -67,10 +83,10 @@ string pull() * Encodes all necessary information into a single string * for stack-based recursion */ -string pack(string xml, int repush, int num_pts) +string pack(string xml, int global_pts) { string tmp; - sprintf(tmp, "%s^%d^%d", xml, repush, num_pts); + sprintf(tmp, "%s^%d", xml, global_pts); return tmp; } @@ -81,9 +97,9 @@ void print(string s) {printf("%s\n", s);} * Sets up the eagle commands for the script. */ void setup_cmd() -{ +{ int i; - + // Search for parameters for(i=0; i= 0) + { + string scaleString = strsub(transform,scalePos+6); + scaleFactor = strtod(strsub(scaleString,0, strlen(scaleString)-1)); // trim last char ")" + } + else + { + scaleFactor = 1.0; + } + printf("Scale factor = %f\n",scaleFactor); + + string svg_pts[]; + int size = strsplit(svg_pts, pts, ' '), new_pts = 0; - int i = 3; - + int i = 2; + int mode; + + + if(svg_pts[0] == "M"){ + // Coords are Absolute + mode = 1; + + sprintf(svg_pts[1], "%s,%s", svg_pts[1], svg_pts[2]); + + }else{ + // Coords are Relative + mode = 0; + } + + poly_start[global_pts] = 1; // Path format: - // M x,y L x,y L x,y L x,y .... - poly_start[num_pts] = 1; - svgcoords_to_coords(svg_cmds[1]); - pts_x[num_pts] = tmp_coords[0]; - pts_y[num_pts++] = tmp_coords[1]; - new_pts++; + // m x,y x,y L X,Y l x,y .... z - printf("%2d) %2.2f,%2.2f\n", num_polys, tmp_coords[0], tmp_coords[1]); - - // Assume that the only commands left are 'L' - for(; i=0; i--) - push(pack(children[i], 0, num_pts)); - }else{ - // Search for and add points - print("Searching group " + xmlattribute(ops[0], "g", "id")); - - string paths[]; - int num_paths = xmlelements(paths, ops[0], "g/path"); - - for(int i=0; i max_x) max_x = pts_x[i]; - + if (pts_y[i] < min_y) min_y = pts_y[i]; if (pts_y[i] > max_y) max_y = pts_y[i]; } - + real x_center = (max_x - min_x)/2, y_center = (max_y - min_y)/2; - + // Apply offsets to all points - for(i=0; i < num_pts; i++){ + for(i=0; i < global_pts; i++){ pts_x[i] -= min_x + x_center; pts_y[i] -= min_y + y_center; } - printf("%f %f\n", x_center, y_center); - + printf("\nObject origin:\n%f %f\n", x_center, y_center); + // Generate eagle command - for(i=0; i