|
| 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