下面,会遍历图块中所有实体,然后求所有实体的最小外包,再计算出外包的中心点,再把块记录的Origin属性修改到中心点。sBlkName是图块名。
//返回控件的数据库对象 var databae = mxOcx.GetDatabase(); //返回数据库中的块表对象 var blkTab = databae.GetBlockTable(); var sBlkName = "123"; var blkRec = blkTab.GetAt(sBlkName); if (blkRec == null) return false; //创建一个遍历块表中所有记录的遍历器 var mBlockTableReciter = blkRec.NewIterator(); var mMaxPt = mxOcx.NewPoint(); var mMinPt = mxOcx.NewPoint(); var mMaxPtTemp = mxOcx.NewPoint(); var mMinPtTemp = mxOcx.NewPoint(); var isFirstEnt = true; //移动当前遍历器位置 for (; !mBlockTableReciter.Done(); mBlockTableReciter.Step(true, false)) { //与用户交互在图面上选择一个实体 var ent = mBlockTableReciter.GetEntity(); //返回实体的最小矩形框外包 var points = ent.GetBoundingBox2(); var mMinPtTemp = points.Item(0); var mMaxPtTemp = points.Item(1); 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; } var mNewBasePt = mxOcx.NewPoint(); mNewBasePt.x = mMinPt.x + (mMaxPt.x - mMinPt.x) / 2; mNewBasePt.y = mMinPt.y + (mMaxPt.y - mMinPt.y) / 2; //块表记录的插入原点属性 blkRec.Origin = mNewBasePt; //重新生成图,重新生成所有显示数据 mxOcx.Regen(); return true;