I'd like to be able to adjust the Attachment offset in order to center an object I have attached to a face in "FlatFace" mode. The face might be at any arbitrary placement, but will be a planar face. In this code snipped I have created a cube and 6 circles, each attached to one of the faces. The method here works for 2 of the faces, but not for the other 4. Is there a better way? I can set some user properties, like Flip U, Flip V, Swap U V to handle the cases where the algorithm fails, but I'd prefer to get it right in the code.
Code: Select all
doc = FreeCAD.newDocument()
box = doc.addObject("Part::Box","Box")
for facenumber in range(1,7):
circle = doc.addObject("Part::Circle","Circle"+str(facenumber))
doc.recompute()
face = box.getSubObject("Face"+str(facenumber))
circle.Support = (box,["Face"+str(facenumber)])
circle.MapMode = "FlatFace"
u,v = face.Surface.parameter(face.CenterOfMass)
circle.AttachmentOffset.Base.x = u
circle.AttachmentOffset.Base.y = v
doc.recompute()
Gui.SendMsgToActiveView("ViewFit")
Basically, I need a way to figure out whether I need u or -u, v or -v, or to swap u and v. Alternatively, a different way without using u,v. But I need to be attached with "FlatFace" map mode and I need to center by adjusting the attachment offset.
I can get the center of the circle after it is attached (but before applying any attachment offset) with:
- Snip macro screenshot-a21c17.png (51.5 KiB) Viewed 1402 times