Message 1 of 7
Not applicable
03-27-2017
10:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I wrote a function that takes a block name, external reference name and a vector of points. The function will look for the external reference and return all the positions of the specified blocks in that xRef. The code works in most cases but sometimes it returns an empty array when I call xRefBlockTableRecord->getBlockReferenceIds( blockIds ) even though it has already found a block table record in the xRef. Is there someway that blocks are not accessible in the xRef? Or is there an error in my code? Thanks for any advice you can offer. The code is below.
bool getPoints( const ACHAR *xRefName, const ACHAR *moduleName, std::vector<AcGePoint2d> *points )
{
Acad::ErrorStatus errorStatus;
AcDbDatabase *database = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTable *blockTable = NULL;
AcDbBlockTableRecord *blockTableRecord = NULL;
AcDbDatabase *xRefDatabase = NULL;
const ACHAR *xRefPath = NULL;
AcDbDatabase *xRefDWGDatabase = NULL;
AcDbBlockTable *xRefBlockTable = NULL;
AcDbBlockTableRecord *xRefBlockTableRecord = NULL;
AcDbEntity *ent = NULL;
AcDbBlockReference *ref = NULL;
AcDbObjectIdArray blockIds;
AcDbObjectIdArray refIds;
//xRef transformations
double xOffset = 0;
double yOffset = 0;
double rotation = 0;
//Get the block table for our database
errorStatus = database->getBlockTable(blockTable, AcDb::kForRead);
if( errorStatus != Acad::eOk )
{
acutPrintf(L"\nError getting block table %s", acadErrorStatusText( errorStatus ) );
return false;
}
//Get the block table record for the external reference
errorStatus = blockTable->getAt( xRefName, blockTableRecord, AcDb::kForRead );
if( errorStatus != Acad::eOk )
{
acutPrintf(L"\nError could not get xref %s %s", xRefName, acadErrorStatusText( errorStatus ) );
blockTable->close();
return false;
}
//Get the transformation of the xRef
errorStatus = blockTableRecord->getBlockReferenceIds( refIds );
if( errorStatus != Acad::eOk || refIds.length() < 1 )
{
acutPrintf(L"\nError could not get xref reference ids %s", acadErrorStatusText( errorStatus ));
blockTable->close();
return false;
}
//Open the reference
acdbOpenObject( ent, refIds.at(0), AcDb::kForRead );
if( ent == NULL )
{
acutPrintf(L"\nError could not get xref reference entity %s", acadErrorStatusText( errorStatus ));
blockTable->close();
return false;
}
//Cast the reference
ref = AcDbBlockReference::cast( ent );
if( ref == NULL )
{
acutPrintf(L"\nError could not get xref reference %s", acadErrorStatusText( errorStatus ));
blockTable->close();
ent->close();
return false;
}
//Get transformations
xOffset = ref->position().x;
yOffset = ref->position().y;
rotation = ref->rotation();
ent->close();
xRefDatabase = blockTableRecord->xrefDatabase(true);
blockTable->close();
blockTableRecord->close();
if( xRefDatabase == NULL )
{
acutPrintf(L"\nCould not get external reference database" );
return false;
}
//Get path for external reference dwg file
xRefPath = acdbOriginalXrefFullPathFor( xRefDatabase );
if( xRefPath == NULL )
{
acutPrintf(L"\nCould not get external reference path" );
return false;
}
//Get database for the external reference drawing
xRefDWGDatabase = new AcDbDatabase(Adesk::kFalse);
errorStatus = xRefDWGDatabase->readDwgFile(xRefPath);
if( errorStatus != Acad::eOk )
{
acutPrintf(L"\nError could not read xref file %s", acadErrorStatusText( errorStatus ));
delete xRefDWGDatabase;
return false;
}
//Get the block table for the open model reference file
errorStatus = xRefDWGDatabase->getBlockTable( xRefBlockTable, AcDb::kForRead );
if( errorStatus != Acad::eOk )
{
acutPrintf(L"\nError could not load xref block table %s", acadErrorStatusText( errorStatus ));
delete xRefDWGDatabase;
return false;
}
//Get the definition for the block in the the external reference
errorStatus = xRefBlockTable->getAt( moduleName, xRefBlockTableRecord, AcDb::kForRead );
if( errorStatus != Acad::eOk )
{
acutPrintf(L"\nError could not get xref block table record %s", acadErrorStatusText( errorStatus ));
xRefBlockTable->close();
delete xRefDWGDatabase;
return false;
}
xRefBlockTable->close();
errorStatus = xRefBlockTableRecord->getBlockReferenceIds( blockIds );
if( errorStatus != Acad::eOk )
{
acutPrintf(L"\nError could not get xref block ids %s", acadErrorStatusText( errorStatus ));
xRefBlockTableRecord->close();
delete xRefDWGDatabase;
return false;
}
//Push all points into the vector
for( int i = 0; i < blockIds.length(); i++ )
{
ent = NULL;
acdbOpenObject( ent, blockIds.at(i), AcDb::kForRead );
if( ent != NULL )
{
ref = NULL;
ref = AcDbBlockReference::cast( ent );
if( ref != NULL )
{
AcGePoint2d pnt;
pnt.x = ref->position().x;
pnt.y = ref->position().y;
//Transfrom point
pnt.x += xOffset;
pnt.y += yOffset;
points->push_back( pnt );
}
ent->close();
}
}
delete xRefDWGDatabase;
xRefBlockTableRecord->close();
return true;
}
Solved! Go to Solution.

