Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Creating a room

Anonymous

Creating a room

Anonymous
Not applicable

Hello everyone.

 

I am trying to make an app based on "placing a room" functionality.

I have looked at the helpers guide for Revit and found that I just need to add this piece of code to get started:

Room CreateRoom(Autodesk.Revit.DB.Document document, Level level)
{
      UV roomLocation = new UV(0, 0);

      Room room = document.Create.NewRoom(level, roomLocation);

      if (null == room)
      {
          throw new Exception("Create a new room failed.");
      }

      return room;
}

What I don't get (probably because of my lack of experince in C#) is where I need to implement this piece of code, in order to make it work.

I figured that it was to go into my public class Command : IExernalCommand, but there I do not know how to trigger it, becuase the program only executes what is inside the public Result Execute, where I can't place the piece of code (or what it seems).

 

Can somebody please tell me how to trigger the piece of code, I have searched and worked on it for two days now.

 

Any help is highly appreciated!

0 Likes
Reply
578 Views
8 Replies
Replies (8)

rosalesduquej
Autodesk Support
Autodesk Support

Hello Kennethgv,

 

What you could do is to create your own function to be included later inside of the Execute which will get access to it once you trigger the command. Have you tried using Macros ? Its a simpler way to test small snippets of code before you create your complete app. Also here I will leave you something for you to start advanzing on your current blocker. 

 

I can refer you to the following link where you can find a lot of information regarding the creation of the Room you are trying to do, Take a look at it and also follow the sublinks that the post refers to you will find the answers in there. 

 

http://thebuildingcoder.typepad.com/blog/2009/03/create-room-on-level-in-phase.html

 

In case you need more help please feel free to add more comments to this thread. 

 

Cheers and happy coding. 



Jaime Rosales D.
Sr. Developer Consultant
Twitter | AEC ADN DevBlog
0 Likes

Anonymous
Not applicable
Thank you very much for your quick reply. I have not tried using macros, but will look into that and your link right away. I will let you know how it goes later on.
0 Likes

Anonymous
Not applicable

I have learned that macros is not possible in Visual Studio 2013, because they stopped maintaining it.

 

Actually I just want to call the mentioned piece of code inside public Result Execute, is that possible? If it is, then how am I able to do it?

Or should I create a class, make the code there and then call it in the execute?

0 Likes

Anonymous
Not applicable

Okay, I have now used way too many hours of being stuck - looking at different videos and trying different coding.

 

I simply want to run the code mentioned above, when I press on my pushbutton in Revit (which is programmed in app.cs).

 

I know that I need to implement the code in the public Result Execute, but I have no clue what I am to write to run the small application for placing a room, as seen above. Can you please help me? I'm on my knees now.

0 Likes

arnostlobel
Alumni
Alumni

Hello Kenneth,

 

the end-user will trigger your external command. Your addin file, which is the mean of telling Revit that you are adding a command to Revit, describes the caption (the part the end-user see) of the command and where the command's executable is (DLL + class). When the end-user clicks on your command, which is by default located in the AddIns menu, Revit will call the Execute method of your ExternalCommand class.

 

I believe that if you take the time and try the HelloWorld samples available in Revit API documentation you will most likely get something workable. Once you do, you can than modify the sample's Execute method to do whatever you want.

 

Good luck

Arnošt Löbel
0 Likes

Anonymous
Not applicable

Hello Arnošt.

 

I might have to rephrase my qustion, creating a better understanding.

First I will like to answer on what you have written.

 

I know that the end-user will trigger the external command when pressing a button. I have an app.cs file creating and initialising a Ribbon Tab, Ribbon Panel and pushbutton. When the user presses the button I have set it to run the command.cs with an execute implemented.

 

I think that the problem is that CreateRoom are calling two parameters - Document and Level (correct me if I'm wrong). These are two parameters I do not know how to call in the Execute.

I have tried making InsertRoom to an object inside the execute and then tried calling that object, but without any form of success.

 

I have already defined that in my .addin file.

 

I have completed the HelloWorld in Revit API Documentation, completed My First Revit-Plugin on the internet, completed the series of two videos from DevTV from Autodesk. None of these explain how I am able to call a method with one or more paramters inside the Execute.

 

So if I was unclear in my question before:

How do I run the class method CreateRoom inside the Execute in command.cs? - Should I put the class method outside of the Execute and call within it the Execute, or what am I to do - if this is the answer, then how?

 

 

Thank you all for your time so far.

0 Likes

Anonymous
Not applicable
Hi Kenneth,

If I understand you correctly then you can execute the command in Revit?
You just dont know where to get the instances of Document and Level that you're looking for?

Well the document can be found in the ExternalCommanDdata object supplied in the Execute method if it inherits IExternalCommand (which it must).

This snipped will get you your document:

UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;

When it comes to your Level, there are many ways of getting it.
This would get you any level (if you just want to get it working):
Level l = new FilteredElementCollector(doc).OfClass(typeof(Level)).First();

Or you could try this
List<Level> levels = new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>().ToList();
And iterate over the levels to find the level you want to Place it on.

I just wrote the code in the Quick reply so there might be Spelling errors in it.

Good luck!

0 Likes

Anonymous
Not applicable

Hello Kenneth,

 

was it something like this you were trying to do? Assuming your visibility allows it, you just add the method inside the exexute method like below and assign its variables.

 

[Transaction(TransactionMode.Manual)]
    public class Command: IExternalCommand
    {  
      
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication rvtUIApp = commandData.Application;
            UIDocument uiDoc = rvtUIApp.ActiveUIDocument;
            
            Document _doc = uiDoc.Document;
            Level lvl = new FilteredElementCollector(doc).OfClass(typeof(Level​)).First();

            Transaction tr = new Transaction(_doc, "Create Room");

                tr.Start();
 
                    CreateRoom(_doc, lvl);
                  
               
                tr.Commit();
            
            return Result.Succeeded;
        }
 
public void CreateRoom(Document document, Level lvl)
{
 code.....
}

}

 But like Erik said, you need to find the level you want to place it on.

 

Good luck

/Martin

0 Likes