www.mxdraw.com
内容索引主面
前一个向上下一个
McDbLinetypeTable 类

数据库中的线型表,所有的线型定义都存放在这里

C++
class McDbLinetypeTable : public McDbSymbolTable;

McDbSymbolTable.h

例如:得到所有的线型

    void GetAllLineTypeName()
    {
        // 读方式打开线型表
        McDbLinetypeTablePointer spLinetypeTable(acdbCurDwg(),McDb::kForRead);
        if(spLinetypeTable.openStatus() != Mcad::eOk)
            return;

        // 创建一个遍历器,准备遍历块表

        McDbLinetypeTableIterator* pIterator = NULL;
        spLinetypeTable->newIterator(pIterator);

        if(pIterator == NULL)
            return;
        // 把遍历器的指针交给智能指针管理
        std::auto_ptr<McDbLinetypeTableIterator> spIterator(pIterator);

        // 遍历所有记录

        for(;!pIterator->done();pIterator->step())
        {
            McDbObjectId id;
            pIterator->getRecordId(id);

            // 读方式打开记录
            McDbObjectPointer<McDbLinetypeTableRecord> spLinetypeRec(id,McDb::kForRead);
            if(spLinetypeRec.openStatus() != Mcad::eOk)
                continue;
            LPCTSTR pszName = NULL;
            spLinetypeRec->getName(pszName);

            CString sT;
            sT.Format(_T("发现线型名:%s"),pszName);
            AfxMessageBox(sT);
        }
    };

例如:向控件增加一个简单线型

    AcDbObjectId AddLinetype(MxDraw::MXDRAWOCXHANDLE m_hDrawOcx,
                             LPCTSTR pszName, LPCTSTR pszLineDefine)
    {
        CString sName(pszName);
        if(sName.IsEmpty() )
            return AcDbObjectId::kNull;

        {
            AcDbLinetypeTableRecordPointer spLinTypeTabRec(sName,
                MxDraw::GetDatabase(m_hDrawOcx),McDb::kForRead);
            if(spLinTypeTabRec.openStatus() == Mcad::eOk)
            {
                // 控件内已经有该名称的线型了。

                return AcDbObjectId::kNull;
            }
        }

        AcDbLinetypeTablePointer spLineTypeTable(MxDraw::GetDatabase(m_hDrawOcx),AcDb::kForWrite);
        if(spLineTypeTable.openStatus() != Acad::eOk)
        {
            return AcDbObjectId::kNull;
        }

        // 读取线数据定义。
        std::vector<double> vecLineData;

        MxStrList list(pszLineDefine);
        int iCount = list.GetCount();
        for(int i = 0; i < iCount;i++)
        {
            double d = list.GetDoubleAt(i);

            vecLineData.push_back(d);

        }

        if(vecLineData.size() < 1)
        {
            return AcDbObjectId::kNull;
        }

        // 创建线形数据。

        AcDbLinetypeTableRecord* pNewRec = new AcDbLinetypeTableRecord;
        CString strT;
        strT.Format(_T("custom line type: %s"),sName);
        pNewRec->setName(sName);
        pNewRec->setAsciiDescription(strT);
        pNewRec->setPatternLength(1.0);
        pNewRec->setNumDashes((int)(vecLineData.size() ) );

        for(unsigned int k = 0; k < vecLineData.size();k++)
        {
            pNewRec->setDashLengthAt(k,vecLineData[k]);
        }

        AcDbObjectId linetypeId;
        spLineTypeTable->add(linetypeId,pNewRec);
        pNewRec->close();

        return linetypeId;
    }

    // 调用代码
    AddLinetype(MxDraw::GetCurOcxHandle(),
                _T("MyLineType"), _T("6,-10"));

例如:向控件增加一个带文字的线型

    McDbObjectId AddLinetypeEx(MxDraw::MXDRAWOCXHANDLE m_hDrawOcx,
                               LPCTSTR pszName, LPCTSTR pszLineDefine,
                               LPCTSTR pszTextStyle)
    {
        CString sName(pszName);
        if(sName.IsEmpty() )
            return McDbObjectId::kNull;

        CString sTextStyle(pszTextStyle);
        if(sTextStyle.IsEmpty() )
            return McDbObjectId::kNull;


        {
            AcDbLinetypeTableRecordPointer spLinTypeTabRec(sName,
                MxDraw::GetDatabase(m_hDrawOcx),McDb::kForRead);
            if(spLinTypeTabRec.openStatus() == Mcad::eOk)
            {
                // 控件内已经有该名称的线型了。

                return McDbObjectId::kNull;
            }
        }

        McDbObjectId lineTypeId;
        {
            McDbTextStyleTableRecordPointer spTextStyleTabRec(sTextStyle,
                MxDraw::GetDatabase(m_hDrawOcx),McDb::kForRead);
            if(spTextStyleTabRec.openStatus() != Mcad::eOk)
            {

                return McDbObjectId::kNull;
            }

            lineTypeId = spTextStyleTabRec->objectId();
        }


        AcDbLinetypeTablePointer spLineTypeTable(MxDraw::GetDatabase(m_hDrawOcx),AcDb::kForWrite);
        if(spLineTypeTable.openStatus() != Acad::eOk)
        {
            return McDbObjectId::kNull;
        }


        enum UnitType
        {
            kInvalid= 0,        // 无效的类型,用于dwgOutFields,dwgInFields
            kNormal = 1,        // 正常的Dashe数据
            kText   = 2,        // 复合的文字的数据,因为线型中可以有文字
            kShap   = 3         // 复合的形数据
        };


        enum RotationType
        {
            kA,     // 旋转角度是相对于坐标系统的X轴

            kR      // 旋转角度是相对曲线该点处的切线方向
        };


        // 临时的线型数据。

        struct stuLineUnit
        {
            UnitType type;              // 数据类型

            double d;                   // Dashe的长度

            CString sTxt;                       // 文字内容
            McDbHardPointerId  textStyleId; // 文字风格ID。

            double  dScale;                     // 缩放比例
            RotationType     rotationType;          //  旋转角度的类型

            double  dRotation;                  // 旋转角度
            double  dXoffset;                   // 文字在线型上位置的X偏移
            double  dYoffset;                   // 文字在线型上位置的Y偏移   

            int     iShapeNumber;               // 形在形文件中的编号.

            stuLineUnit()
            {
                d = 0.0;
                type = kNormal;

                dScale = 1.0;
                rotationType = kR;
                dRotation = 0.0;
                dXoffset = 0.0;
                dYoffset = 0.0;

                iShapeNumber = 0;
            }
        };


        // 读取线数据定义。
        std::vector<stuLineUnit> vecLineData;

        CStrList list(pszLineDefine);
        int iCount = list.GetCount();
        for(int i = 0; i < iCount;i++)
        {
            stuLineUnit tmp;
            tmp.textStyleId = lineTypeId;

            CString sShpDef = list.GetStrAt(i);
            if(Mx::IsNumerical(sShpDef) )
            {
                tmp.d       = _tstof(sShpDef);
                tmp.type    = kNormal;
                vecLineData.push_back(tmp);
            }
            else
            {
                // 可能是个复杂的形文字。

                if(sShpDef.IsEmpty()
                    || sShpDef[0] != _T('(')
                    || sShpDef[sShpDef.GetLength() - 1] != _T(')')
                    )
                    continue;

                bool isGetTxt     = false;
                bool iShapeNumber = false;

                CStrList lstShpDef(sShpDef);
                int iShpDefCount = lstShpDef.GetCount();
                for(int j = 0; j < iShpDefCount;j++)
                {
                    CString sTmpV = lstShpDef.GetStrAt(j);
                    if(sTmpV.IsEmpty() || sTmpV.GetLength() < 3)
                        continue;

                    CString sFlag = sTmpV.Left(2);
                    CString sDefV = sTmpV.Mid(2); 
                    sFlag.MakeUpper();
                    if(sFlag == _T("T=") )
                    {
                        isGetTxt = true;
                        tmp.sTxt = sDefV;
                        tmp.iShapeNumber = 0;
                        tmp.type = kText;
                    }
                    else if(sFlag == _T("I=") )
                    {
                        if(Mx::IsNumerical(sDefV) )
                        {
                            tmp.iShapeNumber = _tstoi(sDefV);
                            if(tmp.iShapeNumber > 0)
                            {
                                tmp.sTxt = _T("");
                                iShapeNumber = true;
                                tmp.type = kShap;
                            }   
                        }
                    }
                    else if(sFlag == _T("X=") )
                    {
                        if(Mx::IsNumerical(sDefV) )
                        {
                            tmp.dXoffset = _tstof(sDefV);
                        }
                    }

                    else if(sFlag == _T("Y=") )
                    {
                        if(Mx::IsNumerical(sDefV) )
                        {
                            tmp.dYoffset = _tstof(sDefV);
                        }
                    }

                    else if(sFlag == _T("S=") )
                    {
                        if(Mx::IsNumerical(sDefV) )
                        {
                            tmp.dScale = _tstof(sDefV);
                        }
                    }

                    else if(sFlag == _T("R=") )
                    {
                        if(Mx::IsNumerical(sDefV) )
                        {
                            tmp.dRotation = _tstof(sDefV);
                            tmp.dRotation *= 3.14159265 / 180.0;
                            tmp.rotationType = kR;
                        }
                    }

                    else if(sFlag == _T("A=") )
                    {
                        if(Mx::IsNumerical(sDefV) )
                        {
                            tmp.dRotation = _tstof(sDefV);
                            tmp.dRotation *= 3.14159265 / 180.0;
                            tmp.rotationType = kA;
                        }
                    }

                    else if(sFlag == _T("L=") )
                    {
                        if(Mx::IsNumerical(sDefV) )
                        {
                            tmp.d = _tstof(sDefV);
                        }
                    }
                }

                if(isGetTxt || iShapeNumber)
                {
                    vecLineData.push_back(tmp);
                }

            }

        }

        if(vecLineData.size() < 1)
        {
            return 0;
        }

        // 创建线形数据。

        AcDbLinetypeTableRecord* pNewRec = new AcDbLinetypeTableRecord;
        CString strT;
        strT.Format(_T("custom line type: %s"),sName);
        pNewRec->setName(sName);
        pNewRec->setAsciiDescription(strT);
        pNewRec->setPatternLength(1.0);
        pNewRec->setNumDashes((int)(vecLineData.size() ) );

        for(unsigned int k = 0; k < vecLineData.size();k++)
        {
            stuLineUnit tmp = vecLineData[k];
            pNewRec->setDashLengthAt(k,tmp.d);
            if(tmp.type == kText || tmp.type == kShap)
            {
                pNewRec->setShapeStyleAt(k,tmp.textStyleId);
                pNewRec->setShapeNumberAt(k,tmp.iShapeNumber);

                McGeVector2d vec(tmp.dXoffset,tmp.dYoffset);
                pNewRec->setShapeOffsetAt(k,vec);

                pNewRec->setShapeScaleAt(k,tmp.dScale );
                pNewRec->setShapeRotationAt(k,tmp.dRotation);
                pNewRec->setShapeIsUcsOrientedAt(k,tmp.rotationType == kA);
                pNewRec->setTextAt(k,tmp.sTxt);
            }
        }

        AcDbObjectId linetypeId;
        spLineTypeTable->add(linetypeId,pNewRec);
        pNewRec->close();

        return (LONG)(linetypeId.asOldId() );
    }
    // 调用代码
    AddLinetypeEx(_T("MyLineType3"), 
                  _T("(12.7,("T=MxDraw","S=2.54","L=-5.08","R=0.0","X=-2.54","Y=-1.27"),-10.08)"),
                  _T("MyLineTypeTextStyle") );
Copyright (c) 2010. All rights reserved.
你认为该帮助怎么样? 发送反馈信息!