Created
July 25, 2013 23:26
-
-
Save zmic/6084730 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Mine | |
{ | |
// helper class | |
public class PluginData | |
{ | |
public DateTime _creation_time; | |
public Autodesk.Revit.UI.IExternalCommand _instance; | |
public PluginData(Autodesk.Revit.UI.IExternalCommand instance) | |
{ | |
_instance = instance; | |
} | |
} | |
// | |
// Base class for auto-reloading external commands that reside in other dll's | |
// (that Revit never knows about, and therefore cannot lock) | |
// | |
public class AutoReload : Autodesk.Revit.UI.IExternalCommand | |
{ | |
// keep a static dictionary of loaded modules (so the data persists between calls to Execute) | |
static System.Collections.Generic.Dictionary<string, PluginData> _dictionary; | |
String _path; // to the dll | |
String _class_full_name; | |
public AutoReload(String path, String class_full_name) | |
{ | |
if (_dictionary == null) | |
{ | |
_dictionary = new System.Collections.Generic.Dictionary<string, PluginData>(); | |
} | |
if (!_dictionary.ContainsKey(class_full_name)) | |
{ | |
PluginData data = new PluginData(null); | |
_dictionary.Add(class_full_name, data); | |
} | |
_path = path; | |
_class_full_name = class_full_name; | |
} | |
public Autodesk.Revit.UI.Result Execute( | |
Autodesk.Revit.UI.ExternalCommandData commandData, | |
ref string message, | |
Autodesk.Revit.DB.ElementSet elements) | |
{ | |
PluginData data = _dictionary[_class_full_name]; | |
DateTime creation_time = new System.IO.FileInfo(_path).LastWriteTime; | |
if (creation_time.CompareTo(data._creation_time) > 0) | |
{ | |
// dll file has been modified, or this is the first time we execute this command. | |
data._creation_time = creation_time; | |
byte[] assembly_bytes = System.IO.File.ReadAllBytes(_path); | |
System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assembly_bytes); | |
foreach (Type type in assembly.GetTypes()) | |
{ | |
if (type.IsClass && type.FullName == _class_full_name) | |
{ | |
data._instance = Activator.CreateInstance(type) as Autodesk.Revit.UI.IExternalCommand; | |
break; | |
} | |
} | |
} | |
// now actually call the command | |
return data._instance.Execute(commandData, ref message, elements); | |
} | |
} | |
// | |
// Derive a class from AutoReload for every auto-reloadable command. Hardcode the path | |
// to the dll and the full name of the IExternalCommand class in the constructor of the base class. | |
// | |
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] | |
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)] | |
public class AutoReloadExample : AutoReload | |
{ | |
public AutoReloadExample() | |
: base("C:\\revit2014plugins\\ExampleCommand.dll", "Mine.ExampleCommand") | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment