[MACRO] Align to face w/ curved faces

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
raidho36
Posts: 14
Joined: Fri May 08, 2020 3:10 pm

[MACRO] Align to face w/ curved faces

Post by raidho36 »

It's a tweak of an existing macro. This is not a 100% failproof solution but it works with simple parametric surfaces.

Code: Select all

# -*- coding: utf-8 -*-
# Set the current view perpendicular to the selected face
# Place la vue perpendiculairement a la face selectionnee
# 2013 Jonathan Wiedemann, 2016 Werner Mayer, 2020 <raidho36>

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

s=Gui.Selection.getSelectionEx()
obj=s[0]
face = obj.SubObjects[0]
dir = face.normalAt((face.ParameterRange[0]+face.ParameterRange[1])/2,(face.ParameterRange[2]+face.ParameterRange[3])/2 )
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))

cam.orientation.setValue(rot.Q)
Gui.SendMsgToActiveView("ViewSelection")
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: [MACRO] Align to face w/ curved faces

Post by mario52 »

hi
rockn wrote:ping
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Post Reply