小庄
小庄
Published on 2024-12-20 / 8 Visits
0

常用代码段

全局函数

acdbHostApplicationServices()->workingDatabase()->dimscale(); //全局比例
acdbHostApplicationServices()->workingDatabase()->dimtxt(); //标注文字高度

//类似于非模态对话框,需要锁定和解锁文档  
acDocManager->lockDocument(acDocManager->curDocument());  
acDocManager->unlockDocument(acDocManager->curDocument());

acDocManager->sendStringToExecute(acDocManager->curDocument(), _T("CXZ_查看DXF\n"));//使用CAD命令
Acad::ErrorStatus es = acDocManager->appContextOpenDocument(_T("F:\\11.dwg"));//打开图纸

acutPrintf(_T("\n错误码= %s"),acadErrorStatusText(es));

AcApDocument * pDoc = acDocManager->curDocument();  //获取当前DWG路径
CString dwgPath = pDoc->fileName();

objId.objectClass()->isDerivedFrom(AcDbText::desc())  //对象ID判断类型
Ent->isKindOf(AcDbPolyline::desc())  //对象判断类型

AcGePoint3d (AcGePoint2d )转换 ads_point 用:asDblArray函数。
ads_point 转换AcGePoint2d  用asPnt2d(const double &pnt) 或aspnt3d
acdbUcs2Wcs(ptAds, asDblArray(m_ptStart) ,false), 是表示在CAD中的转换成常用坐标系WCS 进行转换

数据库遍历

AcDbBlockTable* pBlkTbl = NULL;
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
pDb->getBlockTable(pBlkTbl, AcDb::kForRead);
AcDbBlockTableRecord* pBlkTblRcd = NULL;
pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd, AcDb::kForRead);
pBlkTbl->close();
AcDbBlockTableRecordIterator* pIter = NULL;
pBlkTblRcd->newIterator(pIter);
AcDbObjectId objId;
for (pIter->start(); !pIter->done(); pIter->step())
{
    //获取实体Id
    if (pIter->getEntityId(objId) == Acad::eOk)
    {
        if (objId.objectClass()->isDerivedFrom(AcDbText::desc()) && eId != objId)
        {
            if (acdbOpenObject(pText, objId, AcDb::kForRead) == Acad::eOk)
            {
                pt = pText->position();
                pText->close();
            }
        }
    }
}
//关闭块表
pBlkTblRcd->close();
//删除块表记录迭代器
delete pIter;

acedGrRead 动态交互

void cs()
{
    AcGePoint3d pt;
    int track = 1, type; // 控制位和输入设备类型 8 取消ESC报错
    struct resbuf result;  // 保存鼠标拖动时的动态坐标
    while (track > 0)
    {
        acedGrRead(track, &type, &result); // 追踪光标移动
        pt[X] = result.resval.rpoint[X];  // 获得用户输入点的位置
        pt[Y] = result.resval.rpoint[Y];
        // 使用获得的坐标
        {
            acutPrintf(_RXST("\nX:%f  Y:%f"), pt[X], pt[Y]);
        }
        switch (type)
        {
        case 2:  //键盘操作
            acutPrintf(_RXST("\n键码为:%d"), result.resval.rint);
            break;
        case 3:  // 如果用户按下了鼠标左键 选点 ,跳出循环
            track = 0;
            break;
        default:
            break;
        }
    }
}

AcDbPolyline 二维多段线顶点遍历

AcDbObjectId eId;
AcDbEntity* Ent;
if (acdbOpenObject(Ent, eId, AcDb::kForWrite) == Acad::eOk)
{
    AcGePoint3d Pt;
    if (Ent->isKindOf(AcDbPolyline::desc()))
    {
        AcDbPolyline* ePline = AcDbPolyline::cast(Ent);
        for (unsigned int i = 0; i < ePline->numVerts(); i++)
        {
            ePline->getPointAt(i, Pt);
        }
        ePline->close();
    }
}

AcDb3dPolyline 三维多段线顶点遍历

AcDbObjectId eId;
AcDbEntity* Ent;
if (acdbOpenObject(Ent, eId, AcDb::kForWrite) == Acad::eOk)
{
    AcGePoint3d Pt;
    if (Ent->isKindOf(AcDb3dPolyline::desc()))
    {
        AcDb3dPolyline* ePline = AcDb3dPolyline::cast(Ent);
        AcDbObjectIterator* ptIt = ePline->vertexIterator();
        for (ptIt->start(); !ptIt->done(); ptIt->step())
        {
            AcDb3dPolylineVertex* pVertex = NULL;
            AcDbObjectId vId = ptIt->objectId();
            acdbOpenObject(pVertex, vId, AcDb::kForWrite);
            AcDb::Vertex3dType verType = pVertex->vertexType();
            if (verType != AcDb::k3dControlVertex)
            {
                Pt = pVertex->position();
            }
            pVertex->close();
        }
        delete ptIt;
        ePline->close();
    }
}

选择集遍历

void Xz_cs()
{
    struct resbuf* rb;
    rb = acutBuildList(RTDXF0, L"TEXT", 0);
    ads_name ss;
    if (acedSSGet(NULL, NULL, NULL, rb, ss) != RTNORM) //acedEntSel 选择单个图元
    {
        return;
    }
    acutRelRb(rb); //释放空间
    Adesk::Int32 len;
    acedSSLength(ss, &len);
    ads_name ent;
    AcDbObjectId eId;
    AcDbEntity* Ent;
    for (int i = 0; i < len; i++)
    {
        acedSSName(ss, i, ent);
        acdbGetObjectId(eId, ent);
        if (acdbOpenObject(Ent, eId, AcDb::kForRead) == Acad::eOk)
        {
            if (Ent->isKindOf(AcDbText::desc()))
            {
                AcDbText* textEnt = AcDbText::cast(Ent);
                textEnt->close();
            }
        }
    }
    acedSSFree(ss);  //释放选择集
}