Export STL script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
neo2001
Posts: 34
Joined: Wed Oct 23, 2019 12:25 pm

Export STL script

Post by neo2001 »

I know Python and looking at the Python console in FreeCAD helps a lot to understand what's going on in the background.

Still, before trying this I wanted to ask how I should approach this and where to start:

Creating and exporting a mesh to STL format is something I do very often and it's always the same procedure:

1 - Select object(s) in the Combo View/Tree
2 - Go to the "Mesh Design" Workbench
3 - Select "Meshes" --> "Create Mesh from Shape..."
4 - Select "Standard" (Mesh Engine)
6 - Set 0.01 for Surface deviation
7 - Set 5° for angular deviation
8 - Press "OK"
9 - Right klick on the Mesh created and select "Export mesh..."
10 - Select always the same destination directory
11 - Select STL file type
12 - Replace the filename with the name of the current FreeCAD document
13 - Save the STL file
14 - Hide everything besides the created mesh to review it

-AND/OR-

14 - Delete the mesh
15 - Switch back to the "Part Design" workbench

How would I start you do all this in a script? Or does creating a macro make more sense?
chrisb
Veteran
Posts: 54201
Joined: Tue Mar 17, 2015 9:14 am

Re: Export STL script

Post by chrisb »

You may search the forum, others have done this before.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
neo2001
Posts: 34
Joined: Wed Oct 23, 2019 12:25 pm

Re: Export STL script

Post by neo2001 »

I tried that, but I didn't find anything comprehensive.

I also thought that maybe someone has already done this, but everything that looked promising turned out to be not very useful. It always boils down to a simple one-line export script, maybe in a for loop.

I guess I stick to manually exporting or maybe try to using a macro. It's a little bit annoying since I need to re-enter the values I want every time I open the dialog window. But not annoying enough to invest hours to write a script. ;) :)
chrisb
Veteran
Posts: 54201
Joined: Tue Mar 17, 2015 9:14 am

Re: Export STL script

Post by chrisb »

I have seen this here very recently in the past couple of days :) - which still means a few hundred posts :cry: . I will give the search a try.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
chrisb
Veteran
Posts: 54201
Joined: Tue Mar 17, 2015 9:14 am

Re: Export STL script

Post by chrisb »

This is what I had in mind: https://forum.freecadweb.org/viewtopic. ... 37#p347537. Alas, it is not quite what you wanted, but it handles the critical resolution stuff. You may combine it with the other variants you found or contact the author if he can be of help. You may also look at the python console and watch what happens there if you do it manually or you can try to record a macro.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
bavariaSHAPE
Posts: 406
Joined: Tue Jun 10, 2014 8:31 am
Contact:

Re: Export STL script

Post by bavariaSHAPE »

neo2001 wrote: Sat Nov 16, 2019 11:45 am I know Python and looking at the Python console in FreeCAD helps a lot to understand what's going on in the background.

Still, before trying this I wanted to ask how I should approach this and where to start:

Creating and exporting a mesh to STL format is something I do very often and it's always the same procedure:

1 - Select object(s) in the Combo View/Tree
2 - Go to the "Mesh Design" Workbench
3 - Select "Meshes" --> "Create Mesh from Shape..."
4 - Select "Standard" (Mesh Engine)
6 - Set 0.01 for Surface deviation
7 - Set 5° for angular deviation
8 - Press "OK"
9 - Right klick on the Mesh created and select "Export mesh..."
10 - Select always the same destination directory
11 - Select STL file type
12 - Replace the filename with the name of the current FreeCAD document
13 - Save the STL file
14 - Hide everything besides the created mesh to review it

-AND/OR-

14 - Delete the mesh
15 - Switch back to the "Part Design" workbench

How would I start you do all this in a script? Or does creating a macro make more sense?
Not a script, but with this possibility, I am over 95% successful.
… for a happy FreeCAD-World … JM2C …
neo2001
Posts: 34
Joined: Wed Oct 23, 2019 12:25 pm

Re: Export STL script

Post by neo2001 »

I've also tried using the Export feature, but I basically like the more detailed version were I can review what was created. I also found that the latest nightly build does actually save the values entered in the Standard Mesher dialog window.

What I'm somehow missing is a complete API documentation.

There is a note, that the one in the Wiki isn't up to date anymore and the following page should be used:
https://www.freecadweb.org/api/

Maybe I'm blind? :geek: Do I need to build the documentation first?

EDIT: Yes, I'm blind, I need to select something in the menu above...

I recorded a macro and I try to use this as a base for my own script and added some comments mostly for myself :) :

I guess I need to understand the terminology before getting this right. Like how the "latest" object is called in the tree (Is it the tip?) and how to get it. I know, that there is a 3D object and a GUI object. I'm not sure yet, if I really need the GUI for the mesh creation itself.

Code: Select all

# -*- coding: utf-8 -*-

import FreeCAD
import PartDesignGui
import Mesh
import MeshPartGui, FreeCADGui
import MeshPart

Gui.activateWorkbench("MeshWorkbench")
FreeCADGui.runCommand('MeshPart_Mesher')  # Opens dialog in sidebar (really needed?)

__doc__ = FreeCAD.activeDocument()  # Get active document
__mesh__ = __doc__.addObject("Mesh::Feature", "Mesh")  # Add (empty?) mesh object
__part__ = __doc__.getObject("Pad001")  # This somehow(TM) needs to return the object including all features (without knowing the names)
__shape__ = __part__.Shape.copy(False)  # Not sure what's going on here, yet...
__shape__.Placement = __part__.getGlobalPlacement()  # Get placement for putting the mesh exactly where the source geometry exists.

# Convert to mesh
# AngularDeflection's unit is Radians! Maybe use Math package to convert degrees...
__mesh__.Mesh=MeshPart.meshFromShape(Shape=__shape__, LinearDeflection=0.01, AngularDeflection=0.0872665, Relative=False)
__mesh__.Label="__doc__."  # Somehow(TM) get the filename of the active document and name the mesh or set the name while exporting the STL file (where?)
__mesh__.ViewObject.CreaseAngle=25.0
del __doc__, __mesh__, __part__, __shape__  # Cleaning up stuff

Gui.getDocument("F450_gpsHolder_cup").getObject("Pad001").Visibility=Hide  # Hide the other objects to show only the mesh, needs to work without hard coded names
Not sure yet, where the actual file gets created/exported.
Bayesian
Posts: 90
Joined: Thu Aug 08, 2019 1:49 pm

Re: Export STL script

Post by Bayesian »

I tend to put these thing into Jupyter Notebook Cells, and just run the export commands from there. Also works with multiple models or multiple variants of the same model with different parameters.

I'm working on a different solution, based on FeaturePython objects in the tree. An STL export would then be an object, the parameters (which object, filename etc) would be represented by properties and the attachment would be used for modifying the placement. That way the export information can be saved to the FreeCAD file.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Export STL script

Post by DeepSOIC »

Bayesian wrote: Wed Nov 20, 2019 10:14 am I tend to put these thing into Jupyter Notebook Cells, and just run the export commands from there. Also works with multiple models or multiple variants of the same model with different parameters.

I'm working on a different solution, based on FeaturePython objects in the tree. An STL export would then be an object, the parameters (which object, filename etc) would be represented by properties and the attachment would be used for modifying the placement. That way the export information can be saved to the FreeCAD file.
Part-o-magic Exporter? https://forum.freecadweb.org/viewtopic. ... 50#p164011
Bayesian
Posts: 90
Joined: Thu Aug 08, 2019 1:49 pm

Re: Export STL script

Post by Bayesian »

DeepSOIC wrote: Wed Nov 20, 2019 4:27 pm Part-o-magic Exporter? https://forum.freecadweb.org/viewtopic. ... 50#p164011
I wasn't aware of this and will take a look!
Post Reply