chr
2025-03-04 3f62d18e4361cd1d7a49c126765d95b2ad9c8246
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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()
        {
        }
    }
}