ObjectARX
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

insert block from from drawing container

1 REPLY 1
Reply
Message 1 of 2
Moshe-A
348 Views, 1 Reply

insert block from from drawing container

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

 

1 REPLY 1
Message 2 of 2
StephenPreston
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost