↑top

FreeCAD: Recording operations as a Python script and executing it from the command line

Update: March 15, 2019
FreeCAD 0.17

In this page, we record operations on FreeCAD as a Python script and execute it with command line version FreeCAD "FreeCADCmd".

Check Python code for shape editing

On FreeCAD, GUI operations will be converted to source code and they will be execute to on FreeCAD core components. At first we check the source code to see what kind of source code are generared from the operation that we want to perform.

Selects [View]-[Panels]-[Python console] menu to show Python console.

Shows Python console
Shows Python console

Right-click the Python console and select "Clear console" to clear all displayed source code onece.

Clears Python console
Clears Python console

Performs shape editing with GUI as normally (Sample FreeCAD file). Here we read a FreeCAD file (TestShape.FCStd on "C:\Test" folder) and change the radius of the hole at center (that is defined on Sketch001) from 15 mm to 10 mm. Then we save it as "TestShape-changed.FCStd" on same folder.

変更する形状
Shape that will be changed

Abobe operations generate following source code on Python console (Comments are supplemented for easy reading).

# Reads FreeCAD file
import FreeCAD
FreeCAD.open(u"C:/Test/TestShape.FCStd")
App.setActiveDocument("TestShape")
App.ActiveDocument=App.getDocument("TestShape")
Gui.ActiveDocument=Gui.getDocument("TestShape")

# Changes the radius of the hole at center
Gui.activateWorkbench('SketcherWorkbench')
import PartDesignGui
ActiveSketch = App.ActiveDocument.getObject('Sketch001')
tv = Show.TempoVis(App.ActiveDocument)
if ActiveSketch.ViewObject.HideDependent:
  objs = tv.get_all_dependent(ActiveSketch)
  objs = filter(lambda x: not x.TypeId.startswith("TechDraw::"), objs)
  objs = filter(lambda x: not x.TypeId.startswith("Drawing::"), objs)
  tv.hide(objs)
if ActiveSketch.ViewObject.ShowSupport:
  tv.show([ref[0] for ref in ActiveSketch.Support if not ref[0].isDerivedFrom("PartDesign::Plane")])
if ActiveSketch.ViewObject.ShowLinks:
  tv.show([ref[0] for ref in ActiveSketch.ExternalGeometry])
tv.hide(ActiveSketch)
ActiveSketch.ViewObject.TempoVis = tv
del(tv)

ActiveSketch = App.ActiveDocument.getObject('Sketch001')
if ActiveSketch.ViewObject.RestoreCamera:
  ActiveSketch.ViewObject.TempoVis.saveCamera()

App.ActiveDocument.Sketch001.setDatum(2,App.Units.Quantity('10.000000 mm'))
Gui.getDocument('TestShape').resetEdit()
ActiveSketch = App.ActiveDocument.getObject('Sketch001')
tv = ActiveSketch.ViewObject.TempoVis
if tv:
  tv.restore()
ActiveSketch.ViewObject.TempoVis = None
del(tv)

Gui.activateWorkbench('PartDesignWorkbench')
App.getDocument('TestShape').recompute()

# Saves file with another name
Gui.SendMsgToActiveView("SaveAs")
App.getDocument("TestShape").saveAs(u"C:/Test/TestShape-changed.FCStd")

Python script for shape editing

The displayed source code includes so many unnecessary code such as GUI display updating. So, after closing the document on FreeCAD, we type the each line of abobe code into Python console and check which code is needed.

After removing unnecessary one, remained code will be as following. We save them in a file named "Automation.py". Please select "UTF-8" as character code on saving. So this is the Python script to be performed.

# Reads FreeCAD file
import FreeCAD
FreeCAD.open(u"C:/Test/TestShape.FCStd")

# Changes the radius of the hole at center
App.ActiveDocument.Sketch001.setDatum(2,App.Units.Quantity('10.000000 mm'))
App.getDocument('TestShape').recompute()

# Saves file with another name
App.getDocument("TestShape").saveAs(u"C:/Test/TestShape-changed.FCStd")

Performs shape editing with command line version FreeCAD

To run the Python script, we use command line version FreeCAD that is named "FreeCADCmd". For example, on Windows, if FreeCAD was installed at default path, we can run the script "Automation.py" on "C:\Test" with following command on command prompt.

C:\Test>"C:\Program Files\FreeCAD 0.17\bin\FreeCADCmd.exe" Automation.py

When the script is run, edited shape will be saved in file "C:\Test\TestShape-changed.FCStd".

FreeCAD 0.17, Libs: 0.17R13541 (Git)
(c) Juergen Riegel, Werner Mayer, Yorik van Havre 2001-2018
  #####                 ####  ###   ####
  #                    #      # #   #   #
  #     ##  #### ####  #     #   #  #   #
  ####  # # #  # #  #  #     #####  #   #
  #     #   #### ####  #    #     # #   #
  #     #   #    #     #    #     # #   #  ##  ##  ##
  #     #   #### ####   ### #     # ####   ##  ##  ##

Importing project files......

Editing result is as following. Please note that the shape may be set to invisible in the file "TestShape-changed.FCStd" after editing. The shape will be visible by changing the property of the shape (ex. "Body" and "Pocket") on model tree.

Shape before editing
Shape before editing
Shape after editing
Shape after editing