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 »

onekk wrote: Fri Jan 27, 2023 1:21 pm

Code: Select all

    objects = doc.Objects
    for obj in objects:
        if obj.TypeId in ("App::"):
            pass
        else:
            doc.removeObject(obj.Name)
I don't think this can work. Are you sure this is the correct code?
User avatar
onekk
Veteran
Posts: 6146
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: Fri Jan 27, 2023 1:52 pm ...
Yes I'm sure, I use it almost daily in this method:

Code: Select all

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

    Hack courtesy of edwilliams16
    Parameters:
    name       type        description
    doc_name   string      document name
    """
    doc = FreeCAD.getDocument(doc_name)
    doc.recompute()
    objects = doc.Objects
    for obj in objects:
         if obj.TypeId in ("App::"):
            pass
        else:
            doc.removeObject(obj.Name)

When called it deletes all the content of document which doc_name is the name.

I don't know why is excluding objects that have as TypeID equal to App::, but it works without raising exceptions.

Regards

Carlo D.


Sorry for the multiple edits. today is not a great day :oops:
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/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

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

Post by openBrain »

Roy_043 wrote: Thu Jan 26, 2023 9:06 pm The delay in make_clone.py:
https://github.com/FreeCAD/FreeCAD/blob ... ne.py#L129
Maybe I'm missing something but I commented this out (and the similar line a dozen lines above, to be sure), and I see no difference in behavior. The cloned part has the same color as the original one... :?
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 »

chennes wrote: Fri Jan 27, 2023 1:41 pm I think this should work
Thank you. I'll have a play with this.
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 »

openBrain wrote: Fri Jan 27, 2023 6:07 pmI see no difference in behavior. The cloned part has the same color as the original one
  1. Create a Part_Box.
  2. Change the color of some of its faces with Part_FaceColors.
  3. Create a Draft_Clone.
  4. Result without the workaround: the clone does not display the correct face colors (DiffuseColor). Although the ShapeColor is correct.
  5. Save and reopen the file.
  6. Result: the face colors of the clone are now displayed correctly.

Code: Select all

OS: Windows 8.1 Version 6.3 (Build 9600)
Word size of FreeCAD: 64-bit
Version: 0.21.0.31641 (Git)
Build type: Release
Branch: master
Hash: d28d63b87b60161419c6c0c532fbbfaed96926b8
Python 3.10.8, Qt 5.15.6, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: Dutch/Netherlands (nl_NL)
Installed mods:

Code: Select all

OS: Ubuntu 22.04.1 LTS (ubuntu:GNOME/ubuntu)
Word size of FreeCAD: 64-bit
Version: 0.21.0.31641 (Git)
Build type: Release
Branch: master
Hash: d28d63b87b60161419c6c0c532fbbfaed96926b8
Python 3.10.8, Qt 5.15.6, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: English/United States (en_US)
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

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

Post by openBrain »

@Roy_043 Didn't get it was about the faces. Will retest later. Thx.
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 »

onekk wrote: Fri Jan 27, 2023 4:06 pm Yes I'm sure, I use it almost daily in this method:
The code may work in certain cases, but not as intended. Just create a document with a PD Body (without any additional features) and run:

Code: Select all

doc = App.ActiveDocument
objects = doc.Objects
for obj in objects:
    print(obj.TypeId)
    if obj.TypeId in ("App::"):
        pass
    else:
        doc.removeObject(obj.Name)
Error report:

Code: Select all

PartDesign::Body
App::Origin

Traceback (most recent call last):
  File "<input>", line 6, in <module>
RuntimeError: This object is currently not part of a document
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")
User avatar
onekk
Veteran
Posts: 6146
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 ...
Good to know that is only a partial.workaround.

It is working, but I usually make Part WB DocumentObjects, part.Compounds and maybe some Boolean operations, even PD sketches and features, not much more.

Code was elaborated by @edwilliams16 some time ago probably o a couple of years to solve a specific problem, sadly I could not find the specific post.

I will be happy to know a more generic way to avoid errors when "emptying" a FCStd file.

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/
edwilliams16
Veteran
Posts: 3111
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

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

Post by edwilliams16 »

onekk wrote: Sat Jan 28, 2023 1:20 pm
Code was elaborated by @edwilliams16 some time ago probably o a couple of years to solve a specific problem, sadly I could not find the specific post.
I don't remember it - to today's eyes it doesn't look correct.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

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

Post by onekk »

edwilliams16 wrote: Sat Jan 28, 2023 5:53 pm
onekk wrote: Sat Jan 28, 2023 1:20 pm
Code was elaborated by @edwilliams16 some time ago probably o a couple of years to solve a specific problem, sadly I could not find the specific post.
I don't remember it - to today's eyes it doesn't look correct.
I can't find the original post not a problem now at least for me. If some reliable solution will be found it will be s better thing for all FreeCAD users.

My primary interest is that FreeCAD will improve and will better.

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/
Post Reply