Automatically OpenAndActivate a cloud model on Revit startup

Automatically OpenAndActivate a cloud model on Revit startup

Rockit_for_Revit
Advocate Advocate
245 Views
3 Replies
Message 1 of 4

Automatically OpenAndActivate a cloud model on Revit startup

Rockit_for_Revit
Advocate
Advocate

Hello,

I'm trying to automatically OpenAndActivate a cloud model. Given I need to access UIApplication I am doing it via UIControlledApplication.Idling event.

The command works fine when I open a local Revit model. However when I open a cloud model I am left with the following dialog 'Open Model' indefinitely and I have no option but to kill the Revit session. 

 

Rockit_for_Revit_0-1738498888645.png

I am opening the cloud model using the following:

Dim modelPath As ModelPath = ModelPathUtils.ConvertCloudGUIDsToCloudPath(If(region, ModelPathUtils.CloudRegionUS), ' Default to US region if not provided
    New Guid(projectGUID),
    New Guid(modelGUID)
)
Dim openOptions As New OpenOptions()
Dim objUIdocument As UIDocument = uiApp.OpenAndActivateDocument(modelPath, openOptions, False)

Any ideas?

 

0 Likes
Accepted solutions (1)
246 Views
3 Replies
Replies (3)
Message 2 of 4

ricaun
Advisor
Advisor
Accepted solution

Maybe it's Idling, it's usually not safe to run long processes in there.

 

You could use ExternalEvent and verify if that works.

 

Some sample code like this:

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;

namespace RevitAddin.Forum
{
    public class AppCloud : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication application)
        {
            var region = ModelPathUtils.CloudRegionUS;
            var projectGuid = Guid.Parse("00000000-0000-0000-0000-000000000000");
            var modelGuid = Guid.Parse("00000000-0000-0000-0000-000000000000");

            // Create ExternalEvent and Raise to execute in the next idle cycle
            ExternalEvent.Create(new CloudOpenExternalEvent(region, projectGuid, modelGuid)).Raise();
            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
    }

    public class CloudOpenExternalEvent : IExternalEventHandler
    {
        private string Region;
        private Guid ProjectGuid;
        private Guid ModelGuid;

        public CloudOpenExternalEvent(string region, Guid projectGuid, Guid modelGuid)
        {
            Region = region;
            ProjectGuid = projectGuid;
            ModelGuid = modelGuid;
        }
        public void Execute(UIApplication uiapp)
        {
            try
            {
                var modelPath = ModelPathUtils.ConvertCloudGUIDsToCloudPath(Region, ProjectGuid, ModelGuid);
                var options = new OpenOptions();
                uiapp.OpenAndActivateDocument(modelPath, options, false);
            }
            catch (Exception ex)
            {
                TaskDialog.Show(GetName(), ex.ToString());
            }
        }
        public string GetName()
        {
            return nameof(CloudOpenExternalEvent);
        }
    }
}

 

 

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Message 3 of 4

Rockit_for_Revit
Advocate
Advocate

Perfect thank you so much. I needed to seperate it out of OnStartup, however its essentally the same:

    Private Shared _externalEvent As ExternalEvent

    Public Sub CreateAndRaiseExternalEvent(filePath As String)
        _externalEvent = ExternalEvent.Create(New ExternalEvent(filePath))
        _externalEvent.Raise()
    End Sub
0 Likes
Message 4 of 4

jeremy_tammik
Alumni
Alumni

OnStartup is useful to register event handlers and set up add-ins and stuff, but it is not the optimal time and place to start executing code. ApplicationInitialized is a later and probably more suitable choice:

  

  

Subscribe to this event to get notified after the Revit application has been initialized.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes