Skip to content

Commit 140c127

Browse files
authored
First Upload
Library
1 parent 68be61c commit 140c127

3 files changed

Lines changed: 231 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import clr
3+
clr.AddReference('RevitAPI')
4+
clr.AddReference('RevitAPIUI')
5+
from Autodesk.Revit.DB import *
6+
7+
class ElementGeometry:
8+
9+
@staticmethod
10+
def GetElementSolid(element):
11+
'''
12+
Retrive the Solids of an Element.
13+
'''
14+
solids = []
15+
options = Options()
16+
geometry_element = element.get_Geometry(options)
17+
18+
if geometry_element:
19+
for geometry_object in geometry_element:
20+
solid = geometry_object if isinstance(geometry_object, Solid) and geometry_object.Volume > 0 else None
21+
if solid:
22+
solids.append(solid)
23+
else:
24+
geometry_instance = geometry_object if isinstance(geometry_object, GeometryInstance) else None
25+
if geometry_instance:
26+
for instance_geometry_object in geometry_instance.GetInstanceGeometry():
27+
instance_solid = instance_geometry_object if isinstance(instance_geometry_object, Solid) and instance_geometry_object.Volume > 0 else None
28+
if instance_solid:
29+
solids.append(instance_solid)
30+
31+
return solids
32+
33+
@staticmethod
34+
def GetFaceFromSolid(solid):
35+
'''
36+
Retrieves the Faces from a Solid.
37+
'''
38+
faces = [face for face in solid.Faces]
39+
return faces
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
import clr
3+
clr.AddReference('RevitAPI')
4+
clr.AddReference('RevitAPIUI')
5+
from Autodesk.Revit.DB import *
6+
from Autodesk.Revit.UI import *
7+
from Autodesk.Revit.UI.Selection import *
8+
9+
class ModelSelection:
10+
11+
@staticmethod
12+
def PickElement(uidoc):
13+
'''
14+
Select A Element.
15+
'''
16+
object_type = ObjectType.Element
17+
pick_reference = uidoc.Selection.PickObject(object_type, "Select Model Element")
18+
pick_element = uidoc.Document.GetElement(pick_reference)
19+
return pick_element
20+
21+
@staticmethod
22+
def PickElements(uidoc):
23+
'''
24+
Select Mutiple Elements.
25+
'''
26+
object_type = ObjectType.Element
27+
pick_references = uidoc.Selection.PickObjects(object_type, "Select Model Elements")
28+
pick_elements = [uidoc.Document.GetElement(r) for r in pick_references]
29+
return pick_elements
30+
31+
@staticmethod
32+
def SelectElementByCategory(uidoc, category_name):
33+
'''
34+
Select A Element By Category.
35+
'''
36+
filter = ModelSelection.CategorySelectionFilter([category_name])
37+
object_type = ObjectType.Element
38+
pick_reference = uidoc.Selection.PickObject(object_type, filter, "Select {} Element".format(category_name))
39+
pick_element = uidoc.Document.GetElement(pick_reference)
40+
return pick_element
41+
42+
@staticmethod
43+
def SelectElementsByCategory(uidoc, category_names):
44+
'''
45+
Select Mutiple Elements By Category.
46+
'''
47+
filter = ModelSelection.CategorySelectionFilter(category_names)
48+
object_type = ObjectType.Element
49+
pick_references = uidoc.Selection.PickObjects(object_type, filter, "Select {} Elements".format(", ".join(category_names)))
50+
pick_elements = [uidoc.Document.GetElement(r) for r in pick_references]
51+
return pick_elements
52+
53+
@staticmethod
54+
def SelectImportInstance(uidoc):
55+
'''
56+
Select A Import Instance.
57+
'''
58+
cad_filter = ModelSelection.CADSelectionFilter()
59+
object_type = ObjectType.Element
60+
pick_reference = uidoc.Selection.PickObject(object_type, cad_filter, "Select Import Instance")
61+
pick_element = uidoc.Document.GetElement(pick_reference)
62+
return pick_element
63+
64+
class CategorySelectionFilter(ISelectionFilter):
65+
def __init__(self, category_names):
66+
self._category_names = category_names
67+
68+
def AllowElement(self, element):
69+
return element.Category.Name in self._category_names if element.Category else False
70+
71+
def AllowReference(self, reference, position):
72+
return False
73+
74+
class CADSelectionFilter(ISelectionFilter):
75+
def AllowElement(self, elem):
76+
return isinstance(elem, ImportInstance)
77+
78+
def AllowReference(self, reference, position):
79+
return False
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
import clr
3+
clr.AddReference('RevitAPI')
4+
clr.AddReference('RevitAPIUI')
5+
from Autodesk.Revit.DB import *
6+
from System.Collections.Generic import List
7+
8+
class VisuallizeGeometry:
9+
10+
@staticmethod
11+
def CreateDirectShape(doc, geometry_objects):
12+
direct_shape = DirectShape.CreateElement(doc, ElementId(BuiltInCategory.OST_GenericModel))
13+
direct_shape.SetShape(geometry_objects)
14+
return direct_shape
15+
16+
@staticmethod
17+
def VisuallizePoint(doc, point):
18+
'''
19+
Visuallize a Point by Generic Model.
20+
'''
21+
with Transaction(doc, "Visuallize Point") as t:
22+
t.Start()
23+
geometry_object = List[GeometryObject]([Point.Create(point)])
24+
VisuallizeGeometry.CreateDirectShape(doc, geometry_object)
25+
t.Commit()
26+
27+
@staticmethod
28+
def VisuallizeLine(doc, start_point, end_point):
29+
'''
30+
Visuallize a Line by Generic Model.
31+
'''
32+
with Transaction(doc, "Visuallize Line") as t:
33+
t.Start()
34+
line = List[GeometryObject]([Line.CreateBound(start_point, end_point)])
35+
VisuallizeGeometry.CreateDirectShape(doc, line)
36+
t.Commit()
37+
38+
@staticmethod
39+
def VisuallizeSolid(doc, solids):
40+
'''
41+
Visuallize a list of Solids by Generic Model.
42+
'''
43+
with Transaction(doc, "Visuallize Solid") as t:
44+
t.Start()
45+
geometry_objects = List[GeometryObject]([solid for solid in solids])
46+
VisuallizeGeometry.CreateDirectShape(doc, geometry_objects)
47+
t.Commit()
48+
49+
@staticmethod
50+
def VisuallizeFace(doc, faces):
51+
'''
52+
Visuallize a list of Faces by Generic Model.
53+
'''
54+
with Transaction(doc, "Visuallize Face") as t:
55+
t.Start()
56+
for face in faces:
57+
face_mesh = face.Triangulate()
58+
geometry_object = List[GeometryObject]([face_mesh])
59+
VisuallizeGeometry.CreateDirectShape(doc, geometry_object)
60+
t.Commit()
61+
62+
@staticmethod
63+
def VisuallizeBoundingBoxFromSolid(doc, input_solid):
64+
'''
65+
Visuallize a BoundingBox by Generic Model.
66+
'''
67+
with TransactionGroup(doc, "Visuallize Bounding Box") as tg:
68+
tg.Start()
69+
bbox = input_solid.GetBoundingBox()
70+
pt0 = XYZ(bbox.Min.X, bbox.Min.Y, bbox.Min.Z)
71+
pt1 = XYZ(bbox.Max.X, bbox.Min.Y, bbox.Min.Z)
72+
pt2 = XYZ(bbox.Max.X, bbox.Max.Y, bbox.Min.Z)
73+
pt3 = XYZ(bbox.Min.X, bbox.Max.Y, bbox.Min.Z)
74+
edges = [Line.CreateBound(pt0, pt1), Line.CreateBound(pt1, pt2), Line.CreateBound(pt2, pt3), Line.CreateBound(pt3, pt0)]
75+
height = bbox.Max.Z - bbox.Min.Z
76+
base_loop = CurveLoop.Create(List[Curve](edges))
77+
pre_transform_box = GeometryCreationUtilities.CreateExtrusionGeometry(List[CurveLoop]([base_loop]), XYZ.BasisZ, height)
78+
transform_box = SolidUtils.CreateTransformed(pre_transform_box, bbox.Transform)
79+
VisuallizeGeometry.VisuallizeSolid(doc, [transform_box])
80+
tg.Assimilate()
81+
82+
83+
@staticmethod
84+
def VisuallizeBoundingBoxFromBoundingBox(doc, bbox):
85+
'''
86+
Visualize a BoundingBox by creating a Generic Model.
87+
'''
88+
with TransactionGroup(doc, "Visualize Bounding Box") as tg:
89+
tg.Start()
90+
91+
# Define corner points of the bounding box
92+
pt0 = XYZ(bbox.Min.X, bbox.Min.Y, bbox.Min.Z)
93+
pt1 = XYZ(bbox.Max.X, bbox.Min.Y, bbox.Min.Z)
94+
pt2 = XYZ(bbox.Max.X, bbox.Max.Y, bbox.Min.Z)
95+
pt3 = XYZ(bbox.Min.X, bbox.Max.Y, bbox.Min.Z)
96+
97+
# Create the edges of the bounding box base
98+
edges = [Line.CreateBound(pt0, pt1), Line.CreateBound(pt1, pt2), Line.CreateBound(pt2, pt3), Line.CreateBound(pt3, pt0)]
99+
100+
# Calculate the height of the bounding box and ensure it is positive
101+
height = max(bbox.Max.Z - bbox.Min.Z, 0.1) # Set height to 0.1 if it is 0 or negative
102+
103+
# Create the base loop and extrusion geometry
104+
base_loop = CurveLoop.Create(List[Curve](edges))
105+
pre_transform_box = GeometryCreationUtilities.CreateExtrusionGeometry(List[CurveLoop]([base_loop]), XYZ.BasisZ, height)
106+
107+
# Apply transformation to align with the bounding box's transform
108+
transform_box = SolidUtils.CreateTransformed(pre_transform_box, bbox.Transform)
109+
110+
# Visualize the transformed geometry
111+
VisuallizeGeometry.VisuallizeSolid(doc, [transform_box])
112+
113+
tg.Assimilate()

0 commit comments

Comments
 (0)