找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[每日一码] (7)在ARX里模拟INSERT命令实现代码替代acedCommand调用的解决方案

[复制链接]

1

主题

0

回帖

33

积分

管理员

积分
33
发表于 2024-3-14 21:05:49 | 显示全部楼层 |阅读模式
  1. //----- Read the external DWG file
  2. AcDbDatabase *pDwg =new AcDbDatabase (Adesk::kFalse) ;
  3. pDwg->readDwgFile (采用T("c:\\drawing1.dwg")) ;
  4. //----- Put it into a block table record of the current database
  5. AcDbObjectId id ;
  6. Acad::ErrorStatus es = acdbHostApplicationServices()->workingDatabase()->insert(id, 采用T("drawing1"), pDwg, Adesk::kFalse) ;
  7. if ( es != Acad::eOk )
  8. acutPrintf (采用T("\nError inserting a block.")) ;
  9. delete pDwg ;
  10. As soon as you have a block table record in place you can add an AcDbBlockReference with it. The block reference is the INSERT'ed block table record instance that is displayed in the model or paper space. The below example shows how to create a block reference with attributes from a block table record. You can call it following above code like this:
  11. AcGePoint3d point3D(0,0,0);
  12. addBlockWithAttributes(id,point3D);
  13. //NOTE: You will have to update the code to properly set the attribute values. The code below sets the value of each attribute to "XXX".
  14. static void addBlockWithAttributes (AcDbObjectId blockId, AcGePoint3d basePoint) {
  15. //----- Step 1: Allocate a block reference object
  16. AcDbBlockReference *pBlkRef =new AcDbBlockReference ;
  17. //----- Step 2: Set up the block reference to the newly
  18. //----- created block definition
  19. pBlkRef->setBlockTableRecord (blockId) ;
  20. //---- Give it the current UCS normal.
  21. pBlkRef->setPosition (basePoint) ;
  22. pBlkRef->setRotation (0.0) ;
  23. pBlkRef->setNormal (AcGeVector3d (0.0, 0.0, 1.0)) ;
  24. //----- Step 3: Open current database's Model Space
  25. //----- blockTableRecord
  26. AcDbBlockTable *pBlockTable ;
  27. acdbHostApplicationServices()->workingDatabase()->getBlockTable (pBlockTable, AcDb::kForRead) ;
  28. AcDbBlockTableRecord *pBlockTableRecord ;
  29. pBlockTable->getAt (ACDB采用MODEL采用SPACE, pBlockTableRecord,
  30.   AcDb::kForWrite) ;
  31. pBlockTable->close () ;
  32. //----- Append the block reference to the model space
  33. //----- block table record
  34. AcDbObjectId newEntId ;
  35. pBlockTableRecord->appendAcDbEntity (newEntId, pBlkRef) ;
  36. pBlockTableRecord->close () ;
  37. //----- Step 4: Open the block definition for read
  38. AcDbBlockTableRecord *pBlockDef ;
  39. acdbOpenObject (pBlockDef, blockId, AcDb::kForRead) ;
  40. AcDbBlockTableRecordIterator *pIterator ;
  41. pBlockDef->newIterator (pIterator) ;
  42. AcDbEntity *pEnt ;
  43. AcDbAttributeDefinition *pAttdef ;
  44. for ( pIterator->start () ; !pIterator->done() ; pIterator->step () ) {
  45.   //----- Get the next entity
  46.   pIterator->getEntity (pEnt, AcDb::kForRead) ;
  47.   //----- Make sure the entity is an attribute definition
  48.   //----- and not a constant
  49.   pAttdef =AcDbAttributeDefinition::cast (pEnt) ;
  50.   if ( pAttdef != NULL && !pAttdef->isConstant () ) {
  51.    //----- We have a non-constant attribute definition
  52.    //----- so build an attribute entity
  53.    AcDbAttribute *pAtt =new AcDbAttribute ;
  54.    pAtt->setPropertiesFrom (pAttdef) ;
  55.    pAtt->setInvisible (pAttdef->isInvisible ()) ;
  56.    //----- Translate attribute by block reference.
  57.    //----- To be really correct, entire block
  58.    //----- reference transform should be applied here.
  59.    basePoint =pAttdef->position () ;
  60.    basePoint +=pBlkRef->position ().asVector () ;
  61.    pAtt->setPosition (basePoint) ;
  62.    pAtt->setHeight (pAttdef->height ()) ;
  63.    pAtt->setRotation (pAttdef->rotation ()) ;
  64.    pAtt->setTag (采用T("Tag")) ;
  65.    pAtt->setFieldLength (25) ;
  66.    TCHAR *pStr =pAttdef->tag () ;
  67.    pAtt->setTag (pStr) ;
  68.    delete pStr ;
  69.    pAtt->setFieldLength (pAttdef->fieldLength ()) ;
  70.    //----- Database Column value should be displayed
  71.    //----- INSERT would prompt for this...
  72.    pAtt->setTextString (采用T("XXX")) ;
  73.    //----- Set Alignments
  74.    pAtt->setHorizontalMode( pAttdef->horizontalMode ()) ;
  75.    pAtt->setVerticalMode (pAttdef->verticalMode ()) ;
  76.    pAtt->setAlignmentPoint (
  77.     pAttdef->alignmentPoint ()
  78.     + pBlkRef->position ().asVector ()
  79.     ) ;
  80.    //----- Insert the attribute in the DWG
  81.    AcDbObjectId attId ;
  82.    pBlkRef->appendAttribute (attId, pAtt) ;
  83.    pAtt->close () ;
  84.   }     
  85.   pEnt->close () ; //----- Use pEnt... pAttdef might be NULL
  86. }
  87. delete pIterator ;
  88. pBlockDef->close () ;
  89. pBlkRef->close () ;
  90. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-29 21:28 , Processed in 0.145764 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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