pyRevit

Code
Library
DirectShape: Create 3D Bounding Box Geometry
Visualize BoundingBox
def create_DirectShape(bbox, height = None):
# Create BB Points
pt0 = XYZ(bbox.Min.X, bbox.Min.Y, bbox.Min.Z)
pt1 = XYZ(bbox.Max.X, bbox.Min.Y, bbox.Min.Z)
pt2 = XYZ(bbox.Max.X, bbox.Max.Y, bbox.Min.Z)
pt3 = XYZ(bbox.Min.X, bbox.Max.Y, bbox.Min.Z)
# Create Lines
edge0 = Line.CreateBound(pt0, pt1)
edge1 = Line.CreateBound(pt1, pt2)
edge2 = Line.CreateBound(pt2, pt3)
edge3 = Line.CreateBound(pt3, pt0)
edges = [edge0, edge1, edge2, edge3]
# Calculate Height
if not height:
height = bbox.Max.Z - bbox.Min.Z
# Create Extrusion Geometry
baseLoop = CurveLoop.Create(edges)
loopList = [baseLoop]
geo = GeometryCreationUtilities.CreateExtrusionGeometry(loopList, XYZ.BasisZ, height)
# Create DirectShape + SetShape
cat_id = ElementId(BuiltInCategory.OST_GenericModel)
ds = DirectShape.CreateElement(doc, cat_id)
ds.SetShape([geo])
return ds
import clr
clr.AddReference('System')
from System.Collections.Generic import List
selection = [doc.GetElement(elId) for elId in __revit__.ActiveUIDocument.Selection.GetElementIds()]
# Start a transaction
t = Transaction(doc, 'Create DirectShape from BoundingBox')
t.Start()
def solidBoundingBox(inputSolid):
bbox = inputSolid.get_BoundingBox(None)
# corners in BBox coords
pt0 = XYZ(bbox.Min.X, bbox.Min.Y, bbox.Min.Z)
pt1 = XYZ(bbox.Max.X, bbox.Min.Y, bbox.Min.Z)
pt2 = XYZ(bbox.Max.X, bbox.Max.Y, bbox.Min.Z)
pt3 = XYZ(bbox.Min.X, bbox.Max.Y, bbox.Min.Z)
# edges in BBox coords
edge0 = Line.CreateBound(pt0, pt1)
edge1 = Line.CreateBound(pt1, pt2)
edge2 = Line.CreateBound(pt2, pt3)
edge3 = Line.CreateBound(pt3, pt0)
# create loop, still in BBox coords
edges = [edge0, edge1, edge2, edge3]
height = bbox.Max.Z - bbox.Min.Z
baseLoop = CurveLoop.Create(edges)
loopList = [baseLoop]
preTransformBox = GeometryCreationUtilities.CreateExtrusionGeometry(loopList, XYZ.BasisZ, height)
transformBox = SolidUtils.CreateTransformed(preTransformBox, bbox.Transform)
return transformBox
for el in selection:
solid = solidBoundingBox(el)
# Create geometry for a new DirectShape
#dsGeometry = GeometryCreationUtilities.CreateExtrusionGeometry([CurveLoop.Create(List[Curve]([Line.CreateBound(bbox.Min, bbox.Max)]))], XYZ.BasisZ, 1)
# Create a DirectShape
ds = DirectShape.CreateElement(doc, ElementId(BuiltInCategory.OST_GenericModel))
ds.SetShape([solid])
t.Commit()
Create DirectShape Sphere
import clr, math
clr.AddReference('System')
from System.Collections.Generic import List
def CreateCenterbasedSphere(center, radius):
frame = Frame(center,
XYZ.BasisX,
XYZ.BasisY,
XYZ.BasisZ)
profileloops = List[CurveLoop]()
profileloop = CurveLoop()
cemiEllipse = Ellipse.CreateCurve(center, radius, radius,
XYZ.BasisX,
XYZ.BasisZ,
-math.pi / 2.0, math.pi / 2.0)
profileloop.Append(cemiEllipse);
profileloop.Append(Line.CreateBound(
XYZ(center.X, center.Y, center.Z + radius),
XYZ(center.X, center.Y, center.Z - radius)))
profileloops.Add(profileloop);
return GeometryCreationUtilities.CreateRevolvedGeometry(frame, profileloops, -math.pi, math.pi)
# Create Sphere
pt = XYZ(1,1,1)
r = 1
sphere = CreateCenterbasedSphere(pt, r)
print(sphere)
# Transaction
t = Transaction(doc, 'test')
t.Start()
# Create DS Shape
ds = DirectShape.CreateElement(doc, ElementId(BuiltInCategory.OST_GenericModel))
ds.SetShape([sphere])
t.Commit()

⌨️ Happy Coding!
Erik Frits