表對(duì)象TableAnnotation

如圖,在Solidworks工程圖中會(huì)用到很多表,包括普通的表,明細(xì)表等。本文我們先學(xué)習(xí)普通的表對(duì)象TableAnnotation以及表特征GeneralTableFeature的相關(guān)使用。

image.png

GeneralTableFeature是表的特征體現(xiàn),TableAnnotation則是表特征的一個(gè)具體的表。本文將通過表的插入,獲取等操作,學(xué)習(xí)表類對(duì)象的一些常規(guī)使用方法。

示例1 表格的插入

表格的插入主要使用了工程圖文檔對(duì)象的DrawingDoc::InsertTableAnnotation2方法。本例中將插入一個(gè)使用模板的表格和一個(gè)自定義的表格

實(shí)例代碼

public static void InsertTable(ModelDoc2 SwDoc,string TempPath)
{
    double x = 600 / 1000.0;
    double y1 = 200 / 1000.0;
    double y2 = 100 / 1000.0;
    
    Sheet SwSheet = ((DrawingDoc)SwDoc).GetCurrentSheet();
    double[] sheetprop = SwSheet.GetProperties2();
    double scale = sheetprop[2] / sheetprop[3];

    //使用表模板
    SwDoc.SketchManager.CreateLine(0,0,0,x*1.0/ scale, y1 * 1.0 / scale, 0);
    TableAnnotation SwTable1=((DrawingDoc)SwDoc).InsertTableAnnotation2(false, x, y1, (int)swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight, TempPath, 4, 6);
    SwTable1.Title = "模板表格標(biāo)題";
    SwTable1.GeneralTableFeature.GetFeature().Name = "示例模板表格";

    //自定義表
    SwDoc.SketchManager.CreateLine(0, 0, 0, x * 1.0 / scale, y2 * 1.0 / scale, 0);
    TableAnnotation SwTable2 = ((DrawingDoc)SwDoc).InsertTableAnnotation2(false, x, y2, (int)swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft,"", 4, 6);
    SwTable2.Title = "自定義表格標(biāo)題";
    SwTable2.GeneralTableFeature.GetFeature().Name = "示例自定義表格";
    SwTable2.Text2[2, 3,true] = "測(cè)試文本";
  }

實(shí)例效果

image.png

實(shí)例解析

DrawingDoc::InsertTableAnnotation2參數(shù)解讀

image.png
image.png

從實(shí)例的效果圖與分析可以看到,決定表格的位置,不僅和參數(shù)有關(guān),也與表格的錨點(diǎn)設(shè)置類型有關(guān)。

通過TableAnnotation實(shí)例的屬性和方法,我們可以對(duì)單元格進(jìn)行賦值,插入行列等常規(guī)表格自動(dòng)化操作。

實(shí)例2 獲取表格

本例將通過TableAnnotation,GeneralTableFeature的方法屬性,來獲取上例中插入的表格及獲取相關(guān)消息。

代碼實(shí)例

 public static void GetTable(ModelDoc2 SwDoc,string TableName)
 {
     Feature SwTableFeat = ((DrawingDoc)SwDoc).FeatureByName(TableName);
     if (SwTableFeat != null)
     {
         StringBuilder Sb = new StringBuilder("");
         GeneralTableFeature gtf = SwTableFeat.GetSpecificFeature2();
         TableAnnotation SwTabe = gtf.GetTableAnnotations()[0];
         Sb.Append("表標(biāo)題:"+SwTabe.Title+"\r\n");
         Sb.Append("內(nèi)容:" + SwTabe.Text2[2, 3,true]);
         System.Windows.MessageBox.Show(Sb.ToString().Trim());
     }
 }

實(shí)例效果

image.png

實(shí)例分析

當(dāng)我們需要獲得表格中的信息時(shí),可以通過特征獲取方式先獲得表特征對(duì)象GeneralTableFeature實(shí)例,然后通過該特征獲得所需要操作的表對(duì)象TableAnnotation實(shí)例。這里不妨讀者先思考下GeneralTableFeature與TableAnnotation的關(guān)系。

實(shí)例3 表的拆分與合并

在實(shí)例2中,我們看到我們需要先獲得GeneralTableFeature對(duì)象,再間接獲得TableAnnotation對(duì)象。為什么TableAnnotation不能代表一個(gè)特征呢?我們先來看下本實(shí)例,本實(shí)例對(duì)表進(jìn)行了行拆分和行合并。

實(shí)例代碼

 public static void SetTable(ModelDoc2 SwDoc, string TableName)
 {
      Feature SwTableFeat = ((DrawingDoc)SwDoc).FeatureByName(TableName);
      if (SwTableFeat != null)
      {
          StringBuilder Sb = new StringBuilder("");
          GeneralTableFeature gtf = SwTableFeat.GetSpecificFeature2();
          TableAnnotation SwTabe = gtf.GetTableAnnotations()[0];

          #region 表格拆分
          System.Windows.MessageBox.Show("分割前表數(shù)量:"+ gtf.GetTableAnnotationCount().ToString());
          SwTabe.Split((int)swTableSplitLocations_e.swTableSplit_AfterRow, 1);
          System.Windows.MessageBox.Show("分割后表數(shù)量:" + gtf.GetTableAnnotationCount().ToString());
          SwTabe.Merge((int)swTableMergeLocations_e.swTableMerge_All);
          System.Windows.MessageBox.Show("合并后表數(shù)量:" + gtf.GetTableAnnotationCount().ToString());
          #endregion
     }
}

實(shí)例效果

image.png
image.png

實(shí)例分析

從表格拆分后,我們可以很明顯地看到,一個(gè)表格的特征GeneralTableFeature對(duì)應(yīng)了多個(gè)表格TableAnnotation實(shí)例。從這里我們可以進(jìn)一步理解GeneralTableFeature與TableAnnotation的區(qū)別。

如下圖為本文的示例程序,源碼可上我的Github下載。操作步驟可見文章《公眾號(hào)源碼Github分享庫(kù)》 , 實(shí)例序號(hào)23

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容