How to find and remove an object by its label?

Need help, or want to share a macro? Post here!
User avatar
pablogil
Posts: 856
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

How to find and remove an object by its label?

Postby pablogil » Tue Feb 23, 2021 4:51 pm

Hi,

The subject is self explanatory: How to find and remove an object by its label?

I know how to find it by name but not by label... is it possible? if not, is there any workaround?

Thanks
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
chrisb
Posts: 35223
Joined: Tue Mar 17, 2015 9:14 am

Re: How to find and remove an object by its label?

Postby chrisb » Tue Feb 23, 2021 5:11 pm

Code: Select all

App.ActiveDocument.getObjectsByLabel("MyObjLabel")
The result is a list, because different objects can have the same label.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
openBrain
Posts: 5899
Joined: Fri Nov 09, 2018 5:38 pm

Re: How to find and remove an object by its label?

Postby openBrain » Tue Feb 23, 2021 5:26 pm

Using C++ API, there is no direct function. You have to go through all objects of the document and check each label.
User avatar
pablogil
Posts: 856
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: How to find and remove an object by its label?

Postby pablogil » Wed Feb 24, 2021 11:20 am

chrisb wrote: Tue Feb 23, 2021 5:11 pm

Code: Select all

App.ActiveDocument.getObjectsByLabel("MyObjLabel")
The result is a list, because different objects can have the same label.
And how to delete them all?...

Thanks again
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
User avatar
pablogil
Posts: 856
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: How to find and remove an object by its label?

Postby pablogil » Wed Feb 24, 2021 11:33 am

Umm, I'll try to answer myself, is this a correct way?:

Code: Select all

App.ActiveDocument.removeObject(App.ActiveDocument.getObjectsByLabel("MyObjLabel")[0].Name)
The previous code only works for the first object in the list but I guess it can easily included into a for loop to remove all the items, am I right or is there any smarter way to do it?

Thanks
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
User avatar
ebrahim raeyat
Posts: 422
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: How to find and remove an object by its label?

Postby ebrahim raeyat » Wed Feb 24, 2021 11:36 am

pablogil wrote: Wed Feb 24, 2021 11:20 am
chrisb wrote: Tue Feb 23, 2021 5:11 pm

Code: Select all

App.ActiveDocument.getObjectsByLabel("MyObjLabel")
The result is a list, because different objects can have the same label.
And how to delete them all?...

Thanks again
in a loop, for example:

Code: Select all

def remove_obj(name):
    o = FreeCAD.ActiveDocument.getObject(name)
    if hasattr(o, "Base") and o.Base:
        remove_obj(o.Base.Name)
    FreeCAD.ActiveDocument.removeObject(name)

objects = App.ActiveDocument.getObjectsByLabel("Punch")

for o in objects:
    remove_obj(o.Name)
openBrain
Posts: 5899
Joined: Fri Nov 09, 2018 5:38 pm

Re: How to find and remove an object by its label?

Postby openBrain » Wed Feb 24, 2021 11:41 am

pablogil wrote: Wed Feb 24, 2021 11:33 am Umm, I'll try to answer myself, is this a correct way?:
It's correct.
Better post in the 'Python scripting' subforum if your question is about... Python scripting. :)
User avatar
bernd
Posts: 11636
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: How to find and remove an object by its label?

Postby bernd » Sun Feb 28, 2021 8:57 am

moved to Python
User avatar
pablogil
Posts: 856
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: How to find and remove an object by its label?

Postby pablogil » Mon Mar 01, 2021 12:10 pm

bernd wrote: Sun Feb 28, 2021 8:57 ammoved to Python
thanks
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
cron