Ability to hook into Mesh/STL export process?

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!
Post Reply
User avatar
samudge
Posts: 16
Joined: Fri Nov 19, 2021 11:45 am

Ability to hook into Mesh/STL export process?

Post by samudge »

When I'm creating a complex parametric part/model, I often omit the "final" boolean union on complex part's components, as it often greatly increases computation time. Instead, I'll simply put all the components of the part in a Std_Part container, and when it's time for export, I'll select the Std_Part container to export the STL.

Unfortunately this means the STL model is not "correct", in that it contains internal faces/data where internal shared faces have not been removed by the boolean operation I have omitted.

To get around, this, I've create a simply python script using the 'pymeshlab' library, which very quickly performs a mesh-based boolean union on the exported STL.

The end result is a correct STL part, cleaned of all internal faces. This saves me a lot of time in FreeCAD, not having to wait for recomputation of a complex set of boolean operations.

I currently manually drag-and-drop my STL files onto my python script, but I was hoping for a way to hook into FreeCAD's STL export operation, to run an external script or command on the exported file after it has finished exporting. Obviously I could code this into a private fork, but I was hoping there would be an existing plugin interface to make this possible.
wmayer
Founder
Posts: 20202
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Ability to hook into Mesh/STL export process?

Post by wmayer »

You can implement your own FreeCAD extension module. Therefore determine the path you get with

Code: Select all

App.getUserAppDataDir()
Open a file explorer and navigate to the above returned path. If there is not yet a directory Mod create it. Then create a sub-directory e.g. MeshExt. Inside this sub-directory add two files:
  • Init.py with this content

    Code: Select all

    FreeCAD.changeExportModule("STL Mesh (*.stl *.ast)", "Mesh", "MeshExt")
    
  • MeshExt.py with this content

    Code: Select all

    import Mesh
    
    def export(meshes, filename):
        Mesh.export(meshes, filename)
        print ("Use pymeshlab")
    
In a last step adjust the export() function to your needs.
Post Reply