下面代码,让用在图上选择实体,然后把实体做成一个图块,然后插入到图上。

js代码如下:

//实例化一个构造选择集进行过滤,该类封装了选择集及其处理函数。
var ss = mxOcx.NewSelectionSet();
//构造一个过滤链表
var spFilte = mxOcx.NewResbuf();
//用户选择对象  得到用户选取的实体
ss.Select2(8, null, null, null, spFilte);

var aryId = new Array();
for (var i = 0; i < ss.Count; i++)
{
    var ent = ss.Item(i);
    if (ent != null)
    {
        aryId.push(ent.ObjectID);
        ent.Close();
    }
}
//返回控件的数据库对象
var curDatabase =mxOcx.GetDatabase();
var dMinX = 0, dMinY = 0, dMaxX = 0, dMaxY = 0;
var isFirstEnt = true;
for (var l = 0; l < aryId.length; l++)
{
    var pObj = curDatabase.ObjectIdToObject(aryId[l]);
    if (pObj == null)
        continue;
    var pEnt = pObj;
    if (pEnt == null)
        continue;
    var  points = pEnt.GetBoundingBox2();

    var minPt = points.Item(0);
    var maxPt = points.Item(1);

    if (minPt != null && maxPt != null)
    {
        if(isFirstEnt)
        {
            dMinX = minPt.x;
            dMinY = minPt.y;
            dMaxX = maxPt.x;
            dMaxY = maxPt.y;
            isFirstEnt = false;
        }
        else
        {
            if (dMinX > minPt.x)
                dMinX = minPt.x;

            if (dMinY > minPt.y)
                dMinY = minPt.y;

            if (dMaxX < maxPt.x)
                dMaxX = maxPt.x;

            if (dMaxY < maxPt.y)
                dMaxY = maxPt.y;
        }
    }
}

if(isFirstEnt)
{
    // 没有实体
    return;
}
var pos = mxOcx.NewPoint();
pos.x = dMinX + (dMaxX - dMinX) / 2.0;
pos.y = dMinY + (dMaxY - dMinY) / 2.0;

// 检查新创建的块名,在当前数据库是否已经存在.

var blkTable = curDatabase.GetBlockTable();
var curSpace = curDatabase.CurrentSpace();

var sNewBlakName;

var blkNewRec = blkTable.Add("");
sNewBlakName = blkNewRec.Name;

blkNewRec.Origin = pos;

for (var l = 0; l < aryId.length; l++)
{

    blkNewRec.AddCloneEntity(aryId[l]);
    // 把以前实体删除 。
    var pObj = curDatabase.ObjectIdToObject(aryId[l]);
    if (pObj == null)
        continue;
    pObj.Erase();
}
if (blkNewRec != null)
{
    var blkRef = curSpace.InsertBlock(pos.x,pos.y, sNewBlakName, 1.0, 0.0);
    var attrib = blkRef.AppendAttribute();
    attrib.Position = pos;
    attrib.AlignmentPoint = pos;
    attrib.Oblique = 0.0;
    attrib.Rotation = 0.0;
    attrib.Height = 2.0;
    attrib.TextString = "这是一个属性文字的测试";
    attrib.Tag = "TestTag";
    attrib.IsInvisible = false;

    blkRef.Position = blkRef.Position;

}

return;
02-12 06:33