How to get Face Name or Face ID in Box Part?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

How to get Face Name or Face ID in Box Part?

Post by freecadlzh »

In part workbench, I created a box Part, Now in python script I try follow code,but can not get the face name:

Code: Select all


>>> gg = FreeCAD.ActiveDocument.getObject ("Box")
>>> g=gg.Shape.Faces
>>> g
[<Face object at 000000002762D410>, <Face object at 000000002762D910>, <Face object at 000000002762DD90>, <Face object at 000000002762D350>, <Face object at 00000000205CF5A0>, <Face object at 00000000205CE920>]
>>> g0 = g[0]
>>> g0.Name
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'Part.Face' object has no attribute 'Name'
>>> 

How to get the face name? Thanks a lot.
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to get Face Name or Face ID in Box Part?

Post by freecadlzh »

Is there any way to deal that?
Waiting for help, Thanks a lot.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: How to get Face Name or Face ID in Box Part?

Post by Kunda1 »

Please always post you About info when asking for assistance
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to get Face Name or Face ID in Box Part?

Post by freecadlzh »

Kunda1 wrote: Fri Oct 16, 2020 1:29 am Please always post you About info when asking for assistance
OS: Windows 7 SP 1 (6.1)
Word size of OS: 64-bit
Word size of EME_Simulator_: 64-bit
Version: 0.19.22665 (Git)
Build type: Release
Branch: master
Hash: b0d7850406e046f3b7a8cd5da261e7d54df6bd99
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Chinese/China (zh_CN)
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: How to get Face Name or Face ID in Box Part?

Post by mario52 »

hi

with getSelectionEx() and SubElementNames[0 to xxx]

Code: Select all

try:
    SubElement = FreeCADGui.Selection.getSelectionEx()                    # sub element name with getSelectionEx()
    element_ = SubElement[0].SubElementNames[0]                           # name of 1 element selected
    App.Console.PrintMessage("elementSelec   : "+str(element_)+"\n\n")            
except:
    App.Console.PrintMessage("Oups"+"\n\n")            

Code_snippets

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to get Face Name or Face ID in Box Part?

Post by freecadlzh »

mario52 wrote: Fri Oct 16, 2020 1:10 pm hi

with getSelectionEx() and SubElementNames[0 to xxx]

Code: Select all

try:
    SubElement = FreeCADGui.Selection.getSelectionEx()                    # sub element name with getSelectionEx()
    element_ = SubElement[0].SubElementNames[0]                           # name of 1 element selected
    App.Console.PrintMessage("elementSelec   : "+str(element_)+"\n\n")            
except:
    App.Console.PrintMessage("Oups"+"\n\n")            

Code_snippets

mario
Thanks for your help, but this code can not deal my problem:

Follow your code, when program runs just when user clicked on the model (such as a cube model named Box) ,and pressed the CTRL key with the left mouse clicked on one Face (such as a face of Box named Face2 be clicked) the method "getSelectionEx" will worked, and can return the object (Face2 object), and the "SubElementNames" can contain the name "Face2" that the name of clicked face of the Cube model.
So which subobject can be returned , just depends on the user's mouse click.

But now my needed is : Give a part object, without user's mouse click, by python script list or print all the part object subobject name.
For example: give a Cube object, It consists of 6 faces, named Face1,Face2,Face3....Face6, by "FreeCAD.ActiveDocument.getObject("Box").Shape.Faces" can got the 6 Face object of the Box, But I did not kown the way to get the 6 Face object name, so I can not print the names of the 6 faces. Need the code to got all of the 6 faces name. Thanks a lot.

Code: Select all

gg=FreeCAD.ActiveDocument.getObject("Box")
gg.Shape.Faces
[<Face object at 00000000271F2E30>, <Face object at 00000000271F2EB0>, <Face object at 00000000271F2F30>, <Face object at 00000000271F2FB0>, <Face object at 00000000271F3030>, <Face object at 00000000271F30B0>]
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: How to get Face Name or Face ID in Box Part?

Post by mario52 »

hi

Code: Select all

>>> g0 = g[0]
g[0] = Face1
.
g[3] = Face4
..
..

for g0.display the Name i do not know

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to get Face Name or Face ID in Box Part?

Post by freecadlzh »

mario52 wrote: Sat Oct 17, 2020 10:02 am hi

Code: Select all

>>> g0 = g[0]
g[0] = Face1
.
g[3] = Face4
..
..

for g0.display the Name i do not know

mario
Thanks for your help, I got a picture below:
1.png
1.png (160.17 KiB) Viewed 2389 times
In the picture, I know the face object name is "Face1" , But in freecad did not provide a method to get the Face object(named "g0") name , so the code "g0.Name" runs errors. I need a way to get the Face object name( not by selection).

Need help , Thanks a lot.
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to get Face Name or Face ID in Box Part?

Post by Chris_G »

A shape has no "Name" property.
Instead, every shape has only a convenient shortcut to access its own subshapes.
Take a box object. It is a solid.
It has 8 vertexes.
You can access them in two ways :

Code: Select all

v = box.Vertexes[2]
or :

Code: Select all

v = box.Vertex3
It is the same.
The second version is just slightly more convenient because it is a property, not an item of a list.

Now let's consider the second face of the box.
It has 4 vertexes.
You can access them with :

Code: Select all

v = box.Face2.VertexN # for N in [1...4]
But if you access these same vertexes from the box solid directly, you'll see that they are :

Code: Select all

v = box.VertexN # for N in [5...8]
So, a subshape cannot have a single name, because it depends on the parent shape it is accessed from.
derMart
Posts: 27
Joined: Tue Dec 28, 2021 10:50 pm

Re: How to get Face Name or Face ID in Box Part?

Post by derMart »

I do have the exact same problem as @freecadlzh. I am using the link branch by @realthunder.

I want to add a specific face to the selection. Found out this can be done via

Code: Select all

FreeCADGui.Selection.addSelection(applicationName,objectName,faceName)
so for example:

Code: Select all

FreeCADGui.Selection.addSelection('myapplication','Pad','Face2')
Getting the objectName is no problem via

Code: Select all

App.ActiveDocument.getObject('Pad').Name
but I am also stuck at the subname / face name. Now inferring from comment above, can I be sure that all faces inside an object are named 'Face1', 'Face2', ... or may those names be different than this convention? Especially, when working in the link branch which does some things to prevent naming changes if things are edited around?
Post Reply