↑top

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

FreeCAD 0.19

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
Shape that will be changed

Abobe operations generate following source code on Python console (Some comments were added 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
### Begin command Std_Workbench
Gui.activateWorkbench("PartDesignWorkbench")
### End command Std_Workbench
# Gui.Selection.addSelection('TestShape','Body','Pocket.Sketch001.')
# Gui.Selection.clearSelection()
import Show
ActiveSketch = App.getDocument('TestShape').getObject('Sketch001')
tv = Show.TempoVis(App.ActiveDocument, tag= ActiveSketch.ViewObject.TypeId)
ActiveSketch.ViewObject.TempoVis = tv
if ActiveSketch.ViewObject.EditingWorkbench:
	tv.activateWorkbench(ActiveSketch.ViewObject.EditingWorkbench)
if ActiveSketch.ViewObject.HideDependent:
	tv.hide(tv.get_all_dependent(App.getDocument('TestShape').getObject('Body'), 'Pocket.Sketch001.'))
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)
del(tv)

import PartDesignGui
ActiveSketch = App.getDocument('TestShape').getObject('Sketch001')
if ActiveSketch.ViewObject.RestoreCamera:
	ActiveSketch.ViewObject.TempoVis.saveCamera()

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

# Gui.Selection.addSelection('TestShape','Body','Pocket.Sketch001.')
App.getDocument('TestShape').recompute()

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

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.getDocument('TestShape').getObject('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.19\bin\FreeCADCmd.exe" Automation.py

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

FreeCAD 0.19, Libs: 0.19R24276 (Git)
(c) Juergen Riegel, Werner Mayer, Yorik van Havre and others 2001-2021
FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.
FreeCAD wouldn't be possible without FreeCAD community.
	#####                 ####  ###   ####
	#                    #      # #   #   #
	#     ##  #### ####  #     #   #  #   #
	####  # # #  # #  #  #     #####  #   #
	#     #   #### ####  #    #     # #   #
	#     #   #    #     #    #     # #   #  ##  ##  ##
	#     #   #### ####   ### #     # ####   ##  ##  ##

Importing project files......
Postprocessing......

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