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:

Re: get face number from face object?

Post by dprojects »

openBrain wrote: Sun Jul 31, 2022 8:12 pm 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'.
There is no such things as well.

Code: Select all

>>> FreeCADGui.Selection.getSelectionEx()[0].SubNames[0]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'Gui.SelectionObject' object has no attribute 'SubNames'
>>> FreeCADGui.Selection.getSelectionEx()[0].SubNames
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'Gui.SelectionObject' object has no attribute 'SubNames'
>>> 

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 »

SubElementNames
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 8:52 pm SubElementNames
Yes. This works:

Code: Select all

face = FreeCADGui.Selection.getSelectionEx()[0].SubElementNames[0]
First entry from FreeCAD forum and look how they struggled to get it: SubElementName from Face object and this was in 2019... 3 years ago, and there is no such function. Don't you think it would be better to have simple convert functions?

or look at this flower:

Code: Select all

Gui.getDocument('Unnamed').ActiveView.setActiveObject('pdbody',App.getDocument('Unnamed').getObject('Part'),'Body.')
You have Pad but you can't get the Body for it ;-) and why there is the dot after Body name? to fit to the comma?

Code: Select all

App.getDocument('Unnamed').getObject('Sketch002').Support = (App.getDocument('Unnamed').getObject('panel2pad'),['Face2',])

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: get face number from face object?

Post by onekk »

Probably most of these problems are related to the way information are store in the object.

For me it all about documentation, there is way, so a solution exist.

many of the code showed as:

Code: Select all

App.getDocument('Unnamed').getObject('Sketch002').Support = (App.getDocument('Unnamed').getObject('panel2pad'),['Face2',])
could be rewrite in a more readable manner as:

Code: Select all

DOC = App.getDocument('Unnamed')

object1 = DOC.getObject('Sketch002')
object2 = DOC.getObject('panel2pad')

object1.Support = (object2, ['Face2',])
That may appear too verbose, but as you could reuse DOC object1 and object2 further in the code without typing too much, and without having the overhead to retrieve every object everytime you wnat, as the reference is stored once, and the operation to find the object in the document is done only one time, same for DOC, you will force FC to retrieve two time same object doing a search.

IMHO it seems not a clever way to use "machine cycles".

You could ever find the face descriptor using index of Faces property, and compose a simple string using some more efficient ways than "string concatenation" (+ operator), maybe:

Code: Select all

f"Faces{idx}"
is more short and readable than:

Code: Select all

"Face" + str(idx)
For other considerations, about the API, it will be very interesting to hear something from developers, but actually I prefer that they concentrate on integrating some other most important things, that "helper functions".

Recently I have seen @wmayer very active in "fixing bugs" and solve some questions making very quick commits, so probably developers have done things more productive.

Not citing that "Face order" is extremely arbitrary as it depends on what the "3d engine" is returning as "face index", and it is related to the infamous "TNP", so probably a more clean solution is coming that will permit to store a "reference" similar to Face.Label in the "near" future. (From some post RT is working, but having some problems in "syncing" his code with "master").

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: get face number from face object?

Post by heda »

you are right dprojects, the py-api has some real sharp corners that could benefit from a bit of love.

but you being right does not magically change the api, someone has to do it and it is a complex beast, also considering backward compatibility - i think the few real coders that could do something about will have a different focus for a long time to come, read bugs/functionality...

i think if the api-warts are listed somewhere, maybe there is a chance that a red thread starts to appear, and maybe some casual coders will make it their destiny to iron some of the warts out...

however, what is a wart or not and what is a good way forward will always be highly opinionated, so it might be hard to come to conclusions of what to do :-)

imho questions like this belong to "major" version release points, and if there was someone working on the api-question that would be great, but it is a steep hill to climb, deprecation cycles, get people to adjust to a modified api and so on...

btw, should one strictly follow for example occ-api/naming, or should one straighten some of those warts out in the transition from c to python, so that it becomes decoupled just to make it nicer? i think decoupling them is a recipe for long-term maintainability disaster..., however adding some convenience functions here and there - that is a good thing imho

those convenience functions might as well be in a lib in core, there are plenty of those around in draft wb for example...
but maybe better to just put all things like that in it's own module, where it is easy to discover what is available
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 »

onekk wrote: Mon Aug 01, 2022 12:03 am could be rewrite in a more readable manner as:
This is not about code readability or make it more modular, this is important too, but in this case I mentioned the code fragment:

onekk wrote: Mon Aug 01, 2022 12:03 am

Code: Select all

object1.Support = (object2, ['Face2',])
because if you see something like that you have no idea what is the comma there? this should be there passed as argument? this is just mistake of the recording macro tool? or the function require to pass array of strings? or strings? or maybe string that is looking like array with comma ad the end of last argument?

and you think what will happen if you pass array of string? this will be ok? what if you pass string without the comma? the last character will be cut by the FreeCAD and you get for example ['Face2','Face3' instead of ['Face2','Face3'] what will cause error?

I am talking about this...
Last edited by dprojects on Mon Aug 01, 2022 11:37 am, edited 2 times in total.

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
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 »

heda wrote: Mon Aug 01, 2022 8:47 am i think the few real coders that could do something about will have a different focus for a long time to come, read bugs/functionality...
Yes, I have feature to code and I am discussing at the forum... what I don't like to do. But for sure everybody would like to see FreeCAD more stable, reliable with good described API? I think we all agree on that?

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: Mon Aug 01, 2022 11:26 am
onekk wrote: Mon Aug 01, 2022 12:03 am

Code: Select all

object1.Support = (object2, ['Face2',])
because if you see something like that you have no idea what is the comma there?
If you are not completely new to programming, you should immediately recognize that this is a set with one string element.
Admitted, the comma is superfluous, but it doesn't hurt. Probably there since the original programmer made his life easier to simplify appending more list elements.

One more general note, if you allow: honestly, I've never seen any Free Software improving from just ranting. If you're really interested in FreeCAD's forthcoming, I'm sure there are far better ways to invest your energy.
Cheers,
Markus
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: get face number from face object?

Post by openBrain »

dprojects wrote: Mon Aug 01, 2022 11:31 am But for sure everybody would like to see FreeCAD more stable, reliable with good described API?
This is what I wish for my wife. :mrgreen:

But I was tired filling in tickets for nothing, and finally now I know, I'm happy despite having to use a lot of workarounds. :mrgreen: :mrgreen:
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: Mon Aug 01, 2022 11:42 am I've never seen any Free Software improving from just ranting.
This is not ranting because I solved all those problems long time ago at my library.
But sometimes it looks as if some people wanted certain things not to be coded properly, maybe they get paid from Autodesk or maybe they want to be paid for their support at forum.

For me, open source should be a code model, and if someone is here for the money, they should be falling.

Thanks
Darek
github.com/dprojects

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