I don't think this can work. Are you sure this is the correct code?onekk wrote: ↑Fri Jan 27, 2023 1:21 pmCode: Select all
objects = doc.Objects for obj in objects: if obj.TypeId in ("App::"): pass else: doc.removeObject(obj.Name)
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?
Re: How to check if an object has been deleted?
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
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?
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...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
Re: How to check if an object has been deleted?
- Create a Part_Box.
- Change the color of some of its faces with Part_FaceColors.
- Create a Draft_Clone.
- Result without the workaround: the clone does not display the correct face colors (DiffuseColor). Although the ShapeColor is correct.
- Save and reopen the file.
- 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)
Re: How to check if an object has been deleted?
@Roy_043 Didn't get it was about the faces. Will retest later. Thx.
Re: How to check if an object has been deleted?
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)
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
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")
Re: How to check if an object has been deleted?
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
-
- Veteran
- Posts: 3179
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: How to check if an object has been deleted?
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.edwilliams16 wrote: ↑Sat Jan 28, 2023 5:53 pmI don't remember it - to today's eyes it doesn't look correct.
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/