-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization_solid_script.py
More file actions
187 lines (139 loc) · 5.66 KB
/
Copy pathvisualization_solid_script.py
File metadata and controls
187 lines (139 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# /// script
# dependencies = []
# ///
"""
Test Solid and Face Visualization
Demonstrates complex geometry visualization (solids, faces, meshes).
Similar to RevitDevTool.DotnetDemo/SolidVisualization.cs and FaceVisualization.cs
"""
from Autodesk.Revit import UI, DB
from Autodesk.Revit.UI.Selection import ObjectType
def test_element_solid(uidoc: UI.UIDocument):
"""Pick an element and visualize its solid geometry"""
try:
print("Select an element...")
ref = uidoc.Selection.PickObject(ObjectType.Element, "Select an element")
elem = uidoc.Document.GetElement(ref)
options = DB.Options()
geom = elem.get_Geometry(options)
if not geom:
print("ERROR: No geometry found")
return
print(f"Element: {elem.Name} (ID: {elem.Id.IntegerValue})")
# Extract solids
solids = []
for geo_obj in geom:
if isinstance(geo_obj, DB.Solid) and geo_obj.Volume > 0:
solids.append(geo_obj)
elif isinstance(geo_obj, DB.GeometryInstance):
inst_geom = geo_obj.GetInstanceGeometry()
for inst_obj in inst_geom:
if isinstance(inst_obj, DB.Solid) and inst_obj.Volume > 0:
solids.append(inst_obj)
print(f"Found {len(solids)} solid(s)")
# Visualize each solid
for i, solid in enumerate(solids):
print(f"Solid {i + 1}: Volume={solid.Volume:.2f}, SurfaceArea={solid.SurfaceArea:.2f}")
print(solid)
except Exception as e:
print(f"ERROR: {e}")
def test_element_faces(uidoc: UI.UIDocument):
"""Pick an element and visualize its faces"""
try:
print("Select an element...")
ref = uidoc.Selection.PickObject(ObjectType.Element, "Select an element")
elem = uidoc.Document.GetElement(ref)
options = DB.Options()
geom = elem.get_Geometry(options)
if not geom:
print("ERROR: No geometry found")
return
print(f"Element: {elem.Name} (ID: {elem.Id.IntegerValue})")
# Extract faces from solids
faces = []
for geo_obj in geom:
if isinstance(geo_obj, DB.Solid) and geo_obj.Volume > 0:
for face in geo_obj.Faces:
faces.append(face)
elif isinstance(geo_obj, DB.GeometryInstance):
inst_geom = geo_obj.GetInstanceGeometry()
for inst_obj in inst_geom:
if isinstance(inst_obj, DB.Solid) and inst_obj.Volume > 0:
for face in inst_obj.Faces:
faces.append(face)
print(f"Found {len(faces)} face(s)")
# Visualize each face
for i, face in enumerate(faces):
print(f"Face {i + 1}: Area={face.Area:.2f}")
print(face)
except Exception as e:
print(f"ERROR: {e}")
def test_picked_face(uidoc: UI.UIDocument):
"""Pick a face directly and visualize it"""
try:
print("Select a face...")
ref = uidoc.Selection.PickObject(ObjectType.Face, "Select a face")
elem = uidoc.Document.GetElement(ref)
face = elem.GetGeometryObjectFromReference(ref)
if face:
print(f"Selected face: Area={face.Area:.2f}")
print(face)
else:
print("ERROR: Could not get face geometry")
except Exception as e:
print(f"ERROR: {e}")
def test_bounding_boxes(uidoc: UI.UIDocument):
"""Visualize bounding boxes of selected elements"""
try:
print("Select elements (ESC when done)...")
refs = uidoc.Selection.PickObjects(ObjectType.Element, "Select elements")
if len(refs) == 0:
print("WARNING: No elements selected")
return
print(f"Selected {len(refs)} element(s)")
for i, ref in enumerate(refs):
elem = uidoc.Document.GetElement(ref)
bbox = elem.get_BoundingBox(None)
if bbox:
print(f"Element {i + 1} ({elem.Name}): BBox Min={bbox.Min}, Max={bbox.Max}")
print(bbox)
else:
print(f"Element {i + 1}: No bounding box")
except Exception as e:
print(f"ERROR: {e}")
def test_all_element_solids(uidoc: UI.UIDocument):
"""Visualize solids of all visible elements (warning: may be slow)"""
try:
print("WARNING: This will visualize all element solids in the view")
print("Collecting elements...")
collector = DB.FilteredElementCollector(uidoc.Document, uidoc.ActiveView.Id)
elements = collector.WhereElementIsNotElementType().ToElements()
print(f"Found {len(elements)} elements")
count = 0
options = DB.Options()
for elem in elements:
geom = elem.get_Geometry(options)
if not geom:
continue
for geo_obj in geom:
if isinstance(geo_obj, DB.Solid) and geo_obj.Volume > 0:
print(geo_obj)
count += 1
if count >= 10: # Limit to first 10 for performance
print(f"Limiting to first {count} solids...")
return
print(f"Visualized {count} solid(s)")
except Exception as e:
print(f"ERROR: {e}")
def main():
print("=== Solid and Face Visualization Test ===")
print()
uidoc: UI.UIDocument = __revit__.ActiveUIDocument # type: ignore # noqa: F821
# Uncomment the test you want to run:
test_element_solid(uidoc)
# test_element_faces(uidoc)
# test_picked_face(uidoc)
# test_bounding_boxes(uidoc)
# test_all_element_solids(uidoc)
if __name__ == "__main__":
main()