下載app免費(fèi)領(lǐng)取會(huì)員
網(wǎng)友投稿
更多Revit開(kāi)發(fā)中AddinManager是一個(gè)非常好用的工具,它可以快速的運(yùn)行我們寫(xiě)好的命令
并且生成addin文件,最近突然好奇,它是怎么實(shí)現(xiàn)這些功能的,然后研究了一下,
發(fā)現(xiàn)這里面似乎是通過(guò)反射來(lái)實(shí)現(xiàn)的,他能運(yùn)行我們的寫(xiě)的命令最關(guān)鍵的是因?yàn)橄旅娴拇a
IExternalCommand externalCommand = assembly.CreateInstance(FullClassName) as IExternalCommand;
if (externalCommand != null)
{
result = externalCommand Execute( commandData, ref message, elements)
}
因?yàn)橛梅瓷鋭?chuàng)建我們寫(xiě)的命令的類之前,我們要先加載dll,而加載dl后,如果加載dll的位置和我們生成dll的位置相同
當(dāng)我們?cè)倬幾g原來(lái)的工程就會(huì)失敗,因?yàn)槲覀兩傻膁ll要替換原來(lái)的dll,而原來(lái)的dll已經(jīng)被占用,所有就會(huì)替換失敗,
我想著就是AddInManager 運(yùn)行命令之前,要先把dll文件復(fù)制到其他地方的原因吧
下面嘗試些一個(gè)簡(jiǎn)單的AddInManager,并不包括將dll復(fù)制到其他地方,和生成addin文件的功能
//創(chuàng)建界面
public class MyApp:IExternalApplication
{
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
RibbonPanel panel = application.CreateRibbonPanel(Tab.AddIns, "NewAddInManager");
PushButtonData data = new PushButtonData("NewAddinManager", "MyCommand", this.GetType().Assembly.Location, "NewAddinManager.MyAddInCommand");
panel.AddItem(data);
return Result.Succeeded;
}
}
// addinManager 命令
public class MyAddInCommand : IExternalCommand
{
public string path = null;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
AppDomain.CurrentDomain.AssemblyResolve += LoadAssemble;
Result result;
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string file = dialog.FileName;
path = new FileInfo(file).DirectoryName;
Assembly ab = Assembly.LoadFile(file);
foreach (Type tp in ab.DefinedTypes)
{
IExternalCommand icmd = ab.CreateInstance(tp.FullName) as IExternalCommand;
if (icmd != null)
{
result = icmd.Execute(commandData, ref message, elements);
break;
}
}
}
return Result.Succeeded;
}
private Assembly LoadAssemble(object sender, ResolveEventArgs args)
{
string dllName = args.Name.Split(new char[] { ',' }).First();
Assembly tab = args.RequestingAssembly;
FileInfo fileInfo = new FileInfo(tab.Location);
DirectoryInfo dir = fileInfo.Directory;// new DirectoryInfo(path);
FileInfo[] files = dir.GetFiles("*.dll", SearchOption.TopDirectoryOnly);
foreach (FileInfo f in files)
{
if (f.Name.Contains(dllName))
{
Assembly ab = Assembly.LoadFile(f.FullName);
return ab;
}
}
return null;
}
}
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。
上一篇:二次開(kāi)發(fā)教程:Revit開(kāi)發(fā)之梁柱分段
下一篇:二次開(kāi)發(fā)教程:Revit開(kāi)發(fā)之AddInManager安裝包簡(jiǎn)單制作
推薦專題