Hi Guys,
have a big probbem with inserting a block from drawing container
most of the times autocad is crash (but few does works)
the problem occurs in disposing database memory
if (pDbSrc != NULL)
delete pDbSrc; // dispose memeory
what i'am doing wrong?
thanks in advance
Moshe
Acad::ErrorStatus insert_to_database(const ACHAR *ContainerDwg, const ACHAR *BlkName, AcDbObjectId& BlkTblRecId) { Acad::ErrorStatus es1; ACHAR path [_MAX_PATH] = {0}, dwgFile[_MAX_PATH] = {0}; AcDbDatabase *pDbsrc=NULL; AcDbObjectId idSrcBlk, idDestBlkTbl; AcDbDatabase *pDbDest = NULL; AcDbIdMapping idMap; if (is_block_exist(BlkName, BlkTblRecId) != Acad::eOk) { _stprintf(dwgFile, _T("%s.DWG"), ContainerDwg); // find drawing acedFindFile(dwgFile, path); // allocating memory, create a second database pDbsrc=new AcDbDatabase(false, true); pDbSrc->readDwgFile(path); getBlockTableRecordId(pDbSrc, BlkName, idSrcBlk); // get current database pointer pDbDest = curDoc()->database(); idDestBlkTbl = pDbDest->blockTableId(); idMap.setDestDb(pDbDest); AcDbObjectIdArray idsCopy; idsCopy.append(idSrcBlk); es1 = pDbDest->wblockCloneObjects(idsCopy, idDestBlkTbl, idMap, AcDb::kDrcIgnore); AcDbIdPair idPairBlk; idPairBlk.setKey(idSrcBlk); if (!idMap.compute(idPairBlk)) BlkTblRecId = idPairBlk.value(); if (pDbSrc != NULL) delete pDbSrc; // dispose memeory } return es1; } // return BlkTblRecId Acad::ErrorStatus is_block_exist(ACHAR *BlockName, AcDbObjectId& BlkTblRecId) { Acad::ErrorStatus es1; AcDbBlockTable *pBlockTable = NULL; // open block table for read curDoc()->database()->getBlockTable(pBlockTable, AcDb::kForRead); // get block id es1 = pBlockTable->getAt(BlockName, BlkTblRecId); pBlockTable->close(); return es1; } // return BlkRecId Acad::ErrorStatus getBlockTableRecordId(AcDbDatabase *pDb, const ACHAR *BlkName, AcDbObjectId& BlkRecId) { Acad::ErrorStatus es1; AcDbBlockTable *pBlockTable = NULL; assert(pDb != NULL); pDb->getBlockTable(pBlockTable, AcDb::kForRead); es1 = pBlockTable->getAt(BlkName, BlkRecId); assert(!BlkRecId.isNull()); pBlockTable->close(); return es1; }
Hi Moshe-A,
You must have edited that code because it doesn't compile. However, once I'd corrected your typos, I couldn't get this to crash. So the problem would appear to be either due to the specific contect in which you run this code or the drawing you're trying to clone from/to. I tested on AutoCAD 2011 x64. Perhaps if you post a simplified (non-confidential) sample project and drawings, then someone might be willing to run your code through their debugger for you.
But I'm wondering why you're using wblockCloneObjects to clone a single AcDbBlockTableRecord instead of using this version of AcDbDatabase::insert() ...
Acad::ErrorStatus insert(
AcDbObjectId& blockId,
const ACHAR* pSourceBlockName,
const ACHAR* pDestinationBlockName,
AcDbDatabase* pDb,
bool preserveSourceDatabase = true
);
Cheers,
Stephen Preston
Autodesk Developer Network