{"id":19893,"date":"2025-04-27T22:51:49","date_gmt":"2025-04-27T14:51:49","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=19893"},"modified":"2025-05-10T11:08:28","modified_gmt":"2025-05-10T03:08:28","slug":"objectarx-2025%ef%bc%88c%ef%bc%89%e6%8f%92%e5%85%a5%e5%b8%a6%e5%b1%9e%e6%80%a7%e7%9a%84%e5%9d%97%e5%8f%82%e7%85%a7-acdbblockreference","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=19893","title":{"rendered":"ObjectARX 2025\uff08C++\uff09\u63d2\u5165\u5e26\u5c5e\u6027\u7684\u5757\u53c2\u7167 AcDbAttribute"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=19893\/#%E7%8E%AF%E5%A2%83\" >\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=19893\/#%E5%AE%9E%E7%8E%B0\" >\u5b9e\u73b0<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E7%8E%AF%E5%A2%83\"><\/span>\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>AutoCAD \u673a\u68b0\u7248 2025<br \/>\nObjectARX 2025<br \/>\nVS 2022\uff08ISO C++14\uff09<\/p>\n<p>\u73af\u5883\u914d\u7f6e\u53c2\u8003\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=16480\">https:\/\/blog.iyatt.com\/?p=16480<\/a><br \/>\n\u5757\u5c5e\u6027\u4f7f\u7528\u53c2\u8003\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=19800\">https:\/\/blog.iyatt.com\/?p=19800<\/a><br \/>\n\u81ea\u5b9a\u4e49\u7c7b\u4f7f\u7528\u53c2\u8003\uff1a<a href=\"https:\/\/blog.iyatt.com\/?p=16635\">https:\/\/blog.iyatt.com\/?p=16635<\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E7%8E%B0\"><\/span>\u5b9e\u73b0<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>InsertBlockWithAttribute.hpp<\/p>\n<pre><code class=\"language-cpp\">#pragma once\nclass InsertBlockWithAttribute\n{\npublic:\n    static void init();\n    static void unload();\nprivate:\n    \/**\n     * @brief \u5de5\u4f5c\u547d\u4ee4\n     *\/\n    static void work();\n\n    \/**\n     * @brief \u83b7\u53d6\u5757\u5b9a\u4e49ID\n     * @param blockName \u5757\u540d\u79f0\n     * @return \u5757\u5b9a\u4e49 ID\n     *\/\n    static AcDbObjectId getBlockDefineId(AcString blockName);\n\n    \/**\n     * @brief \u63d2\u5165\u5e26\u5c5e\u6027\u7684\u5757\n     * @param blockDefineId \u5757\u5b9a\u4e49ID\n     * @param insertPtObj \u63d2\u5165\u70b9\n     * @return \u5757\u53c2\u7167 ID\n     *\/\n    static AcDbObjectId insertBlock(const AcDbObjectId&amp; blockDefineId, const AcGePoint3d&amp; insertPtObj);\n\n    \/**\n     * @brief \u5c06\u5b9e\u4f53\u63d2\u5165\u5230\u6a21\u578b\u7a7a\u95f4\u4e2d\n     * @param pEnt \u5b9e\u4f53\n     * @param pDb \u5f53\u524d\u5de5\u4f5c\u6570\u636e\u5e93\n     * @return \u63d2\u5165\u5b9e\u4f53\u7684 ID\n     *\/\n    static AcDbObjectId postToModalSpace(AcDbEntity* pEnt);\n\n    \/**\n     * @brief \u5c06\u5c5e\u6027\u5b9a\u4e49\u6dfb\u52a0\u5230\u5757\u53c2\u7167\u4e2d\n     * @param pBlockReference \u5757\u53c2\u7167\n     * @param pAttributeDefinition \u5c5e\u6027\u5b9a\u4e49\n     *\/\n    static void appendAttributeToBlockReference(AcDbBlockReference* pBlockReference, AcDbAttributeDefinition* pAttributeDefinition);\n\n    \/**\n     * @brief \u8bbe\u7f6e\u5757\u53c2\u7167\u7684\u5c5e\u6027\u503c\n     * @param pBlockReference \u5757\u53c2\u7167\n     * @param tag \u5c5e\u6027\u6807\u8bb0\n     * @param textVal \u5c5e\u6027\u503c\n     *\/\n    static void setBlockReferenceAttribute(AcDbBlockReference* pBlockReference, AcString&amp; tag, AcString&amp; textVal);\n};<\/code><\/pre>\n<p>insertBlockWithAttributeProject.cpp<\/p>\n<pre><code class=\"language-cpp\">#include &quot;stdafx.h&quot;\n#include &quot;InsertBlockWithAttribute.hpp&quot;\n\nvoid InsertBlockWithAttribute::init()\n{\n    acedRegCmds-&gt;addCommand(L&quot;IYATTyxTestInsertBlockWithAttribute&quot;, L&quot;TestInsertBlockWithAttribute&quot;, L&quot;TestInsertBlockWithAttribute&quot;, ACRX_CMD_MODAL, InsertBlockWithAttribute::work);\n}\n\nvoid InsertBlockWithAttribute::unload()\n{\n    acedRegCmds-&gt;removeGroup(L&quot;IYATTyxTestInsertBlockWithAttribute&quot;);\n}\n\nvoid InsertBlockWithAttribute::work()\n{\n    AcString blockName = L&quot;\u5e8f\u53f7\u5757&quot;;\n    \/\/if (acedGetString(Adesk::kFalse, L&quot;\u8f93\u5165\u8981\u63d2\u5165\u7684\u5757\u540d\u79f0\uff1a&quot;, blockName) != RTNORM)\n    \/\/{\n    \/\/  return;\n    \/\/}\n\n    AcDbObjectId blockDefineId = InsertBlockWithAttribute::getBlockDefineId(blockName);\n    if (blockDefineId.isNull())\n    {\n        acutPrintf(L&quot;\u5757\u5b9a\u4e49\u4e0d\u5b58\u5728\uff01\\n&quot;);\n        return;\n    }\n\n    ads_point insertPt;\n    if (acedGetPoint(nullptr, L&quot;\u8f93\u5165\u63d2\u5165\u70b9\uff1a&quot;, insertPt) != RTNORM)\n    {\n        return;\n    }\n    AcGePoint3d insertPtObj = asPnt3d(insertPt);\n\n    \/\/ \u63d2\u5165\u5757\u53c2\u7167\n    AcDbObjectId blockReferenceId = InsertBlockWithAttribute::insertBlock(blockDefineId, insertPtObj);\n\n    \/\/ \u8bbe\u7f6e\u53c2\u6570\u503c\n    static int num = 1;\n    AcDbBlockReference* pBlockReference = nullptr;\n    if (acdbOpenObject(pBlockReference, blockReferenceId, AcDb::kForWrite) == Acad::eOk)\n    {\n        AcString tag = L&quot;\u5e8f\u53f7&quot;;\n        AcString textVal;\n        textVal.format(L&quot;%d&quot;, num);\n        InsertBlockWithAttribute::setBlockReferenceAttribute(pBlockReference, tag, textVal);\n        pBlockReference-&gt;close();\n        ++num;\n    }\n}\n\nAcDbObjectId InsertBlockWithAttribute::getBlockDefineId(AcString blockName)\n{\n    AcDbBlockTable* pBlockTable = nullptr;\n    if (acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getBlockTable(pBlockTable, AcDb::kForRead) != Acad::eOk)\n    {\n        return AcDbObjectId::kNull;\n    }\n\n    AcDbObjectId blockDefineId;\n    Acad::ErrorStatus es = pBlockTable-&gt;getAt(blockName.kACharPtr(), blockDefineId);\n    pBlockTable-&gt;close();\n    if (es == Acad::eOk)\n    {\n        return blockDefineId;\n    }\n    else\n    {\n        return AcDbObjectId::kNull;\n    }\n}\n\nAcDbObjectId InsertBlockWithAttribute::insertBlock(const AcDbObjectId&amp; blockDefineId, const AcGePoint3d&amp; insertPtObj)\n{\n    \/\/ \u5757\u53c2\u7167\u5b9e\u4f8b\n    AcDbBlockReference* pBlockReference = new AcDbBlockReference(insertPtObj, blockDefineId);\n\n    \/\/ \u6bd4\u4f8b\n    double scale = 1;\n    AcGeScale3d scaleObj(scale, scale, scale);\n    pBlockReference-&gt;setScaleFactors(scaleObj);\n\n    \/\/ \u65cb\u8f6c\u89d2\u5ea6\n    double rotation = 0;\n    pBlockReference-&gt;setRotation(rotation);\n\n    AcDbBlockTableRecord* pBlockTableRecord = nullptr;\n    if (acdbOpenObject(pBlockTableRecord, pBlockReference-&gt;blockTableRecord(), AcDb::kForRead) == Acad::eOk)\n    {\n        \/\/ \u5224\u65ad\u5757\u53c2\u7167\u7684\u5757\u8868\u8bb0\u5f55\u4e2d\u662f\u5426\u6709\u5c5e\u6027\u5b9a\u4e49\n        if (pBlockTableRecord-&gt;hasAttributeDefinitions())\n        {\n            AcDbBlockTableRecordIterator* pBlockTableRecordIterator = nullptr;\n            pBlockTableRecord-&gt;newIterator(pBlockTableRecordIterator);\n            for (pBlockTableRecordIterator-&gt;start(); !pBlockTableRecordIterator-&gt;done(); pBlockTableRecordIterator-&gt;step())\n            {\n                AcDbEntity* pEnt = nullptr;\n                if (pBlockTableRecordIterator-&gt;getEntity(pEnt, AcDb::kForRead) == Acad::eOk)\n                {\n                    AcDbAttributeDefinition* pAttributeDefinition = AcDbAttributeDefinition::cast(pEnt);\n                    if (pAttributeDefinition)\n                    {\n                        InsertBlockWithAttribute::appendAttributeToBlockReference(pBlockReference, pAttributeDefinition);\n                    }\n                    pEnt-&gt;close();\n                }\n            }\n            delete pBlockTableRecordIterator;\n        }\n    }\n    pBlockTableRecord-&gt;close();\n    return InsertBlockWithAttribute::postToModalSpace(pBlockReference);\n}\n\nAcDbObjectId InsertBlockWithAttribute::postToModalSpace(AcDbEntity* pEnt)\n{\n    assert(pEnt);\n\n    AcDbBlockTable* pBlockTable = nullptr;\n    acdbHostApplicationServices()-&gt;workingDatabase()-&gt;getBlockTable(pBlockTable, AcDb::kForWrite);\n\n    AcDbBlockTableRecord* pBlockTableRecord = nullptr;\n    pBlockTable-&gt;getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);\n    pBlockTable-&gt;close();\n\n    AcDbObjectId entId;\n    if (pBlockTableRecord-&gt;appendAcDbEntity(entId, pEnt) != Acad::eOk)\n    {\n        pBlockTableRecord-&gt;close();\n        delete pEnt;\n        pEnt = nullptr;\n        return AcDbObjectId::kNull;\n    }\n\n    pBlockTableRecord-&gt;close();\n    pEnt-&gt;close();\n    return entId;\n}\n\nvoid InsertBlockWithAttribute::appendAttributeToBlockReference(AcDbBlockReference* pBlockReference, AcDbAttributeDefinition* pAttributeDefinition)\n{\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u5c5e\u6027\u5bf9\u8c61\n    AcDbAttribute* pAttribute = new AcDbAttribute();\n    \/\/ \u4ece\u5757\u53c2\u7167\u83b7\u53d6\u5c5e\u6027\u5bf9\u8c61\u7684\u7279\u6027\n    pAttribute-&gt;setPropertiesFrom(pBlockReference);\n    pAttribute-&gt;setLayer(pBlockReference-&gt;layerId());\n    \/\/ \u8bbe\u7f6e\u5c5e\u6027\u5bf9\u8c61\u7684\u5176\u5b83\u7279\u6027\n    pAttribute-&gt;setInvisible(pAttribute-&gt;isInvisible());\n    pAttribute-&gt;setPosition(pAttributeDefinition-&gt;position());\n    pAttribute-&gt;setHeight(pAttributeDefinition-&gt;height());\n    pAttribute-&gt;setWidthFactor(pAttributeDefinition-&gt;widthFactor());\n    pAttribute-&gt;setRotation(pAttributeDefinition-&gt;rotation());\n    pAttribute-&gt;setHorizontalMode(pAttributeDefinition-&gt;horizontalMode());\n    pAttribute-&gt;setVerticalMode(pAttributeDefinition-&gt;verticalMode());\n    pAttribute-&gt;setAlignmentPoint(pAttributeDefinition-&gt;alignmentPoint());\n    pAttribute-&gt;setTextStyle(pAttributeDefinition-&gt;textStyle());\n    pAttribute-&gt;setAttributeFromBlock(pBlockReference-&gt;blockTransform());\n\n    \/\/ \u83b7\u53d6\u5c5e\u6027\u5bf9\u8c61\u7684\u6807\u8bb0\u3001\u503c\n    pAttribute-&gt;setTag(pAttributeDefinition-&gt;tag());\n    pAttribute-&gt;setFieldLength(pAttributeDefinition-&gt;fieldLength());\n    pAttribute-&gt;setTextString(pAttributeDefinition-&gt;textString());\n\n    pBlockReference-&gt;appendAttribute(pAttribute);\n    pAttribute-&gt;close();\n}\n\nvoid InsertBlockWithAttribute::setBlockReferenceAttribute(AcDbBlockReference* pBlockReference, AcString&amp; tag, AcString&amp; textVal)\n{\n    AcDbObjectIterator* pAttributeIterator = pBlockReference-&gt;attributeIterator();\n    for (pAttributeIterator-&gt;start(); !pAttributeIterator-&gt;done(); pAttributeIterator-&gt;step())\n    {\n        AcDbAttribute* pAttribute = nullptr;\n        if (acdbOpenObject(pAttribute, pAttributeIterator-&gt;objectId(), AcDb::kForWrite) != Acad::eOk)\n        {\n            continue;\n        }\n        if (tag == pAttribute-&gt;tag())\n        {\n            pAttribute-&gt;setTextString(textVal.kACharPtr());\n        }\n        pAttribute-&gt;close();\n    }\n    delete pAttributeIterator;\n}<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u5757\uff0c\u540d\u79f0\u4e3a\uff1a\u5e8f\u53f7\u5757<br \/>\n\u9644\u5e26\u4e00\u4e2a\u5c5e\u6027\u5b9a\u4e49\uff0c\u6807\u8bb0\uff1a\u5e8f\u53f7<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/04\/image-1745765347539.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 543px; --smush-placeholder-aspect-ratio: 543\/572;\" \/><\/p>\n<p>\u8c03\u8bd5\u8fd0\u884c\uff0c\u6267\u884c\u547d\u4ee4 TestInsertBlockWithAttribute<br \/>\n\u9009\u62e9\u63d2\u5165\u70b9\u4f4d\u7f6e\uff0c\u5e8f\u53f7\u81ea\u52a8\u9012\u589e<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/04\/image-1745765486470.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1060px; --smush-placeholder-aspect-ratio: 1060\/854;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883 AutoCAD \u673a\u68b0\u7248 2025 ObjectARX 2025 VS 2022\uff08ISO C++14\uff09 \u73af [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,616,589,618,621],"tags":[],"class_list":["post-19893","post","type-post","status-publish","format-standard","hentry","category-all","category-autocad","category-c-c","category-cad-cam","category-objectarx"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19893"}],"version-history":[{"count":3,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19893\/revisions"}],"predecessor-version":[{"id":20062,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/19893\/revisions\/20062"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}