Skip to content

Commit 830bba6

Browse files
authored
Merge pull request #2619 from Wurschdhaud/develop
dev/#2606 Fix for orienting section box to faces of objects that have transformations
2 parents fe8f885 + 511cb7a commit 830bba6

File tree

2 files changed

+49
-24
lines changed
  • extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Orient Section Box To Face.pushbutton

2 files changed

+49
-24
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ tooltip:
1414
Fläche im Modell ausgewählt werden. Mit der TAB Taste kann man zwischen angrenzenden
1515
Flächen durchwechseln. Eine Seite des 3D-Schnittbereichs übernimmt die
1616
Ausrichtung der gewählten Fläche!
17+
context: active-3d-view

extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Orient Section Box To Face.pushbutton/script.py

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,63 @@
44
from pyrevit import revit, DB, UI
55
from pyrevit import forms
66

7+
from Autodesk.Revit.UI.Selection import ObjectType
78

89
curview = revit.active_view
910

1011

1112
def orientsectionbox(view):
12-
try:
13-
face = revit.pick_face()
14-
box = view.GetSectionBox()
15-
norm = face.ComputeNormal(DB.UV(0, 0)).Normalize()
16-
boxNormal = box.Transform.Basis[0].Normalize()
17-
angle = norm.AngleTo(boxNormal)
18-
axis = DB.XYZ(0, 0, 1.0)
19-
origin = DB.XYZ(box.Min.X + (box.Max.X - box.Min.X) / 2,
20-
box.Min.Y + (box.Max.Y - box.Min.Y) / 2, 0.0)
21-
if norm.Y * boxNormal.X < 0:
22-
rotate = \
23-
DB.Transform.CreateRotationAtPoint(axis, Math.PI / 2 - angle,
24-
origin)
25-
else:
26-
rotate = DB.Transform.CreateRotationAtPoint(axis,
27-
angle,
28-
origin)
29-
box.Transform = box.Transform.Multiply(rotate)
30-
with revit.Transaction('Orient Section Box to Face'):
31-
view.SetSectionBox(box)
32-
revit.uidoc.RefreshActiveView()
33-
except Exception:
34-
pass
13+
try:
14+
# Pick face to align to using uidoc.Selection instead of revit.pick_face to get the reference instead of the face
15+
face = revit.uidoc.Selection.PickObject(
16+
UI.Selection.ObjectType.Face, "Pick a face to align to:"
17+
)
18+
19+
# Get the section box
20+
box = view.GetSectionBox()
21+
22+
# Get the geometry object of the reference
23+
element = revit.doc.GetElement(face)
24+
geometry_object = element.GetGeometryObjectFromReference(face)
25+
26+
# Check if the object might have a Transformation (by checking if it's Non-Instance)
27+
if isinstance(element, DB.FamilyInstance):
28+
# Get the transform of the family instance (converts local to world coordinates)
29+
transform = element.GetTransform()
30+
# Get the face normal in local coordinates
31+
local_normal = geometry_object.ComputeNormal(DB.UV(0, 0)).Normalize()
32+
# Apply the transform to convert normal to world coordinates
33+
world_normal = transform.OfVector(local_normal).Normalize()
34+
norm = world_normal
35+
else:
36+
norm = geometry_object.ComputeNormal(DB.UV(0, 0)).Normalize()
37+
38+
# Orient the box
39+
boxNormal = box.Transform.Basis[0].Normalize()
40+
angle = norm.AngleTo(boxNormal)
41+
axis = DB.XYZ(0, 0, 1.0)
42+
origin = DB.XYZ(
43+
box.Min.X + (box.Max.X - box.Min.X) / 2,
44+
box.Min.Y + (box.Max.Y - box.Min.Y) / 2,
45+
0.0,
46+
)
47+
if norm.Y * boxNormal.X < 0:
48+
rotate = DB.Transform.CreateRotationAtPoint(
49+
axis, Math.PI / 2 - angle, origin
50+
)
51+
else:
52+
rotate = DB.Transform.CreateRotationAtPoint(axis, angle, origin)
53+
box.Transform = box.Transform.Multiply(rotate)
54+
with revit.Transaction("Orient Section Box to Face"):
55+
view.SetSectionBox(box)
56+
revit.uidoc.RefreshActiveView()
57+
except Exception as ex:
58+
forms.alert("Error: {0}".format(str(ex)))
3559

3660

3761
if isinstance(curview, DB.View3D) and curview.IsSectionBoxActive:
3862
orientsectionbox(curview)
3963
elif isinstance(curview, DB.View3D) and not curview.IsSectionBoxActive:
4064
forms.alert("The section box for View3D isn't active.")
4165
else:
42-
forms.alert('You must be on a 3D view for this tool to work.')
66+
forms.alert("You must be on a 3D view for this tool to work.")

0 commit comments

Comments
 (0)