using PdmSwPlugin.Common.Entity.Pdm;
|
using SolidWorks.Interop.sldworks;
|
using SolidWorks.Interop.swconst;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.Linq;
|
using System.Threading;
|
|
namespace PdmSwPlugin.PDM.Model
|
{
|
public partial class PdmControlModel
|
{
|
///// <summary>
|
///// 多线程递归获取子BOM
|
///// </summary>
|
///// <param name="param"></param>
|
//private static void GetChildrenBomAsync2(object param)
|
//{
|
// object[] objs = (object[])param;
|
// // 父BOM
|
// PdmBom parent = objs[0] as PdmBom;
|
// // 父Sw组件
|
// Component2 parentComponent = objs[1] as Component2;
|
// // BomInfo缓存
|
// Dictionary<string, PdmBom> bomCache = objs[2] as Dictionary<string, PdmBom>;
|
// // 层级
|
// int level = (int)objs[3];
|
// // 缓存列表
|
// List<PdmBom> cacheList = objs[4] as List<PdmBom>;
|
// // 父BOM的children
|
// Collection<PdmBom> children = objs[5] as Collection<PdmBom>;
|
// // 多线程计数器
|
// CountdownEvent counter = objs[6] as CountdownEvent;
|
|
// try
|
// {
|
// if (parentComponent == null)
|
// {
|
// parent.modules = new ObservableCollection<PdmBom>();
|
// return;
|
// }
|
|
// object[] childrenComponents = parentComponent.GetChildren();
|
// if (childrenComponents == null)
|
// {
|
// parent.modules = new ObservableCollection<PdmBom>();
|
// return;
|
// }
|
|
// // 计算每个子料数量的缓存
|
// Dictionary<string, PdmBom> numCache = new Dictionary<string, PdmBom>();
|
// List<PdmBom> modules = new List<PdmBom>();
|
|
// foreach (object child in childrenComponents)
|
// {
|
// Component2 childComponent = child as Component2;
|
// ModelDoc2 childDoc = childComponent.GetModelDoc2();
|
// string childPath = childComponent.GetPathName();
|
// PdmBom bom = null;
|
// if (bomCache.ContainsKey(childPath))
|
// {
|
// bom = bomCache[childPath].ShallowCopy();
|
// bom.level = level + 1;
|
// bom.parent = parent;
|
// // 就算有缓存,也要把新bom的treeId更新了,id在这里不一定唯一,tree是唯一的
|
// bom.treeId = Guid.NewGuid().ToString();
|
// }
|
// else
|
// {
|
// BomInfo info = GetBomInfoFromDoc(childComponent);
|
|
// if (info == null)
|
// {
|
// // 可能会返回null,说明这是个模拟件,直接跳过
|
// continue;
|
// }
|
// bom = ConcatCacheBom(info, level + 1, parent);
|
// if (!bom.lost)
|
// {
|
// // // 如果bom没有丢失,处理子料
|
// if (bom.drawingType == (int)swDocumentTypes_e.swDocASSEMBLY)
|
// {
|
// bom.children = new ObservableCollection<PdmBom>();
|
// counter.TryAddCount();
|
// ThreadPool.QueueUserWorkItem(new WaitCallback(GetChildrenBomAsync2),
|
// new object[] {
|
// bom, childComponent, bomCache, bom.level, cacheList, bom.children, counter
|
// });
|
// }
|
// else
|
// {
|
// bom.modules = new ObservableCollection<PdmBom>();
|
// }
|
// }
|
// bomCache[childPath] = bom;
|
// }
|
// // 列表缓存
|
// cacheList.Add(bom);
|
// modules.Add(bom);
|
|
// // 丢没丢都要处理数量
|
// if (numCache.ContainsKey(bom.name))
|
// {
|
// numCache[bom.name].quantity++;
|
// }
|
// else
|
// {
|
// numCache[bom.name] = bom;
|
// children.Add(bom);
|
// }
|
// }
|
|
// if (modules != null && modules.Count > 0)
|
// {
|
// parent.modules = new ObservableCollection<PdmBom>(modules);// new ObservableCollection<PdmBom>(modules.OrderBy(e => e.name).ToList());
|
// return;
|
// }
|
// parent.modules = new ObservableCollection<PdmBom>();
|
// }
|
// finally
|
// {
|
// counter.Signal();
|
// if (level == 0) counter.Signal();
|
// }
|
//}
|
|
///// <summary>
|
///// 从一个 SolidWorks 的部件递归得到全部的子BOM
|
///// </summary>
|
///// <param name="parent">父BOM</param>
|
///// <param name="currentComponent">当前部件</param>
|
///// <param name="currentComponentName">当前部件的名称</param>
|
///// <param name="level">层级,从 0 开始的</param>
|
///// <param name="results">装所有核价项的列表</param>
|
//private static List<PdmBom> GetChildrenBomDev2(PdmBom parent,
|
// Component2 parentComponent,
|
// Dictionary<string, BomInfo> bomInfoCache,
|
// int level,
|
// List<PdmBom> cacheList,
|
// Collection<PdmBom> children)
|
//{
|
// if (parentComponent == null)
|
// {
|
// return null;
|
// }
|
|
// object[] childrenComponents = parentComponent.GetChildren();
|
// if (childrenComponents == null)
|
// {
|
// return null;
|
// }
|
|
// // 计算每个子料数量的缓存
|
// Dictionary<string, PdmBom> numCache = new Dictionary<string, PdmBom>();
|
// List<PdmBom> modules = new List<PdmBom>();
|
|
// foreach (object child in childrenComponents)
|
// {
|
// Component2 childComponent = child as Component2;
|
// ModelDoc2 childDoc = childComponent.GetModelDoc2();
|
// string childPath = childComponent.GetPathName();
|
// PdmBom bom = null;
|
// if (bomInfoCache.ContainsKey(childPath))
|
// {
|
// BomInfo info = bomInfoCache[childPath];
|
// bom = ConcatCacheBom(info, level + 1, parent);
|
// }
|
// else
|
// {
|
// BomInfo info = GetBomInfoFromDoc(childComponent);
|
|
// if (info == null)
|
// {
|
// // 可能会返回null,说明这是个模拟件,直接跳过
|
// continue;
|
// }
|
// bomInfoCache[childPath] = info;
|
// bom = ConcatCacheBom(info, level + 1, parent);
|
// }
|
// if (!bom.lost)
|
// {
|
// // // 如果bom没有丢失,处理子料
|
// if (bom.drawingType == (int)swDocumentTypes_e.swDocASSEMBLY)
|
// {
|
// bom.children = new ObservableCollection<PdmBom>();
|
// bom.modules = new ObservableCollection<PdmBom>(
|
// GetChildrenBomDev2(bom, childComponent, bomInfoCache, bom.level, cacheList, bom.children)
|
// );
|
// }
|
// else
|
// {
|
// bom.modules = null;
|
// }
|
// }
|
// // 列表缓存
|
// cacheList.Add(bom);
|
// modules.Add(bom);
|
|
// // 丢没丢都要处理数量
|
// if (numCache.ContainsKey(bom.name))
|
// {
|
// numCache[bom.name].quantity++;
|
// }
|
// else
|
// {
|
// numCache[bom.name] = bom;
|
// children.Add(bom);
|
// }
|
// }
|
|
// if (modules != null && modules.Count > 0)
|
// {
|
// return modules.OrderBy(e => e.name).ToList();
|
// }
|
// else { return modules; }
|
//}
|
|
///// <summary>
|
///// 从打开的文档中读取BOM信息
|
///// </summary>
|
///// <returns>树型BOM列表</returns>
|
//public List<PdmBom> GetBomsFromDocDev(ModelDoc2 sldDoc)
|
//{
|
// cacheList.Clear();
|
// List<PdmBom> result = new List<PdmBom>();
|
|
// if (sldDoc == null)
|
// {
|
// return result;
|
// }
|
|
// int drawingType = sldDoc.GetType();
|
// Configuration activeConfiguration = sldDoc.GetActiveConfiguration() as Configuration;
|
// if (activeConfiguration == null)
|
// {
|
// return result;
|
// }
|
|
// Component2 rootComponent = activeConfiguration.GetRootComponent3(false);
|
// string rootComponentName = rootComponent.Name2;
|
// if (drawingType != (int)swDocumentTypes_e.swDocASSEMBLY)
|
// {
|
// // 装配件才有子节点
|
// return result;
|
// }
|
// Dictionary<string, BomInfo> bomInfoCache = new Dictionary<string, BomInfo>();
|
// BomInfo bomInfo = GetBomInfoFromDoc(rootComponent);
|
// PdmBom bom = ConcatCacheBom(bomInfo, 0, null);
|
// /// ConcatBom 可能会返回null,说明这是个模拟件
|
// if (bom == null)
|
// {
|
// return result;
|
// }
|
// bomInfoCache[rootComponent.GetPathName()] = bomInfo;
|
// cacheList.Add(bom);
|
// result.Add(bom);
|
// bom.children = new ObservableCollection<PdmBom>();
|
// bom.modules = new ObservableCollection<PdmBom>
|
// (GetChildrenBomDev2(bom, rootComponent, bomInfoCache, 0, cacheList, bom.children));
|
// return result.OrderBy(e => e.name).ToList();
|
//}
|
}
|
}
|