Automate FreeCAD with Python Scripting. Make custom tools and workflows.

Python freecad

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

  1. Open the Macro Editor (Macro > Macros > Create).
  2. Write your Python script:
    python
    Copy
    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)
  3. Save the macro and run it.

Add a Macro to the Toolbar

  1. Save your macro with a .FCMacro extension in the Macros folder.
  2. Go to Tools > Customize > Macros.
  3. 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

  1. Create a folder for your workbench in the Mod directory of FreeCAD (e.g., FreeCAD/Mod/MyWorkbench).
  2. Add the following files:
    • Init.py: Initializes the workbench.
    • InitGui.py: Defines the GUI and commands.

Example: Simple Workbench

  1. Init.py:
    python
    Copy
    class MyWorkbench: def __init__(self): import MyTools # Import your custom tools Gui.addWorkbench(MyWorkbench())
  2. InitGui.py:
    python
    Copy
    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())
  3. MyTools.py:
    python
    Copy
    import FreeCAD as App import Part def create_sphere(radius): sphere = Part.makeSphere(radius) Part.show(sphere) App.ActiveDocument.recompute()
  4. 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

python
Copy
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 DocumentationFreeCAD 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!

Amar Patel
About Amar Patel 293 Articles
Hi, I am Amar Patel from India. Founder, Author and Administrator of mechnexus.com. I am a Simple Average Man who Loves life and Love living life. Professionally I am a Mechanical Engineer with Solid command over CAD software like FreeCAD, SolidWorks, Autodesk Inventor and AutoCAD Mechanical. I’m here to share my knowledge to help you accomplish your design and engineering tasks quicker and easier. I am Passionate about learning new things especially about Open-Source Software. I love teaching therefore I started my YouTube Channel on FreeCAD and I believe FreeCAD have lots of potential than traditional 3D software. contact me - [email protected]