forked from mcneel/rhino.inside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
87 lines (71 loc) · 2.33 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.IO;
using System.Reflection;
// This line is not mandatory, but improves loading performances
[assembly: ExtensionApplication(typeof(RhinoInside.AutoCAD.Plugin))]
namespace RhinoInside.AutoCAD
{
// This class is instantiated by AutoCAD once and kept alive for the
// duration of the session. If you don't do any one time initialization
// then you should remove this class.
public class Plugin : IExtensionApplication
{
private Rhino.Runtime.InProcess.RhinoCore m_rhino_core;
#region Plugin static constructor
static Plugin()
{
ResolveEventHandler OnRhinoCommonResolve = null;
AppDomain.CurrentDomain.AssemblyResolve += OnRhinoCommonResolve = (sender, args) =>
{
const string rhino_common_assembly_name = "RhinoCommon";
var assembly_name = new AssemblyName(args.Name).Name;
if (assembly_name != rhino_common_assembly_name)
return null;
AppDomain.CurrentDomain.AssemblyResolve -= OnRhinoCommonResolve;
var rhino_system_dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Rhino WIP", "System");
return Assembly.LoadFrom(Path.Combine(rhino_system_dir, rhino_common_assembly_name + ".dll"));
};
}
#endregion // Plugin static constructor
#region IExtensionApplication Members
void IExtensionApplication.Initialize()
{
// Load Rhino
try
{
var scheme_name = ProductName().Replace(' ', '-');
m_rhino_core = new Rhino.Runtime.InProcess.RhinoCore(new[] { $"/scheme={scheme_name}" });
}
catch
{
// ignored
}
}
void IExtensionApplication.Terminate()
{
try
{
m_rhino_core?.Dispose();
}
catch
{
// ignored
}
}
#endregion // IExtensionApplication Members
#region Static Functions
private static string ProductName()
{
var root_key = HostApplicationServices.Current.MachineRegistryProductRootKey;
string product_name = null;
using (var key = Registry.LocalMachine.OpenSubKey(root_key, false))
{
product_name = key.GetValue("ProductName") as string;
}
return product_name;
}
#endregion // Static Functions
}
}