Macro Align View to Face/cs: Difference between revisions

From FreeCAD Documentation
(Created page with "Category:Python Code/cs")
(Updating to match new version of source page)
Line 16: Line 16:
# Set the current view perpendicular to the selected face
# Set the current view perpendicular to the selected face
# Place la vue perpendiculairement à la face selectionnee
# Place la vue perpendiculairement à la face selectionnee
# 2013 Jonathan Wiedemann
# 2013 Jonathan Wiedemann, 2016 Werner Mayer


from pivy import coin
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()
s=Gui.Selection.getSelectionEx()
obj=s[0]
obj=s[0]
Line 24: Line 46:
dir = faceSel.normalAt(0,0)
dir = faceSel.normalAt(0,0)
cam = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()
cam = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()

camValues = cam.position.getValue()
pos = FreeCAD.Vector( (camValues[0], camValues[1], camValues[2],) )
p = pos.add(dir.negative())
print(p.x,p.y,p.z)
if dir.z == 1 :
if dir.z == 1 :
cam.pointAt( coin.SbVec3f(p.x,p.y,p.z), coin.SbVec3f(0.0,1.0,0.0))
rot = pointAt(dir, App.Vector(0.0,1.0,0.0))
print("normal = 1")
elif dir.z == -1 :
elif dir.z == -1 :
cam.pointAt( coin.SbVec3f(p.x,p.y,p.z), coin.SbVec3f(0.0,1.0,0.0))
rot = pointAt(dir, App.Vector(0.0,1.0,0.0))
print("normal = -1")
else :
else :
cam.pointAt( coin.SbVec3f(p.x,p.y,p.z), coin.SbVec3f(0.0,0.0,1.0))
rot = pointAt(dir, App.Vector(0.0,0.0,1.0))

print("normal normale")
cam.orientation.setValue(rot.Q)
Gui.SendMsgToActiveView("ViewSelection")
Gui.SendMsgToActiveView("ViewSelection")



Revision as of 21:24, 7 March 2016

Macro Align View to Face

Popis
This macro aligns the current view to a selected face

Autor: Rockn
Autor
Rockn
Download
None
Odkazy
Verze
1.0
Datum poslední úpravy
None
Verze FreeCAD
None
Výchozí zástupce
None
Viz též
None

Description

This macro rotates the current view to point perpendicularly at a selected face of an existing object.

How to use

  1. Select a face on an object
  2. Run the macro

Script

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

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]
faceSel = obj.SubObjects[0]
dir = faceSel.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))

cam.orientation.setValue(rot.Q)
Gui.SendMsgToActiveView("ViewSelection")