FreeCAD is highly customizable and can be automated using Python scripting. Its entire interface and functionality are built on Python, making it a powerful tool for creating custom workflows, tools, and macros. Below is a guide to help you get started with automating FreeCAD using Python:
1. Basics of FreeCAD Python Scripting-:
FreeCAD provides a built-in Python console and macro editor for scripting. You can access these tools from the View > Panels menu.
- Python Console: Allows you to execute Python commands interactively.
- Macro Editor: Lets you write, save, and execute Python scripts.
2. Accessing FreeCAD’s Python API-:
FreeCAD’s functionality is exposed through its Python API. You can use this API to create and modify objects, automate tasks, and build custom tools.
- Document and Object Manipulation:
import FreeCAD as App import Part # Create a new document doc = App.newDocument() # Create a box box = Part.makeBox(10, 10, 10) # Dimensions: 10x10x10 mm Part.show(box) # Add the box to the document
- Accessing Existing Objects:
# Get the active document doc = App.ActiveDocument # Access an object by name obj = doc.getObject("Box") # Print the object's properties print(obj.PropertiesList)
3. Creating Custom Tools-:
You can create custom tools by writing Python scripts and adding them to FreeCAD as macros or workbenches.
Example: Create a Custom Macro
- Open the Macro Editor (Macro > Macros > Create).
- Write your Python script:
import FreeCAD as App import Part def create_sphere(radius): sphere = Part.makeSphere(radius) Part.show(sphere) App.ActiveDocument.recompute() # Create a sphere with a radius of 5 mm create_sphere(5)
- Save the macro and run it.
Add a Macro to the Toolbar
- Save your macro with a
.FCMacro
extension in theMacros
folder. - Go to Tools > Customize > Macros.
- Assign the macro to a toolbar or menu for easy access.
4. Building a Custom Workbench-:
For more advanced workflows, you can create a custom workbench. A workbench is a collection of tools and commands organized into a single interface.
Steps to Create a Workbench
- Create a folder for your workbench in the
Mod
directory of FreeCAD (e.g.,FreeCAD/Mod/MyWorkbench
). - Add the following files:
- Init.py: Initializes the workbench.
- InitGui.py: Defines the GUI and commands.
Example: Simple Workbench
- Init.py:
class MyWorkbench: def __init__(self): import MyTools # Import your custom tools Gui.addWorkbench(MyWorkbench())
- InitGui.py:
import FreeCADGui as Gui class MyCommand: def Activated(self): import MyTools MyTools.create_sphere(5) # Example command def GetResources(self): return { "Pixmap": "MyIcon", # Path to an icon "MenuText": "Create Sphere", "ToolTip": "Creates a sphere with a radius of 5 mm" } Gui.addCommand("MyCommand", MyCommand())
- MyTools.py:
import FreeCAD as App import Part def create_sphere(radius): sphere = Part.makeSphere(radius) Part.show(sphere) App.ActiveDocument.recompute()
- Restart FreeCAD, and your custom workbench will appear in the workbench dropdown.
5. Automating Repetitive Tasks-:
You can automate repetitive tasks by writing scripts that perform multiple steps in sequence.
Example: Batch Import and Export
import FreeCAD as App
import Mesh
# List of STL files to import
stl_files = ["file1.stl", "file2.stl", "file3.stl"]
for file in stl_files:
# Import STL
Mesh.insert(file)
doc = App.ActiveDocument
# Export to STEP
step_file = file.replace(".stl", ".step")
doc.saveAs(step_file)
# Close the document
App.closeDocument(doc.Name)
6. Debugging and Testing-:
- Use the Python Console to test small snippets of code.
- Use
print()
statements to debug your scripts. - Check the Report View for error messages and logs.
7. Resources for Learning FreeCAD Scripting-:
- FreeCAD Documentation: FreeCAD Python Scripting
- FreeCAD Forum: A great place to ask questions and share scripts.
- GitHub Repositories: Explore open-source FreeCAD projects for inspiration.
By leveraging Python scripting, you can automate FreeCAD, create custom tools, and streamline your workflows. Let me know if you need help with a specific script or workflow!