chr
2024-08-20 b62a2f5ad99995ec01da71b9f93be7284023171b
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using PdmSwPlugin.Common.Entity;
using PdmSwPlugin.Common.Entity.Pdm;
using PdmSwPlugin.Common.Util.Http;
using PdmSwPlugin.Common.Util.Pdm;
using SolidWorks.Interop.sldworks;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net.Http;
 
namespace PdmSwPlugin.PDM.Model
{
    public partial class PdmControlModel : NotifyBase
    {
        private string _SelectedBomCount = "已选中 0 条";
 
        public string SelectedBomCount
        {
            get => _SelectedBomCount;
            set
            {
                RaiseAndSetIfChanged(ref _SelectedBomCount, value);
            }
        }
 
        private string _AllBomCount = "共 0 条";
 
        public string AllBomCount
        {
            get => _AllBomCount;
            set
            {
                RaiseAndSetIfChanged(ref _AllBomCount, value);
            }
        }
 
        public BomTreeReader bomTreeReader { get; private set; } = new BomTreeReader();
 
        /// <summary>
        /// 全部BOM树状结构
        /// </summary>
        public ObservableCollection<PdmBom> _bomTree;
        public ObservableCollection<PdmBom> BomTree
        {
            get => _bomTree;
            set {
                RaiseAndSetIfChanged(ref _bomTree, value);
                int count = CacheList != null ? CacheList.Count : 0;
                AllBomCount = $"共 {count} 条";
                RefreshSelectedBomCount();
            }
        }
 
        public void RefreshSelectedBomCount() {
            int count = CacheList != null ? CacheList.Where(e=>e.selected).Count() : 0;
            SelectedBomCount = $"已选中 {count} 条";
        }
 
        public List<PdmBom> CacheList => bomTreeReader?.CacheList.ToList();
 
        public void DoRefresh(HttpClient client, SldWorks swApp, ModelDoc2 doc)
        {
            List<PdmBom> boms = bomTreeReader.Refresh(swApp, doc);
            if (boms == null || boms.Count <= 0)
            {
                BomTree = new ObservableCollection<PdmBom>();
                return;
            }
            // 顶层BOM
            PdmBom topBom = boms[0];
            Dictionary<string, BomInfo> bomInfos = new Dictionary<string, BomInfo>();
            Dictionary<string, DrawInfo> drawInfos = new Dictionary<string, DrawInfo>();
 
            string topBomModel = topBom.partModel;
            HashSet<string> models = CacheList.Select(e => e.partModel).ToHashSet();
            PdmBomParam param = new PdmBomParam
            {
                topPartModel = topBomModel,
                allModels = models
            };
            // 需要用顶层Bom的型号去查询物料信息,检入检出等
            bomInfos = GetBomsFromWeb(client, param);
            FillBomInfo(boms, bomInfos, drawInfos);
            BomTree = new ObservableCollection<PdmBom>(boms);
        }
 
        /// <summary>
        /// 从服务器获取BOM信息
        /// </summary>
        /// <param name="topBomModel">顶层BOM型号</param>
        /// <returns>型号-Bom字典</returns>
        private Dictionary<string, BomInfo> GetBomsFromWeb(HttpClient Client, PdmBomParam param)
        {
            Result<Dictionary<string, BomInfo>> result =
                    Client.PostSyncAction<Dictionary<string, BomInfo>>(param, "wpf/bom/openApi/bomInfo2");
            Dictionary<string, BomInfo> bomInfo = result.HandleResult();
            return bomInfo;
        }
 
        public void RefreshWebInfo(HttpClient Client)
        {
            if (BomTree == null || BomTree.Count <= 0)
            {
                return;
            }
            PdmBom topBom = BomTree[0];
            string topBomModel = topBom.partModel;
            HashSet<string> models = CacheList.Select(e => e.partModel).ToHashSet();
            PdmBomParam param = new PdmBomParam
            {
                topPartModel = topBomModel,
                allModels = models
            };
            Result<Dictionary<string, BomInfo>> result =
                    Client.PostSyncAction<Dictionary<string, BomInfo>>(param, "wpf/bom/openApi/bomInfo2");
            Dictionary<string, BomInfo> bomInfos = result.HandleResult();
            Dictionary<string, DrawInfo> drawInfos = new Dictionary<string, DrawInfo>();
            FillBomInfo(BomTree.ToList(), bomInfos, drawInfos);
        }
 
 
        /// <summary>
        /// 根据服务器返回的信息拼接BOM
        /// </summary>
        /// <param name="boms">solidwork BOM列表</param>
        /// <param name="bomInfos">BOM信息缓存</param>
        /// <param name="drawInfos">图纸信息缓存</param>
        private void FillBomInfo(ICollection<PdmBom> boms,
            Dictionary<string, BomInfo> bomInfos,
            Dictionary<string, DrawInfo> drawInfos)
        {
            if (bomInfos == null)
            {
                return;
            }
            string partModel;
            foreach (PdmBom bom in boms)
            {
                BomInfo info = null;
                partModel = bom.partModel;
                if (bomInfos.ContainsKey(partModel))
                {
                    info = bomInfos[partModel];
                    bom.inDb = true;
                    //bom.BomInfo = info;
                    bom.BomInfo.id = info.id;
                    bom.BomInfo.bomVersion = info.bomVersion;
                    bom.BomInfo.checkStatus = info.checkStatus;
                    bom.BomInfo.checkUserId = info.checkUserId;
                    bom.BomInfo.checkUserName = info.checkUserName;
 
                    bom.BomInfo.status = info.status;
                    bom.BomInfo.requestUserId = info.requestUserId;
                    bom.BomInfo.requestUser = info.requestUser;
                    bom.BomInfo.auditUserId = info.auditUserId;
                    bom.BomInfo.auditUser = info.auditUser;
 
                }
                else
                {
                    bom.inDb = false;
                }
 
                // 保存图纸信息
                DrawInfo drawInfo;
                if (drawInfos.ContainsKey(partModel))
                {
                    drawInfo = drawInfos[partModel];
                }
                else
                {
                    drawInfo = bom.drawInfo;
                    if (drawInfo == null)
                    {
                        drawInfo = new DrawInfo()
                        {
                            // 图纸信息
                            d3Md5 = info?.d3Md5,
                            d2Md5 = info?.d2Md5,
                            pdfMd5 = info?.pdfMd5,
 
                            // 图纸ID
                            d3FileId = info?.d3FileId,
                            d2FileId = info?.d2FileId,
                            pdfFileId = info?.pdfFileId,
                        };
                    }
                    else
                    {
                        drawInfo.d3Md5 = info?.d3Md5;
                        drawInfo.d2Md5 = info?.d2Md5;
                        drawInfo.pdfMd5 = info?.pdfMd5;
                        drawInfo.d3FileId = info?.d3FileId;
                        drawInfo.d2FileId = info?.d2FileId;
                        drawInfo.pdfFileId = info?.pdfFileId;
                    }
                    drawInfos.Add(partModel, drawInfo);
                }
                bom.drawInfo = drawInfo;
                bom.Init();
 
                ICollection<PdmBom> children = bom.modules;
                if (children != null && children.Count > 0)
                {
                    FillBomInfo(children, bomInfos, drawInfos);
                }
            }
        }
 
        /// <summary>
        /// 检索一个文档里保存的属性
        /// </summary>
        /// <param name="modelDoc">指定的 SolidWorks 文档</param>
        /// <param name="attributesNames">需要检索的属性名称</param>
        /// <returns>检索到的属性名称和值的字典</returns>
        public static Dictionary<string, string> RetrieveAttributesOfModelDoc(ModelDoc2 modelDoc, IEnumerable<string> attributesNames)
        {
            if (modelDoc == null || attributesNames == null || attributesNames.Count() <= 0)
            {
                return null;
            }
            Dictionary<string, string> results = new Dictionary<string, string>();
            try
            {
                ModelDocExtension extension = modelDoc.Extension;
                CustomPropertyManager customPropertyManager = null;
                var allConfigurationNames = (string[])modelDoc.GetConfigurationNames(); // 所有配置名称
                foreach (var configName in allConfigurationNames) // 遍历所有配置
                {
                    Configuration swConfig = (Configuration)modelDoc.GetConfigurationByName(configName);
                    customPropertyManager = extension.CustomPropertyManager[configName];
                    if (customPropertyManager != null) break;
                }
                if (customPropertyManager == null)
                { // 自定义属性管理器必须存在
                    return null;
                }
 
                foreach (string attributeName in attributesNames)
                {
                    try
                    {
                        string paramValue, resolvedValue;
                        customPropertyManager.Get2(attributeName, out paramValue, out resolvedValue);
                        if (!string.IsNullOrEmpty(paramValue))
                        {
                            string trimmedValue = paramValue.Trim();
                            if (!string.IsNullOrEmpty(trimmedValue)) // 每一个参数值必须是非空的才加入到结果集中
                            {
                                results[attributeName] = trimmedValue;
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
            catch
            {
            }
 
            return results;
        }
 
        public static Dictionary<string, string> GetAllAttributesOfModelDoc(ModelDoc2 modelDoc)
        {
            if (modelDoc == null)
            {
                return null;
            }
            string[] attributes = new string[] {
                "MoldName", // 模型名称
                "jgjlx", // 加工件类型
                "MATERIAL", // 材料
                "Finished", // 表处理
                "HEAT TREATMENT", // 热处理
                "jgjPrice" // 价格
            };
            Dictionary<string, string> allAttributes = RetrieveAttributesOfModelDoc(modelDoc, attributes);
            string pathName = modelDoc.GetPathName();
            if (allAttributes != null && !string.IsNullOrEmpty(pathName))
            {
                FileInfo fileInfo = new FileInfo(pathName);
                // 要把模型名称的后缀名给去掉
                allAttributes[attributes[0]] = fileInfo.FullName.Replace(fileInfo.Extension, string.Empty);
            }
            return allAttributes;
        }
 
        /// <summary>
        /// 找出图纸发生变更的BOM
        /// 按型号筛选一遍,免得遍历太多图纸
        /// </summary>
        /// <returns></returns>
        public List<PdmBom> GetModifiedBoms()
        {
            List<PdmBom> result = new List<PdmBom>();
            HashSet<string> models = new HashSet<string>();
 
            List<PdmBom> cache = CacheList.Where(b => b.d3Changed).ToList();
            foreach (PdmBom bom in cache)
            {
                if (models.Contains(bom.partModel))
                {
                    continue;
                }
                result.Add(bom);
                models.Add(bom.partModel);
            }
            return result;
        }
    }
}