C# 从IExternalApplication-Revit获取当前应用程序和文档
执行C# 从IExternalApplication-Revit获取当前应用程序和文档,c#,api,revit,C#,Api,Revit,执行IExternalCommand时,我可以通过ExternalCommandData UIApplication uiApp = commandData.Application; Document doc = uiApp.ActiveUIDocument.Document; Transaction trans = new Transaction(doc); 执行IExternalApplication时,没有ExternalCommandDa
IExternalCommand
时,我可以通过ExternalCommandData
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
Transaction trans = new Transaction(doc);
执行
IExternalApplication
时,没有ExternalCommandData
对象。我需要找到当前打开的Revit文件的路径。如何通过IExternalApplication
访问文档
。调用启用视图的onview
,文档存储在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;
}
}
公共类添加面板:IExternalApplication
{
视图激活时无效(对象发送者、视图激活事件发送者)
{
视图vCurrent=e.CurrentActiveView;
文件文件=电子文件;
字符串路径名=doc.pathname;
TaskDialog.Show(“asd”,路径名);
字符串id=Convert.ToString(vCurrent.id);
字符串名称=vCurrent.name;
字符串用户名=System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string now=Convert.ToString(DateTime.now);
字符串内容=now+“,“+id+”,“+name+”,“+userName+”\n”;
字符串路径=@“E:\H1503200 Montreval Resort Casino\3-CD\views.txt”;
使用(System.IO.StreamWriter sw=System.IO.File.AppendText(路径))
{
软件编写线(内容);
}
}
//OnStartup和OnShutdown都必须作为公共方法实现
启动时的公共结果(uicontrolLED应用程序)
{
//添加新的功能区面板
RibbonPanel RibbonPanel=application.CreateRibbonPanel(“JCJ Addin”);
//创建按钮以触发命令将其添加到功能区面板。
字符串thisAssemblyPath=Assembly.GetExecutionGassembly().Location;
PushButtonData buttonData=新的PushButtonData(“SheetsToUpper”,
“板材到上部”,此装配路径,“板材到上部”);
PushButtonDa ButtonDa1=新的PushButtonDa(“ViewsToUpper”,
“视图\n到上方”,thisAssemblyPath,“视图到上方”);
PushButtonDa ButtonDa2=新的PushButtonDa(“重新编号视图”),
“在Sheet上重新编号\n视图”,thisAssemblyPath,“重新编号视图”);
PushButtonData buttonData3=新的PushButtonData(“viewerLocations”,
“查找实例上的视图”,thisAssemblyPath,“viewerLocations”);
按钮按钮=ribbonPanel.AddItem(buttonData)作为按钮;
按钮按钮1=带状面板。添加项目(按钮DA1)作为按钮;
按钮按钮2=带状面板。添加项目(按钮DA2)作为按钮;
按钮按钮3=带状面板。添加项目(按钮DA3)作为按钮;
//也可以将其他属性指定给按钮
//a)刀尖
putton.ToolTip=“将工作表标题中的所有文本转换为大写-全局操作。”;
pushButton1.ToolTip=“将视图标题中的所有文本转换为大写-全局操作。”;
pushButton2.ToolTip=“按照要重新编号的顺序选择所有视图。”;
按钮3.ToolTip=“选择视图。”;
//b)大位图
Uri URIMAGE=新Uri(@“H:\!PRACTICE GROUPS\Revit Scripts\icon font theme小写大写.png”);
Uri uriImage1=新Uri(@“H:\!PRACTICE GROUPS\Revit脚本\icon font theme小写大写.png”);
Uri uriImage2=新Uri(@“H:\!PRACTICE GROUPS\Revit Scripts\icon font theme小写大写.png”);
Uri uriImage3=新Uri(@“H:\!PRACTICE GROUPS\Revit脚本\icon font theme小写大写.png”);
BitmapImage largeImage=新的BitmapImage(uriImage);
BitmapImage largeImage1=新的BitmapImage(uriImage1);
BitmapImage largeImage2=新的BitmapImage(uriImage2);
BitmapImage largeImage3=新的BitmapImage(uriImage3);
按钮。大图像=大图像;
按钮1.largeImage1=largeImage1;
按钮2.largeImage2=largeImage2;
按钮3.largeImage3=largeImage3;
application.ViewActivated+=新的事件处理程序(onViewActivated);
返回结果。成功;
}
关闭时的公共结果(uicontrol LED应用程序)
{
//在这种简单的情况下,没有什么需要清理的
返回结果。成功;
}
}
您也可以这样做(在IExternalApplication.onStart
中),但它依赖于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
对象的非公共字段(muiapplication
)。这是UIApplication
类型