找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[每日一码] 使用智能指针创建块和块参照

[复制链接]

1

主题

0

回帖

43

积分

管理员

积分
43
发表于 2024-3-14 20:38:08 | 显示全部楼层 |阅读模式
  1. // 随便创建一些实体
  2. static void SquareOfLines(AcDbVoidPtrArray &ents, double size)
  3. {
  4.     AcGePoint3dArray pts;
  5.     pts.append(AcGePoint3d(-size, -size, 0));
  6.     pts.append(AcGePoint3d(size, -size, 0));
  7.     pts.append(AcGePoint3d(size, size, 0));
  8.     pts.append(AcGePoint3d(-size, size, 0));
  9.     size采用t max = pts.length()-1;
  10.     for (size采用t i = 0; i <= max; i++) {
  11.         int j = (i == max ? 0 : i + 1);
  12.         ents.append(new AcDbLine(pts[i],pts[j]));
  13.     }
  14. }
  15. /* 创建块:
  16. *   1. 提示用户输入要创建的块的名字
  17. *   2. 打开块表(BlockTable),new一个新的块表记录(BlockTableRecord),设置这个BlockTableRecord的属性(主要是名字)
  18. *   3. 把上面的BlockTableRecord添加到BlockTable中,同时记录下ID(ObjectID)
  19. *   4. 选择实体(或随便创建出一些实体)添加到BlockTableRecord中
  20. *   5. 关闭BlockTableRecord 和 BlockTable。
  21. *   这样就完成了块的创建,随后就可以在CAD里插入这个块的块参照了。
  22. */
  23. void create采用block()
  24. {
  25.     TCHAR blockName[133];    // buffer for our block name
  26.     AcDbVoidPtrArray ents;   // buffer for holding entities
  27.     AcDbObjectId newBlockId;
  28.     //get an instance of the database
  29.     AcDbDatabase *pDatabase = acdbHostApplicationServices()->workingDatabase();
  30.     //get the name of our new block
  31.     if(acedGetString(1,采用T("\n为块创建名字: "),blockName) != Acad::eNormal){
  32.         acutPrintf(采用T("\nError: 获取块名字失败 "));
  33.         return;
  34.     }
  35.     //validate the block name
  36.     if(acdbSymUtil()->validateSymbolName(blockName,false) != Acad::eOk){
  37.         acutPrintf(采用T("\nError: 符号 %s 不符合CAD的规范"),blockName);
  38.         return;
  39.     }
  40.     //Open the Block Table using a smart Pointer
  41.     AcDbBlockTablePointer pBlockTable(pDatabase->blockTableId(),AcDb::kForWrite);
  42.     if(pBlockTable.openStatus() != eOk){
  43.         acutPrintf(采用T("\nError: 打开块表失败"));
  44.         return;
  45.     }
  46.     //test if our block name already exists
  47.     if(pBlockTable->has(blockName)){
  48.         acutPrintf(采用T("\nError: 块名字 (%s) 已经存在"),blockName);
  49.         return;
  50.     }
  51.     //create our block using a smart pointer
  52.     AcDbBlockTableRecordPointer pNewBlockTableRecord;
  53.     pNewBlockTableRecord.create();
  54.     pNewBlockTableRecord->setName(blockName);
  55.     //add the new block to the Block Table
  56.     if(pBlockTable->add(newBlockId,pNewBlockTableRecord)!= Acad::eOk){
  57.         acutPrintf(采用T("\nError: 添加块 (%s) 失败"), blockName);
  58.         return;
  59.     }
  60.     //make a few lines
  61.     SquareOfLines(ents,5);
  62.     //add the lines to our block
  63.     for(size采用t i = 0 ; i < ents.length();i++)
  64.     {
  65.         AcDbEntity *pTmp = (AcDbEntity*)ents[i];
  66.         if(pNewBlockTableRecord->appendAcDbEntity(pTmp) != eOk)
  67.             delete pTmp;
  68.         else
  69.             pTmp->close();
  70.     }
  71.     //create a reference of our block
  72.     AcDbObjectPointer<AcDbBlockReference> pNewBlockReference;
  73.     pNewBlockReference.create();
  74.     pNewBlockReference->setPosition(AcGePoint3d::kOrigin);
  75.     pNewBlockReference->setBlockTableRecord(newBlockId);
  76.     //open up the btr of the current space
  77.     AcDbBlockTableRecordPointer pSpace(pDatabase->currentSpaceId(),AcDb::kForWrite);
  78.     if(pSpace.openStatus() != eOk)
  79.     {
  80.         acutPrintf(采用T("\nError: 打开当前空间失败"));
  81.         return;
  82.     }
  83.     //add the insert to the current space;
  84.     if(pSpace->appendAcDbEntity(pNewBlockReference) != eOk)
  85.     {
  86.         acutPrintf(采用T("\nError: 添加块参照失败"));
  87.         return;
  88.     }
  89.     //smart pointer do their mojo here.
  90.     //智能指针负责关闭数据库的游标、释放资源等
  91. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-1-10 21:45 , Processed in 0.119624 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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