How to recompute FCStd file from command line.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
How to recompute FCStd file from command line.
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?
Is there any command to recompute my FCStd file from the command line?
Re: How to recompute FCStd file from command line.
Code: Select all
App.ActiveDocument.recompute()
Re: How to recompute FCStd file from command line.
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
And then call freecad headless in the console:
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
Code: Select all
"C:\Program Files\...\FreeCAD x.xx\bin\python.exe" "<currentpath>\macro.py"
Re: How to recompute FCStd file from command line.
@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?
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, 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.
Re: How to recompute FCStd file from command line.
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.
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.
Re: How to recompute FCStd file from command line.
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()
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: How to recompute FCStd file from command line.
@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.
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.
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"
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.
Re: How to recompute FCStd file from command line.
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.
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.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.
Code: Select all
App.ActiveDocument.
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.