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