centering on face using attachment offset

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

centering on face using attachment offset

Post by TheMarkster »

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:

Code: Select all

pos = face.valueAt(0,0)
Snip macro screenshot-a21c17.png
Snip macro screenshot-a21c17.png (51.5 KiB) Viewed 1401 times
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: centering on face using attachment offset

Post by openBrain »

TheMarkster wrote: Tue Nov 16, 2021 11:40 pm 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
Maybe you could ping @Chris_G that should have a good knowledge of all this.

I know it doesn't really answer, but in case it may help without using AttachmentOffset it's pretty easy :)

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.Placement = box.Placement.multiply(App.Placement(face.CenterOfGravity, App.Rotation(App.Vector(0,0,1),face.Surface.Axis)))
doc.recompute()
Gui.SendMsgToActiveView("ViewFit")
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: centering on face using attachment offset

Post by TheMarkster »

Thanks a lot. I'll see if I can incorporate that into the actual macro instead of using the attachment offset.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: centering on face using attachment offset

Post by openBrain »

TheMarkster wrote: Thu Nov 18, 2021 5:36 pm Thanks a lot. I'll see if I can incorporate that into the actual macro instead of using the attachment offset.
Welcome. Can you give a pointer to the "macro" (like to know what we are talking about :lol:) ?
Notice that it assumes that box and circle are in the same CS. Otherwise you have to play with 'getGlobalPlacement()'. ;)
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: centering on face using attachment offset

Post by TheMarkster »

openBrain wrote: Thu Nov 18, 2021 5:41 pm Welcome. Can you give a pointer to the "macro" (like to know what we are talking about :lol:) ?
Notice that it assumes that box and circle are in the same CS. Otherwise you have to play with 'getGlobalPlacement()'. ;)
It's a macro for creating mortise / tenon joints. I based it on the Bevel macro as a template. Still very much a work in progress, lots left to do, not the least of which is giving it its own icon. Here is the current state:
Joint.FCMacro
(31.72 KiB) Downloaded 27 times
Post Reply