Autodesk.Revit.ApplicationServices.Application OnStartup LoginUserId

Autodesk.Revit.ApplicationServices.Application OnStartup LoginUserId

david_rock
Enthusiast Enthusiast
3,569 Views
4 Replies
Message 1 of 5

Autodesk.Revit.ApplicationServices.Application OnStartup LoginUserId

david_rock
Enthusiast
Enthusiast

Hello,

 

I am trying to implement the Revit Entitlement API. And trying to apply the Entitlement OnStartup.

 

However it seems that I need to set Autodesk.Revit.ApplicationServices.Application.

 

How can I set Autodesk.Revit.ApplicationServices.Application at the OnStartup Event? It seems I can't get from Autodesk.Revit.UI.UIControlledApplication to Autodesk.Revit.ApplicationServices.Application.

 

I can get it using ExternalCommandData, however I don't have access to this OnStartup?

 

Dim rvtApp As Application = uiApp.Application
            'Check to see if the user is logged in.
            If Not Application.IsLoggedIn Then
                TaskDialog.Show("Entitlement API", "Please login to Autodesk 360 first" & vbLf)
                Return False
            End If
            Dim userId As String = rvtApp.LoginUserId
            Dim isValid As Boolean = Entitlement(app.appId, userId)
            Dim msg As String = (Convert.ToString((Convert.ToString("userId = ") & userId) + vbLf & "appId = ") & app.appId) + vbLf & "isValid = " + isValid.ToString()
            If isValid Then
                TaskDialog.Show("Entitlement API", msg)
                Return True
            Else
                TaskDialog.Show("Entitlement API", msg)
                Return False
            End If

 

 

Kind Regards

David

 

Accepted solutions (1)
3,570 Views
4 Replies
Replies (4)
Message 2 of 5

FAIR59
Advisor
Advisor
Accepted solution

The Application isn't yet available in the OnStartUp method. You have to wait for the ApplicationInitialized event.

 

in OnStartUp subcribe to the Initialized event:

application.ControlledApplication.ApplicationInitialized += ControlledApplication_ApplicationInitialized;

 

You can get the Autodesk.Revit.ApplicationServices.Application in the event handler:

 

void ControlledApplication_ApplicationInitialized(object sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e)

{

   Autodesk.Revit.ApplicationServices.Application m_app = sender as Autodesk.Revit.ApplicationServices.Application;

}

Message 3 of 5

david_rock
Enthusiast
Enthusiast

Thanks,

 

That did the trick Smiley Happy

 

Kind Regards

David

 

 

0 Likes
Message 4 of 5

m.vallee
Advocate
Advocate

Hi @FAIR59 @david_rock 

 

I have the exact same problem. I tried to use the "ApplicationInitialized" event trick to get a valid Autodesk.Revit.ApplicationServices.Application object in the OnStartup() method of my IExternalCommandData.

 

The trouble is the event is called (so the object is instantiated) after the call to OnStartup, so it is null when looking for its LoginUserId.

Did you move all the Entitlement portion code in the event handler ?

Because in that case, you cannot abort ribbon creation anymore if the user is not entitled for the app ?

 

Thanks for your help.

Maxime

 

0 Likes
Message 5 of 5

FAIR59
Advisor
Advisor

In the ApplicationInitialized  method, you can hide the newly created Button's, Panel's and Tabs if user is not Entitled.

// OnStartUp()
application.CreateRibbonTab("MyTabName");
RibbonPanel m_ribbonPanel = application.CreateRibbonPanel("MyTabName", "MyPanel");
string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
PushButtonData buttonData = new PushButtonData("MyButton",
               string.Format("X"), thisAssemblyPath, "FAIR_Space.My_cmd");
// ControlledApplication_ApplicationInitialized()
adWin.RibbonControl ribbon = adWin.ComponentManager.Ribbon;
adWin.RibbonTab MyTab = null;
adWin.RibbonPanel MyPanel = null;
adWin.RibbonItem MyButton = null;
foreach (var tab in ribbon.Tabs)
{
    if (tab.Id == "MyTabName")
    {
        MyTab = tab;
        foreach (var panel in tab.Panels)
        {
            if (panel.Source.Title == "MyPanel")
            {
                MyPanel = panel;
                foreach (var item in panel.Source.Items)
                {
                    if (item.Id == "CustomCtrl_%CustomCtrl_%My_TabName%MyPanel%MyButton")
                    {
                        MyButton = item;
                        break;
                     }
                }
            }
        }
        break;
    }
}
if (MyPanel != null && MyButton != null && MyTab != null )
{
    MyButton.IsVisible = false;
    MyPanel.IsVisible = false;
    MyTab.IsVisible = false;
}
0 Likes