discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

2d shape to polygon

RW
Raymond West
Fri, Aug 12, 2022 1:44 PM

Given any shape composed of squares and circles, say, such as
square(200);circle(100);  is there a method of converting it to a list
of points for a polygon? The simplest I've found is to export as svg,
then replace the 'L's with' ],['s and delete the rest, sort of, paste
the text back into a polygon, but it runs into the origin difference
problem, and is a bit kludgy..

Given any shape composed of squares and circles, say, such as square(200);circle(100);  is there a method of converting it to a list of points for a polygon? The simplest I've found is to export as svg, then replace the 'L's with' ],['s and delete the rest, sort of, paste the text back into a polygon, but it runs into the origin difference problem, and is a bit kludgy..
F
fred
Fri, Aug 12, 2022 1:56 PM

I don't know if you'd find this method easier or no less kludgy, but I use the Inkscape extension, Path to OpenSCAD for similar operations. The SVG opens in Inkscape, naturally and the extension merely needs to have an extrusion thickness. There's a fluke in the extension that creates a scale factor, but I change it from the coded default to 1 in the generated OpenSCAD code and it remains "true to size."
If the shape has holes, the SVG file has to be filled in order to retain them in the final code.

On Friday, August 12, 2022 at 09:45:27 AM EDT, Raymond West <raywest@raywest.com> wrote:  

Given any shape composed of squares and circles, say, such as
square(200);circle(100);  is there a method of converting it to a list
of points for a polygon? The simplest I've found is to export as svg,
then replace the 'L's with' ],['s and delete the rest, sort of, paste
the text back into a polygon, but it runs into the origin difference
problem, and is a bit kludgy..


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I don't know if you'd find this method easier or no less kludgy, but I use the Inkscape extension, Path to OpenSCAD for similar operations. The SVG opens in Inkscape, naturally and the extension merely needs to have an extrusion thickness. There's a fluke in the extension that creates a scale factor, but I change it from the coded default to 1 in the generated OpenSCAD code and it remains "true to size." If the shape has holes, the SVG file has to be filled in order to retain them in the final code. On Friday, August 12, 2022 at 09:45:27 AM EDT, Raymond West <raywest@raywest.com> wrote: Given any shape composed of squares and circles, say, such as square(200);circle(100);  is there a method of converting it to a list of points for a polygon? The simplest I've found is to export as svg, then replace the 'L's with' ],['s and delete the rest, sort of, paste the text back into a polygon, but it runs into the origin difference problem, and is a bit kludgy.. _______________________________________________ OpenSCAD mailing list To unsubscribe send an email to discuss-leave@lists.openscad.org
TP
Torsten Paul
Fri, Aug 12, 2022 2:17 PM

If you just need the data in OpenSCAD format, you could also use the work-in-progress code from https://github.com/openscad/openscad/pull/3956

ciao,
Torsten.

If you just need the data in OpenSCAD format, you could also use the work-in-progress code from https://github.com/openscad/openscad/pull/3956 ciao, Torsten.
JB
Jordan Brown
Fri, Aug 12, 2022 2:29 PM

On 8/12/2022 6:44 AM, Raymond West wrote:

Given any shape composed of squares and circles, say, such as
square(200);circle(100);  is there a method of converting it to a list
of points for a polygon? The simplest I've found is to export as svg,
then replace the 'L's with' ],['s and delete the rest, sort of, paste
the text back into a polygon, but it runs into the origin difference
problem, and is a bit kludgy..

There is no intra-OpenSCAD way to do that.

However, it isn't hard to represent simple shapes as points to begin
with - e.g.

function square(x, y) = [ [0,0], [0,y], [x,y], [x,0] ];

You can DIY, or you can use a library.  BOSL2 has a number of these: 
https://github.com/revarbat/BOSL2/wiki/shapes2d.scad

It has some operations on those:
https://github.com/revarbat/BOSL2/wiki/paths.scad

On 8/12/2022 6:44 AM, Raymond West wrote: > Given any shape composed of squares and circles, say, such as > square(200);circle(100);  is there a method of converting it to a list > of points for a polygon? The simplest I've found is to export as svg, > then replace the 'L's with' ],['s and delete the rest, sort of, paste > the text back into a polygon, but it runs into the origin difference > problem, and is a bit kludgy.. There is no intra-OpenSCAD way to do that. However, it isn't hard to represent simple shapes as points to begin with - e.g. function square(x, y) = [ [0,0], [0,y], [x,y], [x,0] ]; You can DIY, or you can use a library.  BOSL2 has a number of these:  https://github.com/revarbat/BOSL2/wiki/shapes2d.scad It has some operations on those: https://github.com/revarbat/BOSL2/wiki/paths.scad
RW
Ray West
Fri, Aug 12, 2022 4:13 PM

I've attached an scad file, with comments/rules to explain my method. It
is actually easier/quicker than it looks. I've not thoroughly tested it,
not sure if it does odd stuff like text. It would not be difficult to
write a text editor for the svg file to produce the final polygons. I'm
still not sure if a simple mirror corrects for the origins difference,
more testing/rules required..

Of course, the editing of the svg can be done in the openscad editor,
but then, if you're not careful,  it will mess with your other scad
code, if any.

In the attached, remove the mirror in line 25, and the polygon will be
directly lined up with the shape.

On 12/08/2022 14:44, Raymond West wrote:

Given any shape composed of squares and circles, say, such as
square(200);circle(100);  is there a method of converting it to a list
of points for a polygon? The simplest I've found is to export as svg,
then replace the 'L's with' ],['s and delete the rest, sort of, paste
the text back into a polygon, but it runs into the origin difference
problem, and is a bit kludgy..


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

I've attached an scad file, with comments/rules to explain my method. It is actually easier/quicker than it looks. I've not thoroughly tested it, not sure if it does odd stuff like text. It would not be difficult to write a text editor for the svg file to produce the final polygons. I'm still not sure if a simple mirror corrects for the origins difference, more testing/rules required.. Of course, the editing of the svg can be done in the openscad editor, but then, if you're not careful,  it will mess with your other scad code, if any. In the attached, remove the mirror in line 25, and the polygon will be directly lined up with the shape. On 12/08/2022 14:44, Raymond West wrote: > Given any shape composed of squares and circles, say, such as > square(200);circle(100);  is there a method of converting it to a list > of points for a polygon? The simplest I've found is to export as svg, > then replace the 'L's with' ],['s and delete the rest, sort of, paste > the text back into a polygon, but it runs into the origin difference > problem, and is a bit kludgy.. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
RW
Ray West
Fri, Aug 12, 2022 5:07 PM

text is OK, since it is not svg ltext, it is saved as M/L coordinates.
However, letters such as A,B, D,O,P etc, need differencing with the cut
outs. Not difficult to do, unless a load of text, could possibly be
automated, unless letters were placed on top of each other. (if a
polygon lies inside another polygon, then difference, may be a general
rule.)

On 12/08/2022 17:13, Ray West wrote:

I've attached an scad file, with comments/rules to explain my method.
It is actually easier/quicker than it looks. I've not thoroughly
tested it, not sure if it does odd stuff like text. It would not be
difficult to write a text editor for the svg file to produce the final
polygons. I'm still not sure if a simple mirror corrects for the
origins difference, more testing/rules required..

Of course, the editing of the svg can be done in the openscad editor,
but then, if you're not careful,  it will mess with your other scad
code, if any.

In the attached, remove the mirror in line 25, and the polygon will be
directly lined up with the shape.

On 12/08/2022 14:44, Raymond West wrote:

Given any shape composed of squares and circles, say, such as
square(200);circle(100);  is there a method of converting it to a
list of points for a polygon? The simplest I've found is to export as
svg, then replace the 'L's with' ],['s and delete the rest, sort of,
paste the text back into a polygon, but it runs into the origin
difference problem, and is a bit kludgy..


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

text is OK, since it is not svg ltext, it is saved as M/L coordinates. However, letters such as A,B, D,O,P etc, need differencing with the cut outs. Not difficult to do, unless a load of text, could possibly be automated, unless letters were placed on top of each other. (if a polygon lies inside another polygon, then difference, may be a general rule.) On 12/08/2022 17:13, Ray West wrote: > I've attached an scad file, with comments/rules to explain my method. > It is actually easier/quicker than it looks. I've not thoroughly > tested it, not sure if it does odd stuff like text. It would not be > difficult to write a text editor for the svg file to produce the final > polygons. I'm still not sure if a simple mirror corrects for the > origins difference, more testing/rules required.. > > Of course, the editing of the svg can be done in the openscad editor, > but then, if you're not careful,  it will mess with your other scad > code, if any. > > In the attached, remove the mirror in line 25, and the polygon will be > directly lined up with the shape. > > On 12/08/2022 14:44, Raymond West wrote: >> Given any shape composed of squares and circles, say, such as >> square(200);circle(100);  is there a method of converting it to a >> list of points for a polygon? The simplest I've found is to export as >> svg, then replace the 'L's with' ],['s and delete the rest, sort of, >> paste the text back into a polygon, but it runs into the origin >> difference problem, and is a bit kludgy.. >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
AM
Adrian Mariano
Fri, Aug 12, 2022 8:45 PM

Jordan's answer doesn't go far enough.  BOSL2 will solve the problem
completely, without needing to go outside of OpenSCAD.

include<BOSL2/std.scad>
pts = union([square(200),circle(100)]);

So square() and circle() are defined in BOSL2 as functions that
produce point lists, and union() is a function that computes the union
of polygons defined by point lists.  It returns a list of point lists,
because the answer might be several disjoint polygons, so the desired
answer above will be pts[0].

And the documentation you should check for this is:
https://github.com/revarbat/BOSL2/wiki/regions.scad

On Fri, Aug 12, 2022 at 10:46 AM Jordan Brown
openscad@jordan.maileater.net wrote:

On 8/12/2022 6:44 AM, Raymond West wrote:

Given any shape composed of squares and circles, say, such as square(200);circle(100);  is there a method of converting it to a list of points for a polygon? The simplest I've found is to export as svg, then replace the 'L's with' ],['s and delete the rest, sort of, paste the text back into a polygon, but it runs into the origin difference problem, and is a bit kludgy..

There is no intra-OpenSCAD way to do that.

However, it isn't hard to represent simple shapes as points to begin with - e.g.

function square(x, y) = [ [0,0], [0,y], [x,y], [x,0] ];

You can DIY, or you can use a library.  BOSL2 has a number of these:  https://github.com/revarbat/BOSL2/wiki/shapes2d.scad

It has some operations on those: https://github.com/revarbat/BOSL2/wiki/paths.scad


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Jordan's answer doesn't go far enough. BOSL2 will solve the problem completely, without needing to go outside of OpenSCAD. include<BOSL2/std.scad> pts = union([square(200),circle(100)]); So square() and circle() are defined in BOSL2 as functions that produce point lists, and union() is a function that computes the union of polygons defined by point lists. It returns a list of point lists, because the answer might be several disjoint polygons, so the desired answer above will be pts[0]. And the documentation you should check for this is: https://github.com/revarbat/BOSL2/wiki/regions.scad On Fri, Aug 12, 2022 at 10:46 AM Jordan Brown <openscad@jordan.maileater.net> wrote: > > On 8/12/2022 6:44 AM, Raymond West wrote: > > Given any shape composed of squares and circles, say, such as square(200);circle(100); is there a method of converting it to a list of points for a polygon? The simplest I've found is to export as svg, then replace the 'L's with' ],['s and delete the rest, sort of, paste the text back into a polygon, but it runs into the origin difference problem, and is a bit kludgy.. > > > There is no intra-OpenSCAD way to do that. > > However, it isn't hard to represent simple shapes as points to begin with - e.g. > > function square(x, y) = [ [0,0], [0,y], [x,y], [x,0] ]; > > You can DIY, or you can use a library. BOSL2 has a number of these: https://github.com/revarbat/BOSL2/wiki/shapes2d.scad > > It has some operations on those: https://github.com/revarbat/BOSL2/wiki/paths.scad > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org