不开图使用ObjectARX属性块参照遍历属性值实例代码
AcDbDatabase* pDb = new AcDbDatabase(Adesk::kFalse); Acad::ErrorStatus es; es = pDb->readDwgFile(_T("D:\\A0-产品.dwg")); AcDbBlockTable *pBlkTbl; pDb->getBlockTable(pBlkTbl, AcDb::kForRead); AcDbBlockTableRecord *pBlkTblRcd; pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd, AcDb::kForRead); pBlkTbl->close(); AcDbBlockTableRecordIterator *pltr; pBlkTblRcd->newIterator(pltr); std::map<AcRxClass*,std::vector<AcDbObjectId> > entMap; for (pltr->start(); !pltr->done(); pltr->step()) { AcDbEntity *pEnt; pltr->getEntity(pEnt, AcDb::kForRead); if(pEnt->isKindOf(AcDbBlockReference::desc())) { AcDbBlockReference* pBlkRef = AcDbBlockReference::cast(pEnt); //直接获取图块的属性迭代器 AcDbObjectIterator *pIter=pBlkRef->attributeIterator(); //无论图块是否有属性,迭代器一般不会为NULL if (NULL == pIter) { pEnt->close(); pEnt = NULL; delete pltr; pBlkTblRcd->close(); 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该块参照无属性!")); } } if (pEnt != NULL) { pEnt->close(); pEnt = NULL; } } delete pltr; pBlkTblRcd->close();
代码参考自edata的博客并修改:
https://www.cnblogs.com/edata/p/12656673.html
//获取块参照属性 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该块参照无属性!")); }