Create an Area

Sebastian recently submitted a question on creating a new area element:

Question: I would like to create an area element overlaid on a room in an area plan view.

Answer: The Revit API provides one method on the Document class in the Autodesk.Revit.Creation namespace for creating new areas,

Area NewArea( ViewPlan areaView, UV point );
It is similar to one of the overloads of the NewRoom method mentioned in the discussion on how to create a room:
Room NewRoom( Level level, UV point );

An area can only be created in an area plan view. The determination of the area boundaries is automatic, given a point in its interior.

Therefore, if a room already exists, you just need to determine a point in its interior and feed that and the area plan view to the NewArea method. One easy way to obtain a point in the interior of a room is to use its location point. That returns an XYZ instance. I checked that the returned Z coordinate was zero and simply discarded that to create the UV point required by the NewArea method. Of course, there may be more complex situations where some kind of transformation is required, but I am not sure.

Here is the implementation of the Execute method of a new external command class CmdNewArea which demonstrates this. First, it checks that the current view is an area plan view. It then checks whether a single room has been selected, or prompts you to do so interactively. Once these preliminaries have been completed, the following code creates a new area element based on the selected room boundaries:

CmdResult rc = CmdResult.Failed;
 
ViewPlan view = commandData.View as ViewPlan;
 
if( null == view
  || view.ViewType != ViewType.AreaPlan )
{
  message = "Please run this command in an area plan view.";
  return rc;
}
 
Application app = commandData.Application;
Document doc = app.ActiveDocument;
 
Element room = Util.GetSingleSelectedElement( doc );
 
if( null == room || !(room is Room) )
{
  room = Util.SelectSingleElement( doc, "a room" );
}
 
if( null == room || !( room is Room ) )
{
  message = "Please select a single room element.";
}
else
{
  Location loc = room.Location;
  LocationPoint lp = loc as LocationPoint;
  XYZ p = lp.Point;
  UV q = new UV( p.X, p.Y );
  Area area = doc.Create.NewArea( view, q );
  rc = CmdResult.Succeeded;
}
return rc;

Here is version 1.0.0.30 of the complete Visual Studio solution with the new command.