图块是将多个实体组合成一个整体,并给这个整体命名保存,在以后的图形编辑中图块就被视为一个实体。一个图块包括可见的实体如线、圆、圆弧以及可见或不可见的属性数据。图块的运用可以帮助用户更好的组织工作,快速创建与修改图形,减少图形文件的大小。
一、定义图块
c#将选中的实体做成块实现代码:
MxDrawSelectionSet ss = new MxDrawSelectionSet(); ss.Select(MCAD_McSelect.mcSelectionSetUserSelect, null , null , null ); List<Int64> aryId = new List<Int64>(); for (Int32 i = 0; i < ss.Count; i++) { MxDrawEntity ent = ss.Item(i); if (ent != null ) { aryId.Add(ent.ObjectID); ent.Close(); } } MxDrawDatabase curDatabase = (MxDrawDatabase)(mxdraw.GetDatabase()); double dMinX = 0, dMinY = 0, dMaxX = 0, dMaxY = 0; bool isFirstEnt = true ; for (int l = 0; l < aryId.Count; l++) { MxDrawMcDbObject pObj = curDatabase.ObjectIdToObject(aryId[l]); if (pObj == null ) continue ; MxDrawPoint pt1Ob, pt2Ob; MxDrawEntity pEnt = (MxDrawEntity)pObj; if (pEnt == null ) continue ; pEnt.GetBoundingBox(out pt1Ob, out pt2Ob); MxDrawPoint minPt = (MxDrawPoint)pt1Ob; MxDrawPoint maxPt = (MxDrawPoint)pt2Ob; 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 0; } MxDrawPoint pos = new MxDrawPoint(); pos.x = dMinX + (dMaxX - dMinX) / 2.0; pos.y = dMinY + (dMaxY - dMinY) / 2.0; // 检查新创建的块名,在当前数据库是否已经存在. MxDrawBlockTable blkTable = curDatabase.GetBlockTable(); MxDrawBlockTableRecord curSpace = curDatabase.CurrentSpace(); String sNewBlakName; MxDrawBlockTableRecord blkNewRec = blkTable.Add( "" ); sNewBlakName = blkNewRec.Name; blkNewRec.Origin = pos; for (int l = 0; l < aryId.Count; l++) { blkNewRec.AddCloneEntity(aryId[l]); // 把以前实体删除 。 MxDrawMcDbObject pObj = curDatabase.ObjectIdToObject(aryId[l]); if (pObj == null ) continue ; pObj.Erase(); } if (blkNewRec != null ) { MxDrawBlockReference blkRef = curSpace.InsertBlock(pos.x,pos.y, sNewBlakName, 1.0, 0.0); MxDrawAttribute attrib = blkRef.AppendAttribute(); attrib.Position = pos; attrib.AlignmentPoint = pos; attrib.Oblique = 0.0; attrib.Rotation = 0.0; attrib.Height = 2.0; attrib.TrueColor.SetRGB(255,0,0); // attrib.TextString = "这是一个属性文字的测试"; attrib.Tag = "TestTag" ; attrib.IsInvisible = false ; blkRef.Position = blkRef.Position; return blkRef.ObjectID; } return 0; |
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.Tag = "TestTag" ; attrib.IsInvisible = false ; blkRef.Position = blkRef.Position; } return ; |
二、绘制块引用对象
主要用到函数说明:
_DMxDrawX::DrawBlockReference
绘制块引用对象。详细说明如下:
参数 | 说明 |
---|---|
DOUBLE dPosX | 插入点的X坐标 |
DOUBLE dPosY | 插入点的Y坐标 |
BSTR pszBlkName | 图块名 |
DOUBLE dScale | 图块缩放比例 |
DOUBLE dAng | 图块的旋转角度 |
C#中实现代码说明:
private void DynInsert() { //把颜色改回黑白色 axMxDrawX1.DrawCADColorIndex = 0; //创建一个图层,名为"BlockLayer" axMxDrawX1.AddLayer( "BlockLayer" ); //设置当前图层为"BlockLayer" axMxDrawX1.LayerName = "BlockLayer" ; var sBlkName = "Tree" ; //用户需要出入图块的路径 控件程序在磁盘的文件路径 var sBlkFile = axMxDrawX1.GetOcxAppPath() + "\\Blk\\树.dwg" ; //向控件数据库中插入一个图块,不用它插入匿名块 axMxDrawX1.InsertBlock(sBlkFile, "Tree" ); //绘制块引用对象 //参数一为插入点的X坐标;参数二为插入点的Y坐标; //参数三为图块名;参数四为图块缩放比例;参数五为图块的旋转角度 axMxDrawX1.DrawBlockReference(0, 3600, "Tree" , 1, 0); axMxDrawX1.ZoomAll(); axMxDrawX1.UpdateDisplay(); } |
js中实现代码说明:
function DynInsert() { mxOcx.DrawColor = 0; //创建一个图层,名为"BlockLayer" mxOcx.AddLayer ( "BlockLayer" ); //设置当前图层为"BlockLayer" mxOcx.LayerName = "BlockLayer" ; var sBlkName = "Tree" ; //用户需要出入图块的路径 控件程序在磁盘的文件路径 var sBlkFile = mxOcx.GetOcxAppPath() + "\\Blk\\树.dwg" ; //向控件数据库中插入一个图块,不用它插入匿名块 mxOcx.InsertBlock(sBlkFile, "Tree" ); //绘制块引用对象 //参数一为插入点的X坐标;参数二为插入点的Y坐标; //参数三为图块名;参数四为图块缩放比例;参数五为图块的旋转角度 mxOcx.DrawBlockReference(0, 3600, "Tree" , 1, 0) ; mxOcx.ZoomAll(); mxOcx.UpdateDisplay(); } |
三、动态绘制块
主要用到函数说明:
_DMxDrawX::DrawBlockReference
绘制块引用对象。详细说明如下:
参数 | 说明 |
---|---|
DOUBLE dPosX | 插入点的X坐标 |
DOUBLE dPosY | 插入点的Y坐标 |
BSTR pszBlkName | 图块名 |
DOUBLE dScale | 图块缩放比例 |
DOUBLE dAng | 图块的旋转角度 |
js中实现代码说明:
动态拖放时的绘制事件:
function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) { var sGuid = pData.Guid; mxOcx.SetEventRet(0); if (sGuid == "TestDynDraw" ) { //动态插入图块 // 与用户交互到在图上提取一个点 var firstPt = pData.GetPoint( "pt1" ); if (firstPt == null ) return ; //与用户交互,从命令中取字符串 var sBlkName = pData.GetString( "BlkName" ); var secondPt = mxOcx.NewPoint(); secondPt.x = dX; secondPt.y = dY; //计算this点减去pPoint,得到的向量 var vec = firstPt.SumVector(secondPt); //返回向量顺时针转到与X轴的角度.[0,2PI) var dAng = vec.Angle(); //绘制块引用对象 //参数一为插入点的X坐标;参数二为插入点的Y坐标; //参数三为图块名;参数四为图块缩放比例;参数五为图块的旋转角度 pWorldDraw.DrawBlockReference(firstPt.x,firstPt.y,sBlkName,1.0,dAng * 180.0 / 3.14159265 + 90.0); //设置自定义事件的返回值 mxOcx.SetEventRet(1); } else if (sGuid == "TestDynDraw1" ) { var sBlkName = pData.GetString( "BlkName" ); //绘制块引用对象 pWorldDraw.DrawBlockReference(dX, dY, sBlkName, 1.0, 0.0); } } |
动态插入图块:
function DynInsert() { var sBlkName = "Tree" ; //用户需要出入图块的路径 控件程序在磁盘的文件路径 var sBlkFile = mxOcx.GetOcxAppPath() + "\\Blk\\树.dwg" ; //向控件数据库中插入一个图块,不用它插入匿名块 mxOcx.InsertBlock(sBlkFile, "Tree" ); //新建一个COM组件对象 var getPt = mxOcx.NewComObject( "IMxDrawUiPrPoint" ); getPt.message = "点取插入点" ; //初始动态绘制 var spDrawData1 = getPt.InitUserDraw( "TestDynDraw1" ); //设置string类型的属性 spDrawData1.SetString( "BlkName" , "Tree" ); if (getPt.go() != 1) { return ; } var frstPt = getPt.value(); if (frstPt == null ) { return ; } var getSecondPt = mxOcx.NewComObject( "IMxDrawUiPrPoint" ); var spDrawData = getSecondPt.InitUserDraw( "TestDynDraw" ); getSecondPt.message = "点取旋转角度" ; //得到点时的拖放基点 getSecondPt.basePoint = frstPt; //设置是否启用拖放基点 true:使用,false:不使用 getSecondPt.setUseBasePt( true ); //设置point类型的属性 spDrawData.SetPoint( "pt1" , frstPt); //设置string类型的属性 spDrawData.SetString( "BlkName" , "Tree" ); if (getSecondPt.go() != 1) { return ; } //把实体绘制到图上。一般用于动态拖放时,在拖放结束时,绘制到图上 var ret = spDrawData.Draw(); // 给属性块写文字. var blkRef = ret.AtObject(0); if (blkRef != null ) { var param = mxOcx.NewResbuf(); blkRef.SetProp( "createAttribute" ,param); } } |
四、修复块中心点
主要用到函数说明:
IMxDrawEntity::GetBoundingBox
返回实体的最小矩形框外包。详细说明如下:
参数 | 说明 |
---|---|
[out] IMxDrawPoint** minPoint | 外包框的左下角点 |
[out] IMxDrawPoint** maxPoint | 外包框的右上角点 |
c#中实现代码说明:
MxDrawDatabase databae = (MxDrawDatabase)axMxDrawX1.GetDatabase(); MxDrawBlockTable blkTab = databae.GetBlockTable(); MxDrawBlockTableIterator mBlockTableiter = blkTab.NewIterator(); for (; !mBlockTableiter.Done(); mBlockTableiter.Step()) { MxDrawBlockTableRecord blkRec = mBlockTableiter.GetRecord(); MxDrawBlockTableRecordIterator mBlockTableReciter = blkRec.NewIterator(); MxDrawPoint mMaxPt = new MxDrawPoint(); MxDrawPoint mMinPt = new MxDrawPoint(); MxDrawPoint mMaxPtTemp = new MxDrawPoint(); MxDrawPoint mMinPtTemp = new MxDrawPoint(); for (; !mBlockTableReciter.Done(); mBlockTableReciter.Step( true , false )) { MxDrawEntity ent = mBlockTableReciter.GetEntity(); ent.GetBoundingBox(out mMinPtTemp, out mMaxPtTemp); if ((mMinPtTemp == null ) || (mMaxPtTemp == null )) break ; 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 ((mMinPtTemp == null ) || (mMaxPtTemp == null )) continue ; 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(); } |
js中实现代码说明:
//返回控件的数据库对象 var databae = mxOcx.GetDatabase(); //返回数据库中的块表对象 var blkTab = databae.GetBlockTable(); //创建一个遍历块表中所有记录的遍历器 var mBlockTableiter = blkTab.NewIterator(); //移动当前遍历器位置 for (; !mBlockTableiter.Done(); mBlockTableiter.Step()) { //返回遍历器当前位置的记录 var blkRec = mBlockTableiter.GetRecord(); //创建一个遍历块表中所有记录的遍历器 var mBlockTableReciter = blkRec.NewIterator(); var mMaxPt = mxOcx.NewPoint(); var mMinPt = mxOcx.NewPoint(); var mMaxPtTemp = mxOcx.NewPoint(); var mMinPtTemp =mxOcx.NewPoint(); //移动当前遍历器位置 for (; !mBlockTableReciter.Done(); mBlockTableReciter.Step( true , false )) { //与用户交互在图面上选择一个实体 var ent = mBlockTableReciter.GetEntity(); //返回实体的最小矩形框外包 ent.GetBoundingBox(mMinPtTemp, mMaxPtTemp); if ((mMinPtTemp == null ) || (mMaxPtTemp == null )) break ; 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 ((mMinPtTemp == null ) || (mMaxPtTemp == null )) continue ; 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(); } } |
五、打碎块
主要用到函数说明:
IMxDrawBlockReference::Explode
打碎块引用,被打碎的引用对象还在,打碎的实体是新生成的。详细说明如下:
参数 | 说明 |
---|---|
[out,retval] IMxDrawResbuf** newIds | 返回打碎后新生成的实体id数组 |
c#中实现代码说明:
// 与用户交在图面上选择要打碎的块引对象 MxDrawUtility mxUtility = new MxDrawUtility(); object getPt;
{
for (Int32 i = 1; i < rb.Count;i++)
} } |
js中实现代码说明:
// 与用户交在图面上选择要打碎的块引对象 var ent = mxOcx.GetEntity( "选择图块:" ); if (ent == null ) { alert( "没有选择实体.." ); return ; } if (ent.ObjectName == "McDbBlockReference" ) { // 选择的是一个块对象,就调用打碎函数碎 var blkRef = ent; var rb = blkRef.Explode(); // 得到打碎后新生成的实体的id for ( var i = 1; i < rb.Count;i++) { var entId = rb.AtLong(i); } } |
六、改变块的颜色
颜色
实体的颜色可以通过0~256的数字值来指定和读取,也可以通过 McCmColor 类的实例来指定和读取,McCmColor类可以用来扩展颜色模型。McCmColor类可以用来表示真彩色中的任何一种色彩。
颜色值1~7代表的标准颜色如下:
颜色值 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
颜色 | 红 | 黄 | 绿 | 青 | 蓝 | 紫红 | 白或黑 |
颜色值7对应的颜色与控件背景颜色有关。如果背景是黑,则7对应的颜色是白,反之则对应的颜色是黑。
js中使用如下:
function changecolor1() { var database = mxOcx.GetDatabase(); var blkTab = database.GetBlockTable(); var iter = blkTab.NewIterator(); for (; !iter.Done(); iter.Step( true , false )) { var blkRec = iter.GetRecord(); var iter1 = blkRec.NewIterator(); for (; !iter1.Done(); iter1.Step()) { var tmpEnt = iter1.GetEntity(); tmpEnt.colorIndex = 1; } } } |
七、读取属性块
c#中使用如下:
MxDrawUtility mxUtility = new MxDrawUtility(); MxDrawPoint getPt; MxDrawEntity ent = mxUtility.GetEntity(out getPt, "\n 选择块引用实体:" ); if (ent == null ) { return ; } if (ent is MxDrawBlockReference) { MxDrawBlockReference blkRef = (MxDrawBlockReference)ent; // 读取块名。 mxUtility.Prompt( "\n Name" + blkRef.GetBlockName()); for (int j = 0; j < blkRef.AttributeCount; j++) { MxDrawAttribute attrib = blkRef.AttributeItem(j); mxUtility.Prompt( "\n Tag: " + attrib.Tag + " Text:" + attrib.TextString ); } // 从块记录中,得到提示文字. MxDrawBlockTableRecord blkRec = blkRef.BlockTableRecord; MxDrawBlockTableRecordIterator iter = blkRec.NewIterator(); for (; !iter.Done(); iter.Step( true , false )) { MxDrawEntity tmpEnt = iter.GetEntity(); if (tmpEnt is MxDrawAttributeDefinition) { MxDrawAttributeDefinition attribDef = (MxDrawAttributeDefinition)(tmpEnt); mxUtility.Prompt( "\n Tag: " + attribDef.Tag + " Tip:" + attribDef.Prompt ); } } } else { MessageBox.Show( "选择块引用实体" ); } |
js中使用如下:
function FindBlockAttrib() { var winWidth = 440; var winHeight = 140; var winLeft = (screen.width - winWidth) / 2; var winTop = (screen.height - winHeight) / 2 - 20; var str = 'dialogHeight:' + winHeight + 'px;dialogWidth:' + winWidth + 'px;dialogTop:' + winTop + 'px;dialogLeft:' + winLeft + 'px;resizable:yes;center:yes;status:no;' var rt = window.showModalDialog( "Gettext.htm?tmp=" + Math.random(), "输入属性文字" , str); var txt; if ( typeof (rt) == "undefined" ) { return ; } else { var arr = rt.split( "," ); txt = arr[0]; } var ss = mxOcx.NewSelectionSet(); var spFilte = mxOcx.NewResbuf(); // 把块对象,当着过滤条件. spFilte.AddStringEx( "INSERT" , 5020); // 得到图上,所有块对象. ss.Select2(5, null , null , null , spFilte); // 遍历每个块. for ( var i = 0; i < ss.Count; i++) { var ent = ss.Item(i); if (ent == null ) continue ; if (ent.ObjectName == "McDbBlockReference" ) { var blkref = ent; var j = 0; for (; j < blkref.AttributeCount; j++) { var attrib = blkref.AttributeItem(j); if (attrib.TextString == txt) { // 找到块属性。 mxOcx.PutEntityInView(ent.ObjectID, 300); var dLen = mxOcx.ViewLongToDocCoord(80); // 绘制一个标记圆. mxOcx.DrawVectorCircle(ent.Position.x, ent.Position.y, dLen, 65280); return ; } } } ent = null ; } alert( "没有找到块属性文字" ); // 在这里必须显示释放控件的COM对象指针. ss = null ; spFilte = null ; CollectGarbage(); } |
八、修改属性块
js中使用如下:
function ModifyBlockAttrib() { var ent = mxOcx.GetEntity( "选择图面的属性块:" ); if (ent == null ) { return ; } if (ent.ObjectName != "McDbBlockReference" ) { alert( "选择的对象不是块引用" ); } var blkref = ent; if (blkref.AttributeCount == 0) alert( "图块没有属性" ); //遍历所有属性,修改属性值 var i = 0; for (; i < blkref.AttributeCount; i++) { var attrib = blkref.AttributeItem(i); attrib.TextString += "<被修改>" } // 通知块引,它的属性被修改,用于更新显示. blkref.AssertWriteEnabled(); } |
九、得到所有图块名
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); } |
十、将图块替换成dwg文件
js中使用如下:
var ent = mxOcx.GetEntity( "选择图面的属性块:" ); if (ent == null ) { return ; } if (ent.ObjectName != "McDbBlockReference" ) { alert( "选择的对象不是块引用" ); } else { var blkref = ent; var sBlkFilePath = mxOcx.GetOcxAppPath() + "\\Blk\\Rect.dwg" ; mxOcx.InsertBlock(sBlkFilePath, "MyBlk2" ); var curDatabase = mxOcx.GetDatabase(); var blkTable = curDatabase.GetBlockTable(); var myblkRec = blkTable.GetAt( "MyBlk2" , false ); if (myblkRec == null ) { return ; } blkref.BlockTableRecordId = myblkRec.ObjectID; } |
十一、插入图块前修改图块文字
c#中使用如下:
// 插入块的块名。 String sBlkName = "MyBlkName" ; // 把要插入的块文件引入到控件数据库中。 String sBlkFile = axMxDrawX1.GetOcxAppPath() + "\\Blk\\attribblock.dwg" ; axMxDrawX1.InsertBlock(sBlkFile, sBlkName); // 得到当前应用对象 MxDrawApplication app = new MxDrawApplication(); // 取到控件数据库 MxDrawDatabase database = app.WorkingDatabase(); // 得到块表对象 MxDrawBlockTable blkTable = database.GetBlockTable(); // 当前块表中是否已经有名为sBlkName的块表记录 MxDrawBlockTableRecord blkRec = blkTable.GetAt(sBlkName, true ); if (blkRec == null ) { // 证明InsertBlock函数失败,没有成功把文件插入数据库中。 return ; } // 创建一个用于遍历块表遍历器 MxDrawBlockTableRecordIterator iter = blkRec.NewIterator(); if (iter == null ) return ; for (; !iter.Done(); iter.Step( true , false )) { // 得到遍历器当前的实体 MxDrawEntity ent = iter.GetEntity(); if (ent == null ) continue ; if (ent.ObjectName == "McDbText" ) { // 当前实体是一个文字 MxDrawText text = (MxDrawText)ent; // 修改文字内容为 "MyContent" text.TextString = "MyContent" ; } } // 创建块引用,把图块插入到当前空间,并显示。 axMxDrawX1.DrawBlockReference(0, 0, sBlkName, 1.0, 0.0); |
js中使用如下:
// 插入块的块名。 var sBlkName = "MyBlkName" ; // 把要插入的块文件引入到控件数据库中。 var sBlkFile = mxOcx.GetOcxAppPath() + "\\Blk\\attribblock.dwg" ; mxOcx.InsertBlock(sBlkFile, sBlkName); // 取到控件数据库 var database = mxOcx.GetDatabase(); // 得到块表对象 var blkTable = database.GetBlockTable(); // 当前块表中是否已经有名为sBlkName的块表记录 var blkRec = blkTable.GetAt(sBlkName, true ); if (blkRec == null ) { // 证明InsertBlock函数失败,没有成功把文件插入数据库中。 return ; } // 创建一个用于遍历块表遍历器 var iter = blkRec.NewIterator(); if (iter == null ) return ; for (; !iter.Done(); iter.Step( true , false )) { // 得到遍历器当前的实体 var ent = iter.GetEntity(); if (ent == null ) continue ; if (ent.ObjectName == "McDbText" ) { // 当前实体是一个文字 var text =ent; // 修改文字内容为 "MyContent" text.TextString = "MyContent" ; } } // 创建块引用,把图块插入到当前空间,并显示。 mxOcx.DrawBlockReference(0, 0, sBlkName, 1.0, 0.0); |
十二、获取块文字信息
js中使用如下:
var database = mxOcx.GetDatabase(); var blkTab = database.GetBlockTable(); var iter = blkTab.NewIterator(); for (; !iter.Done(); iter.Step( true , false )) { var blkRec = iter.GetRecord(); var iter1 = blkRec.NewIterator(); for (; !iter1.Done(); iter1.Step()) { var tmpEnt = iter1.GetEntity(); if (tmpEnt == null ) continue ; if (tmpEnt.ObjectName == "McDbText" ) { // 当前实体是一个文字 var text =tmpEnt; var txt=text.TextString; alert(txt.toString()); } } } |
十三、修改块指定文字
js中使用如下:
var database = mxOcx.GetDatabase(); var blkTab = database.GetBlockTable(); var iter = blkTab.NewIterator(); for (; !iter.Done(); iter.Step( true , false )) { var blkRec = iter.GetRecord(); var iter1 = blkRec.NewIterator(); for (; !iter1.Done(); iter1.Step()) { var tmpEnt = iter1.GetEntity(); if (tmpEnt == null ) continue ; if (tmpEnt.ObjectName == "McDbText" ) { // 当前实体是一个文字 var text =tmpEnt; if (text.TextString == "123" ){ // 修改文字内容为 "MyContent" text.TextString = "MyContent" ; } } } } |
十四、根据块名过滤
js中使用如下:
var ss = mxOcx.NewSelectionSet(); // 创建过滤对象. var spFilte = mxOcx.NewResbuf(); // 把图块对象,当着过滤条件. spFilte.AddStringEx( "INSERT" , 5020); // 过滤块名 spFilte.AddStringEx( "123" ,2); // 得到图上,所有图块对象. ss.Select2(5, null , null , null , spFilte); var sTip = "共有" + ss.Count + "个图块" ; alert(sTip); |