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.


Comments

11 responses to “Create an Area”

  1. Sebastian Avatar
    Sebastian

    Hi Jeremy,
    I have found out the error before seeing this post. It came from getting the SketchPlan instance wrongly when I create an AreaViewPlan element. I successfully created a new Area View Plan, added Area elements corresponding to the underlying Room elements inside the AreaBoundaryLines (your command does not create any AreaBoundaryLines, thus when I tried to use it, an exception “Area not in enclosed region” was raised) automatically created by using the RoomBoundaryLines. Nevertheless, I greatly appreciate your help. Your example command helps me assure that I am going the correct direction.
    If you have time, I have some more really interesting questions to ask ^^:
    1. A user can create a custom Area Scheme and create an Area Plan with this scheme. I want to do the same thing in my code. But I found that the API only supports two built-in schemes: BOMA (Rentable) and Gross Building. Is it true?
    2. Assuming a custom Area Scheme is already available, I would like to change the current Area Plan’s scheme from Rentable to this scheme. I tried to set the ObjectType to this Symbol (I used RvtMgdDbg to snoop and found the scheme is actually a Symbol instance), but Revit also forbid this operation (ArgumentException raised “value does not fall within the expected range”.
    3. I would like to add a shared parameter to an Area Plan. I tried but it seems that Revit does not permit this operation.
    I really need to solve these issues for the upcoming project. If you need to see my code, please send me an email, I will reply with the attached source code.
    Looking forward to your comment.
    Sebastian

  2. Dear Sebastian,
    Thank you for your update. Are you saying that the code above does not work for you? Strange, it did work for me. Did you make any simple changes or additions to it to create the area boundary lines you mention? Could you send me the updated code to show what you mean, please, or describe in more detail what it was that did not work and how you fixed it, so that I can complete the code above to work reliably under all circumstances? Thank you!
    I will have to do some research before responding to your additional interesting questions 1 and 2.
    I can respond partially to number 3. at least: to attach a shared parameter to a Revit object, it needs to have a category. If I insert a rentable area plan into a project, I see some available categories associated with it. OST_AreaShemes for the gross building and rentable area scheme, OST_Views for the area plan view ViewPlan instance Rentable. As far as I can tell off-hand, you could associate shared parameters with either the area schemes or the views. If you associate them with the schemes, there will only be one set of parameters per scheme. If you associate them with the views, they will be available on all views, whether area plan, plan, or other. The latter should give you what you want. You just have to ignore all the non-area and non-plan views.
    Cheers, Jeremy.

  3. Dear Sebastian,
    I had a chat with my colleagues regarding your questions. Here are their replies to the first two, and my follow-up on the third:
    1. There is no API method to create a new area scheme.
    2. This cannot be done through the Revit UI or the API. ViewPlan.AreaScheme is read-only.
    3. Did you try to do anything according to my suggestions above? If you are still having problems, can you send me the code that you are using to try to add the shared parameter to the area plan? Thank you!
    Cheers, Jeremy.

  4. Sebastian Avatar
    Sebastian

    Dear Jeremy,
    Here are my feedback about your suggestions and replies from your colleagues (I really appreciate!)
    ** Question number 1 & 2: Originally, I tried to develop a function that:
    (a) can automatically create a new custom Area Scheme
    => With your answers, my conclusion is that it is impossible to create a custom Area Scheme by code.
    According to my understanding, the only way to create an Area View Plan having a custom area scheme is to create it by hand (using the Settings/Area and Volume Computations…/Area Schemes command in the Revit UI).
    (b) Then, create a new area plan (which belongs to the custom Area Scheme) corresponding to a floor plan with the activeDocument.Create.NewAreaViewPlan()
    => The API only supports to create an area plan which belongs to one of 2 built-in schemes: Rentable or Gross Building.
    (c) An Area view plan will have Area elements overlaying the Room elements.
    => And we can only manipulate these pre-created Area Plans (belongs to a custom Area Scheme) by code (i.e. adding area boundary lines and area elements inside it – with activeDocument.Create.NewAreaBoundaryLine() and activeDocument.Create.NewArea() methods)
    ** About Question number 3: Initially I tried to attach a shared parameter which has OST_Views category. My first effort failed because of a known bug of Revit API: when using revit.ActiveDocument.Settings.Categories.get_Item(BuiltInCategory.OST_Views) to get the category instance, this method always return null, as discussed in this thread: http://forums.augi.com/showthread.php?p=857657 . GuyR gave us a workaround solution in the very last post using CategoryFilter. I tried his and finally got the new shared parameter added successfully.
    Regards,
    Sebastian

  5. Sebastian Avatar
    Sebastian

    One more thing, I will post the code fragment that creates the Area Boundary Lines and places an Area element inside the enclosed region ASAP. If I am right, an Area element can only be placed inside a region bounded by a set of joined Area Boundary Line (ModelCurve elements).
    Cheers,
    Sebastian

  6. Dear Sebastian,
    I am glad it helped clarify things and that you made further progress. Thank you very much for your appreciation! I am looking forward to seeing your sample code.
    Cheers, Jeremy.

  7. Hi,
    I would like to thank Mr.Sebastian for his excellent information on Area object.
    But I am not able to get what the below point means
    Then, create a new area plan (which belongs to the custom Area Scheme) corresponding to a floor plan with the activeDocument.Create.NewAreaViewPlan() .
    Can you please explain this?
    Thanks

  8. Hi Jeremy,
    I took a plunge into Revit 2010 3 weeks ago and have done a good progress; unfortunately insufficient to solve the problem I am now facing.
    I am actually developing an External App to generate a series of rooms given the boundaries of each room, its location and name.
    this is working great with the regions and the rooms been created. But only the first room in the list is bound to its related region; and the rest report this error: Room is not in a properly enclosed region.
    below is my code:
    Dim SketchPlane As SketchPlane = mdlInsert.CreateSketchPlane(oRevitApp)
    Dim arrCurve As CurveArray = New CurveArray
    Dim oLine As Line = oRevitApp.Create.NewLineBound(oStart, oEnd)
    arrCurve.Append(oLine)
    Dim arrRoomBoundary As ModelCurveArray
    arrRoomBoundary = RevitApp.ActiveDocument.Create.NewRoomBoundaryLines(SketchPlane, arrCurve, oView)
    Dim oRoomLocation As UV = New UV(intLocX, intLocY)
    oRoom = oRevitApp.ActiveDocument.Create.NewRoom(oLevel, oRoomLocation)
    Could you please shed some light here?
    Regards;
    Fongue

  9. Dear Fongue,
    Congratulations on your good progress. Any special reason why you are working on Revit 2010 instead of 2011?
    Sorry, I have no idea what is going on from your source code snippet.
    Some samples of using the NewRoom method are provided in
    http://thebuildingcoder.typepad.com/blog/2009/01/plantopology-class.html
    http://thebuildingcoder.typepad.com/blog/2009/03/create-room-on-level-in-phase.html
    http://thebuildingcoder.typepad.com/blog/2009/04/create-an-area.html
    http://thebuildingcoder.typepad.com/blog/2010/04/failure-api.html
    The Revit SDK samples AutoTagRooms, ElementsBatchCreation, Rooms, RoomSchedule may also be of interest. RoomSchedule makes use of the NewRoom method, and ElementsBatchCreation uses NewRooms to create a whole batch of rooms, which sounds like a good match for your requirement.
    Cheers, Jeremy.

  10. Hello Jeremy,
    Thanks for your reply. I haven’t been through the links you sent in your reply yet due to the fact that I’m busy installing Revit 2011 and VS 2010.
    To answer your question regarding my choice of 2010 instead on 2011, my company just has the policy not to implement on a latest revision; but on the top previous one to avoid being ahead of the users’ market and also to pick up clients who are not on the latest version yet; since the latest version usually works with the previous one.
    I’ll go through ur proposed solution and definitely let you know.
    Thank you very much to helping us out with this wonderful blog of yours . You are a Great!
    Fongue

  11. Dear Fongue,
    I understand your company’s position, and thank you very much for your appreciation.
    In the case of the Revit API, there is no guaranteed backwards compatibility, you normally have to create a separate version of your plug-in for each major release of Revit.
    Good luck and best regards, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading