Open and close models from OnStartUp

Open and close models from OnStartUp

thomasbjorkroth
Explorer Explorer
933 Views
5 Replies
Message 1 of 6

Open and close models from OnStartUp

thomasbjorkroth
Explorer
Explorer

I'm trying to loop over a list of models, open each model, make an export and then close the model before going to the next one. 

It all works fine if I call the OpenModels-class from and external command in Revit via Add-In-Manager, but if I call it when Revit starts up it stops after the first model is opened and export is made, not continuing to the next model.

 

My guess is that the UIApplication is created in IExternalApplication is different then the one in IExternalCommand?

 

From my ExternalApplication

public Result OnStartup(UIControlledApplication a)
        {
            a.ControlledApplication.ApplicationInitialized += OnApplicationInitialized;

            return Result.Succeeded;
        }

        void OnApplicationInitialized(object sender, ApplicationInitializedEventArgs e)
        {
            Application app = sender as Application;
            UIApplication uiApplication = new UIApplication(app);
            new OpenModels().Open(uiApplication);
        }

In OpenModels

  public class OpenModels
    {
        public void Open(UIApplication uiApplication)
        {
            var modelsToCheck = [];

            foreach (var modelToCheck in modelsToCheck)
            {
                if (modelToCheck.IsWorkshared)
                {
                    var modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(modelToCheck.ModelFilePath);

                    document = _uiApplication.OpenAndActivateDocument(modelPath, openOptions, false);
                }
                else
                {
                    document = _uiApplication.OpenAndActivateDocument(modelToCheck.ModelFilePath);
                }
//
//Exports are made
// if (modelToCheck.IsWorkshared) { _uiApplication.ActiveUIDocument.Document.SaveAs(saveModelPath, saveAsOptions); else { _uiApplication.ActiveUIDocument.Document.SaveAs(savePath, saveAsOptions); } } } }

 

 

 

0 Likes
934 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk

OnStartup is too early.

 

Revit is not ready yet.

 

Wait for the ApplicationInitialized event instead:

 

https://www.revitapidocs.com/2020/f35ba9fc-0b6b-4284-60eb-91788761127c.htm

 

Alternatively, subscribe to the Idling event.

 

When that is called, Revit is ready to do things, including executing your add-in code.

 

Ooops, re-reading your question, I see you are already doing this.

 

Yes, they may indeed be different instances. I ran into such an issue once.

 

Be careful which instance you use.

 

I suggest you maintain one single singleton UIApplication instance and reuse that globally in all your classes.

 

Your code looks quite sensible to me. I see no fundamental problems with it.

  

Good luck sorting it out, and please do let us know how you end up solving this.

  

Thank you!

  



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 6

FAIR59
Advisor
Advisor

I put the whole foreach-loop in a try-catch block and got this message:

CatchMessage.PNG

I think you'll have to do the "work" in an External Command

Message 4 of 6

thomasbjorkroth
Explorer
Explorer

I got that error now as well. 

Do you have any suggestions on how I could call my class that opens the models, that isn't in an event handler. 

 

I would like to automate this task to 100%, so it would involve opening Revit at a certain time at night, performing these exports from a list of models, and then closing again. 

 

As you said, it is working in and ExternalCommand but I can't run an external command when Revit is starting up right? Or can I?

 

Thanks for both of your inputs.

0 Likes
Message 5 of 6

thomasbjorkroth
Explorer
Explorer

Since both OnApplicationInitialized and the Idling Event are EventHandlers I cant perform the change of Active Document in Revit. 

 

A couple of questions:

Can I call my class that performs the opening and exporting from another place, or write it in another way so I can do this soon after Revit has started?

 

I guess that ExternalCommands cannot be run straight at startup?

0 Likes
Message 6 of 6

jeremytammik
Autodesk
Autodesk

You can subscribe to the Idling event during startup.

 

In the Idling event, you can modify the model.

 

I was unaware that you cannot open a new model there.

 

However, if you must indeed be in an external command to open a new model, you can do so as follows:

 

  • Implement your external command that opens a model.
  • In the Idling event handler, unsubscribe from Idling and call PostCommand to launch your external command.

 

In your external command, if you wish to process another file, resubscribe to Idling again.

 

Another idea: In your external command, if you wish to process another file, call PostCommand there, last thing you do before returning.

 

Ah, no, why should you.

 

In your external command, simply process all your files in a loop.

 

I really don't see what all the complexity is about.

 

What are you actually trying to achieve?

 

The Building Coder has processed several articles on batch processing documents.

 

Some of them are listed in the topic group on idling:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 

 

 

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes