Skip to content

Commit 85eb9ad

Browse files
committed
applied improvements done to orient section box to face to the orient view to face
1 parent 511cb7a commit 85eb9ad

File tree

2 files changed

+43
-23
lines changed
  • extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Orient View To Face.pushbutton

2 files changed

+43
-23
lines changed

extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Orient View To Face.pushbutton/bundle.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ tooltip:
1212
Diese Skript richtet die aktuelle Kamera Ansicht senkrecht auf die
1313
gewählte Fläche aus. Das Tool erstellt eine Skizzenebene über der
1414
gewählten Fläche für die 3D Ansicht
15+
context: active-3d-view
16+
min_revit_version: 2016
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,59 @@
1-
from pyrevit import HOST_APP
21
from pyrevit import revit, DB, UI
32
from pyrevit import forms
43

4+
from Autodesk.Revit.UI.Selection import ObjectType
55

6-
def reorient():
7-
face = revit.pick_face()
8-
9-
if face:
10-
with revit.Transaction('Orient to Selected Face'):
11-
# calculate normal
12-
if HOST_APP.is_newer_than(2015):
13-
normal_vec = face.ComputeNormal(DB.UV(0, 0))
14-
else:
15-
normal_vec = face.Normal
6+
curview = revit.active_view
167

17-
# create base plane for sketchplane
18-
if HOST_APP.is_newer_than(2016):
19-
base_plane = \
20-
DB.Plane.CreateByNormalAndOrigin(normal_vec, face.Origin)
21-
else:
22-
base_plane = DB.Plane(normal_vec, face.Origin)
238

24-
# now that we have the base_plane and normal_vec
25-
# let's create the sketchplane
9+
def reorient(view):
10+
try:
11+
# Pick face to align to using uidoc.Selection instead of revit.pick_face to get the reference instead of the face
12+
face_ref = revit.uidoc.Selection.PickObject(
13+
UI.Selection.ObjectType.Face, "Pick a face to align to:"
14+
)
15+
16+
# Get the geometry object of the reference
17+
element = revit.doc.GetElement(face_ref)
18+
geometry_object = element.GetGeometryObjectFromReference(face_ref)
19+
20+
# Check if the object might have a Transformation (by checking if it's Non-Instance)
21+
if isinstance(element, DB.FamilyInstance):
22+
# Get the transform of the family instance (converts local to world coordinates)
23+
transform = element.GetTransform()
24+
# Get the face normal in local coordinates
25+
local_normal = geometry_object.ComputeNormal(DB.UV(0, 0)).Normalize()
26+
# Apply the transform to convert normal to world coordinates
27+
world_normal = transform.OfVector(local_normal).Normalize()
28+
norm = world_normal
29+
else:
30+
norm = geometry_object.ComputeNormal(DB.UV(0, 0)).Normalize()
31+
32+
# since we're working with a reference instead of the face, we can't use face.origin
33+
if isinstance(geometry_object, DB.Face):
34+
centroid = geometry_object.Evaluate(DB.UV(0.5, 0.5))
35+
else:
36+
raise Exception("The geometry object is not a face.")
37+
38+
base_plane = DB.Plane.CreateByNormalAndOrigin(norm, centroid)
39+
# now that we have the base_plane and normal_vec
40+
# let's create the sketchplane
41+
with revit.Transaction("Orient to Selected Face"):
2642
sp = DB.SketchPlane.Create(revit.doc, base_plane)
2743

2844
# orient the 3D view looking at the sketchplane
29-
revit.active_view.OrientTo(normal_vec.Negate())
45+
view.OrientTo(norm.Negate())
3046
# set the sketchplane to active
3147
revit.uidoc.ActiveView.SketchPlane = sp
3248

3349
revit.uidoc.RefreshActiveView()
3450

51+
except Exception as ex:
52+
forms.alert("Error: {0}".format(str(ex)))
3553

36-
curview = revit.active_view
3754

55+
# This check could be skipped, as there is a context file in the bundle.yaml
3856
if isinstance(curview, DB.View3D):
39-
reorient()
57+
reorient(curview)
4058
else:
41-
forms.alert('You must be on a 3D view for this tool to work.')
59+
forms.alert("You must be on a 3D view for this tool to work.")

0 commit comments

Comments
 (0)