1. //获取块参照属性
    2. ads_name ent;
    3. ads_point pt;
    4. if (RTNORM != acedEntSel(NULL,ent,pt))
    5. {
    6. acutPrintf(_T("\n未选择有效对象!"));
    7. return;
    8. }
    9. AcDbObjectId objId;
    10. acdbGetObjectId(objId,ent);
    11. AcDbObjectPointer<AcDbBlockReference> pBlkRef(objId,AcDb::kForRead);
    12. if (Acad::eOk != pBlkRef.openStatus())
    13. {
    14. acutPrintf(_T("\n打开块参照失败,错误码: %s "),pBlkRef.openStatus());
    15. return;
    16. }
    17. //直接获取图块的属性迭代器
    18. AcDbObjectIterator *pIter=pBlkRef->attributeIterator();
    19. //无论图块是否有属性,迭代器一般不会为NULL
    20. if (NULL == pIter)
    21. {
    22. acutPrintf(_T("\n获取属性迭代器失败!"));
    23. return;
    24. }
    25. //设置判断是否能获取到属性
    26. bool bIsAttribBlock=false;
    27. for (pIter->start();!pIter->done();pIter->step())
    28. {
    29. AcDbObjectId attribId=pIter->objectId();
    30. AcDbObjectPointer<AcDbAttribute> pAttrib(attribId,AcDb::kForRead);
    31. if (Acad::eOk != pAttrib.openStatus())
    32. {
    33. continue;
    34. }
    35. //方式一
    36. /*
    37. CString strTag;
    38. strTag=pAttrib->tag();
    39. CString strValue;
    40. strValue=pAttrib->textString();
    41. acutPrintf(_T("\n属性名: %s 属性值: %s"),strTag,strValue);
    42. */
    43. //方式二
    44. ACHAR *szTag=pAttrib->tag();
    45. ACHAR *szValue=pAttrib->textString();
    46. if (szTag!=NULL && szValue!=NULL)
    47. {
    48. acutPrintf(_T("\n属性名: %s 属性值: %s"),szTag,szValue);
    49. }
    50. else
    51. {
    52. acutPrintf(_T("\n获取属性值失败!"),szTag,szValue);
    53. }
    54. if (szTag!=NULL)
    55. {
    56. acutDelString(szTag);
    57. }
    58. if (szValue!=NULL)
    59. {
    60. acutDelString(szValue);
    61. }
    62. //获取到属性,设置标志
    63. bIsAttribBlock=true;
    64. }
    65. //释放迭代器
    66. delete pIter;
    67. if (!bIsAttribBlock)
    68. {
    69. acutPrintf(_T("\n该块参照无属性!"));
    70. }

    1157760-20200407223007542-1653336595.png