|
4 | 4 | from pyrevit import revit, DB, UI
|
5 | 5 | from pyrevit import forms
|
6 | 6 |
|
| 7 | +from Autodesk.Revit.UI.Selection import ObjectType |
7 | 8 |
|
8 | 9 | curview = revit.active_view
|
9 | 10 |
|
10 | 11 |
|
11 | 12 | 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))) |
35 | 59 |
|
36 | 60 |
|
37 | 61 | if isinstance(curview, DB.View3D) and curview.IsSectionBoxActive:
|
38 | 62 | orientsectionbox(curview)
|
39 | 63 | elif isinstance(curview, DB.View3D) and not curview.IsSectionBoxActive:
|
40 | 64 | forms.alert("The section box for View3D isn't active.")
|
41 | 65 | 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