本帖最后由 mxdwg 于 2010-12-29 09:29 编辑
后面会不停更新使用梦想控件中objectarx编程接口和Vba编程接口的例程代码,主要是一些常用功能的函数代码,
例程的代码的编译运行需要控件库才行,最新控件库下载连接:http://www.mxdraw.com/?p=9,技术交流QQ群:112199959
VC: 遍历当前块表记录中的所有实体,并修改颜色的代码
- void ModifyAllEntity()
- {
- // 下面读取图中指定块的属性。
- McDbBlockTableRecordPointer spBlkRec(MCDB_MODEL_SPACE,
- acdbCurDwg(),McDb::kForRead);
- if(spBlkRec.openStatus() != Mcad::eOk)
- {
- return;
- }
- // 生成遍历数据库用的遍历器.
- McDbBlockTableRecordIterator* pIterator = NULL;
- spBlkRec->newIterator(pIterator);
- if(pIterator == NULL)
- return;
- std::auto_ptr<McDbBlockTableRecordIterator> spIter(pIterator);
- // 遍历数据库
- for(;!pIterator->done();pIterator->step() )
- {
- McDbObjectId id;
- pIterator->getEntityId(id);
- McDbObjectPointer<AcDbEntity> spEntity(id,McDb::kForWrite);
- if(spEntity.openStatus() != Mcad::eOk)
- continue;
- spEntity->setColorIndex(MrxDbgUtils::kWhite);
- if(McDbLine::cast(spEntity.object()) != NULL)
- {
- // 是个直线实体
- McDbLine* pLine = McDbLine::cast(spEntity.object());
- }
- else if(McDbArc::cast(spEntity.object()) != NULL)
- {
- // 是个圆弧实体
- McDbArc* pArc = McDbArc::cast(spEntity.object());
- }
- else if(McDbText::cast(spEntity.object()) != NULL)
- {
- // 是个文字实体
- McDbText* pText = McDbText::cast(spEntity.object());
- }
- /*
- else if... ...
- */
- }
- }
复制代码 VB6代码,得到图纸空间中所有实体
- '得到当前图纸空间中所有实体的代码演示
- '当前的图纸空间
- Dim curSpace8 As MxDrawXLib.MxDrawBlockTableRecord
- Set curSpace8 = app.WorkingDatabase.CurrentSpace
- ' 工具对象,用于向命令行输入出字符串
- Dim mxUtility8 As MxDrawXLib.MxDrawUtility
- Set mxUtility8 = New MxDrawXLib.MxDrawUtility
- '遍历器,用于遍历当前图纸空间中所有的实体
- Dim iter As MxDrawXLib.MxDrawBlockTableRecordIterator
- Set iter = curSpace8.NewIterator
- Dim ptDim1 As MxDrawXLib.MxDrawPoint
- Dim ptDim2 As MxDrawXLib.MxDrawPoint
- If (iter Is Nothing) Then
- Exit Sub
- End If
- '循环得到所有实体
- Do While iter.Done = False
- ' 得到遍历器当前的实体
- Dim ent8 As MxDrawXLib.MxDrawEntity
- Set ent8 = iter.GetEntity()
- If (ent8 Is Nothing) = False Then
- If TypeOf ent8 Is MxDrawXLib.MxDrawText Then
- '当前实体是个文字实体
- Dim text8 As MxDrawXLib.MxDrawText
- Set text8 = ent8
- mxUtility8.Prompt Chr(13) + Chr(10) + text8.TextString
- ElseIf TypeOf ent8 Is MxDrawXLib.MxDrawPolyline Then
- '当前实体是一个多义线
- Dim polyline As MxDrawXLib.MxDrawPolyline
- Set polyline = ent8
- mxUtility8.Prompt Chr(13) + Chr(10) + "发现Polyline曲线, 下面是它的坐标信息:"
- '得到Polyline的端点坐标
- Dim iNum As Long
- iNum = 0
- Do While iNum < polyline.NumVerts
- Dim tmpPt As MxDrawXLib.MxDrawPoint
- Set tmpPt = polyline.GetPointAt(iNum)
- mxUtility8.Prompt Chr(13) + Chr(10) + "坐标为x:" & tmpPt.x & "y:" & tmpPt.y & "凸度为:" & polyline.GetBulgeAt(iNum)
- iNum = iNum + 1
- Loop
- ElseIf TypeOf ent8 Is MxDrawXLib.MxDrawDimAligned Then
- Dim dimAligned8 As MxDrawXLib.MxDrawDimAligned
- Set dimAligned8 = ent8
- mxUtility8.Prompt Chr(13) + Chr(10) + dimAligned8.DimensionText
- '当DimensionText返回为空时,可以用下面的ptDim1,ptDim2两点的距离算出标注内容。
- Set ptDim1 = dimAligned8.XLine1Point
- Set ptDim2 = dimAligned8.XLine2Point
- ElseIf TypeOf ent8 Is MxDrawXLib.MxDrawDimRotated Then
- Dim dimRotated8 As MxDrawXLib.MxDrawDimRotated
- Set dimRotated8 = ent8
- mxUtility8.Prompt Chr(13) + Chr(10) + dimRotated8.DimensionText
- '当DimensionText返回为空时,可以用下面的ptDim1,ptDim2两点的距离算出标注内容。
- Set ptDim1 = dimRotated8.XLine1Point
- Set ptDim2 = dimRotated8.XLine2Point
- ' ElseIf TypeOf ent8 Is 其它类型 Then
- ' ... ...
- End If
- End If
- iter.Step
- Loop
C#代码,得到图纸空间中所有实体
- private void GetAllEntity()
- {
- try
- {
- MxDrawApplication app = new MxDrawApplication();
- MxDrawUtility mxUtility = new MxDrawUtility();
- // 得到当前图纸空间
- MxDrawBlockTableRecord blkRec = app.WorkingDatabase().CurrentSpace();
- // 创建一个用于遍历当前图纸空间的遍历器
- MxDrawBlockTableRecordIterator iter = blkRec.NewIterator();
- if (iter == null)
- return;
- // 所有实体的id数组。
- List<Int32> aryId = new List<Int32>();
- int iLineNum = 0;
- // 循环得到所有实体
- for (; !iter.Done(); iter.Step(true, false))
- {
- // 得到遍历器当前的实体
- MxDrawEntity ent = iter.GetEntity();
- if (ent == null)
- continue;
- // 得到实体的id
- aryId.Add(ent.ObjectID);
- if (ent is MxDrawLine)
- {
- // 当前实体是一个直线
- MxDrawLine line = (MxDrawLine)ent;
- iLineNum++;
- }
- else if (ent is MxDrawBlockReference)
- {
- // 当前实体是一个块引用
- MxDrawBlockReference blkRef = (MxDrawBlockReference)ent;
- for (int j = 0; j < blkRef.AttributeCount; j++)
- {
- // 得到块引用中所有的属性
- MxDrawAttribute attrib = blkRef.AttributeItem(j);
- mxUtility.Prompt("n Tag: " + attrib.Tag + "Text:" + attrib.TextString);
- }
- }
- // else if (ent is 其它类型)
- //{
- // ... ....
- //}
- }
- String sT;
- sT = "发现" + aryId.Count + "个实体,其中有" + iLineNum + "个直线";
- MessageBox.Show(sT);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString());
- }
- }
Delphi中的例程,遍历图中的所有实体,找到块名为A23TIT_KH的图块,并得到它的属性
- procedure TForm1.Button1Click(Sender: TObject);
- var
- curDababase : IMxDrawDatabase;
- modleSpace : IMxDrawBlockTableRecord ;
- newIterator : IMxDrawBlockTableRecordIterator ;
- pEnt : IMxDrawEntity;
- pBlkRef : IMxDrawBlockReference;
- pAttribute : IMxDrawAttribute;
- tag : WideString;
- text : WideString;
- ii : Integer;
- fModel : WideString;
- fTitle : WideString;
- fDwgNo : WideString;
- fMaterial : WideString;
- fQuantity : WideString;
- fRemark : WideString;
- fDrawData : WideString;
- fScale : WideString;
- fVersion : WideString;
- fReview : WideString;
- fDrawName : WideString;
- fApprove : WideString;
- begin
- curDababase := MxDrawApplication1.WorkingDatabase;
- modleSpace := curDababase.CurrentSpace ;
- newIterator := modleSpace.NewIterator ;
- if newIterator <> nil then
- begin
- while newIterator.Done() = False do
- begin
- pEnt := newIterator.GetEntity();
- newIterator.Step(True,False);
- if pEnt <> nil then
- begin
- pEnt.QueryInterface(IMxDrawBlockReference, pBlkRef);
- if pBlkRef <> nil then
- begin
- if pBlkRef.GetBlockName() = 'A23TIT_KH' then
- begin
- for ii := 0 to pBlkRef.AttributeCount -1 do
- begin
- pAttribute := pBlkRef.AttributeItem(ii);
- tag := pAttribute.Get_Tag();
- text := pAttribute.Get_TextString();
- showmessage(tag + ':' + text);
- case ii of
- 0: fModel := text;
- 1: fTitle := text;
- 2: fDwgNo := text;
- 3: fMaterial := text;
- 4: fQuantity := text;
- 5,6,7: fRemark := fRemark + text + ' ';
- 8: fDrawData := text;
- 9: fScale := text;
- 10: fVersion := text;
- 11: fReview := text;
- 12: fDrawName := text;
- 13: fApprove := text;
- end;
- end;
- end;
- end;
- end;
- end;
- end;
- if text = '' then
- begin
- showmessage('没有发现块名为' + 'A23TIT_KH' + '的块实体');
- end;
- end;
复制代码
|