How to check if an object has been deleted?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

Another example where @chennes's delay code can solve a problem:
https://forum.freecadweb.org/viewtopic. ... 71#p657271
And another workaround... :mrgreen:
User avatar
chennes
Veteran
Posts: 3879
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: How to check if an object has been deleted?

Post by chennes »

Hilarious -- I never imagined that code being in the actual production product, I wrote it for unit testing! I'm glad it's proving useful to someone else :) .
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

Instead of using a delay it would be preferable to have a 'kill switch' for the callback of course.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

Issue for the DiffuseColor bug:
https://github.com/FreeCAD/FreeCAD/issues/8340
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to check if an object has been deleted?

Post by onekk »

Roy_043 wrote: Sat Jan 28, 2023 12:26 pm ...
One issue is:
if obj.TypeId in ("App::"):
This should be:
if "App::" in obj.TypeId:

But even with that mod the code makes certain assumptions that need not apply. "App::" type objects occur as subelements of PD Bodies. but not just there.

For example:

Code: Select all

doc.addObject("App::DocumentObjectGroup")
Hello, I've tested today the code, and it is not working for PartDeseign body, or better, if you delete the PartDesign body, the subsequent objects are in the list that probably is a clone and not reflect the document changes.

So the method it trying to delete already deleted objects.

This is a doc.Object printout prior to delete <body_object>.

Code: Select all

[<body object>, <App::Origin object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>, <GeoFeature object>, <Sketcher::SketchObject>, <Sketcher::SketchObject>, <PartDesign::Pad>, <Sketcher::SketchObject>, <PartDesign::Pocket>, <Sketcher::SketchObject>, <PartDesign::Pad>, <PartDesign::Mirrored>, <Sketcher::SketchObject>, <PartDesign::Pocket>]
12:30:23  [<Sketcher::SketchObject>, <Sketcher::SketchObject>, <PartDesign::Pad>, <Sketcher::SketchObject>, <PartDesign::Pocket>, <Sketcher::SketchObject>, <PartDesign::Pad>, <PartDesign::Mirrored>, <Sketcher::SketchObject>, <PartDesign::Pocket>]
This is what is present after having deleted <body_object>.

Code: Select all

 [<Sketcher::SketchObject>, <Sketcher::SketchObject>, <PartDesign::Pad>, <Sketcher::SketchObject>, <PartDesign::Pocket>, <Sketcher::SketchObject>, <PartDesign::Pad>, <PartDesign::Mirrored>, <Sketcher::SketchObject>, <PartDesign::Pocket>]
12:31:56  [<Sketcher::SketchObject>, <PartDesign::Pad>, <Sketcher::SketchObject>, <PartDesign::Pocket>, <Sketcher::SketchObject>, <PartDesign::Pad>, <PartDesign::Mirrored>, <Sketcher::SketchObject>, <PartDesign::Pocket>]
So after some fiddling around I've resolved with this method:

Code: Select all

def clear_doc(doc_name):
    """Clear the document deleting all the objects.

    Parameters:
    name       type        description
    doc_name   string      document name
    """
    doc = FreeCAD.getDocument(doc_name)
    try:
        while len(doc.Objects) > 0: 
            doc.removeObject(doc.Objects[0].Name)
    except Exception as e:
        print(f'Exception:  {e}')

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

Thanks, doc.RootObjects may also be useful for your task.

To answer the original question:

Code: Select all

def isDeleted(obj, doc=None):
    if doc is None:
        doc = App.ActiveDocument
    return obj not in doc.Objects
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to check if an object has been deleted?

Post by onekk »

Roy_043 wrote: Sun Feb 26, 2023 12:47 pm ...
Thanks I have to write down this check.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: How to check if an object has been deleted?

Post by Roy_043 »

Another solution:

Code: Select all

def isDeleted(obj):
    return obj.FullName == "?"
Post Reply