找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1757|回复: 1

[分享] DbUtilities

[复制链接]

已领礼包: 1268个

财富等级: 财源广进

发表于 2014-7-12 08:50:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
#include "stdafx.h"
#include "stdarx.h"

#include "DbUtilities.h"


Acad::ErrorStatus postToDb(AcDbDatabase *pDb, AcDbEntity *pEnt, AcDbObjectId &idObj)
{
        assert ( pEnt != NULL ) ;

        if ( pDb == NULL )
                pDb = acdbHostApplicationServices()->workingDatabase();
        //----- Get a pointer to the current drawing
        //----- and get the drawing's block table. Open it for read.
        Acad::ErrorStatus es ;
        AcDbBlockTable *pBlockTable ;
        if ( (es =pDb->getBlockTable (pBlockTable, AcDb::kForRead)) == Acad::eOk ) {
                //----- Get the Model Space record and open it for write. This will be the owner of the new entity.
                AcDbBlockTableRecord *pSpaceRecord ;
                if ( (es =pBlockTable->getAt (ACDB_MODEL_SPACE, pSpaceRecord, AcDb::kForWrite)) == Acad::eOk ) {
                        //----- Append pEnt to Model Space, then close the Model Space record.
                        es =pSpaceRecord->appendAcDbEntity (idObj, pEnt);
                        pSpaceRecord->close () ;
                }
                pBlockTable->close () ;
        }
        //----- It is good programming practice to return an error status
        if(es != Acad::eOk) idObj=NULL;
        return (es) ;
}

Acad::ErrorStatus postToDb(AcDbEntity *pEnt, AcDbObjectId &idObj)
{
        return postToDb(NULL,pEnt,idObj);
}

Acad::ErrorStatus postToDb(AcDbEntity *pEnt)
{
        AcDbObjectId idObj;
        return postToDb(NULL,pEnt,idObj);
}


AcDbObjectId getNODCompanyDictionaryId (AcDbDatabase *pDb,
                                                   const char *key,
                                                   bool bCreateIfNotPresent)
{
        assert ( (key != NULL) && (*key != '\0') ) ;

        if ( pDb == NULL )
                pDb =acdbHostApplicationServices ()->workingDatabase () ;

        AcDbDictionary *pNOD ;
        if ( pDb->getNamedObjectsDictionary (pNOD, AcDb::kForRead) != Acad::eOk )
                return (AcDbObjectId::kNull) ;

        AcDbObjectId id =AcDbObjectId::kNull ;
        if ( !pNOD->has (key) ) {
                if ( !bCreateIfNotPresent ) {
                        pNOD->close () ;
                        return (AcDbObjectId::kNull) ;
                }

                if ( pNOD->upgradeOpen () != Acad::eOk ) {
                        pNOD->close () ;
                        return (AcDbObjectId::kNull) ;
                }

                AcDbDictionary *pDict =new AcDbDictionary ;
                if ( (pNOD->setAt (key, pDict, id)) != Acad::eOk ) {
                        delete pDict ;
                        pNOD->close () ;
                        return (AcDbObjectId::kNull) ;
                }

                pNOD->downgradeOpen () ;

        } else {
                if ( pNOD->getAt (key, id) != Acad::eOk ) {
                        pNOD->close () ;
                        return (AcDbObjectId::kNull) ;
                }

                AcDbObject *pObj ;
                if ( acdbOpenAcDbObject (pObj, id, AcDb::kForRead) != Acad::eOk ) {
                        pNOD->close () ;
                        return (AcDbObjectId::kNull) ;
                }

                if ( AcDbDictionary::cast (pObj) == NULL ) {
                        pObj->close () ;
                        pNOD->close () ;
                        return (AcDbObjectId::kNull) ;
                }
                pObj->close () ;

        }
        pNOD->close () ;

        return (id) ;
}

Acad::ErrorStatus getNODCompanyDictionary (AcDbDatabase *pDb,
                                                 const char *key,
                                                 AcDbDictionary *&pDict,
                                                 AcDb::OpenMode mode,
                                                 bool bCreateIfNotPresent)
{
        assert ( (key != NULL) && (*key != '\0') ) ;
        Acad::ErrorStatus es ;

        if ( pDb == NULL )
                pDb =acdbHostApplicationServices ()->workingDatabase () ;

        AcDbDictionary *pNOD ;
        if ( (es =pDb->getNamedObjectsDictionary (pNOD, AcDb::kForRead)) != Acad::eOk )
                return (es) ;

        AcDbObjectId id ;
        if ( !pNOD->has (key) ) {
                if ( !bCreateIfNotPresent ) {
                        pNOD->close () ;
                        return (Acad::eKeyNotFound) ;
                }

                if ( (es =pNOD->upgradeOpen ()) != Acad::eOk ) {
                        pNOD->close () ;
                        return (es) ;
                }

                pDict =new AcDbDictionary ;
                es =pNOD->setAt (key, pDict, id);
                if ( es != Acad::eOk ) {
                        delete pDict ;
                        pNOD->close () ;
                        return (es) ;
                } else
                        es = Acad::eCreateInvalidName;

                pNOD->downgradeOpen () ;

                //----- We should close and reopen the company dictionary to make sure
                //----- UNDO will get new entries properly. And luckly, it will give us a
                //----- chance to reopen the object in the right mode...
                pDict->close () ;
                acdbOpenAcDbObject ((AcDbObject *&)pDict, id, mode) ;

        } else {
                AcDbObject *pObj ;
                if ( (es =pNOD->getAt (key, pObj, mode)) != Acad::eOk ) {
                        pNOD->close () ;
                        return (es) ;
                }

                pDict =AcDbDictionary::cast (pObj) ;
                if ( pDict == NULL ) {
                        pObj->close () ;
                        pNOD->close () ;
                        return (Acad::eNotThatKindOfClass) ;
                }
        }
        pNOD->close () ;

        return es ;
}

Acad::ErrorStatus getNODCompanyDictionary (const char *key,
                                                 AcDbDictionary *&pDict,
                                                 AcDb::OpenMode mode,
                                                 bool bCreateIfNotPresent)
{
        return getNODCompanyDictionary(NULL,key,pDict,mode,bCreateIfNotPresent);
}

Acad::ErrorStatus getInNOD (AcDbDatabase *pDb ,
                  const char *key,
                  AcDbObject *&pObj,
                  AcDb::OpenMode mode)
{
        assert ( (key != NULL) && (*key != '\0') ) ;
        Acad::ErrorStatus es ;

        if ( pDb == NULL )
                pDb =acdbHostApplicationServices ()->workingDatabase () ;

        AcDbDictionary *pNOD ;
        if ( (es =pDb->getNamedObjectsDictionary (pNOD, AcDb::kForRead)) != Acad::eOk )
                return (es) ;

        AcDbObjectId id ;
        if ( !pNOD->has (key) ) {
                es = Acad::eKeyNotFound;
        } else {
                es =pNOD->getAt (key, pObj, mode);
        }
        pNOD->close () ;

        return es;
}

Acad::ErrorStatus getInNOD (const char *key, AcDbObject *&pObj, AcDb::OpenMode mode)
{
        return getInNOD(NULL,key,pObj,mode);
}


Acad::ErrorStatus getInDictionary (const AcDbDictionary *pDict,
                                 const AcDbObject *pObj,
                                 char *&entryname)
{
        assert(!(pObj==NULL || pDict==NULL));
        AcDbObjectId eId;
        eId = pObj->objectId();

        Acad::ErrorStatus es;
        es = pDict->nameAt(eId,entryname);

        return es;
}


Acad::ErrorStatus getObjectExtCompanyDictionary (AcDbObject *pObj,
                                                           const char *key,
                                                           AcDbDictionary *&pDict,
                                                           AcDb::OpenMode mode,
                                                           bool bCreateIfNotPresent)
{
        Acad::ErrorStatus es ;
        assert ( pObj != NULL && pObj->isKindOf (AcDbObject::desc ()) ) ;
        assert ( (key != NULL) && (*key != '\0') ) ;

        AcDbObjectId id ;
        AcDbDictionary *pExt ;
        if ( (id =pObj->extensionDictionary ()) == AcDbObjectId::kNull ) {
                if ( !bCreateIfNotPresent )
                        return (Acad::eKeyNotFound) ;

                bool bWasOpenForRead =false ;
                if ( !pObj->isWriteEnabled () ) {
                        bWasOpenForRead =true ;
                        if ( (es =pObj->upgradeOpen ()) != Acad::eOk )
                                return (es) ;
                }

                if ( (es =pObj->createExtensionDictionary ()) != Acad::eOk ) {
                        if ( bWasOpenForRead )
                                pObj->downgradeOpen () ;
                        return (es) ;
                }

                id =pObj->extensionDictionary () ;

                if ( (es =acdbOpenAcDbObject ((AcDbObject *&)pExt, id, AcDb::kForWrite, Adesk::kTrue)) != Acad::eOk )
                        return (es) ;

                pDict =new AcDbDictionary ;
                if ( (es =pExt->setAt (key, pDict, id)) != Acad::eOk ) {
                        delete pDict ;
                        pExt->close () ;
                        return (es) ;
                }

                pExt->close () ;

                //----- We should close and reopen the company dictionary to make sure
                //----- UNDO will get new entries properly. And luckly, it will give us a
                //----- chance to reopen the object in the right mode...
                pDict->close () ;
                acdbOpenAcDbObject ((AcDbObject *&)pDict, id, mode) ;
        } else {
                if ( (es =acdbOpenAcDbObject ((AcDbObject *&)pExt, id, AcDb::kForWrite, Adesk::kTrue)) != Acad::eOk )
                        return (es) ;

                //----- Unerase the ext. dictionary if it was erased
                if ( pExt->isErased () ) {
                        if ( !bCreateIfNotPresent ) {
                                pExt->close () ;
                                return (Acad::eWasErased) ;
                        }
                        pExt->erase (Adesk::kFalse) ;
                }

                if ( pExt->has (key) ) {
                        AcDbObject *pTempObj ;
                        if ( (es =pExt->getAt (key, pTempObj, mode)) != Acad::eOk ) {
                                pExt->close () ;
                                return (es) ;
                        }

                        pDict =AcDbDictionary::cast (pTempObj) ;
                        if ( pDict == NULL ) {
                                pTempObj->close () ;
                                pExt->close () ;
                                return (Acad::eNotThatKindOfClass) ;
                        }
                } else if ( bCreateIfNotPresent ) {

                        pDict =new AcDbDictionary ;
                        if ( (es =pExt->setAt (key, pDict, id)) != Acad::eOk ) {
                                delete pDict ;
                                pExt->close () ;
                                return (es) ;
                        }
                        //----- We should close and reopen the company dictionary to make sure
                        //----- UNDO will get new entries properly. And luckly, it will give us a
                        //----- chance to reopen teh object in teh right mode...
                        pDict->close () ;
                        acdbOpenAcDbObject ((AcDbObject *&)pDict, id, mode) ;
                }

                pExt->close () ;
        }

        return (Acad::eOk) ;
}

AcDbObjectId getObjectExtCompanyDictionaryId (AcDbObject *pObj,
                                                                 const char *key,
                                                                 bool bCreateIfNotPresent)
{
        assert ( pObj != NULL && pObj->isKindOf (AcDbObject::desc ()) ) ;
        assert ( (key != NULL) && (*key != '\0') ) ;

        AcDbObjectId id ;
        AcDbDictionary *pExt, *pDict ;
        if ( (id =pObj->extensionDictionary ()) == AcDbObjectId::kNull ) {
                if ( !bCreateIfNotPresent )
                        return (AcDbObjectId::kNull) ;

                bool bWasOpenForRead =false ;
                if ( !pObj->isWriteEnabled () ) {
                        bWasOpenForRead =true ;
                        if ( pObj->upgradeOpen () != Acad::eOk )
                                return (AcDbObjectId::kNull) ;
                }

                if ( pObj->createExtensionDictionary () != Acad::eOk ) {
                        if ( bWasOpenForRead )
                                pObj->downgradeOpen () ;
                        return (AcDbObjectId::kNull) ;
                }

                id =pObj->extensionDictionary () ;

                if ( acdbOpenAcDbObject ((AcDbObject *&)pExt, id, AcDb::kForWrite, Adesk::kTrue) != Acad::eOk )
                        return (AcDbObjectId::kNull) ;

                pDict =new AcDbDictionary ;
                if ( pExt->setAt (key, pDict, id) != Acad::eOk ) {
                        delete pDict ;
                        pExt->close () ;
                        return (AcDbObjectId::kNull) ;
                }

        } else {
                if ( acdbOpenAcDbObject ((AcDbObject *&)pExt, id, AcDb::kForWrite, Adesk::kTrue) != Acad::eOk )
                        return (AcDbObjectId::kNull) ;

                //----- Unerase the ext. dictionary if it was erased
                if ( pExt->isErased () ) {
                        if ( !bCreateIfNotPresent ) {
                                pExt->close () ;
                                return (AcDbObjectId::kNull) ;
                        }
                        pExt->erase (Adesk::kFalse) ;
                }

                if ( pExt->has (key) ) {
                        AcDbObject *pTempObj ;
                        if ( pExt->getAt (key, pTempObj, AcDb::kForRead) != Acad::eOk ) {
                                pExt->close () ;
                                return (AcDbObjectId::kNull) ;
                        }

                        pDict =AcDbDictionary::cast (pTempObj) ;
                        if ( pDict == NULL ) {
                                pTempObj->close () ;
                                pExt->close () ;
                                return (AcDbObjectId::kNull) ;
                        }
                } else if ( bCreateIfNotPresent ) {
                        pDict =new AcDbDictionary ;
                        if ( pExt->setAt (key, pDict, id) != Acad::eOk ) {
                                delete pDict ;
                                pExt->close () ;
                                return (AcDbObjectId::kNull) ;
                        }
                }
        }

        pDict->close () ;
        pExt->close () ;
        return (id) ;
}

// named objects dictionary utility functions
#ifndef NOD_OBJ_NAME_LEN
#define NOD_OBJ_NAME_LEN 31
#endif // named objects dictionary object name length

void replaceChars(char *pStr, const char search, const char replace)
{
        for(int i = 0; i < (int)strlen(pStr); i++)
                if(pStr[i] == search)
                        pStr[i] = replace;
}

//--------------------------------------------------------------------------------
Acad::ErrorStatus postToNODCompanyDictionary(AcDbDatabase *pTargetDb,
                                                   AcDbObject *pObj,
                                                   const char *entryname,
                                                   const char *whichDict,
                                                   bool bOverideIfnameExists)
{
        assert( pObj != NULL );
        assert( !(entryname==NULL || *entryname=='\0') );
        assert( !(whichDict==NULL || *whichDict=='\0') );

        Acad::ErrorStatus stat;
        char *storeName;
        int name_len;
        name_len = strlen(entryname);
        if((name_len>NOD_OBJ_NAME_LEN) || (strlen(whichDict)==0)) {
                stat = Acad::eInvalidInput;
                return stat;
        }
        if(entryname != NULL)
        {
                storeName = new char[name_len];
                strcpy(storeName, entryname);
                replaceChars(storeName, ' ', '_');
//                storeName = strupr(storeName);
        }
       
        if(pTargetDb==NULL)
                pTargetDb = acdbHostApplicationServices()->workingDatabase();
       
        AcDbDictionary *pNamedObj = NULL;
        stat = pTargetDb->getNamedObjectsDictionary(pNamedObj, AcDb::kForWrite);
        if(stat != Acad::eOk) {
                return stat;
        }
       
        AcDbDictionary *pDict = NULL;
        if(pNamedObj->getAt(whichDict, (AcDbObject *&) pDict, AcDb::kForWrite) == Acad::eKeyNotFound) {
                pDict = new AcDbDictionary;
                AcDbObjectId dictId;
                pNamedObj->setAt(whichDict, pDict, dictId);
        }
        pNamedObj->close();
               
        AcDbObjectId eId;
       
        if(pDict->getAt(storeName, eId) == Acad::eOk) {
                // storeName object already exists in the dictionary
                if(!bOverideIfnameExists) {
                        stat = Acad::eAlreadyInDb;
                } else {
                        pDict->remove(storeName);
                        stat = pDict->setAt(storeName, pObj, eId);
                }
        }
        else
                stat = pDict->setAt(storeName, pObj, eId);                       
       
        pDict->close();
        delete [] storeName;
       
        return stat;               
}

Acad::ErrorStatus postToNODCompanyDictionary(AcDbObject *pObj,
                                                   const char *entryname,
                                                   const char *whichDict,
                                                   bool bOverideIfnameExists)
{
        return postToNODCompanyDictionary(NULL,pObj,entryname,whichDict,bOverideIfnameExists);
}


//--------------------------------------------------------------------------------
Acad::ErrorStatus postToDictionary(AcDbObject *pObj,
                                 const char *entryname,
                                 AcDbDictionary *pDict,
                                 bool bOverideIfnameExists)
{
        assert( pObj != NULL );
        assert( pDict != NULL );
        assert( !(entryname==NULL || *entryname=='\0') );

        Acad::ErrorStatus stat;
        char *storeName;
        int name_len;
        name_len = strlen(entryname);
        if(name_len>NOD_OBJ_NAME_LEN) {
                stat = Acad::eInvalidInput;
                return stat;
        }
        if(entryname != NULL)
        {
                storeName = new char[name_len];
                strcpy(storeName, entryname);
                replaceChars(storeName, ' ', '_');
        }
       
        AcDbObjectId eId;

        if(pDict->getAt(storeName, eId) == Acad::eOk) {
                // storeName object already exists in the dictionary
                if(!bOverideIfnameExists) {
                        stat = Acad::eAlreadyInDb;
                } else {
                        if(pDict->isReadEnabled()) pDict->upgradeOpen();
                        pDict->remove(storeName);
                        stat = pDict->setAt(storeName, pObj, eId);
                }
        }
        else
                stat = pDict->setAt(storeName, pObj, eId);                       

        delete [] storeName;
       
        return stat;               
}


Acad::ErrorStatus postToNOD(AcDbDatabase *pTargetDb,
                  AcDbObject *pObj,
                  const char *entryname,
                  bool bOverideIfnameExists)
{
        assert( pObj != NULL );
        assert( !(entryname==NULL || *entryname=='\0') );

        Acad::ErrorStatus stat;
        char *storeName;
        int name_len;
        name_len = strlen(entryname);
        if((name_len>NOD_OBJ_NAME_LEN)) {
                stat = Acad::eInvalidInput;
                return stat;
        }
        if(entryname != NULL)
        {
                storeName = new char[name_len];
                strcpy(storeName, entryname);
                replaceChars(storeName, ' ', '_');
        }
       
        if(pTargetDb==NULL)
                pTargetDb = acdbHostApplicationServices()->workingDatabase();
       
        AcDbDictionary *pNOD = NULL;
        pTargetDb->getNamedObjectsDictionary(pNOD, AcDb::kForWrite);
       
        AcDbObjectId eId;
        if(pNOD->getAt(storeName, eId) == Acad::eOk) {
                // storeName object already exists in the dictionary
                if(!bOverideIfnameExists) {
                        stat = Acad::eAlreadyInDb;
                } else {
                        pNOD->remove(storeName);
                        stat = pNOD->setAt(storeName, pObj, eId);
                }
        }
        else
                stat = pNOD->setAt(storeName, pObj, eId);                       
       
        pNOD->close();
        delete [] storeName;
       
        return stat;               
}

Acad::ErrorStatus postToNOD(AcDbObject *pObj, const char *entryname,bool bOverideIfnameExists)
{
        return postToNOD(NULL,pObj,entryname,bOverideIfnameExists);
}

Acad::ErrorStatus deleteInNODCompanyDictionary(AcDbDatabase *pTargetDb,
                                                         const char *entryname,
                                                         const char *whichDict)
{
        assert( !(entryname==NULL || *entryname=='\0') );
        assert( !(whichDict==NULL || *whichDict=='\0') );

        Acad::ErrorStatus stat;
        char *storeName;
        int name_len;
        name_len = strlen(entryname);
        if((name_len>NOD_OBJ_NAME_LEN) || (strlen(whichDict)==0)) {
                stat = Acad::eInvalidInput;
                return stat;
        }
        if(entryname != NULL)
        {
                storeName = new char[name_len];
                strcpy(storeName, entryname);
                replaceChars(storeName, ' ', '_');
//                storeName = strupr(storeName);
        }

        AcDbObject *pObj;
       
        if(pTargetDb==NULL)
                pTargetDb = acdbHostApplicationServices()->workingDatabase();
       
        AcDbDictionary *pNamedObj = NULL;
        stat = pTargetDb->getNamedObjectsDictionary(pNamedObj, AcDb::kForWrite);
        if(stat != Acad::eOk) {
                return stat;
        }
       
        AcDbDictionary *pDict = NULL;
        if(pNamedObj->getAt(whichDict, (AcDbObject *&) pDict, AcDb::kForWrite) == Acad::eKeyNotFound) {
                pDict = new AcDbDictionary;
                AcDbObjectId dictId;
                pNamedObj->setAt(whichDict, pDict, dictId);
        }
        pNamedObj->close();
               
        AcDbObjectId eId;
       
        if(pDict->getAt(storeName, eId) == Acad::eOk) {
                // storeName object already exists in the dictionary
                acdbOpenAcDbObject(pObj, eId, AcDb::kForWrite);  
               
                pDict->remove(storeName);
               
                pObj->erase();
                pObj->close();
                stat = Acad::eOk;
        }
        else {
                stat = Acad::eKeyNotFound;
        }
       

        pDict->close();
        delete [] storeName;
       
        return stat;               
}

Acad::ErrorStatus deleteInNODCompanyDictionary(const char *entryname,
                                                         const char *whichDict)
{
        return deleteInNODCompanyDictionary(NULL,entryname,whichDict);
}


Acad::ErrorStatus deleteInNOD(AcDbDatabase *pTargetDb, const char *entryname)
{
        assert( !(entryname==NULL || *entryname=='\0') );

        Acad::ErrorStatus es;

        if(pTargetDb==NULL)
                pTargetDb = acdbHostApplicationServices()->workingDatabase();
       
        AcDbDictionary *pNOD = NULL;
        pTargetDb->getNamedObjectsDictionary(pNOD, AcDb::kForWrite);
                       
        AcDbObjectId eId;
        es =  pNOD->getAt(entryname, eId);
        if(es == Acad::eOk) {
                AcDbObject *pObj;
                acdbOpenAcDbObject(pObj, eId, AcDb::kForWrite);
                AcDbDictionary *pDict;
                pDict = AcDbDictionary::cast(pObj);
                if(pDict) {
                        if(pDict->numEntries()) es = Acad::eCannotBeErasedByCaller;
                }
                if(es == Acad::eOk) {
                        pNOD->remove(eId);
                        if(pObj->isReadEnabled()) pObj->upgradeOpen();
                        es = pObj->erase();
                }
                pObj->close();
        }

        pNOD->close();
       
        return es;               
}

Acad::ErrorStatus deleteInNOD(const char *entryname)
{
        return deleteInNOD(NULL,entryname);
}

Acad::ErrorStatus deleteInNOD(AcDbDatabase *pTargetDb, const AcDbObjectId eId)
{

        Acad::ErrorStatus es;

        if(pTargetDb==NULL)
                pTargetDb = acdbHostApplicationServices()->workingDatabase();
       
        AcDbDictionary *pNOD = NULL;
        pTargetDb->getNamedObjectsDictionary(pNOD, AcDb::kForWrite);

        if(pNOD->has(eId)) {
                es = Acad::eOk;
        } else {
                es = Acad::eKeyNotFound;
        }

        if(es == Acad::eOk) {
                AcDbObject *pObj;
                acdbOpenAcDbObject(pObj, eId, AcDb::kForWrite);
                AcDbDictionary *pDict;
                pDict = AcDbDictionary::cast(pObj);
                if(pDict) {
                        if(pDict->numEntries()) es = Acad::eCannotBeErasedByCaller;
                }
                if(es == Acad::eOk) {
                        pNOD->remove(eId);
                        if(pObj->isReadEnabled()) pObj->upgradeOpen();
                        es = pObj->erase();
                }
                pObj->close();
        }
        pNOD->close();
       
        return es;               
}

Acad::ErrorStatus deleteInNOD(const AcDbObjectId eId)
{
        return deleteInNOD(NULL,eId);
}

Acad::ErrorStatus deleteInNOD(AcDbDatabase *pTargetDb, AcDbObject *pObj)
{
        Acad::ErrorStatus es;

        if(pTargetDb==NULL)
                pTargetDb = acdbHostApplicationServices()->workingDatabase();
       
        AcDbDictionary *pNOD = NULL;
        pTargetDb->getNamedObjectsDictionary(pNOD, AcDb::kForWrite);

        AcDbObjectId eId;
        eId = pObj->objectId();

        if(pNOD->has(eId)) {
                es = Acad::eOk;
        } else {
                es = Acad::eKeyNotFound;
        }

        if(es == Acad::eOk) {
                AcDbDictionary *pDict;
                pDict = AcDbDictionary::cast(pObj);
                if(pDict) {
                        if(pDict->numEntries()) es = Acad::eCannotBeErasedByCaller;
                }
                if(es == Acad::eOk) {
                        pNOD->remove(eId);
                        if(pObj->isReadEnabled()) pObj->upgradeOpen();
                        es = pObj->erase();
                }
        }
        pNOD->close();
       
        return es;               
}

Acad::ErrorStatus deleteInNOD(AcDbObject *pObj)
{
        return deleteInNOD(NULL,pObj);
}

Acad::ErrorStatus deleteInDictionary(const AcDbObjectId eId, AcDbDictionary *pDict)
{
        assert(pDict != NULL);

        Acad::ErrorStatus es;
        es = pDict->remove(eId);

        if(es == Acad::eOk) {
                AcDbObject *pObj;
                acdbOpenAcDbObject(pObj, eId, AcDb::kForWrite);                 
                es = pObj->erase();
                pObj->close();
        }
               
        return es;
}

Acad::ErrorStatus deleteInDictionary(const char *entryname, AcDbDictionary *pDict)
{
        assert( !(entryname==NULL || *entryname=='\0') );
        assert(pDict != NULL);

        Acad::ErrorStatus es;
        AcDbObjectId eId;
        es = pDict->getAt(entryname,eId);

        if(es == Acad::eOk) {
                pDict->remove(entryname);
                AcDbObject *pObj;
                acdbOpenAcDbObject(pObj, eId, AcDb::kForWrite);                 
                es = pObj->erase();
                pObj->close();
        }
               
        return es;
}

Acad::ErrorStatus deleteInDictionary(AcDbObject *pObj, AcDbDictionary *pDict)
{
        assert(pDict != NULL);

        Acad::ErrorStatus es;
        AcDbObjectId eId;
        eId = pObj->id();

        es = pDict->remove(eId);
        if(es==Acad::eOk) {
                if(pObj->isReadEnabled()) pObj->upgradeOpen();
                es = pObj->erase();
        }       
       
        return es;
}

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 1268个

财富等级: 财源广进

 楼主| 发表于 2014-7-12 08:51:46 | 显示全部楼层
#ifndef DB_UTILITIES_H
#define DB_UTILITIES_H


//////////////////////////////////////////////////////////////////////////
//
// 与DataBase Table处理相关的函数
//
// Note:
//
//////////////////////////////////////////////////////////////////////////


#ifdef _DBXEXP_
#define DLLIMPEXP __declspec(dllexport)
#else
#define DLLIMPEXP
#endif


/*
*        Purpose:
*                替换字符串中的字符
*        Argument:
*                pStr        [in], 字符串
*                search        [in], 字符串中原来的字符
*                replace        [in], 要替换成的字符
*/
extern DLLIMPEXP void
replaceChars(char *pStr, const char search, const char replace);



/*
*        Purpose:
*                将Db实体加入到数据库的MODEL_SPACE Table中
*        Argument:
*                pDb                [in], 数据库指针, NULL时为当前数据库
*                pEnt        [in], Db实体指针
*                idObj        [out], 返回的实体Id
*        Return:
*                return eOk if success, if not success, idObj set to NULL.
*/
extern DLLIMPEXP Acad::ErrorStatus
postToDb (AcDbDatabase *pDb, AcDbEntity *pEnt, AcDbObjectId &idObj);


/*
*        Purpose:
*                将Db实体加入到数据库的MODEL_SPACE Table中, 默认为当前数据库
*        Argument:
*                pEnt        [in], Db实体指针
*                idObj        [out], 返回的实体Id
*        Return:
*                return eOk if success, if not success, idObj set to NULL.
*/
extern DLLIMPEXP Acad::ErrorStatus
postToDb(AcDbEntity *pEnt, AcDbObjectId &idObj);



/*
*        Purpose:
*                将Db实体加入到数据库的MODEL_SPACE Table中, 默认为当前数据库
*        Argument:
*                pEnt        [in], Db实体指针
*        Return:
*                return eOk if success.
*/
extern DLLIMPEXP Acad::ErrorStatus
postToDb(AcDbEntity *pEnt);



/*
*        Purpose:
*                获取数据库中命名对象字典NOD(Named Object Dictionary)的子字典Id
*        Argument:
*                pDb                                        [in], Db指针, NULL时为当前数据库
*                key                                        [in], NOD子字典名称
*                bCreateIfNotPresent        [in], 如果不存在,是否创建,默认为false
*        Return:
*                返回Id,如果错误时返回NULL.
*/
extern DLLIMPEXP AcDbObjectId
getNODCompanyDictionaryId (AcDbDatabase *pDb,
                                                   const char *key,
                                                   bool bCreateIfNotPresent=false);



/*
*        Purpose:
*                打开数据库中命名对象字典NOD(Named Object Dictionary)的子字典
*        Argument:
*                pDb                                        [in], Db指针,NULL时为当前数据库
*                key                                        [in], NOD子字典名称
*                pDict                                [out], 返回的NOD子字典指针
*                mode                                [in], 打开模式,默认为read
*                bCreateIfNotPresent        [in], 如果不存在,是否创建,默认为false
*        Return:
*                Acad::eOk, 找到并打开
*                Acad::eCreateInvalidName, 未找到但创建
*                Acad::eKeyNotFound, 未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
getNODCompanyDictionary (AcDbDatabase *pDb,
                                                 const char *key,
                                                 AcDbDictionary *&pDict,
                                                 AcDb::OpenMode mode=AcDb::kForRead,
                                                 bool bCreateIfNotPresent=false);




/*
*        Purpose:
*                打开数据库中命名对象字典NOD(Named Object Dictionary)的子字典
*                默认未当前数据库
*        Argument:
*                key                                        [in], NOD子字典名称
*                pDict                                [out], 返回的NOD子字典指针
*                mode                                [in], 打开模式,默认为read
*                bCreateIfNotPresent        [in], 如果不存在,是否创建,默认为false
*        Return:
*                Acad::eOk, 找到并打开
*                Acad::eCreateInvalidName, 未找到但创建
*                Acad::eKeyNotFound, 未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
getNODCompanyDictionary (const char *key,
                                                 AcDbDictionary *&pDict,
                                                 AcDb::OpenMode mode=AcDb::kForRead,
                                                 bool bCreateIfNotPresent=false);



/*
*        Purpose:
*                打开数据库中命名对象字典NOD(Named Object Dictionary)的entry
*        Argument:
*                pDb                [in], Db指针,NULL时为当前数据库
*                key                [in], entry名称
*                pObj        [out], 返回的entry对象指针
*                mode        [in], 打开模式,默认为read
*        Return:
*                Acad::eOk, 找到并打开
*/
extern DLLIMPEXP Acad::ErrorStatus
getInNOD (AcDbDatabase *pDb, const char *key, AcDbObject *&pObj, AcDb::OpenMode mode=AcDb::kForWrite);



/*
*        Purpose:
*                打开数据库中命名对象字典NOD(Named Object Dictionary)的entry
*                默认为当前数据库。
*        Argument:
*                key                [in], entry名称
*                pObj        [out], 返回的entry对象指针
*                mode        [in], 打开模式,默认为read
*        Return:
*                Acad::eOk, 找到并打开
*/
extern DLLIMPEXP Acad::ErrorStatus
getInNOD (const char *key, AcDbObject *&pObj, AcDb::OpenMode mode);



extern DLLIMPEXP Acad::ErrorStatus
/*
*        Purpose:
*                返回字典中对象的entry名称
*        Argument:
*                pDict                [in], 字典指针
*                pObj                [in], 对象指针
*                entryname        [out], 返回的entry名称, 使用完后要注意释放空间
*        Return:
*                Acad::eOk, 成功
*                Acad::eKeyNotFound, 未找到
*/
getInDictionary (const AcDbDictionary *pDict,
                                 const AcDbObject *pObj,
                                 char *&entryname);




/*
*        Purpose:
*                打开对象伴随的扩展字典
*        Argument:
*                pObj                                [in], 对象指针
*                key                                        [in], 字典名称
*                pDict                                [out], 字典指针
*                mode                                [in], 打开模式,默认为read
*                bCreateIfNotPresent        [in], 如果不存在,是否创建,默认为false
*        Return:
*                Acad::eOk, 找到并打开
*                Acad::eCreateInvalidName, 未找到但创建
*                Acad::eKeyNotFound, 未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
getObjectExtCompanyDictionary (AcDbObject *pObj,
                                                           const char *key,
                                                           AcDbDictionary *&pDict,
                                                           AcDb::OpenMode mode=AcDb::kForRead,
                                                           bool bCreateIfNotPresent=true);



/*
*        Purpose:
*                获取对象伴随的扩展字典Id
*        Argument:
*                pObj                                [in], 对象指针
*                key                                        [in], 字典名称
*                bCreateIfNotPresent        [in], 如果不存在,是否创建,默认为false
*        Return:
*                对象Id,如果失败返回NULL
*/
extern DLLIMPEXP AcDbObjectId
getObjectExtCompanyDictionaryId (AcDbObject *pObj,
                                                                 const char *key,
                                                                 bool bCreateIfNotPresent);




/*
*        Purpose:
*                加入对象到命名对象字典NOD(Named Object Dictionary)的子字典中
*        Argument:
*                pTargetDb                        [in], Db指针, NULL时为当前数据库
*                pObj                                [in], 对象指针
*                entryname                        [in], 对象在子字典中entry名称
*                whichDict                        [in], NOD子字典名称
*                bOverideIfnameExists[in], 如果存在是否覆盖,默认为false
*        Return:
*                Acad::eOk, 成功
*                Acad::eAlreadyInDb, 如果存在但不覆盖
*/
extern DLLIMPEXP Acad::ErrorStatus
postToNODCompanyDictionary(AcDbDatabase *pTargetDb,
                                                   AcDbObject *pObj,
                                                   const char *entryname,
                                                   const char *whichDict,
                                                   bool bOverideIfnameExists=false);



/*
*        Purpose:
*                加入对象到命名对象字典NOD(Named Object Dictionary)的子字典中
*                默认为当前数据库
*        Argument:
*                pObj                                [in], 对象指针
*                entryname                        [in], 对象在子字典中entry名称
*                whichDict                        [in], NOD子字典名称
*                bOverideIfnameExists[in], 如果存在是否覆盖,默认为false
*        Return:
*                Acad::eOk, 成功
*                Acad::eAlreadyInDb, 如果存在但不覆盖
*/
extern DLLIMPEXP Acad::ErrorStatus
postToNODCompanyDictionary(AcDbObject *pObj,
                                                   const char *entryname,
                                                   const char *whichDict,
                                                   bool bOverideIfnameExists=false);


/*
*        Purpose:
*                加入对象到字典中
*        Argument:
*                pObj                                [in], 对象指针
*                entryname                        [in], 对象在字典中entry名称
*                pDict                                [in], 字典指针
*                bOverideIfnameExists[in], 如果存在是否覆盖,默认为false
*        Return:
*                Acad::eOk, 成功
*                Acad::eAlreadyInDb, 如果存在但不覆盖
*/
extern DLLIMPEXP Acad::ErrorStatus
postToDictionary(AcDbObject *pObj,
                                 const char *entryname,
                                 AcDbDictionary *pDict,
                                 bool bOverideIfnameExists=false);




/*
*        Purpose:
*                加入对象到命名对象字典NOD(Named Object Dictionary)中
*        Argument:
*                pTargetDb                        [in], 数据库指针,NULL时为当前数据库
*                pObj                                [in], 对象指针
*                entryname                        [in], 对象在子字典中entry名称
*                bOverideIfnameExists[in], 如果存在是否覆盖,默认为false
*        Return:
*                Acad::eOk, 成功
*                Acad::eAlreadyInDb, 如果存在但不覆盖
*/
extern DLLIMPEXP Acad::ErrorStatus
postToNOD(AcDbDatabase *pTargetDb,
                  AcDbObject *pObj,
                  const char *entryname,
                  bool bOverideIfnameExists=false);




/*
*        Purpose:
*                加入对象到命名对象字典NOD(Named Object Dictionary)中
*                默认为当前数据库
*        Argument:
*                pObj                                [in], 对象指针
*                entryname                        [in], 对象在子字典中entry名称
*                bOverideIfnameExists[in], 如果存在是否覆盖,默认为false
*        Return:
*                Acad::eOk, 成功
*                Acad::eAlreadyInDb, 如果存在但不覆盖
*/
extern DLLIMPEXP Acad::ErrorStatus
postToNOD(AcDbObject *pObj,
                  const char *entryname,
                  bool bOverideIfnameExists=false);



/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)子字典中对象entry
*        Argument:
*                pTargetDb                        [in], 数据库指针,NULL时为当前数据库
*                entryname                        [in], 对象在子字典中entry名称
*                whichDict                        [in], 子字典名称
*        Return:
*                Acad::eOk, 成功
*                Acad::eInvalidInput, 输入的名称为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInNODCompanyDictionary(AcDbDatabase *pTargetDb,
                                                         const char *entryname,
                                                         const char *whichDict);



/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)子字典中对象entry
*                默认为当前数据库
*        Argument:
*                entryname                        [in], 对象在子字典中entry名称
*                whichDict                        [in], 子字典名称
*        Return:
*                Acad::eOk, 成功
*                Acad::eInvalidInput, 输入的名称为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInNODCompanyDictionary(const char *entryname,
                                                         const char *whichDict);



/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)中对象entry
*        Argument:
*                pTargetDb                        [in], 数据库指针,NULL时为当前数据库
*                entryname                        [in], 对象在字典中entry名称
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInNOD(AcDbDatabase *pTargetDb, const char *entryname);



/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)中对象entry
*                默认为当前数据库
*        Argument:
*                entryname                        [in], 对象在字典中entry名称
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus deleteInNOD(const char *entryname);




/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)中对象
*        Argument:
*                pTargetDb        [in], 数据库指针,NULL时为当前数据库
*                eId                        [in], 对象Id
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInNOD(AcDbDatabase *pTargetDb, const AcDbObjectId eId);



/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)中对象
*                默认为当前数据库
*        Argument:
*                eId                        [in], 对象Id
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus deleteInNOD(const AcDbObjectId eId);



/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)中对象
*        Argument:
*                pTargetDb        [in], 数据库指针,NULL时为当前数据库
*                pObj                [in], 对象指针
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInNOD(AcDbDatabase *pTargetDb, AcDbObject *pObj);



/*
*        Purpose:
*                删除命名对象字典NOD(Named Object Dictionary)中对象
*                默认为当前数据库
*        Argument:
*                pObj                [in], 对象指针
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus deleteInNOD(AcDbObject *pObj);



/*
*        Purpose:
*                删除字典中对象
*        Argument:
*                entryname        [in], 对象entry名称
*                pDict                [in], 字典指针
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInDictionary(const char *entryname, AcDbDictionary *pDict);




/*
*        Purpose:
*                删除字典中对象
*        Argument:
*                eId                        [in], 对象Id
*                pDict                [in], 字典指针
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInDictionary(const AcDbObjectId eId, AcDbDictionary *pDict);



/*
*        Purpose:
*                删除字典中对象
*        Argument:
*                pObj                [in], 对象指针
*                pDict                [in], 字典指针
*        Return:
*                Acad::eOk, 成功
*                Acad::eCannotBeErasedByCaller, entry为字典,且不为空
*                Acad::eKeyNotFound, 子字典或对象未找到
*/
extern DLLIMPEXP Acad::ErrorStatus
deleteInDictionary(AcDbObject *pObj, AcDbDictionary *pDict);

#endif // DB_UTILITIES_H
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-12 03:47 , Processed in 0.364188 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表