CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2023 开发者帮助

使用数据库反应器

2024-5-18 19:00| 发布者: admin| 查看: 87| 评论: 0|原作者: admin|来自: AutoCAD

使用数据库反应器

下面的示例使用派生自 的 reactor 来跟踪数据库中当前对象的数量。它为 reactor 类实现了三个通知函数:、 和 。该函数将反应器添加到当前数据库。该函数从数据库中删除反应器并删除数据库反应器。AcDbDatabase-ReactorobjectAppended()objectModified()objectErased()watch_db()clear_reactors()

class AsdkDbReactor;
long gEntAcc = 0;            // Global entity count
AsdkDbReactor *gpDbr = NULL; // Pointer to database reactor
// Custom AcDbDatabaseReactor class for database
// event notification.
//
class AsdkDbReactor : public AcDbDatabaseReactor
{
public:
    virtual void objectAppended(const AcDbDatabase* dwg,
        const AcDbObject* dbObj);
    virtual void objectModified(const AcDbDatabase* dwg,
        const AcDbObject* dbObj);
    virtual void objectErased(const AcDbDatabase* dwg,
        const AcDbObject* dbObj, Adesk::Boolean pErased);
};
// Called whenever an object is added to the database.
//
void
AsdkDbReactor::objectAppended(const AcDbDatabase* db,
    const AcDbObject* pObj)
{
    printDbEvent(pObj, "objectAppended");
    acutPrintf(" Db==%lx\n", (long) db);
    gEntAcc++;
    acutPrintf("Entity Count = %d\n", gEntAcc);
}
// Called whenever an object in the database is modified.
//
void
AsdkDbReactor::objectModified(const AcDbDatabase* db,
    const AcDbObject* pObj)
{
    printDbEvent(pObj, "objectModified");
    acutPrintf(" Db==%lx\n", (long) db);
}
// Called whenever an object is erased from the database.
//
void
AsdkDbReactor::objectErased(const AcDbDatabase* db,
    const AcDbObject* pObj, Adesk::Boolean pErased)
{
    if (pErased) {
        printDbEvent(pObj, "objectErased");
        gEntAcc--;
    } else {
        printDbEvent(pObj, "object(Un)erased");
        gEntAcc++;
    }
    acutPrintf(" Db==%lx\n", (long) db);
    acutPrintf("Entity Count = %d\n", gEntAcc);
}
// Prints the message passed in by pEvent; then 
// calls printObj() to print the information about
// the object that triggered the notification.
//
void
printDbEvent(const AcDbObject* pObj, const char* pEvent)
{
    acutPrintf("  Event: AcDbDatabaseReactor::%s ", pEvent);
    printObj(pObj);
}
// Prints out the basic information about the object pointed
// to by pObj.
//
void
printObj(const AcDbObject* pObj)
{
    if (pObj == NULL) {
        acutPrintf("(NULL)");
        return;
    }
    AcDbHandle objHand;
    char  handbuf[17];
    // Gets the handle as a string.
    //
    pObj->getAcDbHandle(objHand);
    objHand.getIntoAsciiBuffer(handbuf);
    acutPrintf(
        "\n   (class==%s, handle==%s, id==%lx, db==%lx)",
        pObj->isA()->name(), handbuf,
        pObj->objectId().asOldId(), pObj->database());
}
// Adds a reactor to the database to monitor changes.
// This can be called multiple times without any ill
// effect because subsequent calls are ignored.
//
void
watchDb()
{
    if (gpDbr == NULL) {
        gpDbr = new AsdkDbReactor();
    }
    acdbHostApplicationServices()->workingDatabase()->addReactor(
        gpDbr);
    acutPrintf(
        "  Added Database Reactor to "
        "acdbHostApplicationServices()->workingDatabase().\n");
}
// Removes the database reactor.
//
void
clearReactors()
{
    if (acdbHostApplicationServices()->workingDatabase() != NULL) {
        acdbHostApplicationServices()->workingDatabase(
            )->removeReactor(gpDbr);
        delete gpDbr;
        gpDbr = NULL;
    }
}
// ObjectARX entry point function
//
extern "C" AcRx::AppRetCode
acrxEntryPoint(AcRx::AppMsgCode msg, void* appId)
{
    switch (msg) {
    case AcRx::kInitAppMsg:
        acrxDynamicLinker->unlockApplication(appId);
        acrxDynamicLinker->registerAppNotMDIAware(appId);
        acedRegCmds->addCommand("ASDK_NOTIFY_TEST",
            "ASDK_WATCH",
            "WATCH",
            ACRX_CMD_TRANSPARENT,
            watchDb);
        acedRegCmds->addCommand("ASDK_NOTIFY_TEST",
            "ASDK_CLEAR",
            "CLEAR",
            ACRX_CMD_TRANSPARENT,
            clearReactors);
        break;
    case AcRx::kUnloadAppMsg:
        clearReactors();
        acedRegCmds->removeGroup("ASDK_NOTIFY_TEST");
        break;
    }
    return AcRx::kRetOK;
}

父主题:

  1. 使用反应器

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 22:18

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部