Using macros in FreeCAD is a fantastic way to automate repetitive tasks, extend functionality, and customize your workflow. Macros are small scripts written in Python that can perform a wide range of actions in FreeCAD. Here’s a step-by-step guide to help you start using FreeCAD macros right away:
1. What Are FreeCAD Macros?
- Macros are Python scripts that automate tasks in FreeCAD.
- They can be used to create custom tools, modify objects, or perform complex operations.
- FreeCAD comes with a built-in Macro Editor and Macro Manager to create, edit, and run macros.
2. Enable the Macro Workbench
- Open FreeCAD.
- Go to the Macro workbench by selecting it from the workbench dropdown menu.
- If you don’t see the Macro workbench, enable it in
Tools > Customize > Workbenches
.
3. Access the Macro Manager
- Go to
Macro > Macros...
to open the Macro Manager. - The Macro Manager shows a list of available macros and allows you to create, edit, and run them.
4. Create a New Macro
- In the Macro Manager, click Create to make a new macro.
- Give your macro a name (e.g.,
MyFirstMacro
). - Click Edit to open the macro in the Macro Editor.
5. Write Your First Macro
Here’s a simple example macro to create a cube:
import FreeCAD as App
import Part
# Create a new document
doc = App.newDocument()
# Add a cube to the document
cube = Part.makeBox(10, 10, 10) # Dimensions: 10x10x10 mm
Part.show(cube)
# Save the document
doc.saveAs("MyCube.FCStd")
- Explanation:
App.newDocument()
creates a new FreeCAD document.Part.makeBox()
creates a 3D cube with the specified dimensions.Part.show()
adds the cube to the document.doc.saveAs()
saves the document with the specified name.
6. Run the Macro
- Save the macro by clicking the Save button in the Macro Editor.
- Close the Macro Editor.
- In the Macro Manager, select your macro and click Execute.
- You should see a new document with a 10x10x10 mm cube.
7. Install Pre-Made Macros
FreeCAD has a rich library of pre-made macros created by the community. Here’s how to install them:
- Go to the FreeCAD Macro Repository or the FreeCAD GitHub Macros page.
- Download the macro file (usually a
.FCMacro
file). - Place the file in your FreeCAD macros folder:
- On Windows:
C:\Users\<YourUsername>\AppData\Roaming\FreeCAD\Macro\
- On macOS:
/Users/<YourUsername>/Library/Preferences/FreeCAD/Macro/
- On Linux:
/home/<YourUsername>/.FreeCAD/Macro/
- On Windows:
- Restart FreeCAD and access the macro via the Macro Manager.
8. Useful Macros for Beginners
Here are some popular macros to get you started:
- Fasteners: Adds bolts, nuts, and screws to your designs.
- ShapeString: Creates 3D text for engraving or embossing.
- Parametric Curve: Generates complex curves and shapes.
- Exploded Assembly: Creates exploded views of assemblies.
9. Learn Python for FreeCAD
To write more advanced macros, you’ll need to learn some Python. Here are some resources:
- FreeCAD API Documentation: https://wiki.freecad.org/Python_scripting_tutorial
- Python Basics: Learn Python basics from platforms like Codecademy or W3Schools.
- FreeCAD Forum: Ask questions and share your macros on the FreeCAD Forum.
10. Debugging and Testing
- Use the Python Console: Open the Python console (
View > Panels > Python Console
) to test small snippets of code. - Print Debugging: Use
print()
statements to debug your macros and check variable values. - Check for Errors: If your macro doesn’t work, check the Report View (
View > Panels > Report View
) for error messages.
11. Share Your Macros
- Upload to GitHub: Share your macros with the community by uploading them to GitHub.
- Contribute to the FreeCAD Wiki: Add your macros to the FreeCAD Macro Recipes page.
- Post on the Forum: Share your macros and get feedback on the FreeCAD Forum.
12. Example: A Parametric Cylinder Macro
Here’s a more advanced example macro to create a parametric cylinder:
import FreeCAD as App
import Part
# Parameters
radius = 5 # Radius of the cylinder
height = 20 # Height of the cylinder
# Create a new document
doc = App.newDocument()
# Add a cylinder to the document
cylinder = Part.makeCylinder(radius, height)
Part.show(cylinder)
# Save the document
doc.saveAs("MyCylinder.FCStd")
- Modify the
radius
andheight
variables to create cylinders of different sizes.
Conclusion
FreeCAD macros are a powerful way to automate tasks, extend functionality, and customize your workflow. By starting with simple macros and gradually learning Python, you can unlock the full potential of FreeCAD. Experiment with pre-made macros, write your own, and share your creations with the community. Happy scripting! 🚀