2

While executing IExternalCommand i can easily obtain the Application and Document via ExternalCommandData

        UIApplication uiApp = commandData.Application;
        Document doc = uiApp.ActiveUIDocument.Document;
        Transaction trans = new Transaction(doc);

While executing IExternalApplication, there is no ExternalCommandData object. I need to find the path of the currently opened Revit file. How do I gain access to Document via IExternalApplication?

Skinner
  • 1,461
  • 4
  • 17
  • 27

2 Answers2

4

You can also do this (in IExternalApplication.OnStartup), but it relies on undocumented features of the UIControlledApplication object. I have used this technique from Revit 2012 through 2017, so I guess it is a stable assumption for now:

var versionNumber = uiControlledApplication.ControlledApplication.VersionNumber;
var fieldName = versionNumber == "2017" ? "m_uiapplication" : "m_application";
var fi = uiControlledApplication.GetType().GetField(
    fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
var uiApplication = (UIApplication)fi.GetValue(uiControlledApplication);

```

The idea is to use introspection to access the non-public field (m_uiapplication) of the UIControlledApplication object. This is of type UIApplication.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
2

Well, This worked. Calling onViewActivated, document is stored in e

public class AddPanel : IExternalApplication
{

    void onViewActivated(object sender, ViewActivatedEventArgs e)
    {
        View vCurrent = e.CurrentActiveView;
        Document doc = e.Document;
        string pathname = doc.PathName;
        TaskDialog.Show("asd", pathname);
        string id = Convert.ToString(vCurrent.Id);
        string name = vCurrent.Name;
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        string now = Convert.ToString(DateTime.Now);
        string content = now + ", " + id + ", " + name + ", " + userName + "\n";

        string path = @"E:\H1503200 Montreign Resort Casino\3-CD\views.txt";
        using (System.IO.StreamWriter sw = System.IO.File.AppendText(path))
        {
            sw.WriteLine(content);
        }
    }

    // Both OnStartup and OnShutdown must be implemented as public method
    public Result OnStartup(UIControlledApplication application)
    {
        // Add a new ribbon panel
        RibbonPanel ribbonPanel = application.CreateRibbonPanel("JCJ Addin");

        // Create a push button to trigger a command add it to the ribbon panel. 
        string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
        PushButtonData buttonData = new PushButtonData("SheetsToUpper",
           "Sheets\n To Upper", thisAssemblyPath, "SheetsToUpper");
        PushButtonData buttonData1 = new PushButtonData("ViewsToUpper",
           "Views\n To Upper", thisAssemblyPath, "ViewsToUpper");
        PushButtonData buttonData2 = new PushButtonData("RenumberViews",
           "Renumber\n Views on\nSheet", thisAssemblyPath, "RenumberViews");
        PushButtonData buttonData3 = new PushButtonData("viewerLocations",
           "Find\n View on\nInstances", thisAssemblyPath, "viewerLocations");

        PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;
        PushButton pushButton1 = ribbonPanel.AddItem(buttonData1) as PushButton;
        PushButton pushButton2 = ribbonPanel.AddItem(buttonData2) as PushButton;
        PushButton pushButton3 = ribbonPanel.AddItem(buttonData3) as PushButton;

        // Optionally, other properties may be assigned to the button
        // a) tool-tip
        pushButton.ToolTip = "Converts all the text in Sheet titles to uppercase - Global Operation.";
        pushButton1.ToolTip = "Converts all the text in View titles to uppercase - Global Operation.";
        pushButton2.ToolTip = "Select all views in the order you want them re-numbered.";
        pushButton3.ToolTip = "Select View.";
        // b) large bitmap
        Uri uriImage = new Uri(@"H:\!PRACTICE GROUPS\Revit Scripts\icon-font-theme-lowercase-uppercase.png");
        Uri uriImage1 = new Uri(@"H:\!PRACTICE GROUPS\Revit Scripts\icon-font-theme-lowercase-uppercase.png"); 
        Uri uriImage2 = new Uri(@"H:\!PRACTICE GROUPS\Revit Scripts\icon-font-theme-lowercase-uppercase.png");
        Uri uriImage3 = new Uri(@"H:\!PRACTICE GROUPS\Revit Scripts\icon-font-theme-lowercase-uppercase.png");

        BitmapImage largeImage = new BitmapImage(uriImage);
        BitmapImage largeImage1 = new BitmapImage(uriImage1);
        BitmapImage largeImage2 = new BitmapImage(uriImage2);
        BitmapImage largeImage3 = new BitmapImage(uriImage3);

        pushButton.LargeImage = largeImage;
        pushButton1.LargeImage = largeImage1;
        pushButton2.LargeImage = largeImage2;
        pushButton3.LargeImage = largeImage3;

        application.ViewActivated += new EventHandler<Autodesk.Revit.UI.Events.ViewActivatedEventArgs>(onViewActivated);

        return Result.Succeeded;
    }

    public Result OnShutdown(UIControlledApplication application)
    {
        // nothing to clean up in this simple case
        return Result.Succeeded;
    }
}
Skinner
  • 1,461
  • 4
  • 17
  • 27