Revit – Copy rooms from link with Python

Or yet another tutorial in how to recreate rooms from links in Revit post!

I know, there are tons of posts explaining how to create rooms from rooms on a link in Revit, but I’m not letting this to stop me.
Here is the problem, we need to copy the rooms from a linked file in Revit 2017 back to our host model, keeping not only the limits, but the rooms’ names and numbers as well.

Disclaimer: There are several ways to do it with Dynamo, but I like this one which allow us to work directly with Python 🙂

First prompt the user to select the link where the rooms are placed:

def selectLink():
    link = uidoc.Selection.PickObject(Selection.ObjectType.Element, "Select Revit link")
    return doc.GetElement(link.ElementId)

Now we get all rooms from the link that are visible in a view in the linked file with the same elevation. They can be changed to use the linked view name instead:

def getRooms(link, doc):
    print( doc.ActiveView.Id)
    linkViews =  FilteredElementCollector(link).OfCategory(BuiltInCategory.OST_Views).OfClass(ViewPlan).ToElements()
    lView = None
    for i in linkViews:
        if i.GenLevel:
            if round(i.GenLevel.Elevation, 2) == round(doc.ActiveView.GenLevel.Elevation, 2):
                lView = i
                break
    rooms = FilteredElementCollector(link, lView.Id).OfCategory(BuiltInCategory.OST_Rooms).ToElements()
    return rooms

From the rooms, we need to extract the level, number, name boundary segments and position point:

def RoomsBoundaries(room):
    level = room.Level
    number = room.Number
    name= room.LookupParameter("Name").AsString()
    opt = SpatialElementBoundaryOptions()
    boundaries = room.GetBoundarySegments(opt)
    curves = []
    for segment in boundaries:
        for b in segment:
            curve = b.GetCurve()
            curves.append(curve)
    position = room.Location
    return name, number, level, curves, position.Point

Once we have all the information, we can go ahead and recreate the rooms:

def recreateRoom(LinkRoom):
    app = doc.Application
 
    docCreation =Autodesk.Revit.Creation.Document
    curveArr =  CurveArray()
    for c in LinkRoom[3]: 
        curveArr.Append(c )
 
    doc.Create.NewRoomBoundaryLines( doc.ActiveView.SketchPlane,curveArr, doc.ActiveView )    
 
    tagPoint =  UV( LinkRoom[4].X,  LinkRoom[4].Y )
    
    room = doc.Create.NewRoom( doc.ActiveView.GenLevel, tagPoint )
    room.Number = LinkRoom[1]
    room.Name = LinkRoom[0]
    return room

The solution can be executed using the RevitPythonShell or inside a pyRevit script!

After running the script you will see all new rooms created in place and displaying the right number and name. We’ve also created Room separation lines, used to limit the rooms boundaries.

Here is the complete code of the execution


Pablo Derendinger
Innovation Team | GeeksGang

Call us: (949) 340-6924