get face number from face object?

Need help, or want to share a macro? Post here!
User avatar
dprojects
Posts: 463
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get face number from face object?

Post by dprojects »

openBrain wrote: Mon Aug 01, 2022 11:44 am
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:
I think this is best comment ever ;-) I like it !

Thanks
Darek
github.com/dprojects

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

Re: get face number from face object?

Post by onekk »

probably the comma won't hurt in lists, if you have a tuple probably it is necessary as a 1 element tuple should be written as:

Code: Select all

 (element,)
About API, sometimes the problem is simply a different point if view.

If you follow some discussion on Developer forum and have had some coversation with some "core developer" you are seing that mostly of developers concern is not breaking "compatibility" with older code.

Probably because the way FC is made were many developers develop WB that are relatively indepedent one to another.

Probably Part WB and Draft WB are more delicate ones as many WB are developed on them, so some changes made could affect many other part if FC that are developed by others and risk to break the "whole FC experience".

I agree with you that improving API is a good thing, but the actual approach I think is more correct, probably the way is extending the API with new methods or add some new parameters to existing methods without breaking compatibility.

This is one the reason of the slow integration of TNP, I think.

Let's see how thing are evolving, but it seems that something is improving in many matters even in the wiki documentation and some other area of FC like TechDraw where many users have found new useful things recently.

Regards

Carlo D.
My English and Italian Scripting Guide https://github.com/onekk/freecad-doc.
My Blog: https://onekk-maker.blogspot.com/

To help development see on GitHub Page

For enquiry about customized FC Scripting job PM me.
heron
Posts: 223
Joined: Mon Apr 20, 2020 5:32 pm

Re: get face number from face object?

Post by heron »

Hello @dprojects,
I am a beginner learning Python and aslo was dealing with this topic, which still don't fully understand.

This thread makes things pretty clear, but I would like to comment something not mentioned that seems weird.
I can get one face of a cube by two similar ways:

Code: Select all

>>> face_a = App.ActiveDocument.Box.Shape.Faces[0]
>>> face_b = App.ActiveDocument.Box.Shape.Face1
I wonder if face_a and face_b are the same object because if you ask in console get false:

Code: Select all

>>> face_a == face_b
False
However, if you call to previous edi function whichFace(shp, face), or isEqual() function or any property the result is as expected, both faces appear be the same. :?:
Sorry for my English, your corrections are welcome.
User avatar
onekk
Veteran
Posts: 3216
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: get face number from face object?

Post by onekk »

heron wrote: Fri Aug 05, 2022 6:13 pm Hello @dprojects,
I am a beginner learning Python and aslo was dealing with this topic, which still don't fully understand.

This thread makes things pretty clear, but I would like to comment something not mentioned that seems weird.
I can get one face of a cube by two similar ways:

Code: Select all

>>> face_a = App.ActiveDocument.Box.Shape.Faces[0]
>>> face_b = App.ActiveDocument.Box.Shape.Face1
I wonder if face_a and face_b are the same object ...
They are same object but obtained in different ways.

Faces[0] is returning first element of Faces that is the list of faces in the object.

Face1 is using the internal name assigned by FreeCAD to the face.

The check using == depend on some other considerations, probably using isSame() is more correct but check as there is a message from @wmayer that explain the differences between these checks on objects.

Sadly I'm on mobile so I could not check, probably tomorrow I could check and post here the proper link.

Regards

Carlo D.
My English and Italian Scripting Guide https://github.com/onekk/freecad-doc.
My Blog: https://onekk-maker.blogspot.com/

To help development see on GitHub Page

For enquiry about customized FC Scripting job PM me.
User avatar
mfro
Posts: 350
Joined: Sat Sep 23, 2017 8:15 am

Re: get face number from face object?

Post by mfro »

onekk wrote: Fri Aug 05, 2022 7:17 pm They are same object but obtained in different ways.
Not exactly. They would be the same object if they had the same id(), but they have not (printing the objects will show you the id).

In fact, they are different Python objects referencing the same FreeCAD/OCC object (which is - in essence - pretty much the same thing).
Cheers,
Markus
User avatar
onekk
Veteran
Posts: 3216
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: get face number from face object?

Post by onekk »

mfro wrote: Fri Aug 05, 2022 8:42 pm
onekk wrote: Fri Aug 05, 2022 7:17 pm They are same object but obtained in different ways.
Not exactly. They would be the same object if they had the same id(), but they have not (printing the objects will show you the id).

In fact, they are different Python objects referencing the same FreeCAD/OCC object (which is - in essence - pretty much the same thing).
I've not taken in account this thing. Probably is for that reason that the equal confront will fail.

I've to check but if isSame() or isEqual() that "in theory" will confront the underlying edge will get "proper result".

EDIT:

Sadly I'm not at home with a computer to check things as said in the post. (I will amend the post and write results after the check, as it is one of the "tricks" to note down and remember when coding)

It seems that my suspect were correct and the note from @mfro too, this sample code will print some information:

Code: Select all

import Part
tp_box = Part.makeBox(10,20,10)

box =Part.show(tp_box, "Test_Box")

face_a = box.Shape.Faces[0]
face_b = box.Shape.Face1

print(face_a, face_b)

print(f"isSame: {face_a.isSame(face_b)}")
print(f"isEqual: {face_a.isEqual(face_b)}")

maybe thiese posts from @wmayer will make some more light:

https://forum.freecadweb.org/viewtopic. ... 36#p586336

https://forum.freecadweb.org/viewtopic. ... 76#p614776

END EDIT:

Sorry for the first unproper affirmation.

Regards

Carlo D.
My English and Italian Scripting Guide https://github.com/onekk/freecad-doc.
My Blog: https://onekk-maker.blogspot.com/

To help development see on GitHub Page

For enquiry about customized FC Scripting job PM me.
User avatar
dprojects
Posts: 463
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: get face number from face object?

Post by dprojects »

heron wrote: Fri Aug 05, 2022 6:13 pm

Code: Select all

>>> face_a == face_b
False
@heron I don't feel like a God of coding to teach someone how to code at python. However, in C++ to compare two objects you need to overload the comparison operator first. Coding at FreeCAD in python is like coding at C++ but with python syntax. In corporation I had episode as C++ source code builder and C++ source code database admin, but I am not the C++ guy, I was always more into front-end development. Werner is more familiar with C++ and he know how everything is coded at FreeCAD so probably if he find some time, he will tell you why.

From my experience in many languages generally comparing two objects with == operator is tricky in any language. While I was programming in Java 15 years ago I always had to use .trim() at the end because there was problem with simple string comparison.

At FreeCAD you may see many values like 1.000006 so if you compare value1 == value2 you get pretty unstable code. This may works fine, but after few minutes the same code is not working. This is why the basic functions like .toString(), .toFloat(), .toInt(), .Normalize() should be always at API, and any code should start from it. If you want anyone to compare things on its own its like making pretty unstable software. Developers should use .toString() always and the .toString should round things according to the precision settings at FreeCAD GUI. So the developer don't have to even think about it, if the API is good written.

This code:

Code: Select all

import Part

FreeCAD.ActiveDocument.addObject("Part::Box", "Box")
FreeCAD.ActiveDocument.addObject("Part::Box", "Box")
FreeCAD.ActiveDocument.recompute()

f1 = FreeCAD.ActiveDocument.Box.Shape.Face1 
f2 = FreeCAD.ActiveDocument.Box001.Shape.Face1 

FreeCAD.Console.PrintMessage("\n")
FreeCAD.Console.PrintMessage(f1.isSame(f2))
FreeCAD.Console.PrintMessage("\n")
FreeCAD.Console.PrintMessage(f1.isEqual(f2))
FreeCAD.Console.PrintMessage("\n")
FreeCAD.Console.PrintMessage(f1.BoundBox)
FreeCAD.Console.PrintMessage("\n")
FreeCAD.Console.PrintMessage(f2.BoundBox)
FreeCAD.Console.PrintMessage("\n")
FreeCAD.Console.PrintMessage(f1.BoundBox == f2.BoundBox)
FreeCAD.Console.PrintMessage("\n")
FreeCAD.Console.PrintMessage(str(f1.BoundBox) == str(f2.BoundBox))

returns:

Code: Select all

False
False
BoundBox (0, 0, 0, 0, 10, 10)
BoundBox (0, 0, 0, 0, 10, 10)
False
True
No matter how buggy is FreeCAD, it allows to make beautiful things. If the stability will be improved it will be probably best software for 3D modeling ever made. For me FreeCAD is fine as it is.

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
heron
Posts: 223
Joined: Mon Apr 20, 2020 5:32 pm

Re: get face number from face object?

Post by heron »

Many Thanks for your responses Onekk, mfo and dprojects.
Cheers!
Sorry for my English, your corrections are welcome.
Post Reply