Checking Object's Class ?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
paullee
Veteran
Posts: 5098
Joined: Wed May 04, 2016 3:58 pm

Checking Object's Class ?

Post by paullee »

Would like to check the class of objects before further action, searching around and found below 2 options.

Any recommendation what is more optimal?

Thanks for any idea :)

Code: Select all

import ArchEquipment, ArchWindow
SupportedTypes = [ArchEquipment._Equipment, ArchWindow._Window]

if type(obj.Proxy) in SupportedTypes:
    ...

Code: Select all

SupportedTypes = ['_Equipment', '_Window']

if type(obj.Proxy).__name__ in SupportedTypes:
    ...
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Checking Object's Class ?

Post by heda »

Code: Select all

>>> App.ActiveDocument.addObject("Part::Box","Box")
>>> App.ActiveDocument.ActiveObject.Label = "Cube"
>>> App.ActiveDocument.recompute()
>>> ### End command Part_Box
>>> obj = Arch.makeEquipment(FreeCAD.ActiveDocument.Box)
>>> ### End command Arch_Equipment
>>>
>>> obj
<Part::PartFeature>
>>> obj.Proxy
<ArchEquipment._Equipment object at 0x7fec6ac76610>
>>> ## end of prep to do type check
>>>
>>> import ArchEquipment
>>> isinstance(obj.Proxy, ArchEquipment._Equipment)
True
>>> 
>>> help(isinstance)
Help on built-in function isinstance in module builtins:

isinstance(obj, class_or_tuple, /)
    Return whether an object is an instance of a class or of a subclass thereof.
    
    A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
    check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
    or ...`` etc.

>>> 
paullee
Veteran
Posts: 5098
Joined: Wed May 04, 2016 3:58 pm

Re: Checking Object's Class ?

Post by paullee »

Thanks ! :)
Post Reply