access gui created part objects using python

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!
Post Reply
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

access gui created part objects using python

Post by bernd »

I'm trying to lern python scripting inside freecad. I've been doing all examples from the wiki page Topological Data Scripting http://freecadweb.org/wiki/index.php?ti ... _scripting Amazing Stuff, it really makes fun because this is something my CAD at work ist not able to do at all.

Two things I was not able to do.

I would like to highligt one or more parts using python, just as I would klick these parts using the mouse. The whole part gets green.

I would like to access parts I've been created using the GUI and the mouse. I know the name (label) of them. I've found a script to find all names of the part objects of a freecad document but I'm not able te access them. I can not even calculate a volume of a simple box if the box was created using the gui and mouse.

thanks for any help, bernd
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: access gui created part objects using python

Post by ickby »

Hello,

If you have a Object called "Box" in your document (no matter how it's created, in console or by scripting) you can access it by its name. Try it in the python console, the class browser will show your objects as part of the active document. That technique can be used to select the object too.

Code: Select all

App.ActiveDocument.Box    #Access document object named "Box"
Gui.Selection.addSelection(App.ActiveDocument.Box)    #select document object named "Box"
To get the Volume of a part the document object is not enough, you need to retrieve the geometrical shape. This shape has a property to access its volume (again, use the class browser to search what else is in that shape, pretty cool stuff)

Code: Select all

App.ActiveDocument.Box.Shape.Volume
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: access gui created part objects using python

Post by bernd »

ickby wrote:...If you have a Object called "Box" in your document (no matter how it's created, in console or by scripting) you can access it by its name. Try it in the python console, the class browser will show your objects as part of the active document. That technique can be used to select the object too. ...
But if the name was changed using the GUI the python browser still shows the old names which where given from freecad. Same with following code. It shows the old names. I only can access the objects using the names given from freecad.

Code: Select all

objs = FreeCAD.ActiveDocument.Objects
for obj in objs:
    name = obj.Name        # list the names of the objects
    print name             # Displays the name of the object
If I use a changed name in python it says

Code: Select all

AttributeError: 'App.Document' object has no attribute 'Rebar001a'
logical, because python console doesn't know the new given names. Does it mean I should not change the object names?

EDIT: I found about the difference of the name (internal name) and label.

Code: Select all

App.ActiveDocument.Box.Name
App.ActiveDocument.Box.Label
Post Reply