get face number from face object?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

get face number from face object?

Post by dprojects »

Hi, I was looking into code new feature for woodworking, this time related to making holes. I was looking at the hole, countersink, counterbore, macro record code and I see something like that:

Code: Select all

App.getDocument('Unnamed').getObject('Sketch004').Support = (App.getDocument('Unnamed').getObject('Hole'),['Face2',])
I see not first time that face is given as string "Face2" not as object's reference to the face and I was wandering if there is function to get face number to create such string like "Face"+faceindex ?

I solved this problem with other way some time ago while I was coding chamfer frame but I see it again so I thinking why it is string not object? it is more simple to use for FreeCAD developers as a string not as object? if so how they get face index? from face selection you get reference to the face object, so how they covert it to the string? and why it is given as string if the selection returns face object?

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
instance
Posts: 20
Joined: Tue Jul 14, 2015 1:28 am
Location: Ontario, Canada
Contact:

Re: get face number from face object?

Post by instance »

I think it might be similar to edges. You can do

Code: Select all

shape.Edges[i]
to get a specific edge. I haven't used this, but I'd guess that there's a Faces property that you can use, so you would have

Code: Select all

App.getDocument('Unnamed').getObject('Hole').Faces[1]
Since the faces will be indexed from 0, but named from 1.
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get face number from face object?

Post by dprojects »

instance wrote: Sun Jul 31, 2022 2:44 pm I think it might be similar to edges. You can do

Code: Select all

shape.Edges[i]
to get a specific edge. I haven't used this, but I'd guess that there's a Faces property that you can use, so you would have

Code: Select all

App.getDocument('Unnamed').getObject('Hole').Faces[1]
Since the faces will be indexed from 0, but named from 1.
This is not the question ;-) I am looking something like Face.Label but the face object has not such thing like label. Also there is no such thing like Face.Index or even Face.getIndex(). So what on the Earth they use almost everywhere face as string? For example, you select face and assign it to the object:

Code: Select all

Face = FreeCADGui.Selection.getSelectionEx()[0].SubObjects[0]
and now you want to print the label name like Face2, so how they do it? ;-)
I created function getFaceIndex(iObj, iFace) which return index for given face object but maybe it wasn't needed?

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: get face number from face object?

Post by edwilliams16 »

I think one could construct an object with two faces sharing the same BoundBox. I use

Code: Select all

def whichFace(shp, face):
    for i, f in enumerate(shp.Faces):
        if f.isEqual(face):
            return i
    return -1
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get face number from face object?

Post by dprojects »

It means, I am right, there is no such thing like selectedFace.Label or selectedFace.getIndex() ?
It means each macro developer add such function on its own? ;-) well OK ;-)

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
mfro
Posts: 663
Joined: Sat Sep 23, 2017 8:15 am

Re: get face number from face object?

Post by mfro »

dprojects wrote: Sun Jul 31, 2022 3:24 pm It means, I am right, there is no such thing like selectedFace.Label or selectedFace.getIndex() ?
It means each macro developer add such function on its own? ;-) well OK ;-)
The simple answer is, Face1 is the first element of the Faces array of a shape (i.e. Faces[0]). Face2 the second (Faces[1]) and so on.

The real answer is much more complex, however.
Cheers,
Markus
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get face number from face object?

Post by dprojects »

mfro wrote: Sun Jul 31, 2022 4:10 pm The simple answer is, Face1 is the first element of the Faces array of a shape (i.e. Faces[0]). Face2 the second (Faces[1]) and so on.
But this is not the question.
mfro wrote: Sun Jul 31, 2022 4:10 pm The real answer is much more complex, however.
The question is why they use string here:

Code: Select all

App.getDocument('Unnamed').getObject('Sketch004').Support = (App.getDocument('Unnamed').getObject('Hole'),['Face2',])
or why there is no function at API which convert face or edge objects to the string? if you select edge or face you have object not string. So what is the reason for this inconsistency?

FreeCAD is nice software but sometimes I look and I don't believe it what I see, some solutions are beyond my comprehension. Basic API functions missing. The question is why? I wonder how these functionalities are used if there are no basic functions to use them?

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: get face number from face object?

Post by openBrain »

This shouldn't be seen as a string but as an ID.
You can get the face from it's ID, but can't (directly) get ID from face.
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get face number from face object?

Post by dprojects »

openBrain wrote: Sun Jul 31, 2022 7:32 pm This shouldn't be seen as a string but as an ID.
You can get the face from it's ID, but can't (directly) get ID from face.
There is no such thing like Face.ID as well. For me it is no matter if this is ID or whatever. But if you select edge or face you get object. So to be consistent you have to pass object as an argument or you should have at API function which covert object into something you want.

If each developer need to find out and write such functions, don't expect from FreeCAD to be stable and reliable software. Good API is a must for good software. FreeCAD is great software but sometimes my mind just blow up ;-) I don't want to believe that no one has been able to figure out how to do it for 20 years ;-)

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: get face number from face object?

Post by openBrain »

IMO you're just trying to do something in a "weird" way.
As in your OP you talk about selection, you can just use 'SubNames' instead of 'SubObjects'.
Post Reply