找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 163|回复: 0

objectarx代码 acedSSGet 选择集生成匿名块

[复制链接]

1

主题

0

回帖

35

积分

管理员

积分
35
发表于 2024-3-16 10:06:07 | 显示全部楼层 |阅读模式
这段代码中,我们没有创建新的块定义,而是直接使用了一个空的AcDbObjectId来创建匿名块参照,然后将选中的每个实体移动到该匿名块中。请注意,匿名块的特点是它不会出现在块表中,而且通常仅用于临时存储或内部操作。当涉及到多个实体合并为一个整体时,这种方式可以简化处理流程,但请确保了解其适用场景和限制。
  1. #include "acedinpt.h" // For acedSSGet
  2. #include "dbmain.h" // For AcDbEntity and related classes
  3. #include "dbidmap.h" // For AcDbIdMapping
  4. // Function to create anonymous blocks from the selected set of entities
  5. void CreateAnonymousBlocksFromSelection()
  6. {
  7.     ads采用name ss;
  8.     struct resbuf rb;
  9.     // Get the user selection
  10.     acedSSGet(NULL, NULL, NULL, NULL, ss);
  11.     // Check if there's a valid selection
  12.     if (ss != RTNORM)
  13.         return;
  14.     AcDbVoidPtrArray entities;
  15.     Acad::ErrorStatus es = acdbGsMarker(ss, entities);
  16.     if (es != Acad::eOk)
  17.     {
  18.         acutPrintf("\nFailed to get entities from selection.");
  19.         return;
  20.     }
  21.     if (entities.isEmpty())
  22.     {
  23.         acutPrintf("\nNo entities were selected.");
  24.         return;
  25.     }
  26.     // Iterate over selected entities
  27.     for (int i = 0; i < entities.length(); ++i)
  28.     {
  29.         AcDbEntity* pEnt;
  30.         es = acdbOpenObject(pEnt, (AcDbObjectId)entities[i], AcDb::kForWrite);
  31.         if (es == Acad::eOk)
  32.         {
  33.             AcGeMatrix3d oldToNew;
  34.             oldToNew.setToIdentity();
  35.             // Create an anonymous block reference
  36.             AcDbBlockReference* pBlkRef = new AcDbBlockReference();
  37.             // Instead of creating a named block, we'll use an empty objectId to create an anonymous block
  38.             AcDbObjectId blkRecId = AcDbObjectId::kNull;
  39.             pBlkRef->setBlockTableRecord(blkRecId);
  40.             pBlkRef->setPosition(pEnt->position()); // Set insert point to entity's original position
  41.             pBlkRef->setTransform(oldToNew);
  42.             // Copy the entity into the anonymous block
  43.             AcDbIdMapping idMap;
  44.             es = pEnt->handOverTo(idMap, pBlkRef);
  45.             if (es != Acad::eOk)
  46.             {
  47.                 delete pBlkRef;
  48.                 acutPrintf("\nFailed to handover entity to anonymous block reference.");
  49.                 continue;
  50.             }
  51.             // Erase the original entity and replace with the anonymous block reference
  52.             pEnt->erase();
  53.             pBlkRef->close(); // Close the block reference without explicitly opening it for write (since it was just created)
  54.             // No need to close pEnt here as it should have been erased already
  55.         }
  56.     }
  57.     // Clear the selection set
  58.     acdbClear采用ss(ss);
  59. }
  60. // Call the function
  61. CreateAnonymousBlocksFromSelection();
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|膜结构网

GMT+8, 2025-1-1 17:22 , Processed in 0.105062 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表