How to save fcstd file in headless mode in python scripts

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
SharonZhou
Posts: 2
Joined: Wed Mar 22, 2023 5:14 am

How to save fcstd file in headless mode in python scripts

Post by SharonZhou »

Code: Select all

FREECADPATH = "/Applications/FreeCAD.app/Contents/Resources/lib/"

import sys
sys.path.append(FREECADPATH)

import FreeCAD as App
import Part


doc = App.newDocument("house")
doc.addObject("Part::Feature", "ground").Shape = Part.makePlane(16000 , 12000, App.Vector(0,0,0), App.Vector(0,0,1) )
doc.recompute()
App.getDocument("house").saveAs("MacroTest.FCStd")
I am using the headless mode with python to operate FreeCAD. This code can create the file "MacroTest.FCStd" as I wish, but when i open it in FreeCAD, nothing would be shown.
But if I run the code in macro, the face can be shown both on screen and in the file "MacroTest.FCStd" it created.
I have tried to find the instruction about how to save Fcstd file in headless mode but failed.

Tons of thanks for help!

operate system: MacOS
Version: FreeCAD 0.20.0
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to save fcstd file in headless mode in python scripts

Post by onekk »

FreeCAD as a library permit to use some FreeCAD methods, but it is not using the whole program.

As said in the wiki, there are some differences between using FreeCAD "as an application" and FreeCAD "as a library".

It is standard Python behaviour, if you know Python import mechanism.

I'm not a developer but probably some init phases are not performed when FC is used "as a library".

There are not much documentation about FreeCAD "as as a library" simply because is FreeCAD is an Application (a Program) and not a library, not every functionality of FreeCAD has been programmed in Python and his exposed by it's Python API, something is made in C++ and have no a Python method exposed.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
SharonZhou
Posts: 2
Joined: Wed Mar 22, 2023 5:14 am

Re: How to save fcstd file in headless mode in python scripts

Post by SharonZhou »

onekk wrote: Wed Mar 22, 2023 6:34 am FreeCAD as a library permit to use some FreeCAD methods, but it is not using the whole program.

As said in the wiki, there are some differences between using FreeCAD "as an application" and FreeCAD "as a library".

It is standard Python behaviour, if you know Python import mechanism.

I'm not a developer but probably some init phases are not performed when FC is used "as a library".

There are not much documentation about FreeCAD "as as a library" simply because is FreeCAD is an Application (a Program) and not a library, not every functionality of FreeCAD has been programmed in Python and his exposed by it's Python API, something is made in C++ and have no a Python method exposed.

Regards

Carlo D.
I get it.
But I wonder why it works in macro mode. Dont they use the same library of FC?
thank you for your help again!
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to save fcstd file in headless mode in python scripts

Post by onekk »

You essentially have some way of executing Python code in FC.

- freecadcmd that is FC only with the GUI pert not activated. Here you essentially are in FC and you could use almost all of his code, even GUI parts with some tricks explained by wmayer in some forum posts.

- Python Console that is a sort of command line that has already imported some modules, as it is running in FC .

- Macros that are executed by the "internal Python interpreter"

-Script files loaded in the "Macro Editor" that would better be called "internal python editor", this are similar to Macros but have some little differences as you could tune some behaviour like spawning a separate interpreter for the code in the editor. (Sorry I don't remember the location of the preference now)

Of above ways what you intend for macro mode?

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply