-
-
Save carlosbaraza/8c91b6dd6f1c977912fec59b3f4a0cd2 to your computer and use it in GitHub Desktop.
FreeCAD macro to export all visible parts as STL. Include all groups children.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Export Root Bodies to STL | |
| # | |
| # This is a FreeCAD script to export all visible root bodies in STL mesh format. | |
| # Files will be named as "documentname_bodylabel.stl". | |
| # | |
| import FreeCAD | |
| import os.path | |
| doc = FreeCAD.activeDocument() | |
| # Check if the document is saved (i.e., has a file name) | |
| if not doc.FileName: | |
| FreeCAD.Console.PrintError("Document has not been saved. Please save the document first.\n") | |
| exit() | |
| base_filename = os.path.splitext(doc.FileName)[0] | |
| for obj in doc.Objects: | |
| # Check if the object is a root object (not used by any other objects) | |
| if not obj.InList: | |
| # Check if the object has the "Shape" attribute and it's visible | |
| if hasattr(obj, 'Shape') and obj.ViewObject.Visibility: | |
| # Remove any non-allowed characters from the label for file naming | |
| sanitized_label = ''.join(e for e in obj.Label if e.isalnum() or e in ['_', '-']) | |
| filename = base_filename + "_" + sanitized_label + ".stl" | |
| try: | |
| obj.Shape.exportStl(filename) | |
| FreeCAD.Console.PrintMessage(f"Exported {filename}\n") | |
| except Exception as e: | |
| FreeCAD.Console.PrintError(f"Error exporting {filename}: {str(e)}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment