MoI discussion forum
MoI discussion forum

Full Version: STL import script with some helpers

From: r2d3
12 Mar 2018   [#1]
Hi all,
i worked on a STL import script for MoI, and that is the result (for now...)
First i have to thank Karsten, Marco, Pilou and Michael for guiding the way!
Second to say: Its a stupid thing to import STL files into MoI because MoI is NURBS and STL is only Polygones!
But if you only have a STL file you can try to rebuild it to NURBS.
The script can help a little...

WARNING: IF YOU IMPORT BIG (>5mb) STL FILES IT CAN TAKE HOURS TO PROCESS AND IT CAN CRASH MOI!



This is what the script does:
1. reading the STL file, creating triangles and joining them to a solid (if possible..)
2. cleaning flat surfaces (removing all needless faces with a double boolean difference)
If your STL object only has flat surfaces -> YOU ARE DONE you will have a NURB object!
3. Redrawing curves and circles
Mostly your STL file will have curved or round surfaces. These will be converted to polylines in your STL file and the script helps you to redraw the polys to NURBS. In the end you will have nice outlines of the original STL object and you can rebuild it with the normal tools of MoI (Joining, Loft and Boolean operations).

SOME screenshots:

The imported Triangles as a solid:


Result after "Clean flat surfaces"


Select all polyline curves (no straight lines)


Choose if it should be a curve:


or a circle:

(Line is for a straight polyline which has more than two points. It will be converted to a 2 point line. Dont select this together with a curved polyline)

Result:


After some face deleting, joining, lofting:


and the final boolean difference you end up with a clean NURBS !


Use the script also for "Only Cleaning":


For every state of the import you find a object in the Objects folder:


HAVE FUN!

EDIT 23.03: added a binary STL Fault detection and putting the script files into a zip

Attachments:
STLimport.zip

Image Attachments:
2018.03.12-14.48.20-[3D].png  2018.03.12-14.49.20-[3D].png  2018.03.12-14.58.16-[3D].png  2018.03.12-14.59.42-[3D].png  2018.03.12-15.15.58-[3D].png  2018.03.12-15.17.23-[3D].png  Ohne Titel 11.png  Ohne Titel 12.png  Ohne Titel 2.png  Ohne Titel 3.png  Ohne Titel.png 


From: Frenchy Pilou (PILOU)
12 Mar 2018   [#2]
That is a task! Bravo!

But what about load an STL file inside a "Polygon modeler" then export as OBJ, then import "OBJ file" inside MOi ?
From: mkdm
12 Mar 2018   [#3] In reply to [#1]
Hello "r2d3"!

Many thanks for this very interesting "coding"! My congratulations!

Maybe not really useful for "real life" use-cases but anyway, very interesting piece of code.

Thank you very much :)

Ciao!
From: r2d3
12 Mar 2018   [#4] In reply to [#2]
>>But what about load an STL file inside a "Polygon modeler" then export as OBJ, then import "OBJ file" inside MOi ?

My old way to import STL was to use FreeCad to prepare the file for MoI.. But you still have no clean NURBS file...
Or do you have a SECRET PILOU trick??
From: r2d3
12 Mar 2018   [#5] In reply to [#3]
>>> Maybe not really useful for "real life"...

For 3Dprint-MoIers the STL is a very common format.
But sometimes a only as STL available print file needs a little tweak to fit and then you need a clean file ...

>> but anyway, very interesting piece of code.

THX :-)
From: mkdm
12 Mar 2018   [#6] In reply to [#5]
Hello r23d!

Just a little clarification to my previous words :)

When I've written "Maybe not really useful for "real life"..." I only meant that unfortunately the execution of the scripts with objects containing many faces is rather slow.
Clearly because Moi is not very well suited to manage that kind polygonal information, and this is normal.

@You : "...For 3Dprint-MoIers the STL is a very common format..."

Yes, for sure!

Anyway as I've said, it's a really useful bunch of JS code, a kind of "proving ground" :)

Thanks.

Marco (mkdm)
From: Frenchy Pilou (PILOU)
12 Mar 2018   [#7]
<< a SECRET trick??
_import OBJ by Max Smirnov ?
But not sure that result will be different than your before some cleaning...
From: r2d3
12 Mar 2018   [#8] In reply to [#6]
Absolut no clarification necessary :)

>> Anyway as I've said, it's a really useful bunch of JS code, a kind of "proving ground" :)
Exactly, just playing around and learning whats possible to realise with MoI.
From: Hamish Mead (HAIRYKIWI)
12 Mar 2018   [#9]
Very nice work Ralf!

There can never be enough conversion tools - even if extra time is still needed to clean and tweak such imported files.

Many thanks :)

- Hamish
From: Chris (UGMENTALCASE)
18 Aug 2022   [#10]
Hi,

Is there a way to edit this script so it just imports the stl without trying to make it solid and so on? I have literally no idea when it comes to scripts so forgive me :-)
From: Michael Gibson
18 Aug 2022   [#11] In reply to [#10]
Hi Chris, you might want to try converting the STL file to .obj format and then use the OBJ to 3DM wireframe converter from here:
https://moi3d.com/wiki/Resources#Obj23dmWireframe_converter

That makes wireframe lines or closed polylines for each polygon instead of trying to construct a solid out of it which is much more heavyweight.

If you did want to edit this script to not try to join it into a solid, the joining part is done by a function named JoinTriangles(). The function is called on line number 17 in the file _STLimport.js . It's a long line of code, look for the piece of it that has this:

var solid=JoinTriangles(triangles);var sol=solid.numSolids;if(sol<1){var message="SORRY!\nI WAS NOT ABLE TO MAKE A SOLID!\nDont't try to \"Clean flat surfaces\"";}else{var message=sol+" SOLID CREATED!";};

Delete all of that or "comment it out" by putting /*    */ around it and then it shouldn't do the joining part.

- Michael
From: Chris (UGMENTALCASE)
19 Aug 2022   [#12] In reply to [#11]
Cheers I'll take a look. I have used the other script a little bit, but sometimes there is just too many lines!