We talked about Revit Application and ControlledApplication previously. In this article, let us have a look at the Revit Application and UIApplication.
The UIApplication has an Application property so we can get the Application instance from an UIApplication object.
The UIApplication has a constructor which accepts an Application argument. So it seems we can create a Revit UIApplication object with some code like the following in VB.NET:
Dim app As New Autodesk.Revit.ApplicationServices.Application()
Dim uiApp As New Autodesk.Revit.UI.UIApplication(app)
MessageBox.Show("LoadedApplications count: " + uiApp.LoadedApplications.Size.ToString())
But the constructor would throw out an exception:
“Autodesk.Revit.Exceptions.InvalidObjectException: Object has not been initialized yet.”
The following counterpart C# code:
Autodesk.Revit.ApplicationServices.Application app = new Autodesk.Revit.ApplicationServices.Application();
Autodesk.Revit.UI.UIApplication uiApp = new Autodesk.Revit.UI.UIApplication(app);
MessageBox.Show("LoadedApplications count: " + uiApp.LoadedApplications.Size.ToString());
throws out the same exception.
What’s going on here?
We did similar things before, didn’t we?
OK, got it. We provided some real Revit Application to the constructor of the UIApplication before instead of one constructed from the Application itself:
Code in VB.NET:
Autodesk.Revit.UI.UIApplication uiApp = new Autodesk.Revit.UI.UIApplication(cmdData.Application.Application);
MessageBox.Show("LoadedApplications count: " + uiApp.LoadedApplications.Size.ToString());
Code in C#:
Dim uiApp As New Autodesk.Revit.UI.UIApplication(cmdData.Application.Application)
MessageBox.Show("LoadedApplications count: " + uiApp.LoadedApplications.Size.ToString())
This time the code works well, no exceptions anymore and the count of loaded applications being correctly reported.
So, we have to amend what we said last time about creating an Application explicitly. The constructor of the Application does not really create or retrieve the same Revit application instance as the ExternalCommandData provides us. At least the constructor of the UIApplication does not think so.
Then people may wonder why the need to go a long way then back to the same thing?
UIApplication -> Application -> UIApplication
Maybe to address a situation like the UIApplication is lost after some long journey?
RevitAddinWizard uses the working version regarding UIApplication construction in its wizards, coders and widgets when necessary.
Our Software http://netspiderstudio.com/Software.html
Support: mailto:[email protected]
Query: mailto:[email protected]
Blog: http://spiderinnet.typepad.com
More: http://netspiderstudio.com
Is is possible to create the New instance for the UIApplication with out launching the revit application. I have to read file attributes silently without launching the application in batch mode.
Posted by: Bangaruraju | 08/04/2016 at 09:28 AM
Bangaruraju, without seeing more details it's hard to say possible or not. For good and common coding practice with Revit API, keeping both the Application and UIApplication in global variables look the only good way. Revit .NET Addin Wizard follows that practice.
Posted by: Spiderinnet | 08/04/2016 at 03:00 PM
Thank You for the reply.
My Requirement is to develop a standalone application to read the Revit file attributes and update to the spread sheet. Mainly i need to extract the Floors and Spaces information from the Revit file and export to a external spread sheet.
This should be done with Revit .NET Standalone application not the Addin.
Posted by: Bangaruraju | 08/10/2016 at 01:39 AM
Bangaruraju, thanks for the further info. Unfortunately it does not see possible. Revit .NET API is not designed to be used outside of Revit itself. That is, it can only be used to create addins for Revit instead of standalone Windows applications.
Posted by: Spiderinnet | 08/10/2016 at 02:21 AM
Thanks for the clarification.
are there any other third party applications or API or SDK etc. Which can be used to extract BIM information from the Revit files?
This may be with or without Revit application installed.
Posted by: Bangaruraju | 08/10/2016 at 04:14 AM
Bangaruraju, as far as we know, there is no such an APP/SDK.
Posted by: Spiderinnet | 08/11/2016 at 03:02 AM