📄 dbutilities.cpp
字号:
#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') );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -