找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[每日一码] 图块位置不变的情况下重设块的插入点

[复制链接]

0

主题

0

回帖

26

积分

管理员

积分
26
发表于 2024-2-26 10:28:46 | 显示全部楼层 |阅读模式
  1. void ResetAllInstances(AcDbBlockTableRecord * pBlockTableRecord, AcGeVector3d &translation)
  2. {
  3. // Note that this function does not implement any error checking.
  4. // We are assuming that pBlockTableREcord was opened for write by the calling function
  5. // Iterate through all block references of this block table record.
  6. AcDbBlockReferenceIdIterator * pIterator;
  7. pBlockTableRecord->newBlockReferenceIdIterator(pIterator);
  8. for (pIterator->start();!pIterator->done();pIterator->step())
  9. {
  10.   AcDbBlockReference * pBlockRef;
  11.   pIterator->getBlockReference(pBlockRef, AcDb::kForWrite,true);
  12.   // Transform the translation vector from block coordinates to world coordinates.
  13.   AcGeVector3d realTranslation = (pBlockRef->blockTransform())*translation;
  14.   // Translate the block reference to counter the effect of the origin change.
  15.   pBlockRef->setPosition(pBlockRef->position()+realTranslation);
  16.   pBlockRef->close();
  17. } // Next block reference
  18. delete pIterator;
  19. }
  20. // This is command 'SGP'
  21. void testFunc()
  22. {
  23. // Note that error checking is minimal.
  24. ads采用name ename ;
  25. ads采用point pt ;
  26. // Ask the user to select an object
  27. if( acedEntSel ("\nSelect the Block Reference", ename, pt) != RTNORM )
  28.   return ;
  29. // Open it
  30. AcDbObjectId objId;
  31. acdbGetObjectId (objId, ename) ;
  32. AcDbEntity * pEnt;
  33. acdbOpenObject (pEnt, objId, AcDb::kForRead);
  34. // Check its a block reference
  35. AcDbBlockReference *pRef = AcDbBlockReference::cast(pEnt);
  36. if (pRef == NULL)
  37. {
  38.   pEnt->close();
  39.   return;
  40. }
  41. // Get the block reference's associated block table record.
  42. AcDbObjectId blockId =pRef->blockTableRecord ();
  43. pRef->close();
  44. AcDbBlockTableRecord *pBlockTableRecord;
  45. acdbOpenObject (pBlockTableRecord, blockId, AcDb::kForWrite);
  46. // Store the BTR's current origin (we'll need it later).
  47. AcGePoint3d origin = pBlockTableRecord->origin();
  48. // And set its new origin to (0, 0, 0)
  49. pBlockTableRecord->setOrigin(AcGePoint3d(0, 0, 0));
  50. // Create a translation vector for the change in displacement required to offset the origin change (in the block's coordinate system)
  51. AcGeVector3d translation;
  52. translation.set(-origin[X], -origin[Y], -origin[Z]);
  53. // Now process all instances of this block reference to compensate for the change in the block origin
  54. ResetAllInstances(pBlockTableRecord, translation);
  55. pBlockTableRecord->close();
  56. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-28 13:45 , Processed in 0.120863 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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