CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

错误处理

2023-1-1 06:08| 发布者: admin| 查看: 250| 评论: 0|来自: AutoCAD

本指南中的示例省略了必要的错误检查以简化代码。但是,您始终需要检查退货状态并采取适当的措施。以下示例显示了对数据库入门中首先显示的几个示例的错误检查的适当用法。

Acad::ErrorStatus createCircle(AcDbObjectId& circleId)
{
    circleId = AcDbObjectId::kNull;
    AcGePoint3d center(9.0, 3.0, 0.0);
    AcGeVector3d normal(0.0, 0.0, 1.0);
    AcDbCircle *pCirc = new AcDbCircle(center, normal, 2.0);
    if (pCirc == NULL)
        return Acad::eOutOfMemory;
    AcDbBlockTable *pBlockTable;
    Acad::ErrorStatus es = 
        acdbHostApplicationServices()->workingDatabase()->
            getSymbolTable(pBlockTable, AcDb::kForRead);
    if (es != Acad::eOk) {
        delete pCirc;
        return es;
    }
    AcDbBlockTableRecord *pBlockTableRecord;
    es = pBlockTable->getAt(ACDB_MODEL_SPACE,
        pBlockTableRecord, AcDb::kForWrite);
    if (es != Acad::eOk) {
        Acad::ErrorStatus es2 = pBlockTable->close();
        if (es2 != Acad::eOk) {
            acrx_abort("\nApp X failed to close Block"
                " Table. Error: %d",
                acadErrorStatusText(es2));
        }
        delete pCirc;
        return es;
    }
    es = pBlockTable->close();
    if (es != Acad::eOk) {
        acrx_abort("\nApp X failed to close Block Table."
            " Error: %d", acadErrorStatusText(es));
    }
    es = pBlockTableRecord->appendAcDbEntity(circleId,
        pCirc);
    if (es != Acad::eOk) {
        Acad::ErrorStatus es2 = pBlockTableRecord->close();
        if (es2 != Acad::eOk) {
            acrx_abort("\nApp X failed to close"
                " Model Space Block Record. Error: %s",
                acadErrorStatusText(es2));
        }
        delete pCirc;
        return es;
    }
    es = pBlockTableRecord->close();
    if (es != Acad::eOk) {
        acrx_abort("\nApp X failed to close"
            " Model Space Block Record. Error: %d",
            acadErrorStatusText(es));
    }
    es = pCirc->close();
    if (es != Acad::eOk) {
        acrx_abort("\nApp X failed to"
            " close circle entity. Error: %d",
            acadErrorStatusText(es));
    }
    return es;
}

Acad::ErrorStatus createNewLayer()
{
    AcDbLayerTableRecord *pLayerTableRecord
        = new AcDbLayerTableRecord;
    if (pLayerTableRecord == NULL)
        return Acad::eOutOfMemory;
    Acad::ErrorStatus es
        = pLayerTableRecord->setName("ASDK_MYLAYER");
    if (es != Acad::eOk) {
        delete pLayerTableRecord;
        return es;
    }
    AcDbLayerTable *pLayerTable;
    es = acdbHostApplicationServices()->workingDatabase()->
        getSymbolTable(pLayerTable, AcDb::kForWrite);
    if (es != Acad::eOk) {
        delete pLayerTableRecord;
        return es;
    }
    // The linetype object ID default is 0, which is
    // not a valid ID.  Therefore, it must be set to a
    // valid ID, the CONTINUOUS linetype.
    // Other data members have valid defaults, so
    // they can be left alone.
    //
    AcDbLinetypeTable *pLinetypeTbl;
    es = acdbHostApplicationServices()->workingDatabase()->
        getSymbolTable(pLinetypeTbl, AcDb::kForRead);
    if (es != Acad::eOk) {
        delete pLayerTableRecord;
        es = pLayerTable->close();
        if (es != Acad::eOk) {
            acrx_abort("\nApp X failed to close Layer"
                " Table. Error: %d",
                acadErrorStatusText(es));
        }
        return es;
    }
    AcDbObjectId ltypeObjId;
    es = pLinetypeTbl->getAt("CONTINUOUS", ltypeObjId);
    if (es != Acad::eOk) {
        delete pLayerTableRecord;
        es = pLayerTable->close();
        if (es != Acad::eOk) {
            acrx_abort("\nApp X failed to close Layer"
                " Table. Error: %d",
                acadErrorStatusText(es));
        }
        return es;
    }
    pLayerTableRecord->setLinetypeObjectId(ltypeObjId);
    es = pLayerTable->add(pLayerTableRecord);
    if (es != Acad::eOk) {
        Acad::ErrorStatus es2 = pLayerTable->close();
        if (es2 != Acad::eOk) {
            acrx_abort("\nApp X failed to close Layer"
                " Table. Error: %d",
                acadErrorStatusText(es2));
        }
        delete pLayerTableRecord;
        return es;
    }
    es = pLayerTable->close();
    if (es != Acad::eOk) {
        acrx_abort("\nApp X failed to close Layer"
            " Table. Error: %d",
            acadErrorStatusText(es));
    }
    es = pLayerTableRecord->close();
    if (es != Acad::eOk) {
        acrx_abort("\nApp X failed to close Layer"
            " Table Record. Error: %d",
            acadErrorStatusText(es));
    }
    return es;
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 16:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部