找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 660|回复: 0

[图块] Copying an existing block entity with new block name and definition

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-8 13:07:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

问题:
I want to copy an existing block reference, however, the new block reference,
while identical to the original block reference, will have a different name. How
is this achieved?

解答:
1. Select the block reference (AcDbBlockReference) you want to copy.

2. Get the block reference's definition, which is an AcDbBlockTableRecord, by
using the blockTableRecord() function.

3. Iterate through the entities that make up the block definition and copy these
entities into a  new block table record (AcDbBlockTableRecord).

4. Using the new block table record, create an instance (AcdbBlockReference) of
the block definition and place that block reference into model space. (The
sample code that follows demonstrates how this is achieved. In addition, there
is an attached sample ObjectARX project that defines an AutoCAD XCOPY command.)

5. In the TEST.DWG drawing that is included with the application, select a block
reference named BLKTEST. The XCOPY command will create an identical block
reference as the BLKTEST block but with a different name.

6. After executing the XCOPY command, move the new block reference as it will be
placed on top of the original block reference. Note the call to DeepCloneObjects
in the user-defined cloneBlockRecord function. The ObjectARX DeepCloneObjects
will copy the entities and the attribute definitions that make up the block
definition.

For more information on DeepCloneObjects, refer to the ObjectARX documentation.
For more information on DeepCloning, refer to the API technical paper
"DeepCloning in AutoCAD 2000" document on the ADN web site.

  1. void asdkgtxxxcopy()
  2. {
  3.             // TODO: Implement the command
  4.             AcDbEntity *pEnt = NULL;
  5.             AcDbBlockReference *pRef = NULL;
  6.             AcDbObjectId objId, blockId, idNewBlock, blockRefLayId, newRefId;
  7.             AcGePoint3d basepoint;
  8.             AcGeScale3d scalefact;
  9.             AcGeVector3d vector;
  10.             double rotationang;
  11.             ads_name ename;
  12.             ads_point pt;


  13.             if( acedEntSel ("\nSelect the Block Reference", ename, pt) != RTNORM)
  14.             {
  15.                         return;
  16.             }

  17.             acdbGetObjectId (objId, ename);

  18.             acdbOpenObject (pEnt, objId, AcDb::kForRead);

  19.             // Is pEnt really a block reference entity?

  20.             pRef = AcDbBlockReference::cast(pEnt);
  21.             if(pRef != NULL)
  22.             {
  23.                         blockId = pRef->blockTableRecord();

  24.                         idNewBlock = cloneBlockRecord(blockId, "MYBLOCKCLONED");

  25.                         basepoint = pRef->position();
  26.                         scalefact = pRef->scaleFactors();
  27.                         vector = pRef->normal();
  28.                         rotationang = pRef->rotation();
  29.                         blockRefLayId = pRef->layerId();        
  30.                         pRef->close();
  31.                         newRefId = createInsertForBlock(idNewBlock,
  32.                                                                         basepoint,
  33.                                                                         scalefact,
  34.                                                                         rotationang,
  35.                                                                         vector,
  36.                                                                         objId,
  37.                                                                         blockRefLayId);
  38.             } // if

  39.             // pRef and pEnt point to the same object
  40.             pEnt->close();
  41. }


  42. AcDbObjectId cloneBlockRecord(AcDbObjectId from, char *to)
  43. {
  44.             AcDbBlockTable *pBlockTable;
  45.             AcDbBlockTableRecord *pBlockTableRecord;
  46.             AcGePoint3d tempOrigin;
  47.             AcDbObjectIdArray arrayId;
  48.             AcDbBlockTableRecordIterator *pIter;
  49.             AcDbObjectId objId, tableId, blockTargetId;
  50.             AcDbDatabase *pDB;
  51.             AcDbIdMapping idMap;

  52.             char buffer[21];


  53.             pDB = acdbHostApplicationServices()->workingDatabase();

  54.             acdbOpenAcDbObject ((AcDbObject *&)pBlockTableRecord, from,
  55. AcDb::kForRead);
  56.             tempOrigin = pBlockTableRecord->origin();     
  57.             pBlockTableRecord->newIterator(pIter);
  58.             while (!pIter->done())
  59.             {
  60.                         pIter->getEntityId(objId);
  61.                         arrayId.append(objId);
  62.                         pIter->step();
  63.             }
  64.             delete pIter ;

  65.             tableId = pBlockTableRecord->ownerId();
  66.             pBlockTableRecord->close();


  67.             pDB->getBlockTable(pBlockTable, AcDb::kForRead);

  68.             strcpy(buffer, to);
  69.             if(pBlockTable->has(buffer))
  70.             {
  71.                         for (int i = 0; i < 255; i++)
  72.                         {
  73.                                     sprintf(buffer, "%s%d", to, i);
  74.                                     if(!pBlockTable->has(buffer))
  75.                                     {
  76.                                                 break;
  77.                                     }
  78.                         }
  79.             }

  80.             pBlockTable->upgradeOpen ();
  81.             pBlockTableRecord = new AcDbBlockTableRecord;
  82.             pBlockTableRecord->setName(buffer);
  83.             pBlockTableRecord->setOrigin(tempOrigin);

  84.             pBlockTable->add(blockTargetId, pBlockTableRecord);
  85.             pBlockTableRecord->close();
  86.             pBlockTable->close();

  87.             pDB->deepCloneObjects(arrayId, blockTargetId, idMap);

  88.             return blockTargetId;
  89. }

  90. AcDbObjectId createInsertForBlock (AcDbObjectId blockId, AcGePoint3d basePoint,
  91.                                                    AcGeScale3d scaleFactor, double rotationAng,
  92.                                                    AcGeVector3d vector, AcDbObjectId ownerRefId,
  93.                                                    AcDbObjectId blockRefLayId)
  94. {
  95.             AcDbDatabase *pDB;
  96.             AcDbBlockReference *pBlkRef;
  97.             AcDbBlockTable *pBlockTable;
  98.             AcDbBlockTableRecord *pBlockTableRecord;
  99.             AcDbObjectId newEntId;
  100.             AcDbBlockTableRecord *pBlockDef;
  101.             AcDbBlockTableRecordIterator *pIterator;
  102.             AcDbBlockReference *pOriginalRef;
  103.             AcDbObjectIterator *pIterRef;
  104.             AcDbEntity *pEnt;
  105.             AcDbAttributeDefinition *pAttdef;

  106.             pDB = acdbHostApplicationServices()->workingDatabase();

  107.             pBlkRef = new AcDbBlockReference;   
  108.             pBlkRef->setBlockTableRecord(blockId);
  109.             pBlkRef->setNormal(vector);
  110.             pBlkRef->setPosition(basePoint);
  111.             pBlkRef->setScaleFactors(scaleFactor);
  112.             pBlkRef->setRotation(rotationAng);
  113.             pBlkRef->setLayer(blockRefLayId, Adesk::kFalse);


  114.             pDB->getBlockTable(pBlockTable, AcDb::kForRead);


  115.             pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
  116. AcDb::kForWrite);
  117.             pBlockTable->close();

  118.             pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef);
  119.             pBlockTableRecord->close();

  120.             acdbOpenObject(pBlockDef, blockId, AcDb::kForRead);

  121.             pBlockDef->newIterator(pIterator);   

  122.             //____iterating original block ref for attributes
  123.             acdbOpenObject(pOriginalRef,ownerRefId,AcDb::kForRead);
  124.             pIterRef =pOriginalRef->attributeIterator ();   
  125.             pOriginalRef->close();


  126.             for(pIterator->start(); !pIterator->done(); pIterator->step())
  127.             {
  128.                         pIterator->getEntity(pEnt, AcDb::kForRead);
  129.                         pAttdef = AcDbAttributeDefinition::cast (pEnt);
  130.                         if (pAttdef != NULL && !pAttdef->isConstant())
  131.                         {         
  132.                                     //____iteration of original block ref continues...
  133.                                     AcDbObject *pObj;
  134.                                     AcDbObjectId ida;
  135.                                     AcDbAttribute *pAtt;
  136.                                     AcDbObjectId attId;

  137.                                     ida = pIterRef->objectId();
  138.                                     acdbOpenObject(pObj, ida, AcDb::kForRead);
  139.                                     pAtt = AcDbAttribute::cast(pObj->clone());
  140.                                     pObj->close();

  141.                                     pAtt->setTextString("XXX");
  142.                                     pBlkRef->appendAttribute(attId, pAtt);
  143.                                     pAtt->close();
  144.                                     pIterRef->step();
  145.                         }

  146.                         pEnt->close();
  147.             }

  148.             delete pIterator;         
  149.             delete pIterRef;
  150.             pBlockDef->close();
  151.             pBlkRef->close();

  152.             return newEntId;
  153. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-29 06:25 , Processed in 0.303147 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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