Accessing objects properties

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
baudry-matth
Posts: 1
Joined: Thu Nov 08, 2012 12:53 pm

Accessing objects properties

Postby baudry-matth » Thu Nov 08, 2012 4:09 pm

Hi all,

I am very glad to have found a good tool for my CAD trials
I am a beginner in FreeCAD, even with CAD generally, my purpose is to make simple things...

My question may be simple, and I'll try to ask it the right way :

What is the simple way to access the area property of the selected face of an object (selected in the Gui) ?

The name of the selected Face appears in the "Show Selection" view :
myDoc.Cut002.Face3
I can get the area of a given face :

Code: Select all

Reso.Shape.Faces[0].Area
   1071.9999999999998
But I can't get the name of this face :

Code: Select all

Reso.Shape.Faces[0].Name
...
Reso.Shape.Faces[0].Label
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'Part.TopoShape' object has no attribute 'Label'
I also tried that (without really knowing what it gives) :

Code: Select all

face=Gui.Selection.getSelection
but I can't do anything with it (no property available in the autocompletion for that object)

I hope there's a way to access the properties of an entity that's been selected on the screen,
or at least to get a list of all faces of selected object with labels/names and areas...

Hoping my question is clear enough, and that someone finds time to answer it.

regards,
Matthieu

ps : working on FreeCAD v0.12, Windows XP 32bit
(I'll try on Ubuntu 12.10 at home, together with Salome)
wmayer
Site Admin
Posts: 16097
Joined: Thu Feb 19, 2009 10:32 am

Re: Accessing objects properties

Postby wmayer » Thu Nov 08, 2012 7:34 pm

The best way of doing this is like this:

Code: Select all

sel=Gui.Selection.getSelectionEx()
for i in sel:
    for j in i.SubElementNames:
        print "Area of ",j,": ", getattr(i.Object.Shape,j).Area
FYI, getSelectionEx() returns a list of selection objects. A selection object keeps a reference to the actual object and the sub-elements. However, the sub-elements list is empty if the complete object was selected e.g. over the tree view.

The above code snippet iterates over the list of selection objects and inside it over the selected sub-elements. The sub-elements are strings and are exactly the names you wanted. So, with getattr() we access the shape data of the object and query its sub-element. The returned object of getattr() is then the face object and we can then directly access its Area attribute.