using PdmSwPlugin.Common.Constants;
|
using PdmSwPlugin.Common.Interface;
|
using PdmSwPlugin.Common.Setting;
|
using PdmSwPlugin.Common.Util;
|
using PdmSwPlugin.Common.Util.Http;
|
using SolidWorks.Interop.sldworks;
|
using SolidWorks.Interop.swconst;
|
using System;
|
using System.IO;
|
using System.Linq;
|
using System.Reflection;
|
using System.Threading.Tasks;
|
using System.Windows;
|
|
namespace PdmSwPlugin.MaterialSelect
|
{
|
/// <summary>
|
/// 分部类,主要是与Sw插件交互的方法
|
/// </summary>
|
public partial class MaterialSelectControl : IActiveDocChangeHandler
|
{
|
static MaterialSelectControl()
|
{
|
// AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
}
|
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
{
|
string dir = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.Parent.FullName;
|
if (args.Name.Contains(".resources"))
|
{
|
return default;
|
}
|
if (AppDomain.CurrentDomain.GetAssemblies()
|
.Where(x => !x.IsDynamic)
|
.First(x => x.FullName == args.Name) is Assembly alreadyLoaded)
|
{
|
return alreadyLoaded;
|
}
|
if (Directory.EnumerateFiles(dir).First(x => Path.GetFileNameWithoutExtension(x) == new AssemblyName(args.Name).Name)
|
is string toBeLoaded
|
)
|
{
|
return Assembly.LoadFile(toBeLoaded);
|
}
|
throw new DllNotFoundException();
|
}
|
|
public SldWorks SwApp { get; set; }
|
public ModelDoc2 LastActiveDoc { get; set; }
|
public ModelDoc2 ActiveDoc { get; set; }
|
public int ActiveDocType { get; set; }
|
private HttpClientCreator clientCreator { get; set; }
|
/// <summary>
|
/// 图纸文件存放的公共文件夹
|
/// </summary>
|
private readonly PluginSetting GlobalConfig;
|
|
public MaterialSelectControl() : base()
|
{
|
InitializeComponent();
|
self.DataContext = this;
|
GlobalConfig = PluginSetting.Instance;
|
clientCreator = new HttpClientCreator(new HttpConfig(PluginSetting.Instance.BaseAddress));
|
}
|
|
public MaterialSelectControl(SldWorks swAddin) : this()
|
{
|
SwApp = swAddin;
|
}
|
|
public void SetSwApp(SldWorks SwApp)
|
{
|
this.SwApp = SwApp;
|
}
|
|
public void OnSwActiveDocChange(ModelDoc2 lastDoc, ModelDoc2 doc, Component2 comp)
|
{
|
ActiveDoc = doc;
|
ActiveDocType = doc.GetType();
|
ActiveDocPath = doc.GetPathName();
|
}
|
|
private async void AddComponent(string componentPath)
|
{
|
ActiveDoc = ActiveDoc == null ? SwApp.IActiveDoc2 : ActiveDoc;
|
if (ActiveDoc == null || ActiveDoc.GetType() != (int)swDocumentTypes_e.swDocASSEMBLY)
|
{
|
StatusBarText = "请打开装配体文件";
|
return;
|
}
|
if (!File.Exists(componentPath))
|
{
|
MessageBox.Show("未找到图纸!路径:\r\n" + componentPath, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
return;
|
}
|
try
|
{
|
// 这要用异步,不然App调试的时候OpenDoc6卡死,MD
|
await Task.Run(() =>
|
{
|
ModelDoc2 current = ActiveDoc;
|
int errors = 0, warings = 0;
|
ModelDoc2 model = SwApp.OpenDoc6(componentPath,
|
(int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warings);
|
model.Visible = false;
|
current = SwApp.ActivateDoc3(current.GetTitle(), true, 0, errors);
|
AssemblyDoc doc = (AssemblyDoc)current;
|
var comp = doc.AddComponent5(model.GetTitle(), (int)swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,
|
"", false, "", -0.1, 0, 0);
|
}).ContinueWith(t =>
|
{
|
if (t.IsFaulted)
|
{
|
throw t.Exception;
|
}
|
});
|
//SelectionMgr selMgr = current.SelectionManager;
|
//SelectData selData = selMgr.CreateSelectData();
|
//comp.Select4(false, selData, false);
|
}
|
catch (Exception e)
|
{
|
Logger.Error("加载图纸失败!错误:{0}", e);
|
MessageBox.Show("加载图纸失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
}
|
}
|
|
public void OnSwActiveDocSaved(ModelDoc2 doc, Component2 comp)
|
{
|
Logger.Info("123");
|
}
|
|
public void OnCustomPropertyChange(string propName, string Configuration, string oldValue, string NewValue, int valueType)
|
{
|
//throw new NotImplementedException();
|
}
|
|
public void OnDocDestroy(ModelDoc2 doc)
|
{
|
|
}
|
|
public void AfterDocDestroy()
|
{
|
|
}
|
|
public void DisabledHandler()
|
{
|
}
|
}
|
}
|