How to check if an object has been deleted?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: How to check if an object has been deleted?
Another example where @chennes's delay code can solve a problem:
https://forum.freecadweb.org/viewtopic. ... 71#p657271
And another workaround...
https://forum.freecadweb.org/viewtopic. ... 71#p657271
And another workaround...
Re: How to check if an object has been deleted?
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 .
Re: How to check if an object has been deleted?
Instead of using a delay it would be preferable to have a 'kill switch' for the callback of course.
Re: How to check if an object has been deleted?
Issue for the DiffuseColor bug:
https://github.com/FreeCAD/FreeCAD/issues/8340
https://github.com/FreeCAD/FreeCAD/issues/8340
Re: How to check if an object has been deleted?
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.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")
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>]
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>]
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: How to check if an object has been deleted?
Thanks, doc.RootObjects may also be useful for your task.
To answer the original question:
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
Re: How to check if an object has been deleted?
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: How to check if an object has been deleted?
Another solution:
Code: Select all
def isDeleted(obj):
return obj.FullName == "?"