#RTCNA Wish 3: Copy rooms from one phase to another

Matt and some others asked how to “Copy All Rooms/Spaces to all phases in project”. This example shows how we can specify a “source phase” and create new rooms for each of those rooms in a “target phase”.

public void copyRoomPhase()
{
    string sourcePhaseName = "New Construction";
    string targetPhaseName = "Existing";
    
    Document doc = this.ActiveUIDocument.Document;
    
    Phase targetPhase = new FilteredElementCollector(doc)
        .OfClass(typeof(Phase))
        .Cast<Phase>()
        .FirstOrDefault(q => q.Name == targetPhaseName);

    Phase sourcePhase = new FilteredElementCollector(doc)
        .OfClass(typeof(Phase))
        .Cast<Phase>()
        .FirstOrDefault(q => q.Name == sourcePhaseName);
    
    IList<Element> roomList = new FilteredElementCollector(doc)
        .OfClass(typeof(SpatialElement))
        .OfCategory(BuiltInCategory.OST_Rooms).ToList();
    
    PlanTopologySet planTopologies = null;
    using (Transaction t = new Transaction(doc,"get topology"))
    {
        t.Start();
        planTopologies = doc.PlanTopologies;
        t.Commit();
    }

    foreach (Element sourceRoom in roomList)
    {
        XYZ pointInSourceRoom = ((LocationPoint)(sourceRoom.Location)).Point;
        
        Parameter phaseParam = sourceRoom.get_Parameter("Phase");
        if (doc.GetElement(phaseParam.AsElementId()).Name != sourcePhaseName)
            continue;
        
        Room newRoom = null;
        using (Transaction t = new Transaction(doc,"New Room"))
        {
            t.Start();
            newRoom = doc.Create.NewRoom(targetPhase);
            t.Commit();
        }
        
        bool found = false;
        
        foreach (PlanTopology pt in planTopologies)
        {
            if (found)
                break;
            
            foreach (PlanCircuit pc in pt.Circuits.Cast<PlanCircuit>())
            {
                using (Transaction t = new Transaction(doc,"Place Room"))
                {
                    t.Start();
                    Room placedNewRoom = doc.Create.NewRoom(newRoom,pc);
                    doc.Regenerate();
                    
                    if (placedNewRoom.IsPointInRoom(pointInSourceRoom))
                    {
                        t.Commit();
                        found = true;
                        break;
                    }
                    else
                    {
                        t.RollBack();
                    }
                }
            }
        }
    }
}
Advertisements

2 thoughts on “#RTCNA Wish 3: Copy rooms from one phase to another

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s