objectarx代码 acedSSGet 选择集生成匿名块
这段代码中,我们没有创建新的块定义,而是直接使用了一个空的AcDbObjectId来创建匿名块参照,然后将选中的每个实体移动到该匿名块中。请注意,匿名块的特点是它不会出现在块表中,而且通常仅用于临时存储或内部操作。当涉及到多个实体合并为一个整体时,这种方式可以简化处理流程,但请确保了解其适用场景和限制。#include "acedinpt.h" // For acedSSGet
#include "dbmain.h" // For AcDbEntity and related classes
#include "dbidmap.h" // For AcDbIdMapping
// Function to create anonymous blocks from the selected set of entities
void CreateAnonymousBlocksFromSelection()
{
ads采用name ss;
struct resbuf rb;
// Get the user selection
acedSSGet(NULL, NULL, NULL, NULL, ss);
// Check if there's a valid selection
if (ss != RTNORM)
return;
AcDbVoidPtrArray entities;
Acad::ErrorStatus es = acdbGsMarker(ss, entities);
if (es != Acad::eOk)
{
acutPrintf("\nFailed to get entities from selection.");
return;
}
if (entities.isEmpty())
{
acutPrintf("\nNo entities were selected.");
return;
}
// Iterate over selected entities
for (int i = 0; i < entities.length(); ++i)
{
AcDbEntity* pEnt;
es = acdbOpenObject(pEnt, (AcDbObjectId)entities, AcDb::kForWrite);
if (es == Acad::eOk)
{
AcGeMatrix3d oldToNew;
oldToNew.setToIdentity();
// Create an anonymous block reference
AcDbBlockReference* pBlkRef = new AcDbBlockReference();
// Instead of creating a named block, we'll use an empty objectId to create an anonymous block
AcDbObjectId blkRecId = AcDbObjectId::kNull;
pBlkRef->setBlockTableRecord(blkRecId);
pBlkRef->setPosition(pEnt->position()); // Set insert point to entity's original position
pBlkRef->setTransform(oldToNew);
// Copy the entity into the anonymous block
AcDbIdMapping idMap;
es = pEnt->handOverTo(idMap, pBlkRef);
if (es != Acad::eOk)
{
delete pBlkRef;
acutPrintf("\nFailed to handover entity to anonymous block reference.");
continue;
}
// Erase the original entity and replace with the anonymous block reference
pEnt->erase();
pBlkRef->close(); // Close the block reference without explicitly opening it for write (since it was just created)
// No need to close pEnt here as it should have been erased already
}
}
// Clear the selection set
acdbClear采用ss(ss);
}
// Call the function
CreateAnonymousBlocksFromSelection();
页:
[1]