天天看点

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

1. 得到所有图块

(1)下面代码,遍历块表的每条记录,然后得到块名,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);
 
}      

(2)判断当前数据库中,是有指定的块名

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

(3)遍历某名称图块下所有实体

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());      

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

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

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);
    
}      

3. 添加块表记录

(1)下面代码,判断块表中是否有指定名称的块表记录,如果没有就增一个块表记录。

// sName 是新增加的块名。
// blkOriginPoint 是图块的插入基点。
public bool AddBlockRrecord(String sName, MxDrawPoint blkOriginPoint)
{
    // 取当前数据库.
    MxDrawDatabase curDatabase = (MxDrawDatabase)(axMxDrawX1.GetDatabase());
    // 得到块表
    MxDrawBlockTable blkTable = curDatabase.GetBlockTable();
     
    // 判断块表是否已经有该名称的块名。
    if (blkTable.Has(sName))
    {
        MxDrawBlockTableRecord blkRec = blkTable.GetAt(sName, true);
        // 如果块被删除,就反删除。
        blkRec.SetProp("unErase", null);
        return true;
    }
    // 新增一个块表记录.
    MxDrawBlockTableRecord blkNewRec = blkTable.Add(sName);
    if (blkNewRec == null)
        return false;
    // 设置块表记录的插入基点
    blkNewRec.Origin = blkOriginPoint;
    return true;
}      
MxDrawPoint pt = new MxDrawPoint();
 pt.x = 100;
 pt.y = 100;
 AddBlockRrecord("MyBlkName", pt);