ObjectARX

Reply
Distinguished Mentor
Moshe-A
Posts: 784
Registered: ‎09-14-2003
Message 1 of 2 (186 Views)

insert block from from drawing container

186 Views, 1 Replies
07-13-2011 10:39 PM

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;	
}

 

Board Manager
StephenPreston
Posts: 418
Registered: ‎05-22-2006
Message 2 of 2 (146 Views)

Re: insert block from from drawing container

07-26-2011 10:15 AM in reply to: Moshe-A

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

 

 

Cheers,

Stephen Preston
Autodesk Developer Network
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!