1 static void lzxARX_AcDbDatabaseRefEdit(void) 2 { 3 AcDbObjectId transId; 4 AcDbDatabase* pDb; 5 ACHAR* fname; 6 struct resbuf* rb; 7 8 //提示让用户选择一个图形文件 9 rb = acutNewRb(RTSTR); 10 int stat = acedGetFileD(L"Pick a drawing",NULL,L"dwg",0,rb); 11 12 //如果用户选择有误,则提示用户,退出命令 13 if ((stat != RTNORM) || (rb == NULL)) 14 { 15 acutPrintf(L"\nYou must pick a drawing file."); 16 return; 17 } 18 19 //将获得的文件名转换成字符串 20 fname = (TCHAR*)acad_malloc(_tcslen(rb->resval.rstring)+1); 21 _tcscpy(fname, rb->resval.rstring); 22 acutRelRb(rb); 23 24 //打开前面选中的图形文件 25 pDb = new AcDbDatabase(Adesk::kFalse); 26 27 //如果打开失败或用户已经打开了些图形 28 if (pDb->readDwgFile(fname)!= Acad::eOk) 29 { 30 acutPrintf(L"\nSorry, that draing is probably already open."); 31 return; 32 } 33 34 //获得这个图形文件数据库的块表 35 AcDbBlockTable* pBlockTable; 36 pDb->getSymbolTable(pBlockTable,AcDb::kForRead); 37 38 //获得模型空间块表记录 39 AcDbBlockTableRecord* pOtherMsBtr; 40 pBlockTable->getAt(ACDB_MODEL_SPACE,pOtherMsBtr,AcDb::kForRead); 41 pBlockTable->close(); 42 43 //创建一个遍历器 44 AcDbBlockTableRecordIterator* pIter; 45 pOtherMsBtr->newIterator(pIter); 46 47 //创建一个对象ID数组 48 AcDbObjectIdArray objIdArray; 49 50 //对模型空间块表记录进行遍历 51 for (pIter->start();!pIter->done();pIter->step()) 52 { 53 //获得实体 54 AcDbEntity* pEntity; 55 pIter->getEntity(pEntity,AcDb::kForRead); 56 57 //对实体类型进行断送 58 //如果实体是圆,则将实体的对象ID添加到数组中 59 if (pEntity->isKindOf(AcDbCircle::desc())) 60 { 61 objIdArray.append(pEntity->objectId()); 62 } 63 64 //关闭实体 65 pEntity->close(); 66 } 67 delete pIter; 68 pOtherMsBtr->close(); 69 70 //如果图形中没有圆对象,则释放文件名,返回 71 if (objIdArray.isEmpty()) 72 { 73 acad_free(fname); 74 acutPrintf(L"\nYou must pick a drawing file that contains Circles."); 75 return; 76 } 77 78 //现在获得当前数据库和当前数据库的块表 79 AcDbBlockTable* pThisBlockTable; 80 acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pThisBlockTable,AcDb::kForRead); 81 82 //获得当前数据库的模型空间块表记录 83 AcDbBlockTableRecord* pThisMsBtr; 84 pThisBlockTable->getAt(ACDB_MODEL_SPACE,pThisMsBtr,AcDb::kForWrite); 85 pThisBlockTable->close(); 86 87 //获得当前数据库模型空间记录的对象ID 88 AcDbObjectId id = pThisMsBtr->objectId(); 89 pThisMsBtr->close(); 90 91 //创建长事务,这将从外部数据库中获得外部对象 92 AcDbIdMapping errorMap; 93 acapLongTransactionManagerPtr()->checkOut(transId,objIdArray,id,errorMap); 94 95 //下面修改这些对象的颜色和半径 96 int colorIndex; 97 double radius; 98 acedGetInt(L"\nEnter color number to change entityes to:",&colorIndex); 99 acedGetReal(L"\nEnter circle radius:",&radius); 100 101 //如果半径小于或等于0,则返回 102 if (radius<=0) 103 { 104 acutPrintf(L"\nError:Radius can't less than 0!"); 105 return; 106 } 107 108 AcDbObject* pObj; 109 110 //打开对象成功 111 if(acdbOpenObject(pObj,transId,AcDb::kForRead) == Acad::eOk) 112 { 113 //获得事务的指针 114 AcDbLongTransaction* pLongTrans = AcDbLongTransaction::cast(pObj); 115 if (pLongTrans != NULL) 116 { 117 //获得工作集遍历器 118 AcDbLongTransWorkSetIterator* pWorkSetIter; 119 pLongTrans->newWorkSetIterator(pWorkSetIter); 120 121 //对工作集中实体进行遍历,改变实体颜色和半径 122 for (pWorkSetIter->start();!pWorkSetIter->done();pWorkSetIter->step()) 123 { 124 AcDbEntity* pEntity; 125 acdbOpenAcDbEntity(pEntity,pWorkSetIter->objectId(),AcDb::kForWrite); 126 127 //改变实体颜色和半径然后关闭对象 128 pEntity->setColorIndex(colorIndex); 129 ((AcDbCircle*)pEntity)->setRadius(radius); 130 pEntity->close(); 131 } 132 delete pWorkSetIter; 133 } 134 pObj->close(); 135 } 136 137 //暂停使用户能看见这种变化 138 ACHAR str[50]; 139 acedGetString(0,L"\nSee the new colors and radius. Press retrn to back",str); 140 141 //将实体放回原数据库 142 acapLongTransactionManagerPtr()->checkIn(transId, errorMap); 143 144 //保存原数据库 145 pDb->saveAs(fname); 146 147 //关闭数据库然后删除些对象 148 delete pDb; 149 pDb = NULL; 150 151 //释放文件名 152 acad_free(fname); 153 }