__title__ = 'ScopeBox to Sections'
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import *
from pyrevit import forms
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
class ScopeBoxSelectionFilter(ISelectionFilter):
def AllowElement(self, element):
if element.Category.Id == ElementId(BuiltInCategory.OST_VolumeOfInterest):
return True
ref_scope_boxes = uidoc.Selection.PickObjects(ObjectType.Element, ScopeBoxSelectionFilter())
scope_boxes = [doc.GetElement(ref) for ref in ref_scope_boxes]
if not scope_boxes:
forms.alert('No Box selected. Please Try Again',exitscript=True)
t = Transaction(doc, 'BB to Sections')
t.Start()
for sbox in scope_boxes:
BB = sbox.get_BoundingBox(None)
BB_height = BB.Max.Z - BB.Min.Z
pt_1 = XYZ(BB.Min.X, BB.Min.Y, BB.Min.Z)
pt_2 = XYZ(BB.Max.X, BB.Min.Y, BB.Min.Z)
pt_3 = XYZ(BB.Max.X, BB.Max.Y, BB.Min.Z)
pt_4 = XYZ(BB.Min.X, BB.Max.Y, BB.Min.Z)
line_1 = Line.CreateBound(pt_1, pt_2)
line_2 = Line.CreateBound(pt_2, pt_3)
line_3 = Line.CreateBound(pt_3, pt_4)
line_4 = Line.CreateBound(pt_4, pt_1)
lines = [line_1, line_2, line_3, line_4]
for line in lines:
pt_start = line.GetEndPoint(0)
pt_end = line.GetEndPoint(1)
pt_mid = (pt_start + pt_end)/2
vector = pt_end - pt_start
trans = Transform.Identity
trans.Origin = pt_mid
vector = vector.Normalize() * -1
trans.BasisX = vector
trans.BasisY = XYZ.BasisZ
trans.BasisZ = vector.CrossProduct(XYZ.BasisZ)
section_box = BoundingBoxXYZ()
offset = 1
half = line.Length/2
section_box.Min = XYZ(-half - offset , 0 - offset , 0)
section_box.Max = XYZ(half + offset , BB_height + offset , line.Length)
section_box.Transform = trans
section_type_id = doc.GetDefaultElementTypeId(ElementTypeGroup.ViewTypeSection)
new_section = ViewSection.CreateSection(doc, section_type_id, section_box)
t.Commit()