First, I am a **complete** beginner on FreeCAD (but I have some knowledge on python).
I tried FreeCAD for a modest DIY woodworking project of mine (to help me figuring out all the angles and measures).
When all the pieces were done I struggle to create a good "TechDraw" because I can't manage to position the view exactly above the faces I wanted.
I found the Align View to Face macro but it doesn't work when parent object of the selected one have placement with a rotation.
For example
- create a body "Body"
- create a sketch "Sketch" (a simple rectangle)
- pad the sketch.
- change the "Body" placement (e.g. angle 45, axis (1,1,1))
- try to align the view with any of the pad face → it doesn't work
Code: Select all
faceSel.normalAt(0,0)
I don't know if it a bug or if it's a well known behavior.
I modified the macro to apply a combination of the previously computed rotation with all the parents rotation.
It seems to work, but as I said I am a complete beginner at FreeCAD and maybe I am doing it wrong.
I didn't found way to do a PR on this macro on github (I didn't found from where the addon manager retrieve the macro code), also I have the feeling that FreeCAD is a lot "forum driven"… so here I am .
Well here is my modified version of "Align View to Face" macro:
Code: Select all
from pivy import coin
def pointAt(normal, up):
z = normal
y = up
x = y.cross(z)
y = z.cross(x)
rot = App.Matrix()
rot.A11 = x.x
rot.A21 = x.y
rot.A31 = x.z
rot.A12 = y.x
rot.A22 = y.y
rot.A32 = y.z
rot.A13 = z.x
rot.A23 = z.y
rot.A33 = z.z
return App.Placement(rot).Rotation
sel=Gui.Selection.getSelectionEx()[0]
obj=sel.Object
face=sel.SubObjects[0]
dir = face.normalAt(0,0)
cam = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()
if dir.z == 1 :
rot = pointAt(dir, App.Vector(0.0,1.0,0.0))
elif dir.z == -1 :
rot = pointAt(dir, App.Vector(0.0,1.0,0.0))
else :
rot = pointAt(dir, App.Vector(0.0,0.0,1.0))
def computeRotation(obj):
if not obj.Parents:
# the object has no parent
return obj.Placement.Rotation
# the object has parent
# we compute the rotation of it's parent and multiply it with it's rotation
return parentRotate(obj.Parents[0][0]).multiply(obj.Placement.Rotation)
if obj.Parents:
obj_par = obj.Parents[0][0]
rot_par = computeRotation(obj_par)
cam.orientation.setValue(rot_par.multiply(rot).Q)
else:
cam.orientation.setValue(rot.Q)
Gui.SendMsgToActiveView("ViewSelection")
Thanks in advance