Is object instance of Body. 'PartDesign' is not defined.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
koirat
Posts: 66
Joined: Tue Oct 05, 2021 5:24 pm

Is object instance of Body. 'PartDesign' is not defined.

Post by koirat »

This line:
print( isinstance(Gui.Selection.getSelection()[0],PartDesign.Body) )

I basically want to know if selected object is a body.

I will use it to select all visible bodies.
But this gives me:

Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'PartDesign' is not defined
Syres
Veteran
Posts: 2898
Joined: Thu Aug 09, 2018 11:14 am

Re: Is object instance of Body. 'PartDesign' is not defined.

Post by Syres »

Something like:

Code: Select all

# -*- coding: utf-8 -*-
import FreeCAD
import FreeCADGui
try:
    s = FreeCADGui.Selection.getSelection()[0]
except:
    print('Nothing selected')

for selection in FreeCADGui.Selection.getSelectionEx():
    if str(type(selection.Object)) == "<class 'PartDesign.Body'>":
        print(selection.Object.Label)
    else:
        print('You must only select PartDesign Bodies')
        break
Tested using:

OS: Linux Mint 19.3 (X-Cinnamon/cinnamon)
Word size of FreeCAD: 64-bit
Version: 0.20.26155 (Git)
Build type: Release
Branch: master
Hash: 0926a4148bcff11249fd4f56bc30256102ffe105
Python version: 3.6.9
Qt version: 5.9.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedKingdom (en_GB)
koirat
Posts: 66
Joined: Tue Oct 05, 2021 5:24 pm

Re: Is object instance of Body. 'PartDesign' is not defined.

Post by koirat »

Thank you for your guidelines.
I have used this in the end:

Code: Select all

obj.isDerivedFrom("PartDesign::Body")
Post Reply