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

Need help, or want to share a macro? Post here!
User avatar
freecadlzh
Posts: 99
Joined: Fri Mar 06, 2020 12:52 pm

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

Postby freecadlzh » Thu Oct 15, 2020 12:29 am

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: 99
Joined: Fri Mar 06, 2020 12:52 pm

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

Postby freecadlzh » Thu Oct 15, 2020 11:52 pm

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

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

Postby Kunda1 » Fri Oct 16, 2020 1:29 am

Please always post you About info when asking for assistance
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
freecadlzh
Posts: 99
Joined: Fri Mar 06, 2020 12:52 pm

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

Postby freecadlzh » Fri Oct 16, 2020 7:55 am

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
Posts: 3153
Joined: Wed May 16, 2012 2:13 pm

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

Postby mario52 » 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
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting, Dialog creation. My macros on Gist.github.
User avatar
freecadlzh
Posts: 99
Joined: Fri Mar 06, 2020 12:52 pm

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

Postby freecadlzh » Fri Oct 16, 2020 2:42 pm

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
Posts: 3153
Joined: Wed May 16, 2012 2:13 pm

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

Postby mario52 » 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
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting, Dialog creation. My macros on Gist.github.
User avatar
freecadlzh
Posts: 99
Joined: Fri Mar 06, 2020 12:52 pm

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

Postby freecadlzh » Sat Oct 17, 2020 6:08 pm

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 130 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
Posts: 1476
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

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

Postby Chris_G » Sun Oct 18, 2020 5:54 pm

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.