天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 70|回复: 0

通过acedSSGet函数获取用户选择集,然后将这些选择集中的图形转换成一个新块,并在同

[复制链接]
  • TA的每日心情
    开心
    2024-5-16 08:01
  • 签到天数: 82 天

    [LV.6]常住居民II

    1607

    主题

    204

    回帖

    214748万

    积分

    管理员

    积分
    2147483647
    发表于 2024-3-16 10:02:42 | 显示全部楼层 |阅读模式
    这段代码首先从用户选择集中获取实体,然后创建一个新的块并将这些实体复制到新块中。最后,将原来的实体替换为指向新块的新插入参照,保持其原始位置不变。注意这只是一个基础示例,实际操作中还需要考虑更多细节,如错误处理、事务管理等。
    1. #include "acedinpt.h" // For acedSSGet
    2. #include "dbmain.h" // For AcDbEntity and related classes
    3. #include "dbdict.h" // For AcDbDictionary
    4. #include "dbidmap.h" // For AcDbIdMapping
    5. #include "dbtrans.h" // For AcDbTransaction

    6. // Function to create a block from the selected set of entities and insert it back at the same location
    7. void CreateBlockFromSelection()
    8. {
    9.     ads_name ss;
    10.     struct resbuf rb;

    11.     // Get the user selection
    12.     acedSSGet(NULL, NULL, NULL, NULL, ss);

    13.     // Check if there's a valid selection
    14.     if (ss != RTNORM)
    15.         return;

    16.     AcDbVoidPtrArray entities;
    17.     Acad::ErrorStatus es = acdbGsMarker(ss, entities);
    18.     if (es != Acad::eOk)
    19.     {
    20.         acutPrintf("\nFailed to get entities from selection.");
    21.         return;
    22.     }

    23.     if (entities.isEmpty())
    24.     {
    25.         acutPrintf("\nNo entities were selected.");
    26.         return;
    27.     }

    28.     // Start a transaction
    29.     AcDbTransaction trans;
    30.     es = trans.start();
    31.     if (es != Acad::eOk)
    32.     {
    33.         acutPrintf("\nFailed to start transaction.");
    34.         return;
    35.     }

    36.     try
    37.     {
    38.         // Open the current database in write mode
    39.         AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();

    40.         // Define a unique block name
    41.         char blockName[64];
    42.         _itoa(time(NULL), blockName, 10); // Use timestamp as a simple way to generate a unique name

    43.         // Create the block definition
    44.         AcDbBlockTableRecord* pNewBlock = new AcDbBlockTableRecord();
    45.         es = pNewBlock->setName(blockName);
    46.         if (es != Acad::eOk)
    47.         {
    48.             delete pNewBlock;
    49.             throw std::runtime_error("Failed to set block name.");
    50.         }

    51.         // Append entities to the block table record
    52.         AcDbBlockTableRecord::Iterator iter(*pNewBlock);
    53.         for (int i = 0; i < entities.length(); ++i)
    54.         {
    55.             AcDbEntity* pEnt;
    56.             es = acdbOpenObject(pEnt, (AcDbObjectId)entities[i], AcDb::kForWrite);
    57.             if (es == Acad::eOk)
    58.             {
    59.                 pEnt->upgradeOpen();
    60.                 es = pNewBlock->appendAcDbEntity(iter, pEnt);
    61.                 if (es != Acad::eOk)
    62.                 {
    63.                     pEnt->close();
    64.                     throw std::runtime_error("Failed to append entity to block.");
    65.                 }
    66.                 pEnt->close();
    67.             }
    68.         }

    69.         // Add the block definition to the block table dictionary
    70.         AcDbDictionary* pBlockDict;
    71.         es = pDb->getSymbolTable(pBlockDict, AcDb::kForWrite);
    72.         if (es == Acad::eOk)
    73.         {
    74.             es = pBlockDict->setAt(blockName, pNewBlock, AcDb::kTrue);
    75.             if (es != Acad::eOk)
    76.             {
    77.                 throw std::runtime_error("Failed to add block to dictionary.");
    78.             }
    79.             pBlockDict->close();
    80.         }

    81.         // Insert the block back to the original locations
    82.         AcDbIdMapping idMap;
    83.         AcDbBlockReference* pBlkRef;
    84.         for (int i = 0; i < entities.length(); ++i)
    85.         {
    86.             AcDbEntity* pEnt;
    87.             es = acdbOpenObject(pEnt, (AcDbObjectId)entities[i], AcDb::kForWrite);
    88.             if (es == Acad::eOk)
    89.             {
    90.                 AcGeMatrix3d oldToNew;
    91.                 oldToNew.setToIdentity();
    92.                 pBlkRef = new AcDbBlockReference();
    93.                 pBlkRef->setBlockTableRecord(pNewBlock->objectId());
    94.                 pBlkRef->setPosition(pEnt->position()); // Set insert point to entity's original position
    95.                 pBlkRef->setTransform(oldToNew);

    96.                 es = pEnt->handOverTo(idMap, pBlkRef);
    97.                 if (es != Acad::eOk)
    98.                 {
    99.                     delete pBlkRef;
    100.                     throw std::runtime_error("Failed to handover entity to block reference.");
    101.                 }
    102.                 pBlkRef->close();
    103.                 pEnt->erase();
    104.                 pEnt->close();
    105.             }
    106.         }

    107.         // Commit the transaction
    108.         trans.commit();
    109.     }
    110.     catch (const std::exception& ex)
    111.     {
    112.         acutPrintf("\nError: %s", ex.what());
    113.         trans.abort();
    114.     }

    115.     // Clear the selection set
    116.     acdbClear_ss(ss);
    117. }

    118. // Call the function
    119. CreateBlockFromSelection();
    复制代码

     

     

     

     

    通过acedSSGet函数获取用户选择集,然后将这些选择集中的图形转换成一个新块,并在同
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网_中国空间膜结构协会

    GMT+8, 2024-5-24 06:18 , Processed in 0.058469 second(s), 22 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

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