Proper way to fully delete an object

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
kaktus018
Posts: 20
Joined: Mon May 14, 2018 2:51 pm

Proper way to fully delete an object

Post by kaktus018 »

Hello everybody,
I use a script that repeatedly makes some objects inside a document, computes some volumes and deletes the objects afterwards. I use the removeObject method of the Document-class to do that. This seems to work as they are no longer present in App.ActiveDocument.Objects. However, I noticed that the computations get very slow after a while and I suspect that the deleted objects are still somewhere in the memory. If I close the document and open a new one this does not happen.
Is there another way to fully delete the objects in the document?

Best regards,
Dominik
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Proper way to fully delete an object

Post by microelly2 »

removeObject should do the job,
how many objects do you create temporary and delete them again?
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Proper way to fully delete an object

Post by ickby »

you could try to disable the transactions (undo/redo) as this keeps the objects in memory. The document has a proeprty for that, don't remember the exact name right now... something like: mydoc.UndoMode = 0
kaktus018
Posts: 20
Joined: Mon May 14, 2018 2:51 pm

Re: Proper way to fully delete an object

Post by kaktus018 »

microelly2 wrote: Mon Oct 15, 2018 9:12 am removeObject should do the job,
how many objects do you create temporary and delete them again?
Around 100000.
ickby wrote: Mon Oct 15, 2018 9:37 am you could try to disable the transactions (undo/redo) as this keeps the objects in memory. The document has a proeprty for that, don't remember the exact name right now... something like: mydoc.UndoMode = 0
Thank you! I will try that and see if it helps.
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Proper way to fully delete an object

Post by microelly2 »

if you only calculate some geometric values you need not to create a document object.
You can hold the shape information in memory
kaktus018
Posts: 20
Joined: Mon May 14, 2018 2:51 pm

Re: Proper way to fully delete an object

Post by kaktus018 »

if you only calculate some geometric values you need not to create a document object.
You can hold the shape information in memory
I need some objects stored in a template file, add some more objects programmatically, perform some boolean operations and finally calculate some volumes. I don't think I can do that without a document.

Unfortunately, UndoMode = 0 did not work. I still have got the issue that the computations slow down after a lot of evaluations. If nobody else has an idea how to fix this, I guess I have to stick with reloading the document from time to time.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Proper way to fully delete an object

Post by TheMarkster »

kaktus018 wrote: Thu Oct 18, 2018 7:23 pm
if you only calculate some geometric values you need not to create a document object.
You can hold the shape information in memory
I need some objects stored in a template file, add some more objects programmatically, perform some boolean operations and finally calculate some volumes. I don't think I can do that without a document.

Unfortunately, UndoMode = 0 did not work. I still have got the issue that the computations slow down after a lot of evaluations. If nobody else has an idea how to fix this, I guess I have to stick with reloading the document from time to time.
Document object doesn't refer to the document; it refers to the objects, example "Cut" is a Document object. Microelly2's suggestion is to create the object in memory, but don't add it to the document.

So, instead of:

Code: Select all

cyl = App.ActiveDocument.addObject("Part::Cylinder","Cylinder")
App.ActiveDocument.ActiveObject.Label = "Cylinder"
App.ActiveDocument.recompute()
print(str(cyl.Shape.Volume))
You would do something like:

Code: Select all

import Part
cyl2 = Part.makeCylinder(10,2) #radius 10, height 2
print(str(cyl2.Volume))
The difference is the first method results in an object getting added to the tree and to the 3d view, which consumes resources that the second method does not consume. Notice the extra ".Shape" in there. That's because Shape is an attribute of the document object whereas in the second example, cyl2 is the Shape object itself.

Try this:

Code: Select all

cyl.Shape = cyl2
kaktus018
Posts: 20
Joined: Mon May 14, 2018 2:51 pm

Re: Proper way to fully delete an object

Post by kaktus018 »

Alright, that's indeed an option. I don't know if I will change the scripts but I'm pretty certain that this will also solve my issue. Thanks!
Post Reply