New automatic tool to convert STL back to parametric sketches

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
xryl669
Posts: 32
Joined: Fri Jul 31, 2020 1:19 pm

New automatic tool to convert STL back to parametric sketches

Post by xryl669 »

To follow this thread asking for a documented file format for parametric modeling, I've made a lot of progress on my tool to convert a mesh based STL model back to sketches that could be reproduced in FreeCAD if we were to have a documentation of the file format :roll:

The tool is web based (based on ThreeJS and my STL model viewer plugin).
It's plugin based, so you can enable/disable feature to help reconstructing the parametric model.

Here it's some screenshot of the tool in action (and a description about how it's working):
1. You upload a STL file like this one:
InitialModel.png
InitialModel.png (241.39 KiB) Viewed 4674 times
2. The tool computes the convex hull and an oriented bounding box (like this):
ConvexHull.png
ConvexHull.png (90.9 KiB) Viewed 4674 times
3. The tool then transform the object so it's aligned with world axis like this:
OrientedModel.png
OrientedModel.png (87.42 KiB) Viewed 4674 times
4. Then it starts its smart job, by enumerating all sides of the object (that is, the physical planar sides of the solid). So instead of triangles like above, you know have planes with curves in them:
DetectedSides.png
DetectedSides.png (137.42 KiB) Viewed 4674 times
5. There are many heuristics going on here like converting segments on a potential arcs to an actual arc, and merging segments, and so on. This builds many curves (wire in OCCT wording) and those are collected to sketches. The sketches are then rendered with a renderer (here, it's canvas) like this:
Sketches.png
Sketches.png (112.74 KiB) Viewed 4674 times
Please notice arcs (which are detected and drawn with a different color), construction lines (like angle and arc's diameter), line length and angles between them.

Right now, that's where it stops.

I plan to add the following features:
  • Export to a FCStd file format. The idea would be to use OpenCascade.js to generate a BREP document per sketch (instead of the browser's canvas code here). I think it's feasible.
  • Detect simple extrusion (like in the obvious example above), I think I have some heuristics that could work for this
  • Detect fillet/chamfer and remove them before computing curves. I have some idea for these, for basic geometry "correction".
  • Make a standalone version that could bind with my viewer so the user could ask to generate a sketch from the selected/clicked plane
Obviously, it won't help for very complex models (like those using a sweep or an helix), but at least, it should help a lot when trying to fix/reproduce an object where only the STL model is available.

For this roadmap, I'd like to get a bit more details on the FCStd file format (the minimum required to build a body with some sketches in them).
xryl669
Posts: 32
Joined: Fri Jul 31, 2020 1:19 pm

Re: New automatic tool to convert STL back to parametric sketches

Post by xryl669 »

Few more capture with a more complex object:

The object with its detected sides:
square.png
square.png (128.97 KiB) Viewed 4670 times
The computed sketches:
SketchesSquare.png
SketchesSquare.png (169.43 KiB) Viewed 4670 times
fsteinha
Posts: 1
Joined: Mon Oct 18, 2021 5:26 am

Re: New automatic tool to convert STL back to parametric sketches

Post by fsteinha »

I'm looking for such a tool.
Have you reach any progress with this ? And is there trial version of it ?
xryl669
Posts: 32
Joined: Fri Jul 31, 2020 1:19 pm

Re: New automatic tool to convert STL back to parametric sketches

Post by xryl669 »

I'm very close to release it. I'll post the link to the tool here once it's released.
Right now, I'm able to export sketches as PNG and SVG but not FreeCAD format yet.
Also, the plugin to detect chamfer and fillet isn't working as expected (but I'm still working on it).
xryl669
Posts: 32
Joined: Fri Jul 31, 2020 1:19 pm

Re: New automatic tool to convert STL back to parametric sketches

Post by xryl669 »

Ok, it's alive on the internet on this page, click the "Parametizer" link to go to the application.

Feedback welcome!
xryl669
Posts: 32
Joined: Fri Jul 31, 2020 1:19 pm

Re: New automatic tool to convert STL back to parametric sketches

Post by xryl669 »

And now, there's an alpha version of the export to FreeCad sketch plugin:
FreeCadExport.png
FreeCadExport.png (26.72 KiB) Viewed 3815 times
It's finally a fully automatic and parametric conversion of STL to FreeCAD!

Available here for testing
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: New automatic tool to convert STL back to parametric sketches

Post by chrisb »

I tried to convert this file. It hangs at 1% of "Draw sides".
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
xryl669
Posts: 32
Joined: Fri Jul 31, 2020 1:19 pm

Re: New automatic tool to convert STL back to parametric sketches

Post by xryl669 »

Ok, I've found out the reason why it fails for your file: it's because it's huge.
When I'm making the side of the N-Gon for each plane of the model, I was using a map whose key is made from the ((index of A vertex of the segment) << N) | (index of B vertex of segment)). In your model, the N was chosen to 16 because you have close to 49000 vertices and the closed power of two was 2¹⁶. Because of this, the resulting key was using all 32 bits, it became negative when used as a index in the vertices array, it crashed.

I've fixed this now, but it's not loading either for another reason: out of memory. I've 32GB of memory on my computer, but the huge amount of faces in your model, with each one trying to create a sketch consumes the whole memory and make the tab crash.

That's very interesting since it means that it's not scalable.
I guess I'll need to either:
1. Simplify the model
2. Or change the behavior of the tool so that the user clicks on the plane she wants to produce a sketch for instead.

Simplifying the model might be easy to do (there are libs for this), but the resulting sketches will be off because of the approximations when merging vertices. Letting the user pick the plane means it's not automatic anymore, but in the end, who cares ?
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: New automatic tool to convert STL back to parametric sketches

Post by chrisb »

It is of course ok, that there are limits. If possible you could name them.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
xryl669
Posts: 32
Joined: Fri Jul 31, 2020 1:19 pm

Re: New automatic tool to convert STL back to parametric sketches

Post by xryl669 »

I'll try to simplify the model if it's too big. It'll be another plugin and it shouldn't bother too much (except for the loss of precision here).
As for selecting the face/plane it'll require more work, but it's not impossible to implement, since I've already a pick tool plugin for debugging.
Post Reply