Insert block from another database

Insert block from another database

wesbird
Collaborator Collaborator
885 Views
1 Reply
Message 1 of 2

Insert block from another database

wesbird
Collaborator
Collaborator
Hi,
I am new to ObjectARX so please bear with me if I ask something already answered.
Now I like to insert a block from another drawing(source drawing) in current drawing. I would not like to insert the whole drawing and delete all others. now from the sample I found in this newsgroup/sample/google,
1. use readDwgFile to read source drawing.
2. pDwgBlockTbl->getAt("TPLT-DR", pBlkTblRcd, AcDb::kForRead ) to get the objectid of block
3. pBlkTblRcd->getBlockReferenceIds( list );
4. wblock to a temp database
AcDbDatabase *pCurrentDB = acdbHostApplicationServices()->workingDatabase();
es = pDwg->wblock(pTempDb, list, AcGePoint3d(0,0,0));
5. insert in current drawing
es = pCurrentDB->insert(AcGeMatrix3d::kIdentity, pTempDb);

now I have 2 problem, if there are more than 1 INSERT in source drawing, then all of them will insert in current drawing. I fix by remove all but 1 left in AcDbObjectIdArray list;
2nd if there is no INSERT for that block in source drawing. only block definition exist in blocktable. How I can make it work.
I tried:
AcDbBlockReference *pWindowBlock =new AcDbBlockReference(point, blockId);
It crash and I understand it try to create a new blockReference in current drawing which does not exist the definition yet.
How I can make this work?
actually block definition is all I want. so how to copy a block definition from one database to another if there is no instance (INSERT) in source database?


Thank you,
Wes
AutoCAD 2004, Visual Studio 2002, Windows XP pro
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes
886 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
just try out this code

/*
The "INSERT" option will insert the whole source entities to the destination database. Thus
the below procedure is followed
1. The selected blocks can be transfered from one DB to another using "wblock" only. But the
wblock command is destructive, thus the required block is "wblock"ed to a temp Database.
2. From the above temp Database the "insert" command is used to insert those entites tothe
destination file.
3. The "readDwgFile" must have the option _SH_DENYNO.
*/

ACAD_ERROR Insert(char * source_Filename, char * source_Blockname,
char * destination_Filename, const AcGePoint3d &Position,
const double &scale )
{
//Create the source database for storing the block( to be copied) ie the source
AcDbDatabase *sourceDB = new AcDbDatabase (Adesk::kFalse,Adesk::kTrue);
Acad::ErrorStatus stat = sourceDB->readDwgFile((LPCTSTR)source_Filename);

if(stat != Acad::eOk)
return NO_DWGFILE;

AcDbObjectId sourceBlockId;
GetBlockTableRecordIdFromString(sourceDB,source_Blockname,sourceBlockId);


AcDbDatabase *tempBlockDB = new AcDbDatabase(Adesk ::kFalse,Adesk::kTrue);
stat = sourceDB->wblock(tempBlockDB,sourceBlockId);

//Create a new database object for the destination dwg file to which we have to copy
//the block

AcDbDatabase CurrentDbObject(Adesk::kFalse);

stat = CurrentDbObject.readDwgFile(destination_Filename,_SH_DENYNO);

if(stat != Acad::eOk)
return NO_DWGFILE;


AcDbObjectId destBlockID ;

stat = CurrentDbObject.insert(destBlockID,"InsertList", tempBlockDB );

if(tempBlockDB)
{
delete tempBlockDB;
tempBlockDB = NULL;
}


AcDbBlockTableRecordIterator *pBlockTableRecordItr = NULL;
AcDbBlockTable *pBlockTable = NULL;

stat = CurrentDbObject.getSymbolTable(pBlockTable, AcDb::kForRead);


if(!pBlockTable)
{
acutPrintf("\nthe BlockTable could not be obtained");
}
else
{
AcDbBlockTableRecord *pBlockTableRecord;
stat = pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite);
pBlockTable->close();

if(!pBlockTableRecord)
acutPrintf("\nthe AcDbBlockTableRecord could not be obtained");
else
{
//now open the inserted block in the current DB;

AcDbBlockReference *newBlock = new AcDbBlockReference(Position,destBlockID);
stat = pBlockTableRecord->appendAcDbEntity(newBlock);
stat = newBlock->setScaleFactors(scale);
stat = newBlock->close();


stat = pBlockTableRecord->close();

stat = CurrentDbObject.saveAs(destination_Filename);

}
}

_CrtDumpMemoryLeaks();

return OK;
}


try out and give me the feedback
0 Likes