下面,会遍历图块中所有实体,然后求所有实体的最小外包,再计算出外包的中心点,再把块记录的Origin属性修改到中心点。sBlkName是图块名。

private bool ModifyBlockInsertPointToCenter(String sBlkName)
{
    //返回控件的数据库对象
    MxDrawDatabase databae = (MxDrawDatabase)axMxDrawX1.GetDatabase();
    //返回数据库中的块表对象
    MxDrawBlockTable blkTab = databae.GetBlockTable();
    MxDrawBlockTableRecord blkRec = blkTab.GetAt(sBlkName);
    if (blkRec == null)
        return false;
    //创建一个遍历块表中所有记录的遍历器
    MxDrawBlockTableRecordIterator mBlockTableReciter = blkRec.NewIterator();
    MxDrawPoint mMaxPt = new MxDrawPoint();
    MxDrawPoint mMinPt = new MxDrawPoint();
    MxDrawPoint mMaxPtTemp = new MxDrawPoint();
    MxDrawPoint mMinPtTemp = new MxDrawPoint();
    bool isFirstEnt = true;
    //移动当前遍历器位置
    for (; !mBlockTableReciter.Done(); mBlockTableReciter.Step(true, false))
    {
        //与用户交互在图面上选择一个实体
        MxDrawEntity ent = mBlockTableReciter.GetEntity();
        //返回实体的最小矩形框外包
        ent.GetBoundingBox(out mMinPtTemp, out mMaxPtTemp);
        if ((mMinPtTemp == null) || (mMaxPtTemp == null))
            break;
        if (isFirstEnt)
        {
            mMinPt.x = mMinPtTemp.x;
            mMinPt.y = mMinPtTemp.y;
            mMaxPt.x = mMaxPtTemp.x;
            mMaxPt.y = mMaxPtTemp.y;
            isFirstEnt = false;
        }
        else
        {
            mMaxPt.x = Math.Max(mMaxPt.x, mMaxPtTemp.x);
            mMaxPt.y = Math.Max(mMaxPt.y, mMaxPtTemp.y);
            mMinPt.x = Math.Min(mMinPt.x, mMinPtTemp.x);
            mMinPt.y = Math.Min(mMinPt.y, mMinPtTemp.y);
        }
    }
    if (isFirstEnt)
    {
        // 没有实体
        return false;
    }
    MxDrawPoint mNewBasePt = new MxDrawPoint();
    mNewBasePt.x = mMinPt.x + (mMaxPt.x - mMinPt.x) / 2;
    mNewBasePt.y = mMinPt.y + (mMaxPt.y - mMinPt.y) / 2;
    //块表记录的插入原点属性
    blkRec.Origin = mNewBasePt;
    //重新生成图,重新生成所有显示数据
    axMxDrawX1.Regen();
    return true;
}
12-23 21:24