ObjectARX属性块参照遍历属性值实例代码
属性块属性值遍历参考代码.
//获取块参照属性
ads_name ent;
ads_point pt;
if (RTNORM != acedEntSel(NULL,ent,pt))
{
acutPrintf(_T("\n未选择有效对象!"));
return;
}
AcDbObjectId objId;
acdbGetObjectId(objId,ent);
AcDbObjectPointer<AcDbBlockReference> pBlkRef(objId,AcDb::kForRead);
if (Acad::eOk != pBlkRef.openStatus())
{
acutPrintf(_T("\n打开块参照失败,错误码: %s "),pBlkRef.openStatus());
return;
}
//直接获取图块的属性迭代器
AcDbObjectIterator *pIter=pBlkRef->attributeIterator();
//无论图块是否有属性,迭代器一般不会为NULL
if (NULL == pIter)
{
acutPrintf(_T("\n获取属性迭代器失败!"));
return;
}
//设置判断是否能获取到属性
bool bIsAttribBlock=false;
for (pIter->start();!pIter->done();pIter->step())
{
AcDbObjectId attribId=pIter->objectId();
AcDbObjectPointer<AcDbAttribute> pAttrib(attribId,AcDb::kForRead);
if (Acad::eOk != pAttrib.openStatus())
{
continue;
}
//方式一
/*
CString strTag;
strTag=pAttrib->tag();
CString strValue;
strValue=pAttrib->textString();
acutPrintf(_T("\n属性名: %s 属性值: %s"),strTag,strValue);
*/
//方式二
ACHAR *szTag=pAttrib->tag();
ACHAR *szValue=pAttrib->textString();
if (szTag!=NULL && szValue!=NULL)
{
acutPrintf(_T("\n属性名: %s 属性值: %s"),szTag,szValue);
}
else
{
acutPrintf(_T("\n获取属性值失败!"),szTag,szValue);
}
if (szTag!=NULL)
{
acutDelString(szTag);
}
if (szValue!=NULL)
{
acutDelString(szValue);
}
//获取到属性,设置标志
bIsAttribBlock=true;
}
//释放迭代器
delete pIter;
if (!bIsAttribBlock)
{
acutPrintf(_T("\n该块参照无属性!"));
}


浙公网安备 33010602011771号