There are many things that are not very clear when using RevitAPI to create a room. It is even more difficult to understand for someone like me who has rarely and almost never manually operated rooms in Revit.
RevitAPI Demo
Room InsertNewRoomInPlanCircuit(Autodesk.Revit.DB.Document document, Level level, Phase newConstructionPhase)
{
// create room using Phase
Room newScheduleRoom = document.Create.NewRoom(newConstructionPhase);
// set the Room Number and Name
string newRoomNumber = "101";
string newRoomName = "Class Room 1";
newScheduleRoom.Name = newRoomName;
newScheduleRoom.Number = newRoomNumber;
// Get a PlanCircuit
PlanCircuit planCircuit = null;
// first get the plan topology for given level
PlanTopology planTopology = document.get_PlanTopology(level);
// Iterate circuits in this plan topology
foreach (PlanCircuit circuit in planTopology.Circuits)
{
// get the first circuit we find
if (null != circuit)
{
planCircuit = circuit;
break;
}
}
Room newRoom2 = null;
if (null != planCircuit)
{
using (Transaction transaction = new Transaction(document, "Create Room"))
{
if (transaction.Start() == TransactionStatus.Started)
{
// The input room must exist only in the room schedule,
// meaning that it does not display in any plan view.
newRoom2 = document.Create.NewRoom(newScheduleRoom, planCircuit);
// a model room with the same name and number is created in the
// view where the PlanCircuit is located
if (null != newRoom2)
{
// Give the user some information
TaskDialog.Show("Revit", "Room placed in Plan Circuit successfully.");
}
transaction.Commit();
}
}
}
return newRoom2;
}
There are three ways to create a room using the API:
public Room NewRoom(Phase phase)
public Room NewRoom(Level level,UV point)
public Room NewRoom(Room room,PlanCircuit circuit)
If you already have a closed outline and need to create a room within the outline, you can use the following method:
-
Draw room dividers
//Create room separation
CurveArray slabCurveArray = new CurveArray();
foreach (var vmCeilingEdgeInfo in vm.CeilingEdgeInfos)
{ slabCurveArray.Append(vmCeilingEdgeInfo.Edge); }var sketchPlane = SketchPlane.Create(ActiveDocument, Plane.CreateByNormalAndOrigin(-bottomFace.FaceNormal, bottomFace.Origin));
var viewPlan = FilterUtils.FilterByFunc(ActiveDocument, o => o.LookupParameter("Related elevation")?.AsString() == level.Name).FirstOrDefault();
roomSepartions = ActiveDocument.Create.NewRoomBoundaryLines(sketchPlane, slabCurveArray, viewPlan);
ActiveDocument.Regenerate();
-
Get all the contours of the same elevation in the project and determine whether the contour has generated a room
PlanCircuit planCircuit = null;
PlanTopology planTopology = ActiveDocument.get_PlanTopology(level);
foreach (PlanCircuit circuit in planTopology.Circuits)
{
if (null != circuit && !circuit.IsRoomLocated)
{
planCircuit = circuit;
break;
}
} -
Create a room (the phase parameter in this method can be null if you don’t need it)
//Create room
if (null != planCircuit)
{ var room = ActiveDocument.Create.NewRoom(null, planCircuit); }
PS:
- After creating a room, you cannot delete the room divider, otherwise the room will become a room with no outline and no area.
- The view used to create the room must be a plan view, if it is not a normal view an error will be reported ("An attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted").