材料明细表对象

接着上一文的工程图的表对象的介绍,本文将给大家介绍与工程图材料明细表有关的几个对象BomTableAnnotation,BomFeature,这两个对象与上文中的TableAnnotation,我们就能自如地读写Soldiworks中的明细表。

如下图,我们将通过实例对工程图进行材料明细表的插入,获得与读取操作

image.png

实例一:插入材料明细表

思路:材料明细表反应了装配体中的部件信息,那么插入材料明细表之前,我们就需要先为材料明细表选择一个使其关联的视图。

代码实例:

public static void InsertBom(ModelDoc2 SwDoc, string ViewName, string BomTemp)
{
    Feature SwFeat = ((DrawingDoc)SwDoc).FeatureByName(ViewName);
    if (SwFeat != null)
    {
          View SwView = SwFeat.GetSpecificFeature2();
          BomTableAnnotation SwBomAnn = SwView.InsertBomTable4(false, 0.05, 0.05, (int)swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_BottomLeft, (int)swBomType_e.swBomType_PartsOnly, "", BomTemp, false, (int)swNumberingType_e.swNumberingType_None, false);
          BomFeature Bf = SwBomAnn.BomFeature;
          Bf.Name = "总明细表";
          MessageBox.Show("明细表:" + Bf.Name + "-->插入成功!");
   }
}

运行效果

image.png

代码解析:

通过插入明细表,可以获得BomTableAnnotation对象,再通过BomTableAnnotation的属性,可以获得该明细表的特征对象,进而可以对该明细表进行重命名,以便后期寻找。

实例二:获得材料明细表对象

思路:已知材料明细表的特征名称,则可以先获取明细表的特征对象Feature,通过特征对象的方法获得材料明细表特征对象BomFeature。

代码实例:

public static BomFeature GetBomTable(ModelDoc2 SwDoc, string BomName, bool ShowInfo)
{
    BomFeature Bf = null;
    Feature SwFeat = ((DrawingDoc)SwDoc).FeatureByName(BomName);
    if (SwFeat.GetTypeName2() == "BomFeat")
    {
         Bf = SwFeat.GetSpecificFeature2();
         if (Bf != null)
         {
              if (ShowInfo)
              {
                   StringBuilder Sb = new StringBuilder("明细表获得成功:\r\n");
                   Sb.Append("特征名:" + Bf.Name + "\r\n");
                   Sb.Append("表类型:" + Enum.GetName(typeof(swBomType_e), Bf.TableType).ToString().Trim() + "\r\n");
                   Sb.Append("所含表数:" + Bf.GetTableAnnotationCount().ToString().Trim() + "\r\n");
                   Sb.Append("当前配置:" + Bf.Configuration.ToString().Trim() + "\r\n");
                   Sb.Append("参考模型:" + Bf.GetReferencedModelName().ToString().Trim() + "\r\n");
                   MessageBox.Show(Sb.ToString().Trim());
             }
        }
    }
    return Bf;
}

运行效果:

image.png

实例解读:

从本实例可以看到BomFeature对象,就是代表了图中选中工程图材料明细表后,红色箭头所指的属性信息。

image.png

实例三:读取材料明细表内容

思路:通过BomFeature,即可获得每个明细表实例BomTableAnnotation,再通过将BomTableAnnotation转化为TableAnnotation,即可同时获得材料明细表的内容信息和材料明细表的参考引用信息。

代码实例:

public static void GetBomInfo(ModelDoc2 SwDoc, BomFeature Bf)
{
     object[] btas = Bf.GetTableAnnotations();
     foreach (object objbat in btas)
     {
          StringBuilder Sb = new StringBuilder("表信息:\r\n");
          BomTableAnnotation bta = (BomTableAnnotation)objbat;
          string ItemNumber = "";
          string PartNumber = "";
          Sb.Append("明细:\r\n");
          TableAnnotation ta = (TableAnnotation)bta;
          int rowcount = ta.RowCount;
          int colcount = ta.ColumnCount;
          for (int i = 0; i < rowcount; i++)
          {
               #region 获得明细
               for (int j = 0; j < colcount; j++)
               {
                   Sb.Append(ta.Text2[i, j, true] + ",");
               }
               #endregion

               #region 获得模型
               string[] path = bta.GetModelPathNames(i, out ItemNumber, out PartNumber);
               Sb.Append("路径:");
               if (path != null)
               {
                   foreach (string aa in path)
                   {
                       Sb.Append(aa + ",");
                   }
               }
               #endregion
               Sb.Append("\r\n");
          }
          MessageBox.Show(Sb.ToString().Trim());
     }
}

运行效果:

image.png

通过以上3个实例,我们可以对BomTableAnnotation,BomFeature以及TableAnnotation之间的关系总结如下

image.png

如下图为本文的示例程序,源码可上我的Github下载。操作步骤可见文章《公众号源码Github分享库》实例序号24

image.png

你可能感兴趣的:(材料明细表对象)