www.mxdraw.com
内容索引主面
前一个向上下一个
IMxDrawDatabase 接口

控件的数据库对象,图形中的所有信息都存放在该对象中 注意:在一些具有内存自动回收的语言中使用控件,因回收的事件不确定,容易引起错误, 对控件的一些对象需要显示释放内存。比如,所有遍历器对象,MxDrawResbuf对象。 如C#语言: 调用代码如下 System.Runtime.InteropServices.Marshal.ReleaseComObject(iter); Js语言: 代码如下: iter = null; CollectGarbage(); 支持扩展属性"GetVariable,系统变量名",得到系统变量;

IDL
[ object, uuid(302F93D2-04D6-44CB-A4D7-57649DE748C4), dual, nonextensible, helpstring("IMxDrawDatabase interface"), pointer_default(unique) ]
interface IMxDrawDatabase : IMxDrawMcRxObject;

MxDrawDatabase.idl

例如: C#读取OSMDOE系统变量值

        MxDrawDatabase database = (MxDrawDatabase)axMxDrawX1.GetDatabase();
        MxDrawResbuf val = database.GetProp("GetVariable,OSMODE");
        if (val != null)
        {
            MessageBox.Show(val.AtLong(0).ToString());
        }

支持扩展属性"title",类型为: CString,得到函数:getTitle,设置函数setTitle; 

例如: C#读写 dwg文件的标题.

        MxDrawDatabase database = (MxDrawDatabase)axMxDrawX1.GetDatabase();
        MxDrawResbuf param = new MxDrawResbuf();
        param.AddString("MyTitleVal");

        // 设置dwg文件说明信息标题为"MyTitleVal"
        database.SetProp("setTitle", param);

        // 取dwg文件说明信息标题.
        MxDrawResbuf ret = database.GetProp("getTitle");
        MessageBox.Show(ret.AtString(0) );

 

例如: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());
        }
        // 需要显示释放递代器.
      System.Runtime.InteropServices.Marshal.ReleaseComObject(iter);
    }

例如: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;

例如: JS代码

        function GetAllEntity()
        {

            var axMxDrawX1 = document.getElementById("MxDrawXCtrl");

                    // 得到当前图纸空间
                    var blkRec = axMxDrawX1.GetDatabase().CurrentSpace();
              // 创建一个用于遍历当前图纸空间的遍历器
                    var iter = blkRec.NewIterator();

                var iNum = 0;  // 循环得到所有实体

            if (iter!= null)
                    {

                    for (; !iter.Done(); iter.Step(true, false))
                    {
                iNum++;

                        // 得到遍历器当前的实体
                        var ent = iter.GetEntity();

                        if (ent.ObjectName == "McDbLine")
                {
                    // var line = ent;// 当前实体是一个直线
                    ;

                }
                        else if (ent.ObjectName=="McDbBlockReference")
                        {
                           MxDrawBlockReference blkRef = ent;// 当前实体是一个块引用
                            for (var j = 0; j < blkRef.AttributeCount; j++)
                            {
                                // 得到块引用中所有的属性
                               var attrib = blkRef.AttributeItem(j);
                                mxUtility.Prompt("n Tag: " + attrib.Tag + "Text:" + attrib.TextString);
                            }

                        }
                        // else if (ent is 其它类型)
                        //{
                        //  ... ....
                        //}

                    }

             }

            var sT;
            sT = "发现" + iNum + "个实体";
            alert(sT);
            // 注意需要显示释放递代器.,不然会引起错误
            iter = null;
            CollectGarbage();
        }

例如: JS 查找图上,某个文字,并定位过去

                function FindText() {

                    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("TEXT",5020);

                    // 得到图上,所有文字对象.
                    ss.Select2(5,null,null,null,spFilte);


                    // 遍历每个文字.
                    var bFind = false;
                    for(var i = 0; i < ss.Count;i++)
                    {
                        var ent = ss.Item(i);
                        if(ent == null)
                            continue;
                        if(ent.ObjectName != "McDbText")
                            continue;

                        var sTxt = ent.TextString;

                        if (sTxt == txt) {
                            // 把文字放到视区中间.
                            mxOcx.PutEntityInView(ent.ObjectID, 300);


                            var dLen = mxOcx.ViewLongToDocCoord(80);

                            // 绘制一个标记圆.
                            mxOcx.DrawVectorCircle(ent.Position.x,
                                ent.Position.y,
                                dLen, 65280);
                            bFind = true
                        }
                        ent = null;

                    }

                    if (!bFind) {
                    alert("没有找到文字对象");
                    }


                    // 在这里必须显示释放控件的COM对象指针.
                    ss = null;
                    spFilte = null;
                    CollectGarbage();
                }

例如:VC代码,找到图上某个文字

    void CTestOcxForVc6Dlg::FindText(CString sText)
    {
        MxDrawXLib::IMxDrawSelectionSetPtr ssGet;
        ssGet.CreateInstance(__uuidof( MxDrawXLib::MxDrawSelectionSet ));
        if(ssGet == NULL)
        {
            return;
        }
        ssGet->Select(MxDrawXLib::mcSelectionSetAll);

        if(ssGet->GetCount() == 0)
            return;

        for(long i = 0; i < ssGet->GetCount();i++)
        {
            MxDrawXLib::IMxDrawEntityPtr spEnt =  ssGet->Item(i);
            if(spEnt == NULL)
                continue;

            CComPtr<IMxDrawText> spText;
            spEnt->QueryInterface(&spText);

            CComPtr<IMxDrawMText> spMText;
            spEnt->QueryInterface(&spMText);

            if(spText != NULL)
            {
                // 是一个文字.

                CString sVal;
                _bstr_t sBstr= spText->GetTextString();

                sVal= CString(LPCTSTR(sBstr) );

                if(sVal.Find(sText) != -1)
                {
                    // 找到要找的文字.

                    m_ocx.TwinkeEnt(spEnt->ObjectID);
                    return;
                }
            }
            else if(spMText != NULL)
            {
                // 是个多行文字.
                CString sVal;
                _bstr_t sBstr =  spMText->GetContents();

                sVal= CString(LPCTSTR(sBstr) );

                if(sVal.Find(sText) != -1)
                {
                    // 找到要找的文字.

                    m_ocx.TwinkeEnt(spEnt->ObjectID);
                    return;
                }


            }

        }
    }

例如: VC, 查找图上文字,找到文字内空为"AAA"的文字,并把文字放到视区中间,使用标记圆标出.

        IMxDrawSelectionSetPtr ss;
        ss.CreateInstance(__uuidof( MxDrawXLib::MxDrawSelectionSet ));

        IMxDrawResbufPtr spFilte;
        spFilte.CreateInstance(__uuidof( MxDrawXLib::MxDrawResbuf ));

        // 把文字对象,当着过滤条件.
        spFilte->AddStringEx("TEXT",5020);

        // 得到图上,所有文字对象.
        ss->Select(mcSelectionSetAll,NULL,NULL,spFilte);


        // 遍历每个文字.
        for(long i = 0; i < ss->Count;i++)
        {
            IMxDrawEntityPtr ent = ss->Item(i);
            if(ent == NULL)
                continue;
            IMxDrawTextPtr spTxt;
            ent->QueryInterface(&spTxt);

            if(spTxt == NULL)
                continue;

            CString sTxt = spTxt->TextString;

            if(sTxt == "AAA")
            {
                // 把文字放到视区中间.
                m_ocx.PutEntityInView(spTxt->ObjectID,300);


                double dLen = m_ocx.ViewLongToDocCoord(80);

                // 绘制一个标记圆.
                m_ocx.DrawVectorCircle(spTxt->Position->x,
                    spTxt->Position->y,
                    dLen, 65280);

    
            }

        }
Copyright (c) 2010. All rights reserved.
你认为该帮助怎么样? 发送反馈信息!