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!
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

## object
sel = FreeCADGui.Selection.getSelection()                   # selection object (list)
selObjectName = FreeCADGui.Selection.getSelection()[0].Name # name of object first selection

## sub object
SubElement  = FreeCADGui.Selection.getSelectionEx()[0]      # detection sub object (face, vertex, edge) to first object selected
subObjectSel = SubElement.SubObjects[0]                     # first sub object selected to first object selected
subElemName = SubElement.SubElementNames[0]                 # first sub object element name to first object selected
see my signature for more FC code info

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.
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 »

Hi, thanks alot, but I am afraid that was not what my question is about. I know that FreeCADGui.Selection.getSelectionEx() will give sub element names. But that only works if things are selected :D. For objects which are not selected, it is not possible to get sub element names via the API, hence the question if the naming convention using Face1, Face2... etc. is reliable, especially in the link branch.

EDIT: Usually one wants to add things to the selection which were not selected beforehand ;)
EDIT2: I also just confirmed that FreeCADGui.Selection.getSelectionEx()[0].SubElementNames will only return the subelement names which are actually manually selected. So if a full object is selected via the Combo View, one won't get any sub element names which exist in that object. Only sub elements which are actually selected manually in the 3d view will be returned. See also https://forum.freecadweb.org/viewtopic. ... 15#p440015
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

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

Post by edwilliams16 »

You can construct the names of faces, edges etc. trivially.

Code: Select all

boxShp = App.ActiveDocument.getObject('Box').Shape
faceNames = [('Face'+str(i+1), face)  for i, face in enumerate(boxShp.Faces)]
[getattr(boxShp, name).isSame(face) for name, face in faceNames] # => [True, True, True, True, True, True]
i.e boxShp.Face1 is boxShp.Faces[0]

But this doesn't tell you which face of the cube is which. When you select one through the Gui the application knows which one you are referring to.

Because of TNP, if you alter the object, the index of a given face (whatever that might mean and assuming it still exists!) may change. If you want to refer to a particular face in a robust way, without human help through the Gui, you need to come up with criteria that will identify it when searching through the candidates.

Others may know just how much this is mitigated in the link branch. I would expect the best it could do is provide a robust reference to a face that remains unchanged on modification of the object - but I'm guessing here - and would like to be educated.
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

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

Post by dprojects »

freecadlzh wrote: Thu Oct 15, 2020 12:29 am

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]
How to get the face name? Thanks a lot.
You can use my MagicPanels library: API for developers

Code: Select all


import FreeCAD
import MagicPanels

gg = FreeCAD.ActiveDocument.getObject ("Box")
g = gg.Shape.Faces
g0 = g[0]

# simple way
index = MagicPanels.getFaceIndex(gg, g0)
name = "Face"+str(index)
FreeCAD.Console.PrintMessage("\n\n")
FreeCAD.Console.PrintMessage(name)

# extended way
[ face, faceName, faceIndex ] = MagicPanels.getSubByKey(gg, [ g0.BoundBox ], "BoundBox", "face")
FreeCAD.Console.PrintMessage("\n\n")
FreeCAD.Console.PrintMessage([ face, faceName, faceIndex ])


Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
Post Reply