How to recompute FCStd file from command line.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
leoheck
Veteran
Posts: 1225
Joined: Tue Mar 13, 2018 5:56 pm
Location: Coffee shop

How to recompute FCStd file from command line.

Post by leoheck »

I am changing a text inside the FCStd file with a script using sed.
Is there any command to recompute my FCStd file from the command line?
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: How to recompute FCStd file from command line.

Post by ickby »

Code: Select all

App.ActiveDocument.recompute()
AtD
Posts: 50
Joined: Mon Mar 18, 2019 7:14 am

Re: How to recompute FCStd file from command line.

Post by AtD »

I think he is in a bash environment? I dont know if its possible directly there but you might create a short python script you put next to your bash script which looks like this

Code: Select all

import FreeCAD as App
App.open(<pathname and filename with extension>)
App.setActiveDocument(filename)
App.ActiveDocument.recompute()
App.setActiveDocument("")
App.ActiveDocument=None
And then call freecad headless in the console:

Code: Select all

"C:\Program Files\...\FreeCAD x.xx\bin\python.exe" "<currentpath>\macro.py"
leoheck
Veteran
Posts: 1225
Joined: Tue Mar 13, 2018 5:56 pm
Location: Coffee shop

Re: How to recompute FCStd file from command line.

Post by leoheck »

@ickby, thanks I was trying that, but it is still not working.

@AtD, yeah, you are right I am in bash, but I am creating a python script as you said. Thanks. Your snippet is really good since I didn't have all these lines.

This is my code now, it does not give me errors but the recompute does not work either.

What I am doing wrong?

Code: Select all

if __name__ == '__main__':
	argv = parse_args()

	freecad_file = argv.freecad_file
	doc_name = str(os.path.splitext(freecad_file)[0].split("/")[-1])

	FreeCADGui.setupWithoutGUI()
	App.open(freecad_file)
	App.setActiveDocument(doc_name)
	App.ActiveDocument.recompute()
	App.setActiveDocument("")
	App.ActiveDocument=None
	App.closeDocument(doc_name)

What I'm doing:
- I unpack the .FCStd file, to replace a text string with a script (it is a text engraved with the Part Workbench on my model)
- Then I pack it again
- Then when opening this changed file, it has the same text as before. Nothing has changed.
Should I add something to save the file?

I can also do everything with python, but right now, I don't know to select an object and change its text.

So, to recompute it, I have to right-click my draft object and click "Recompute" using Freecad GUI. But I want to do this from the command line.
AtD
Posts: 50
Joined: Mon Mar 18, 2019 7:14 am

Re: How to recompute FCStd file from command line.

Post by AtD »

When in bash, place a pause after calling the freecad python, this should print out errors. Unfortunately, I have no experience with sed and how it works with FC. But I think you are better of doing it all in python.

You can get an object by its internal label and make changes from there. In the FreeCAD GUI under View you can activate the python console. This will print out all the things you do in the GUI as a console command. Modify some object and see the syntax for that action, then adapt it in your script.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: How to recompute FCStd file from command line.

Post by vocx »

leoheck wrote: Thu Jan 09, 2020 2:00 pm ...
I can also do everything with python, but right now, I don't know to select an object and change its text.
It really depends on what that object that you are modifying is. What text? Usually the properties of an object, that you can see in the property editor, are attributes of the object.

You can touch an object to mark it as changed, even if you didn't modify anything about it.

Code: Select all

import Draft
obj = Draft.makeText("Something")

obj.Text = "Something else"
obj.touch()

App.ActiveDocument.recompute()
leoheck wrote: Thu Jan 09, 2020 2:00 pm App.open(freecad_file)
App.setActiveDocument(doc_name)
App.ActiveDocument.recompute()
App.setActiveDocument("")
App.ActiveDocument=None
App.closeDocument(doc_name)
This code seems weird to me because you open the file, recompute, and then close it, but you never save it.

Code: Select all

App.ActiveDocument.FileName = "/home/user/New_document.FCStd"
App.ActiveDocument.save()
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
leoheck
Veteran
Posts: 1225
Joined: Tue Mar 13, 2018 5:56 pm
Location: Coffee shop

Re: How to recompute FCStd file from command line.

Post by leoheck »

@vocx

Thanks, I am already changing the text with python. With sed, was also working the same, however, looks cleaner

I can set the property in the properties list with this. It changes it in the GUI.
It is strange that the property is called String, but it is in properties list with that name.

Code: Select all

App.ActiveDocument.getObjectsByLabel("number")[0].String = "00"
However, the recompute didn't work yet.
I am also touching all the objects to make sure everything changes.
Then, recomputing, then saving.

Ah, thanks for pointing me the command to save the file, this was one of my questions. I was not able to find it on Freecad API.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: How to recompute FCStd file from command line.

Post by vocx »

leoheck wrote: Thu Jan 09, 2020 9:41 pm ...
It is strange that the property is called String, but it is in properties list with that name.
...
As explained in the Scripting section of property editor, all properties of the object are attributes of it. They are named in CamelCase in the code, and are shown with a space before each capital letter.
Ah, thanks for pointing me the command to save the file, this was one of my questions. I was not able to find it on Freecad API.
There is a lack of documentation of many functions in the online generated documentation, so some times it is easier just to explore the different functions from the Python console. Just type a dot at the end of a class to show different possibilities.

Code: Select all

App.ActiveDocument.
Also see Std_PythonHelp.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply