hi all,
i’m trying to make a script to calculated the room heights in a linked model using raybounce technique.
The raytracing seems to work but something is off with how the points are being recalculated to internal units and back so that it gives completly wrong info.
i’m using a simple generic block family to indicate the calculation points:
before the units convertion and after
here is my Code where i’m at:
import sys
sys.path.append(r'H:\\_GROEPEN\\CAD\\DEV\\Tools\\CMRP')
from pyrevit import script
from pyrevit import UI
from pyrevit import revit
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import StructuralType
from CMRPGUI import *
from CMRP import *
# Doc & App
# -----------------------------------------------------------------------
doc = revit.doc
uidoc = revit.uidoc
links = link_selection(doc)
room_points = []
room_Id = []
for link in links:
link = link.GetLinkDocument()
rooms = FilteredElementCollector(link).OfCategory(BuiltInCategory.OST_Rooms).ToElements()
# Get rooms information from linked model
# -----------------------------------------------------------------------
for room in rooms:
if isinstance(room.Location, LocationPoint):
room_points.append(room.Location.Point)
room_Id.append(room.UniqueId)
def get_type_by_name(type_name):
param_type = ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME)
f_param = ParameterValueProvider(param_type)
evaluator = FilterStringEquals()
f_rule = FilterStringRule(f_param, evaluator, type_name, True)
filter_type_name = ElementParameterFilter(f_rule)
return FilteredElementCollector(doc).WherePasses(filter_type_name).WhereElementIsElementType().FirstElement()
with Transaction(doc, "Place Generic Model Instances") as t:
t.Start()
symbol = get_type_by_name('test-Type1')
if not symbol.IsActive:
symbol.Activate()
for p in room_points:
doc.Create.NewFamilyInstance(p, symbol, StructuralType.NonStructural)
t.Commit()
views = FilteredElementCollector(doc).OfClass(View).WhereElementIsNotElementType().ToElements()
for x in views:
if x.Name == "ray3D":
view = x
break
#Document UI Units
try:
UIunit = Document.GetUnits(doc).GetFormatOptions(UnitType.UT_Length).DisplayUnits
except:
UIunit = Document.GetUnits(doc).GetFormatOptions(SpecTypeId.Length).GetUnitTypeId()
# translation_vector = XYZ(0, 0, 1000)
# # Translate each point in room_points so that it is above the floor
# for i in room_points:
# i = i.Add(translation_vector)
# #Inputs : Points, Direction, 3D View
if isinstance(room_points,list):
points = [XYZ(UnitUtils.ConvertToInternalUnits(i.X,UIunit),UnitUtils.ConvertToInternalUnits(i.Y,UIunit),UnitUtils.ConvertToInternalUnits(i.Z,UIunit)) for i in room_points]
else:
points = [XYZ(UnitUtils.ConvertToInternalUnits(room_points.X,UIunit),UnitUtils.ConvertToInternalUnits(room_points.Y,UIunit),UnitUtils.ConvertToInternalUnits(room_points.Z,UIunit))]
direction = XYZ(0,0,1)
ex = []
pts = []
elems = []
ri = ReferenceIntersector(view)
ri.FindReferencesInRevitLinks = True
# # Find nearest reference for each point and compute height
heights = []
# Find nearest reference for each point and compute height
with Transaction(doc, "Place Generic Model Instances") as t:
t.Start()
for p in points:
# Place a family instance at the original point
instance1 = doc.Create.NewFamilyInstance(p, symbol, StructuralType.NonStructural)
ref = ri.FindNearest(p, direction)
if ref is not None:
refel = ref.GetReference()
link_instance = doc.GetElement(refel.ElementId)
try:
elem = link_instance.GetLinkDocument().GetElement(refel.LinkedElementId)
print(elem.Id)
refp = ref.GetReference().GlobalPoint
# Place a family instance at the detected reference point
instance2 = doc.Create.NewFamilyInstance(refp, symbol, StructuralType.NonStructural)
height = UnitUtils.ConvertFromInternalUnits(refp.Z, UIunit) - UnitUtils.ConvertFromInternalUnits(p.Z, UIunit)
heights.append(height)
print("Element: {}, Height: {}".format(elem.Name, height))
except:
pass
else:
heights.append(None)
print("No reference found")
t.Commit()
print(heights)