Recalculate some objects through python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Recalculate some objects through python

Post by dcapeletti »

Hello, I have the following problem. I want to mark the document so that it does not recalculate when there are changes in the objects, but does so by means of a python instruction, but recalculates some objects that I want, not all of them. For example, if I made changes to a cube and a cylinder and I only want the cylinder recalculated, not the cube. Then later, you may want to recalculate the whole document.

I need to know how to do all this with the python instructions.

Thanks ;)
edi
Posts: 482
Joined: Fri Jan 17, 2020 1:32 pm

Re: Recalculate some objects through python

Post by edi »

I cannot imagine how this should work.
Each object (cube, cylinder etc.) has an internal method onChanged, which is executed if you recalculate the document. onChanged is called from "inside" of FC.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Recalculate some objects through python

Post by openBrain »

To disable global recompute at document level :

Code: Select all

App.ActiveDocument.RecomputesFrozen = True
Set to 'False' to enable again

Then for each object you can call :

Code: Select all

OBJECT.recompute()
If you want a recursive recompute (ie. children are recomputed) :

Code: Select all

OBJECT.recompute(True)
mario52
Veteran
Posts: 4696
Joined: Wed May 16, 2012 2:13 pm

Re: Recalculate some objects through python

Post by mario52 »

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
User avatar
dcapeletti
Posts: 504
Joined: Wed Jul 23, 2014 2:27 pm

Re: Recalculate some objects through python

Post by dcapeletti »

Thank you all for your help. ;)
suzanne.soy
Posts: 54
Joined: Sat Dec 19, 2020 11:55 pm

Re: Recalculate some objects through python

Post by suzanne.soy »

edi wrote: Thu Jan 21, 2021 9:46 am I cannot imagine how this should work.
Each object (cube, cylinder etc.) has an internal method onChanged, which is executed if you recalculate the document. onChanged is called from "inside" of FC.
I think that onChanged is meant to update the object's properties (e.g. if you have a property which appears only when another is set to True, or your properties are linked in some other way (please don't do conditional properties if you can avoid doing so, it prevents other tools from iterating over the properties to display them in an alternate interface, e.g. a nodal interface)). I've seen it used as a hack to go from a list of strings to a list of App::PropertyLink: when the list of strings was updated, it was copied to the App::PropertyLink property to get a real link to the objec, IIUC the code correctly (in the Lattice2 workbench by DeepSOIC).

In order to do something when the object gets recomputed, the execute() method should be implemented instead 8-)
:?: Please mark your posts as [solved] :!:
:idea: If the answer you got is a good fit for the wiki, you can edit it!
FreeCAD modelling & coding workbenches+macros: twitch GitHub
Post Reply