CAD关于块表操作1(com接口c#语言)

A. 得到所有图块

下面代码,遍历块表的每条记录,然后得到块名,C#如下:

MxDrawDatabase databae = (MxDrawDatabase)axMxDrawX1.GetDatabase();
MxDrawBlockTable blkTab = databae.GetBlockTable();
MxDrawBlockTableIterator iter = blkTab.NewIterator();
for (; !iter.Done(); iter.Step())
{
    MxDrawBlockTableRecord blkRec = iter.GetRecord();
    MessageBox.Show(blkRec.Name);
 
}

B.  判断当前数据库中,是有指定的块名

MxDrawDatabase data = (MxDrawDatabase)axMxDrawX1.GetDatabase();
if (data.GetBlockTable().Has("BlkName"))
{
    // 已经插入.
}

C.  遍历某名称图块下所有实体

MxDrawDatabase database = (MxDrawDatabase)axMxDrawX1.GetDatabase();
      MxDrawBlockTableRecord blkRec = database.GetBlockTable().GetAt(sBlkName);
      MxDrawBlockTableRecordIterator iter = blkRec.NewIterator();
      if (iter == null)
          return;
      int iNum = 0;
      // 循环得到所有实体
      for (; !iter.Done(); iter.Step(true, false))
      {
          // 得到遍历器当前的实体
          MxDrawEntity ent = iter.GetEntity();
          MessageBox.Show(ent.ObjectName);
 
          iNum++;
      }
      MessageBox.Show(iNum.ToString());

D.   得到当前空间中所有实体

下面代码演示如何得到当前块表记录,然后遍历块表记录,取到每个对象,判断对象类型,然后得到对象的属性数据。

private void GetAllEntity()
{
  
        MxDrawApplication app = new MxDrawApplication();
        MxDrawUtility mxUtility = new MxDrawUtility();
 
        // 得到当前图纸空间
        MxDrawBlockTableRecord blkRec = app.WorkingDatabase().CurrentSpace();
 
        // 创建一个用于遍历当前图纸空间的遍历器
        MxDrawBlockTableRecordIterator iter = blkRec.NewIterator();
        if (iter == null)
            return;
 
        // 所有实体的id数组。
        List<Int32> aryId = new List<Int32>();
 
        int iLineNum = 0;
        // 循环得到所有实体
 
        for (; !iter.Done(); iter.Step(true, false))
        {
            // 得到遍历器当前的实体
            MxDrawEntity ent = iter.GetEntity();
            if (ent == null)
                continue;
 
            // 得到实体的id
            aryId.Add(ent.ObjectID);
 
            if (ent is MxDrawLine)
            {
                // 当前实体是一个直线
                MxDrawLine line = (MxDrawLine)ent;
                iLineNum++;
            }
            else if (ent is MxDrawBlockReference)
            {
                // 当前实体是一个块引用
                MxDrawBlockReference blkRef = (MxDrawBlockReference)ent;
                for (int j = 0; j < blkRef.AttributeCount; j++)
                {
                    // 得到块引用中所有的属性
                    MxDrawAttribute attrib = blkRef.AttributeItem(j);
                    mxUtility.Prompt("n Tag: " + attrib.Tag + "Text:" + attrib.TextString);
                }
 
            }
            // else if (ent is 其它类型)
            //{
            //  ... ....
            //}
        }
 
        String sT;
        sT = "发现" + aryId.Count + "个实体,其中有" + iLineNum + "个直线";
        MessageBox.Show(sT);
    
}

 

posted on 2019-12-11 14:58  梦想CAD控件  阅读(378)  评论(0)    收藏  举报

导航