Export STL script

Need help, or want to share a macro? Post here!
neo2001
Posts: 25
Joined: Wed Oct 23, 2019 12:25 pm

Export STL script

Postby neo2001 » 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?
chrisb
Posts: 19590
Joined: Tue Mar 17, 2015 9:14 am

Re: Export STL script

Postby chrisb » Sat Nov 16, 2019 1:55 pm

You may search the forum, others have done this before.
neo2001
Posts: 25
Joined: Wed Oct 23, 2019 12:25 pm

Re: Export STL script

Postby neo2001 » Sat Nov 16, 2019 4:17 pm

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
Posts: 19590
Joined: Tue Mar 17, 2015 9:14 am

Re: Export STL script

Postby chrisb » Sat Nov 16, 2019 4:24 pm

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.
chrisb
Posts: 19590
Joined: Tue Mar 17, 2015 9:14 am

Re: Export STL script

Postby chrisb » Sat Nov 16, 2019 4:35 pm

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.
User avatar
bavariaSHAPE
Posts: 275
Joined: Tue Jun 10, 2014 8:31 am

Re: Export STL script

Postby bavariaSHAPE » Sat Nov 16, 2019 9:43 pm

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.
Gruß Achim (der Bau-Raum/FreeCAD)
neo2001
Posts: 25
Joined: Wed Oct 23, 2019 12:25 pm

Re: Export STL script

Postby neo2001 » Sun Nov 17, 2019 3:23 pm

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: 57
Joined: Thu Aug 08, 2019 1:49 pm

Re: Export STL script

Postby Bayesian » 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.
User avatar
DeepSOIC
Posts: 7149
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Export STL script

Postby DeepSOIC » Wed Nov 20, 2019 4:27 pm

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: 57
Joined: Thu Aug 08, 2019 1:49 pm

Re: Export STL script

Postby Bayesian » Thu Nov 21, 2019 8:59 am

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!