Revit Macros, UIApp, and PostCommand

Revit Macros, UIApp, and PostCommand

stever66
Advisor Advisor
1,834 Views
5 Replies
Message 1 of 6

Revit Macros, UIApp, and PostCommand

stever66
Advisor
Advisor

I can't seem to find a way to get the UIApplication in a Sharp Macro.

 

Is the UIApplication not available in a Macro? 

 

And if so, does that mean PostCommand cannot be run from a Macro?

 

I'm still pretty much a beginner, so it may be missing something simple.  Here is what I've tired:

 

public void PcmdMacro()
        {
             
             UIDocument uidoc = this.ActiveUIDocument;  
            Document doc = this.ActiveUIDocument.Document;
            UIApplication uiapp = ??;
          
            RevitCommandId id = RevitCommandId.LookupPostableCommandId(PostableCommand.ViewRange);
            
            uiapp.PostCommand( id );
            
            
        }

0 Likes
Accepted solutions (1)
1,835 Views
5 Replies
Replies (5)
Message 2 of 6

so-chong
Advocate
Advocate
Accepted solution

Hi,

You were almost there i think.

Is this what you're looking for?

    Document doc = this.ActiveUIDocument.Document;   
    Application app = doc.Application;
    UIApplication uiapp = new UIApplication(app);
0 Likes
Message 3 of 6

stever66
Advisor
Advisor

I got an error saying Application was a type but used like a variable (or something like that).

 

The Help files says the Application is obtained by the Application keyword in a macro, so I made one slight change, and it seems to work.


Unfortunately, ViewRange doesn't seem to be a postable command like I had hoped.

 

public void PcmdMacro()
        {
            Document doc = this.ActiveUIDocument.Document;   
            //Application app = doc.Application;

            //UIApplication uiapp = new UIApplication(app);
            UIApplication uiapp = new UIApplication(Application);
            
            try
            {
                //RevitCommandId id = RevitCommandId.LookupPostableCommandId(PostableCommand.ViewRange);
                 RevitCommandId id = RevitCommandId.LookupPostableCommandId(PostableCommand.PlaceAComponent);
                if (id != null)
                    {
                    TaskDialog.Show("Test""Found ID");
                    uiapp.PostCommand(id);
                    }
 
             }
            catch
            {
                TaskDialog.Show("Test""error");
            } 
         }

Message 4 of 6

stever66
Advisor
Advisor

Oh, and Thanks!!

0 Likes
Message 5 of 6

MiguelGT17
Advocate
Advocate

Hi there Mr. So-Chong,

 

what is the keyword "this" linking to? I'm curious about that, perhaps it is referring to the instance class "uiApp? 

0 Likes
Message 6 of 6

RPTHOMAS108
Mentor
Mentor

'This' is ApplicationEntryPoint which inherits UIApplication and Implements IEntryPoint.

 

However you see most things related to creating RibbonItems overridden at ApplicationEntryPoint, such as CreateRibbonTab.

 

PostCommand not overridden.