Create arcs and circles from tessellated segments

This forum is for 'Work-in-Progress' QCAD user developed script Add Ons and Plug-ins.

Please use this forum to request/submit your script Add-Ons & Plug-in idea's.

Moderators: andrew, Husky, J-J

Post Reply
bergdesign
Newbie Member
Posts: 8
Joined: Sat May 25, 2019 4:23 pm

Create arcs and circles from tessellated segments

Post by bergdesign » Fri Oct 09, 2020 3:06 pm

I would love to have a feature/tool/script/plug-in added to QCAD. I work regularly with DXF files generated from SketchUp, and the problem is that SketchUp generates tessellated arc and circles in all 2D output. This tessellated output doesn't work well with CNC machines because the tool path generation can create stops and starts as it randomly sees fit, or the automatic conversion to smooth curves just flat out sucks and a perfect 24 segment circle gets converted to three or four bezier curves that are no longer accurate.

So it's common practice for me to use three-point arcs and two point circles to replace the tessellated geometry, which goes fairly quickly but the deletion of the existing line segments is the tedious and error prone part... Because there is now overlapping geometry, I find it best to drag a selection box across part of the arc/circle (without selecting the arc/circle) and delete those segments then drag a selection box across the remaining segments to delete them.

So, I have been mulling writing a plug-in to take a user selection of line segments, order the endpoints, pick the start and end points and one near the middle (depends on odd or even number of segments if you have a midpoint), then generate a three-point arc, delete the original selection and select the newly generated arc so the user has visual confirmation. If the points all share two entities, you have a complete circle and can generate a two point circle, again deleting the original selection and selecting the newly generated circle. Of course there are inaccuracies and edge-cases to automating anything, but 99% of the scenarios can be covered with really simlpe logic and minimal error checking. The tessellated geometry is always quite clean.

I'm an old-school developer, but this has been on my to-do list for waaayyyyyy too long because it's always hardest to jump into a framework and gain familiarity before you can actually be constructive, and I just haven't done that yet :/ I pulled the source and have browsed through many of the scripts for the native tools (like the ShapeAlgorithms class), but I haven't found a good example script to jump-off from that does something with a user selection of existing entities (can anyone suggest an appropriate existing script?) It looks like I would use queryContainedEntities() and then work with the returned set of entities, then create an RArcEntity or RCircleEntity and insert it into the document tree, deleting the original contained entities. Then write the other 99% of the code that makes it usable :)

If anyone with experience wants to go at this together, I'm all ears. I'd generate icons and psuedo-code the behavior logic I'd like to see, as well as handling edge cases and error conditions, and whatever else I can write.

Thanks,
Brock

CVH
Premier Member
Posts: 1210
Joined: Wed Sep 27, 2017 4:17 pm

Re: Create arcs and circles from tessellated segments

Post by CVH » Fri Oct 09, 2020 7:41 pm

Hi,
You mean curved lines that are approximated with line segments.
Kind of interpolated.
Is that standard for SketchUp?
I would see 'to segments' as an optional output method for an old CNC setup.

3 scripts of mine in the open take a selection.
Encrypted by a completely uneducated idle starting with no knowledge of the language, the object oriented nature and the QCAD framework.
1- FlexPainter + there is a ton of do and don'ts in the script file itself.
2 - 3 can be found here:
viewtopic.php?f=30&t=7544
viewtopic.php?f=30&t=7497
1 is already for some time a part of a standard installation.

The fourth one is in development and is a different story on its own.
Tile2Hatch is an addon that converts a tile to a hatch pattern and it starts with approximating all to line segments. :wink:

It is the other way around that is quite another story.

A lot off scripts are included in compiled form.
A fairly complete source apart from PRO scripts is github : https://github.com/qcad/qcad

All the classes are documented here: https://www.qcad.org/doc/qcad/latest/de ... tated.html

Getting lists from the resources is usually "get" or "query".
Just key "query" in the classes search field will reveal a multitude of queries to choose from.

A file or different files, small and handy, with some example would be nice.
If there is indeed a pattern in it the way back to curved is much easier. :)

Regards,
CVH

bergdesign
Newbie Member
Posts: 8
Joined: Sat May 25, 2019 4:23 pm

Re: Create arcs and circles from tessellated segments

Post by bergdesign » Sat Oct 10, 2020 5:46 pm

Hi CVH.
CVH wrote:
Fri Oct 09, 2020 7:41 pm
You mean curved lines that are approximated with line segments. Kind of interpolated. Is that standard for SketchUp?
Unfortunately, that is the standard for SketchUp. It's a polygonal modeler, and while it maintains some internal data about each curve which allows you to change the number of segments used to represent each curve inside your 3D model, SketchUp's 2D output to DXF is always the segmented representation. If the 2D DXF is not subsequently edited to change the segments to smooth curves, the CAM software typically stops and starts at the endpoints of each segment causing wasted machine time and excessive tool wear :/

I appreciate the tips for additional scripts to look at. It's always hardest getting familiar with the "domain" that is being programmed, so it helps a lot to have more code samples when learning how to accomplish specific tasks. The logic of my feature is the easy part, once I know how to speak QCAD's language. It's all the supporting code that integrates it into QCAD that I need to get familiar with.

Thanks,
Brock

CVH
Premier Member
Posts: 1210
Joined: Wed Sep 27, 2017 4:17 pm

Re: Create arcs and circles from tessellated segments

Post by CVH » Sat Oct 10, 2020 7:27 pm

bergdesign wrote:
Sat Oct 10, 2020 5:46 pm
causing wasted machine time and excessive tool wear :/
Tell me what ... I am an engraver, engraving points down to the sub 0.1 mm.
A simple 0.05 plunge down in non-ferro can ruin it.
Sharp dead end corners will rip it apart.
Re-dressing is the hardest endeavor and take the longest.

Continuous motion within a narrow margin is the key to increase standing time.
I have the luck that my CNC driver has a look ahead feed algorithm.
Simply because the guy who is marketing it has real life know how.
Look up: Eding CNC ... it is no longer a hobby tool.

Happy reading the resources,
CVH

bergdesign
Newbie Member
Posts: 8
Joined: Sat May 25, 2019 4:23 pm

Re: Create arcs and circles from tessellated segments

Post by bergdesign » Sun Oct 11, 2020 3:15 pm

I'm at the opposite end of the scale, running a 5ft x 10ft CNC router :) It's a older machine by a Canadian company AXYZ that started by making CNC controllers, and while it uses its own DNC software that does some optimization when downloading g-code to the machine, it relies on 3rd party software like Enroute or VCarve to do the initial tool path generation. Unfortunately, the DNC software doesn't seem to do any look-ahead X-Y optimizations because it will happily allow the head to dwell at the end of a straight segment even if the next straight segment is only a fraction of a degree off... So if you leave a tessellated curve as, for example, ten (10) separate straight segments, the machine will dwell at the end of every segment. If you instead combine the ten separate segments into a polyline in the CAM software, you will get continuous motion but will have a ten-sided "curve" cut in your material. So to make the router bit stay cool and sharp and make the curves nice and smooth, I convert all tessellated curves into true arcs that cut smoothly and continuously.

So my problem isn't that it breaks bits or ruins material, it's that the dwelling at corners causes rubbing and heating of the bit which shortens its lifespan. I have to keep it moving and keep the chip size up to make it happy.

Interesting stuff at Eding - thanks. I have a friend that manufactures 3D printers that may be interested in their controllers, so I'll pass the info along. My older CNC router could be retrofitted with a newer controller, but the task would be a monumental undertaking so I'll just upgrade to one of AXYZ' newer modern machines some day so that I can spend my time designing and making things :)

Thanks,
Brock

CVH
Premier Member
Posts: 1210
Joined: Wed Sep 27, 2017 4:17 pm

Re: Create arcs and circles from tessellated segments

Post by CVH » Mon Oct 12, 2020 9:42 am

Brock,

The method you need is quite similar as on my wish list.
> The inverse of "Arc to line segments"

I think that we are speaking of closed contours mostly.
Select All >> Polyline from Selection
This creates logically closed polylines with closed contours.
My Eding CAM handles these but that is not certain for all.
>> Logically open Polylines

At that point one can check the selection:
if NOT a poly >> Reject
else Collect to process for arc's

Now we can step the segments of each collected poly:
When fewer vertexes (left) as 3 >> skip to next collected
Circle from the 3 first vertexes 1/2/3
Circle2 from vertex 1/2/4
When Circle equals Circle2 >> check vertex 1/2/5
NOT equals >> try next set of 3
When the former circle fitted over a wider span as N/N+1/N+2
>> replace segments by a single segment with a proper bulge.
>> start next set at N+2

When the poly ends up to be a full circle arc what is a fault condition for the bulge >> replace by circle

Quite rude I know, but I think it is a valid approach.
Bad thing is that any regular polygon and quite a lot specific polygons ends up as a circle.

Regards,
CVH

bergdesign
Newbie Member
Posts: 8
Joined: Sat May 25, 2019 4:23 pm

Re: Create arcs and circles from tessellated segments

Post by bergdesign » Wed Feb 17, 2021 5:10 pm

CVH,

Being locked up in-home during the long freeze in Texas is finally giving me some free time to work on this :) The shop is too cold and the roads are treacherous, so I'm having fun using the uninterrupted time to get acclimated with QCADs scripting.

I have a working script that is basically just a proof of concept at this point that uses your suggestion of first converting to a polyline and then walking the segments. I am focusing on open arcs to begin with because my most common scenario is converting tessellated fillets to smooth curves. I do the polyline conversion, walk the segments and extract the points, then take the first, last and a middle point to generate a three-point arc. In my SketchUp geometry, the arcs are tessellated perfectly so the results are exact (the tessellated curves are inscribed vs circumscribed, so the points lie on the ideal curve).

A problem that I'm encountering is that the PolyLineFromSelection object needs to be "initialized" by manually using it once from the toolbar or menu item before it is available to the script, without which I get the error "ReferenceError: Can't find variable: PolylineFromSelection". I have read Andrew's comments in a couple of threads about it being undocumented, but there is nothing that suggests what file to include or command to run before it is available to the script. Should I just instantiate it as an object in my script to wake it up?

I'm still in the struggling stage with the framework because documentation is pretty sparse, but being an old C/C++ developer, I now understand that it's all a structured inheritance model that's integral to the app itself and not a scripting environment bolted onto the side... The web site has a few scripting pages that don't make this clear at all, and that made it really difficult to even figure out how to get my script into the "Misc" menu so that I could run it from the UI. I'll probably write a few comments for Andrew to help improve the docs, since my difficulties arose from these web site pages that caused more confusion than they helped... Things sank in once I ignored them and focused on the tutorials under the intro section of the docset :)

And one other question... Because the PolylineFromSelection is an object and not an operation that is applied, what is the correct way to "wrap it" so that its result is included in a transaction group that can be reversed with a single undo? Using this selection-to-polyline-to-arcs methodology means that I perform a number of operations that need to be coalesced into a single undo. It's easy to create the transaction group and operations for the arc creation and polyline deletions, but the PolylineFromSelection.autoJoinSegments() function is outside of the scope of the transaction group :/

Thanks,
Brock

CVH
Premier Member
Posts: 1210
Joined: Wed Sep 27, 2017 4:17 pm

Re: Create arcs and circles from tessellated segments

Post by CVH » Wed Feb 17, 2021 6:03 pm

bergdesign wrote:
Wed Feb 17, 2021 5:10 pm
A problem that I'm encountering is that the PolyLineFromSelection object needs to be "initialized" by manually using it once from the toolbar or menu item before it is available to the script
That looks like an 'include' flaw ... :!:
bergdesign wrote:
Wed Feb 17, 2021 5:10 pm
I'm still in the struggling stage with the framework because documentation is pretty sparse,
No comment to add ... :oops:
bergdesign wrote:
Wed Feb 17, 2021 5:10 pm
PolylineFromSelection, what is the correct way to "wrap it"
Rather 'unwrap' ...
Copy the 'GUI way' over to your script.
bergdesign wrote:
Wed Feb 17, 2021 5:10 pm
and that made it really difficult to even figure out how to get my script into the "Misc" menu so that I could run it from the UI.
I pointed to 3 of my endevoars.
All 3 are rather over documented ... All 3 add a GUI entry.
Just follow the general example ... :wink:


About off topics I have replied private too.
See Private Messages.

Regards,
CVH

bergdesign
Newbie Member
Posts: 8
Joined: Sat May 25, 2019 4:23 pm

Re: Create arcs and circles from tessellated segments

Post by bergdesign » Thu Feb 18, 2021 5:55 pm

CVH,

Here's a functioning alpha-quality script for QCAD Pro that converts both open and closed sequences of line segments into arcs and circles. I still have a number of features that I wish to implement, but it's a starting point for conversation purposes :)

The following instructions are for the users that don't know where to put this script or how to access it...

In its current incarnation, create a parent "ArcFromSelection" folder and place the folder and attached Javascript file in the following relative location for your platform. I'm on a Mac, so my scripts folder is inside the app package in /Contents/Resources/scripts/.

scripts/Misc/Examples/drawExamples/ArcFromSelection/ArcFromSelection.js

When you restart QCAD Pro, the script will appear under Misc > Script Examples > Draw > Arc From Selection. Open the attached example dxf file, select the corner fillets and circle-shaped polygons and run the script.

In a nutshell, here's the script's current functionality:

1) Filters the user selection to remove any entity that is not a line entity
2) Converts all remaining entities in the selection set to polylines using the native "polyline from selection" tool
3) Walks the resulting set of polylines, assuming each polyline is composed of a sequence of uniform segments that represent a tessellated arc or circle (think polygonal geometry exported from apps such as SketchUp)
4) For geometrically open polylines, a three point arc is created using the endpoints and the midpoint
5) For geometrically closed polylines, a three point circle is created using the start point, the 1/3 point and the 2/3 point
6) If a circle or arc is successfully created, the converted polyline is deleted
7) The resulting arcs and circles are selected

In this version, the undo menu item is not properly labeled... The function name shows up for redo but not undo :/ I want to filter out sequences of segments that are not uniform in length because these do not represent tessellated arcs and circles, and would instead represent freehand curves and squished polygons like ellipses. It is not my intention to handle these cases, but once I get enough safety logic implemented for my own usage purposes, it might be nice to at least handle the case of ellipses.

Let the feedback and improvements begin :)

Edited: Added before and after images to show the selected entities and the results

Brock
Attachments
before.png
before.png (44.74 KiB) Viewed 319 times
after.png
after.png (45.1 KiB) Viewed 319 times
ArcFromSelection example file.dxf
(163.31 KiB) Downloaded 5 times
ArcFromSelection.js
(7.27 KiB) Downloaded 7 times

bergdesign
Newbie Member
Posts: 8
Joined: Sat May 25, 2019 4:23 pm

Re: Create arcs and circles from tessellated segments

Post by bergdesign » Thu Feb 18, 2021 7:14 pm

This may be a comment and question for Andrew, but it has to do with the Undo menu item text...

I cannot find a way to set the menu item text for a transaction group, but it appears that if I name every operation before it is added to the transaction group and subsequently applied, the menu then displays the correct text. So, does the Undo menu item simply display the text from the last operation applied?

Post Reply

Return to “QCAD 'Script Add-On & Plug-in challenge' - Work in Progress”