How to recompute FCStd file from command line.

Need help, or want to share a macro? Post here!
Post Reply
leoheck
Posts: 161
Joined: Tue Mar 13, 2018 5:56 pm

How to recompute FCStd file from command line.

Post by leoheck » Wed Jan 08, 2020 10:53 pm

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
Posts: 2936
Joined: Wed Oct 05, 2011 7:36 am

Re: How to recompute FCStd file from command line.

Post by ickby » Thu Jan 09, 2020 10:17 am

Code: Select all

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

Re: How to recompute FCStd file from command line.

Post by AtD » Thu Jan 09, 2020 11:46 am

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
Posts: 161
Joined: Tue Mar 13, 2018 5:56 pm

Re: How to recompute FCStd file from command line.

Post by leoheck » Thu Jan 09, 2020 2:00 pm

@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: 23
Joined: Mon Mar 18, 2019 7:14 am

Re: How to recompute FCStd file from command line.

Post by AtD » Thu Jan 09, 2020 4:59 pm

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
Posts: 2768
Joined: Thu Oct 18, 2018 9:18 pm

Re: How to recompute FCStd file from command line.

Post by vocx » Thu Jan 09, 2020 6:13 pm

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()
To support the documentation effort, and code development, your donation is appreciated: paypal.
leoheck
Posts: 161
Joined: Tue Mar 13, 2018 5:56 pm

Re: How to recompute FCStd file from command line.

Post by leoheck » Thu Jan 09, 2020 9:41 pm

@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
Posts: 2768
Joined: Thu Oct 18, 2018 9:18 pm

Re: How to recompute FCStd file from command line.

Post by vocx » Thu Jan 09, 2020 9:55 pm

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.
To support the documentation effort, and code development, your donation is appreciated: paypal.
Post Reply

Who is online

Users browsing this forum: Tamirov.ru and 6 guests