2017-02-22 47 views
1

在執行IExternalCommand我可以容易地獲得應用程序和文檔經由ExternalCommandData獲取從IExternalApplication當前應用程序和文檔 - 的Revit

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

在執行IExternalApplication,沒有ExternalCommandData對象。我需要找到當前打開的Revit文件的路徑。如何通過IExternalApplication訪問Document

回答

1

嗯,這工作。調用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; 
    } 
} 
0

你也可以做到這一點(在IExternalApplication.OnStartup),但它依賴於UIControlledApplication對象的未記錄的功能。我已經經歷了2017年使用從2012年的Revit這種技術,所以我想這是現​​在穩定的假設:

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

```

的想法是用內省來訪問非公開領域( m_uiapplication)的UIControlledApplication對象。這是UIApplication的類型。

相關問題