diff --git a/Sinvo.EplanHpD.Plugin/AppDomainDllLoader.cs b/Sinvo.EplanHpD.Plugin/AppDomainDllLoader.cs new file mode 100644 index 0000000..94fea4c --- /dev/null +++ b/Sinvo.EplanHpD.Plugin/AppDomainDllLoader.cs @@ -0,0 +1,71 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; + +namespace Sinvo.EplanHpD.Plugin +{ + public class AppDomainDllLoader + { + public static bool isLoaded = false; + public static void SetLaoder() + { + if (isLoaded) + { + return; + } + // 接管DLL加载,解决依赖问题 + // 插件被加载时,AppContext.BaseDirectory 会指向Eplan的安装目录,导致一部分依赖的DLL在加载时找不到 + // 接管之后,从两个目录都查找一次 + //throw new NotImplementedException(); + try + { + // 防止重复加载 先卸载事件 + AppDomain.CurrentDomain.AssemblyResolve -= AssemblyResolveHandle; + } + catch (Exception ex) + { + Trace.TraceError(ex.Message); + } + + AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandle; + isLoaded = true; + } + + public static Assembly AssemblyResolveHandle(object sender, ResolveEventArgs args) + { + var assemblyName = new AssemblyName(args.Name); + + // 判断是否已经被加载 + var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies() + .FirstOrDefault(a => a.FullName == assemblyName.FullName); + if (loadedAssembly != null) + { + Debug.WriteLine($"Dll is loaded -> {loadedAssembly.FullName}"); + return loadedAssembly; + } + + // 从当前路径下加载 + var path = Path.Combine(AppContext.BaseDirectory, $"{assemblyName.Name}.dll"); + if (File.Exists(path)) + { + Debug.WriteLine($"Load dll for path -> {path}"); + + return Assembly.LoadFile(path); + } + var envPath = System.Environment.CurrentDirectory; + // 从插件路径中加载依赖 + path = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(AppDomainDllLoader)).Location), $"{assemblyName.Name}.dll"); + if (File.Exists(path)) + { + //MessageBox.Show($"Load dll for self path -> {path}"); + Debug.WriteLine($"Load dll for self path -> {path}"); + return Assembly.LoadFile(path); + } + //MessageBox.Show(path); + Debug.WriteLine($"File not load! -> {path}"); + return null; + } + } +} diff --git a/Sinvo.EplanHpD.Plugin/DesignPluginEntry.cs b/Sinvo.EplanHpD.Plugin/DesignPluginEntry.cs new file mode 100644 index 0000000..440589b --- /dev/null +++ b/Sinvo.EplanHpD.Plugin/DesignPluginEntry.cs @@ -0,0 +1,64 @@ +using EPLAN.Harness.API; +using EPLAN.Harness.API.Plugins; +using EPLAN.Harness.API.Plugins.Core; +using Sinvo.EplanHpD.Plugin.WPFUI; +using System; +using System.Drawing; +using System.IO; +using System.Reflection; + +namespace Sinvo.EplanHpD.Plugin +{ + public class DesignPluginEntry : IHpDPlugin + { + public string Name => "兴禾ProD插件-3D"; + + public string Author => "Sinvo"; + + public string Description => "兴禾EPLAN Harness proD 2.9 Studio插件,提供线材数据抓取功能"; + + /// + /// 获取DLL版本 + /// + public Version Version => Assembly.GetAssembly(this.GetType()).GetName().Version; + + public System.Drawing.Image ToolbarIcon + { + get + { + var imageBytes = Resource.Sinvo; + if (imageBytes != null) + { + using (var ms = new MemoryStream(imageBytes)) + { + return Image.FromStream(ms); + } + } + return null; + } + } + + public string ToolbarText => "抓取线材数据"; + + public string ToolbarTooltip => "抓取线材数据"; + + public HpDModule Module => HpDModule.WorkSpace; + + public void Execute(HpdApi api) + { + var doc = api.CurrentProject.GetActiveDocument(); + new LectotypeWindow(doc.ID).Show(); + } + + public void Initialize() + { + AppDomainDllLoader.SetLaoder(); + } + + + public void Terminate() + { + //throw new NotImplementedException(); + } + } +} diff --git a/Sinvo.EplanHpD.Plugin/PluginEntry.cs b/Sinvo.EplanHpD.Plugin/PluginEntry.cs index 3058b6d..a9bd831 100644 --- a/Sinvo.EplanHpD.Plugin/PluginEntry.cs +++ b/Sinvo.EplanHpD.Plugin/PluginEntry.cs @@ -25,7 +25,7 @@ namespace Sinvo.EplanHpD.Plugin /// /// 获取DLL版本 /// - public Version Version => Assembly.GetExecutingAssembly().GetName().Version; + public Version Version => Assembly.GetAssembly(this.GetType()).GetName().Version; public System.Drawing.Image ToolbarIcon { @@ -56,6 +56,7 @@ namespace Sinvo.EplanHpD.Plugin try { var doc = api.CurrentProject.GetActiveDocument(); + //doc.ID //if(doc is FlexReport) if (doc is EPLAN.Harness.API.Projects.Documents.Report report) { @@ -140,6 +141,8 @@ namespace Sinvo.EplanHpD.Plugin public void Initialize() { //throw new NotImplementedException(); + AppDomainDllLoader.SetLaoder(); + } public void Terminate() diff --git a/Sinvo.EplanHpD.Plugin/Properties/AssemblyInfo.cs b/Sinvo.EplanHpD.Plugin/Properties/AssemblyInfo.cs index 71a2793..f372369 100644 --- a/Sinvo.EplanHpD.Plugin/Properties/AssemblyInfo.cs +++ b/Sinvo.EplanHpD.Plugin/Properties/AssemblyInfo.cs @@ -28,6 +28,6 @@ using System.Runtime.InteropServices; // 生成号 // 修订号 // -[assembly: AssemblyVersion("1.0.0.5")] -[assembly: AssemblyFileVersion("1.0.0.1")] -[assembly: AssemblyInformationalVersion("1.0.0.2")] \ No newline at end of file +[assembly: AssemblyVersion("1.0.0.6")] +[assembly: AssemblyFileVersion("1.0.0.6")] +[assembly: AssemblyInformationalVersion("1.0.0.6")] \ No newline at end of file diff --git a/Sinvo.EplanHpD.Plugin/RefDLL/EPLAN.Harness.MathLib.dll b/Sinvo.EplanHpD.Plugin/RefDLL/EPLAN.Harness.MathLib.dll new file mode 100644 index 0000000..22d4be4 Binary files /dev/null and b/Sinvo.EplanHpD.Plugin/RefDLL/EPLAN.Harness.MathLib.dll differ diff --git a/Sinvo.EplanHpD.Plugin/Sinvo.EplanHpD.Plugin.csproj b/Sinvo.EplanHpD.Plugin/Sinvo.EplanHpD.Plugin.csproj index 541acec..9b228a3 100644 --- a/Sinvo.EplanHpD.Plugin/Sinvo.EplanHpD.Plugin.csproj +++ b/Sinvo.EplanHpD.Plugin/Sinvo.EplanHpD.Plugin.csproj @@ -68,6 +68,9 @@ False RefDLL\EPLAN.Harness.ProjectCore.dll + + ..\packages\HandyControl.3.2.0\lib\net48\HandyControl.dll + @@ -84,6 +87,8 @@ + + @@ -104,5 +109,8 @@ Sinvo.EplanHpD.Plugin.WPFUI + + + \ No newline at end of file diff --git a/Sinvo.EplanHpD.Plugin/packages.config b/Sinvo.EplanHpD.Plugin/packages.config new file mode 100644 index 0000000..32729ae --- /dev/null +++ b/Sinvo.EplanHpD.Plugin/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file