从 IExternalApplication 获取当前应用程序和文档 - Revit

Getting the current Application and Document from IExternalApplication - Revit

在执行 IExternalCommand 时,我可以通过 ExternalCommandData

轻松获取应用程序和文档
        UIApplication uiApp = commandData.Application;
        Document doc = uiApp.ActiveUIDocument.Document;
        Transaction trans = new Transaction(doc);

执行IExternalApplication时,没有ExternalCommandData对象。我需要找到当前打开的 Revit 文件的路径。如何通过 IExternalApplication 访问 Document

好吧,这成功了。调用 onViewActivated,文档存储在 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-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;
    }
}

您也可以这样做(在 IExternalApplication.OnStartup 中),但它依赖于 UIControlledApplication 对象的未记录的功能。我从 Revit 2012 到 2017 一直使用这种技术,所以我想这是一个稳定的假设:

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);

```

想法是使用内省来访问 UIControlledApplication 对象的非 public 字段 (m_uiapplication)。这是 UIApplication.

类型