由于个人XX,目前会使用SolidWorks,需要加工的模型要出工程图,而模型中的“数量”这个属性是由手工添加进去的,这就导致了可能出现手工填写出错的情况。如果某个零件只需要1个,结果不小心写成了2个,一旦东西做出来之后心里会有一万只草泥马在奔腾了吧。所以我秉承着没有工具就自己做,能动手就少BB的思想,决定开发一个功能,来自动检查模型中的“数量”这个属性是否有误,一劳永逸的解决这个问题,提高设计的准确率。
思路如下:获取总装配体中所有零件的BOM,这个BOM应该是完全正确的,因为你装配了多少零部件进去,这个BOM中的量就是多少。然后再逐一获取每个零件中的“数量”这个属性,并进行对比,对比后将结果导出为EXCEL,以供查询。
这里我采用的是遍历整个装配体的方式来获取BOM,不好意思,这一段核心代码我真的是在书上抄的,感谢 SOLIDWORKS API二次开发实例详解(微视频版)这本书,有兴趣的同学可以买一本,要支持正版啊,鄙视白嫖!
public void TraverseFeature(ModelDoc2 Modle, Dictionary<string, BomBean> BomDic, Dictionary<string, int> PartCountDic)//顶层部件 { FeatureManager FM = Modle.FeatureManager; Feature swFeat = Modle.FirstFeature(); CustomPRopertyManager CPM; string FileNameNoExt = ""; int PartNoInNowLevetl = 1;//当前层级下的件号 while (swFeat != null) { object ObjComp = swFeat.GetSpecificFeature2(); if (ObjComp is Component2) { Component2 Comp = (Component2)ObjComp; if (Comp.ExcludeFromBOM == true || Comp.GetSuppression() == 0)//压缩或排除在明细表外的部件 { swFeat = swFeat.GetNextFeature(); continue; } else if (Comp.GetSuppression() == 1 || Comp.GetSuppression() == 3 || Comp.GetSuppression() == 4)//轻化 { Comp.SetSuppression2(2);//全部还原 } ModelDoc2 SubModle = Comp.GetModelDoc2(); CPM = SubModle.Extension.CustomPropertyManager[""]; FileNameNoExt = SubModle.GetTitle().Substring(0, SubModle.GetTitle().LastIndexOf(".")); if (Comp.IsPatternInstance())//是镜像阵列之类的 { if (PartCountDic.ContainsKey(FileNameNoExt)) { PartCountDic[FileNameNoExt] = PartCountDic[FileNameNoExt] + 1; } else { PartCountDic.Add(FileNameNoExt, 1); } swFeat = swFeat.GetNextFeature(); continue; } #region 登记零件信息 BomBean bb = new BomBean(); bb.DwgNo = GetBomInfo(CPM, "代号"); bb.PartName = GetBomInfo(CPM, "名称"); bb.Material = GetBomInfo(CPM, "规格"); //bb.PreMass = double.Parse(GetBomInfo(CPM, "单重")); bb.Remark = GetBomInfo(CPM, "备注"); bb.FileNameNoExt = FileNameNoExt; bb.pathname = Comp.GetPathName(); BomDic.Add(PartNoInNowLevetl.ToString().Trim(), bb); #endregion if (SubModle.GetType() == (int)swDocumentTypes_e.swDocASSEMBLY)//说明是装配体//开分进程扫描子装配 { TraverseComp(Comp, BomDic, PartNoInNowLevetl.ToString().Trim(), PartCountDic, CPM); } PartNoInNowLevetl = PartNoInNowLevetl + 1; } swFeat = swFeat.GetNextFeature(); } } public void TraverseComp(Component2 Comp, Dictionary<string, BomBean> BomDic, string ParentPartNo, Dictionary<string, int> PartCountDic, CustomPropertyManager CPM)// 这个是扫描子部件,TopPbb是相对的顶层,StdDwg=true,是标准图 { Feature swFeat = Comp.FirstFeature(); int PartNoInNowLevetl = 1;//当前层级下的件号 string FileNameNoExt = ""; while (swFeat != null) { object ObjComp = swFeat.GetSpecificFeature2(); if (ObjComp is Component2) { Component2 Comp1 = (Component2)ObjComp; if (Comp1.ExcludeFromBOM == true || Comp1.GetSuppression() == 0)//压缩或排除在明细表外的部件 { swFeat = swFeat.GetNextFeature(); continue; } else if (Comp1.GetSuppression() == 1 || Comp1.GetSuppression() == 3 || Comp1.GetSuppression() == 4)//轻化 { Comp1.SetSuppression2(2);//全部还原 } ModelDoc2 SubModle = Comp1.GetModelDoc2(); CPM = SubModle.Extension.CustomPropertyManager[""]; FileNameNoExt = SubModle.GetTitle().Substring(0, SubModle.GetTitle().LastIndexOf(".")); if (Comp.IsPatternInstance())//是镜像阵列之类的 { if (PartCountDic.ContainsKey(FileNameNoExt)) { PartCountDic[FileNameNoExt] = PartCountDic[FileNameNoExt] + 1; } else { PartCountDic.Add(FileNameNoExt, 1); } swFeat = swFeat.GetNextFeature(); continue; } #region 登记零件信息 BomBean bb = new BomBean(); bb.DwgNo = GetBomInfo(CPM, "代号"); bb.PartName = GetBomInfo(CPM, "名称"); bb.Material = GetBomInfo(CPM, "规格"); //bb.PreMass = double.Parse(GetBomInfo(CPM, "单重")); bb.Remark = GetBomInfo(CPM, "备注"); bb.pathname = Comp1.GetPathName(); bb.FileNameNoExt = FileNameNoExt; BomDic.Add(ParentPartNo + "-" + PartNoInNowLevetl.ToString().Trim(), bb); #endregion if (SubModle.GetType() == (int)swDocumentTypes_e.swDocASSEMBLY)//说明是装配体//开分进程扫描子装配 { TraverseComp(Comp1, BomDic, ParentPartNo + "-" + PartNoInNowLevetl.ToString().Trim(), PartCountDic, CPM); } PartNoInNowLevetl = PartNoInNowLevetl + 1; } swFeat = swFeat.GetNextFeature(); } }
成品的效果如下:
后续因要考虑数量获取的各种干扰因素,烂尾中……