Issue
Can I use ObjectARX to get the name of a block-reference inside an ObjectARX application? I know that I can do this by using ads_entget, and then look at the corresponding Group Code.
Solution
There is no method on the block reference class that returns the name of the block definition. The block reference (class AcDbBlockReference) keeps an object ID of the used block definition object (class AcDbBlockTableRecord). The name of the block is stored by the block definition object. To get the block name, you have to obtain the object ID of the block definition and to open it. Then you can ask the block definition for the block name. The following code lets the user select a block reference and extracts the block name:
void AsdkGetBlockName()
{
// Let the user select an INSERT (Block reference).
ads_name ename;
ads_point pt;
if (RTNORM != acedEntSel(L"\nSelect INSERT: ",
ename,
pt))
return;
AcDbObjectId objId;
AcDbEntity *pEnt;
AcDbBlockReference *pInsert;
Acad::ErrorStatus es;
// Test the entity type.
acdbGetObjectId(objId, ename);
if (Acad::eOk != (es = acdbOpenAcDbEntity(pEnt,
objId,
AcDb::kForRead)))
{
acutPrintf(L"\nCannot access selected entity.\n");
return;
}
pInsert = AcDbBlockReference::cast(pEnt);
if (!pInsert)
{
acutPrintf(L"\nSelected entity is not an INSERT.\n");
pEnt->close();
return;
}
// Get the objectID of the block definition.
AcDbObjectId blockDefId = pInsert->blockTableRecord();
// Close the selected INSERT.
pInsert->close();
// Open the block definition.
AcDbBlockTableRecord *pBlkRecord;
if (Acad::eOk != (es = acdbOpenObject(pBlkRecord,
blockDefId,
AcDb::kForRead)))
{
acutPrintf(L"\nCannot access block definition.\n");
return;
}
// Get the name of the block definition.
const TCHAR* pBlkName;
es = pBlkRecord->getName(pBlkName);
pBlkRecord->close();
if ((Acad::eOk != es) || !pBlkName)
{
acutPrintf(L"\nCannot extract block definition name.\n");
return;
}
acutPrintf(L"\nName of block definition: '%s'\n", pBlkName);
}