Get app.VersionName in ControlledApplication Event?

Get app.VersionName in ControlledApplication Event?

Anonymous
Not applicable
1,136 Views
5 Replies
Message 1 of 6

Get app.VersionName in ControlledApplication Event?

Anonymous
Not applicable

How does one get the application in the event to call the VersionName?  

Reference the bottom of the code: 

var appver = ctrlApp.VersionName;

 

 

 

 using something
 
 namespace something
 {   
    public class ExtDbAppEventLogger : IExternalDBApplication
    {
        public static ControlledApplication _cachedCtrlApp;

        //IExternalApplication Members

        public ExternalDBApplicationResult OnStartup(ControlledApplication ctrlApp)
        {
            try
            {
                _cachedCtrlApp = ctrlApp;
                _cachedCtrlApp.DocumentClosed += new EventHandler<DocumentClosedEventArgs>(CachedCtrlApp_DocumentClosed);
                return ExternalDBApplicationResult.Succeeded;
            }
            catch (Exception ex)
            {
                return ExternalDBApplicationResult.Failed;
            }
        }

        void CachedCtrlApp_DocumentClosed(object sender, DocumentClosedEventArgs e)
        {
            using (StreamWriter sw = new StreamWriter(@"\\Server\files\log.txt", true))
            {
                var appver = ctrlApp.VersionName;
                sw.WriteLine(appver);
            }
        }
    }

}

 

 

 

Thanks 

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

Anonymous
Not applicable

Do I pass the application from the OnStartup as an arg in the call for the CachedCtrlApp_DocumentClosed?

 

_cachedCtrlApp.DocumentClosed += new EventHandler<DocumentClosedEventArgs>(CachedCtrlApp_DocumentClosed(ctrlApp);

 

 

 

CachedCtrlApp_DocumentClosed(object sender, DocumentClosedEventArgs e, ControlledApplication appver)

 

I am very new to C# and programming in general. I apologize if this is all way off base. Thank you for reading.

 

0 Likes
Message 3 of 6

Anonymous
Not applicable
Accepted solution

string xxx = _cachedCtrlApp.VersionName.ToString();

 

using System;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;


namespace MyNamespace
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExtDbAppEventLogger : IExternalDBApplication
    {
        #region Cached Variables

        public static ControlledApplication _cachedCtrlApp;

        #endregion

        #region IExternalApplication Members

        public ExternalDBApplicationResult OnStartup(ControlledApplication ctrlApp)
        {
            try
            {
                _cachedCtrlApp = ctrlApp;

                _cachedCtrlApp.DocumentCreated += new EventHandler<DocumentCreatedEventArgs>(CachedCtrlApp_DocumentCreated);

                _cachedCtrlApp.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(CachedCtrlApp_DocumentOpened);
                _cachedCtrlApp.DocumentClosed += new EventHandler<DocumentClosedEventArgs>(CachedCtrlApp_DocumentClosed);

                return ExternalDBApplicationResult.Succeeded;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return ExternalDBApplicationResult.Failed;
            }
        }

        void CachedCtrlApp_DocumentClosed(object sender, DocumentClosedEventArgs e)
        {
            string xxx = _cachedCtrlApp.VersionBuild.ToString();
            string yyy = _cachedCtrlApp.Product.ToString();
            using (StreamWriter sw = new StreamWriter(@"\\Server\log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString() + ", " + Environment.UserName + ", Closed, " + xxx + ", " + yyy + ", " + e.DocumentId.ToString());
            }
        }

        void CachedCtrlApp_DocumentOpened(object sender, DocumentOpenedEventArgs e)
        {
            string xxx = _cachedCtrlApp.VersionBuild.ToString();
            string yyy = _cachedCtrlApp.Product.ToString();
            using (StreamWriter sw = new StreamWriter(@"\\Server\log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString() + ", " + Environment.UserName + ", Opened, " + xxx + ", " + yyy + ", " + e.Document.PathName);
            }
        }

        void CachedCtrlApp_DocumentCreated(object sender, DocumentCreatedEventArgs e)
        {
            string xxx = _cachedCtrlApp.VersionBuild.ToString();
            string yyy = _cachedCtrlApp.Product.ToString();
            using (StreamWriter sw = new StreamWriter(@"\\Server\log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString() + ", " + Environment.UserName + ", Created, " + xxx + ", " + yyy + ", " + e.Document.PathName);
            }
        }

        public ExternalDBApplicationResult OnShutdown(ControlledApplication ctlApp)
        {
            try
            {

                return ExternalDBApplicationResult.Succeeded;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return ExternalDBApplicationResult.Failed;
            }
        }

        #endregion       
    }
}
0 Likes
Message 4 of 6

RPTHOMAS108
Mentor
Mentor

You can access version via UIApplication.Application.Version (you don't need to store UIControledApplication) you can get from event sender in a lot of cases.

 

Feel sorry for the guy that has to read log.txt here. Also should consider that this approach may cause some file access issues if you have multiple users all with this addin opening/closing/creating.

 

A tally system could be better for this:

Each user add-in writes a file to the network.

At the top of the file you assign an integer to each version number of Revit found to open i.e. each time Revit starts it looks through the versions already recorded and adds a new one if not found.

At the same time you store a table of events by that user e.g.

 

Ver, Revit2020, 0

Ver, Revit2021, 1

0 , 2 , 2,  8

1, 14, 14, 9

 

First column: if not a number is a definition of Revit version

First column: if a number is the version id

Second column: number of opening events

Third column: number of closing events (do we expect it to be different from number of opening I don't know)

Fourth column: number of creating events

 

This is more advanced logging it requires first reading the log and then replacing it. One file for every user means no file access violation exceptions (I don't expect you to read them all manually).

 

Since you are not recording dates then the information collected is almost the same. The only difference is sequence i.e. you will never know for sure close follows open for a version and what session that was in but are you really going to trawl through hundreds of almost identical lines (without timestamp) to find that out?

Message 5 of 6

Anonymous
Not applicable

@RPTHOMAS108,
Thank you for your post! It is folks like you taking the time to lend a hand that makes the community amazing. Thank you for the mention of the file access issue. I will work out a solution for this next. I am working through a proof of concept for my first Revit add-in. It continues to grow and improve by the day. With the help of the community, I am confident the end result prior to launching it to several users will be good.

 

I am off to investigate the event sender that you mentioned can provide the needed access to get the properties. Stay tuned.

0 Likes
Message 6 of 6

RPTHOMAS108
Mentor
Mentor

Thanks

 

In the end the version properties you are looking for are actually found on:


Autodesk.Revit.ApplicationServices.Application

 

Senders of the below events were found to be:

DocumentClosed: sender = Autodesk.Revit.ApplicationServices.Application
DocumentCreated: sender = Autodesk.Revit.ApplicationServices.Application
DocumentOpened: sender = Autodesk.Revit.ApplicationServices.Application

(subscribed to within IExternalApplication however)

 

If you end up with UIApplication anywhere you can still get:
Autodesk.Revit.ApplicationServices.Application
from
UIApplication.Application

 

If you are dealing with UIControlledApplication you can get:

Autodesk.Revit.ApplicationServices.ControlledApplication
From
UIControlledApplication.ControlledApplication

Which contains same version properties

 

So largely not a problem getting at version properties from anywhere.