【问题标题】:Getting the current Application and Document from IExternalApplication - Revit从 IExternalApplication 获取当前应用程序和文档 - Revit
【发布时间】:2017-02-23 10:21:01
【问题描述】:

在执行IExternalCommand时,我可以通过ExternalCommandData轻松获取应用程序和文档

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

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

【问题讨论】:

    标签: c# api revit


    【解决方案1】:

    您也可以这样做(在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对象的非公共字段(m_uiapplication)。这是UIApplication 类型。

    【讨论】:

      【解决方案2】:

      嗯,这行得通。调用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\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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-03
        • 2011-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        相关资源
        最近更新 更多