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;
|
}
|
}
|
}
|