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(); /// /// 全部BOM树状结构 /// public ObservableCollection _bomTree; public ObservableCollection 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 CacheList => bomTreeReader?.CacheList.ToList(); public void DoRefresh(HttpClient client, SldWorks swApp, ModelDoc2 doc) { List boms = bomTreeReader.Refresh(swApp, doc); if (boms == null || boms.Count <= 0) { BomTree = new ObservableCollection(); return; } // 顶层BOM PdmBom topBom = boms[0]; Dictionary bomInfos = new Dictionary(); Dictionary drawInfos = new Dictionary(); string topBomModel = topBom.partModel; HashSet 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(boms); } /// /// 从服务器获取BOM信息 /// /// 顶层BOM型号 /// 型号-Bom字典 private Dictionary GetBomsFromWeb(HttpClient Client, PdmBomParam param) { Result> result = Client.PostSyncAction>(param, "wpf/bom/openApi/bomInfo2"); Dictionary 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 models = CacheList.Select(e => e.partModel).ToHashSet(); PdmBomParam param = new PdmBomParam { topPartModel = topBomModel, allModels = models }; Result> result = Client.PostSyncAction>(param, "wpf/bom/openApi/bomInfo2"); Dictionary bomInfos = result.HandleResult(); Dictionary drawInfos = new Dictionary(); FillBomInfo(BomTree.ToList(), bomInfos, drawInfos); } /// /// 根据服务器返回的信息拼接BOM /// /// solidwork BOM列表 /// BOM信息缓存 /// 图纸信息缓存 private void FillBomInfo(ICollection boms, Dictionary bomInfos, Dictionary 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 children = bom.modules; if (children != null && children.Count > 0) { FillBomInfo(children, bomInfos, drawInfos); } } } /// /// 检索一个文档里保存的属性 /// /// 指定的 SolidWorks 文档 /// 需要检索的属性名称 /// 检索到的属性名称和值的字典 public static Dictionary RetrieveAttributesOfModelDoc(ModelDoc2 modelDoc, IEnumerable attributesNames) { if (modelDoc == null || attributesNames == null || attributesNames.Count() <= 0) { return null; } Dictionary results = new Dictionary(); 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 GetAllAttributesOfModelDoc(ModelDoc2 modelDoc) { if (modelDoc == null) { return null; } string[] attributes = new string[] { "MoldName", // 模型名称 "jgjlx", // 加工件类型 "MATERIAL", // 材料 "Finished", // 表处理 "HEAT TREATMENT", // 热处理 "jgjPrice" // 价格 }; Dictionary 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; } /// /// 找出图纸发生变更的BOM /// 按型号筛选一遍,免得遍历太多图纸 /// /// public List GetModifiedBoms() { List result = new List(); HashSet models = new HashSet(); List 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; } } }