Re: get face number from face object?
Posted: Mon Aug 01, 2022 12:08 pm
The help and development forum of FreeCAD
https://forum.freecad.org/
Code: Select all
(element,)
Code: Select all
>>> face_a = App.ActiveDocument.Box.Shape.Faces[0]
>>> face_b = App.ActiveDocument.Box.Shape.Face1
Code: Select all
>>> face_a == face_b
False
They are same object but obtained in different ways.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:I wonder if face_a and face_b are the same object ...Code: Select all
>>> face_a = App.ActiveDocument.Box.Shape.Faces[0] >>> face_b = App.ActiveDocument.Box.Shape.Face1
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).
I've not taken in account this thing. Probably is for that reason that the equal confront will fail.mfro wrote: ↑Fri Aug 05, 2022 8:42 pmNot 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).
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)}")
@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.
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))
Code: Select all
False
False
BoundBox (0, 0, 0, 0, 10, 10)
BoundBox (0, 0, 0, 0, 10, 10)
False
True