CAD关于块表操作(com接口网页版)

A. 得到所有图块

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

var database = mxOcx.GetDatabase();
var blkTab = database.GetBlockTable();
var iter = blkTab.NewIterator();
for (; !iter.Done(); iter.Step())
{
    var blkRec = iter.GetRecord();
    alert(blkRec.Name);
  
}

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

var database = mxOcx.GetDatabase();
 
var sBlkName = "Tree";
if (database.GetBlockTable().Has(sBlkName))
{
 
    alert("有指定块名");
    // 已经插入.
}
else{
    alert("无指定块名")
}

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

 

var database = mxOcx.GetDatabase();
var sBlkName = "Tree";
var blkRec = database.GetBlockTable().GetAt(sBlkName);
var iter = blkRec.NewIterator();
if (iter == null)
    return;
var iNum = 0;
// 循环得到所有实体
for (; !iter.Done(); iter.Step(true, false))
{
    // 得到遍历器当前的实体
    var ent = iter.GetEntity();
    alert(ent.ObjectName);
 
    iNum++;
}
alert(iNum.ToString());

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

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

var mxUtility = mxOcx.NewUtility;
 
// 得到当前图纸空间
var blkRec = mxOcx.GetDatabase().CurrentSpace();
 
// 创建一个用于遍历当前图纸空间的遍历器
var iter = blkRec.NewIterator();
if (iter == null)
    return;
 
// 所有实体的id数组。
var aryId =  new Array();
 
var iLineNum = 0;
// 循环得到所有实体
 
for (; !iter.Done(); iter.Step(true, false))
{
    // 得到遍历器当前的实体
    var ent = iter.GetEntity();
    if (ent == null)
        continue;
 
    // 得到实体的id
    aryId.push(ent.ObjectID);
 
 
    var c= aryId.length;
    console.log(c);
 
    if (ent.ObjectName == "McDbLine")
    {
        // 当前实体是一个直线
        var line = ent;
        iLineNum++;
    }
else if (ent.ObjectName ==  "McDbBlockReference")
    {
        // 当前实体是一个块引用
        var blkRef =ent;
        for (var j = 0; j < blkRef.AttributeCount; j++)
        {
            // 得到块引用中所有的属性
            var attrib = blkRef.AttributeItem(j);
            mxUtility.Prompt("n Tag: " + attrib.Tag + "Text:" + attrib.TextString);
        }
    }
}
 
var sT;
sT = "发现" + aryId.length + "个实体,其中有" + iLineNum + "个直线";
alert(sT);

 

posted on 2019-12-09 16:05  梦想CAD控件  阅读(383)  评论(0)    收藏  举报

导航