|
static void lzxARX_AcDbDatabaseRefEdit(void)
{
AcDbObjectId transId;
AcDbDatabase* pDb;
ACHAR* fname;
struct resbuf* rb;
//提示让用户选择一个图形文件
rb = acutNewRb(RTSTR);
int stat = acedGetFileD(L"Pick a drawing",NULL,L"dwg",0,rb);
//如果用户选择有误,则提示用户,退出命令
if ((stat != RTNORM) || (rb == NULL))
{
acutPrintf(L"\nYou must pick a drawing file.");
return;
}
//将获得的文件名转换成字符串
fname = (TCHAR*)acad_malloc(_tcslen(rb->resval.rstring)+1);
_tcscpy(fname, rb->resval.rstring);
acutRelRb(rb);
//打开前面选中的图形文件
pDb = new AcDbDatabase(Adesk::kFalse);
//如果打开失败或用户已经打开了些图形
if (pDb->readDwgFile(fname)!= Acad::eOk)
{
acutPrintf(L"\nSorry, that draing is probably already open.");
return;
}
//获得这个图形文件数据库的块表
AcDbBlockTable* pBlockTable;
pDb->getSymbolTable(pBlockTable,AcDb::kForRead);
//获得模型空间块表记录
AcDbBlockTableRecord* pOtherMsBtr;
pBlockTable->getAt(ACDB_MODEL_SPACE,pOtherMsBtr,AcDb::kForRead);
pBlockTable->close();
//创建一个遍历器
AcDbBlockTableRecordIterator* pIter;
pOtherMsBtr->newIterator(pIter);
//创建一个对象ID数组
AcDbObjectIdArray objIdArray;
//对模型空间块表记录进行遍历
for (pIter->start();!pIter->done();pIter->step())
{
//获得实体
AcDbEntity* pEntity;
pIter->getEntity(pEntity,AcDb::kForRead);
//对实体类型进行断送
//如果实体是圆,则将实体的对象ID添加到数组中
if (pEntity->isKindOf(AcDbCircle::desc()))
{
objIdArray.append(pEntity->objectId());
}
//关闭实体
pEntity->close();
}
delete pIter;
pOtherMsBtr->close();
//如果图形中没有圆对象,则释放文件名,返回
if (objIdArray.isEmpty())
{
acad_free(fname);
acutPrintf(L"\nYou must pick a drawing file that contains Circles.");
return;
}
//现在获得当前数据库和当前数据库的块表
AcDbBlockTable* pThisBlockTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pThisBlockTable,AcDb::kForRead);
//获得当前数据库的模型空间块表记录
AcDbBlockTableRecord* pThisMsBtr;
pThisBlockTable->getAt(ACDB_MODEL_SPACE,pThisMsBtr,AcDb::kForWrite);
pThisBlockTable->close();
//获得当前数据库模型空间记录的对象ID
AcDbObjectId id = pThisMsBtr->objectId();
pThisMsBtr->close();
//创建长事务,这将从外部数据库中获得外部对象
AcDbIdMapping errorMap;
acapLongTransactionManagerPtr()->checkOut(transId,objIdArray,id,errorMap);
//下面修改这些对象的颜色和半径
int colorIndex;
double radius;
acedGetInt(L"\nEnter color number to change entityes to:",&colorIndex);
acedGetReal(L"\nEnter circle radius:",&radius);
//如果半径小于或等于0,则返回
if (radius<=0)
{
acutPrintf(L"\nError:Radius can't less than 0!");
return;
}
AcDbObject* pObj;
//打开对象成功
if(acdbOpenObject(pObj,transId,AcDb::kForRead) == Acad::eOk)
{
//获得事务的指针
AcDbLongTransaction* pLongTrans = AcDbLongTransaction::cast(pObj);
if (pLongTrans != NULL)
{
//获得工作集遍历器
AcDbLongTransWorkSetIterator* pWorkSetIter;
pLongTrans->newWorkSetIterator(pWorkSetIter);
//对工作集中实体进行遍历,改变实体颜色和半径
for (pWorkSetIter->start();!pWorkSetIter->done();pWorkSetIter->step())
{
AcDbEntity* pEntity;
acdbOpenAcDbEntity(pEntity,pWorkSetIter->objectId(),AcDb::kForWrite);
//改变实体颜色和半径然后关闭对象
pEntity->setColorIndex(colorIndex);
((AcDbCircle*)pEntity)->setRadius(radius);
pEntity->close();
}
delete pWorkSetIter;
}
pObj->close();
}
//暂停使用户能看见这种变化
ACHAR str[50];
acedGetString(0,L"\nSee the new colors and radius. Press retrn to back",str);
//将实体放回原数据库
acapLongTransactionManagerPtr()->checkIn(transId, errorMap);
//保存原数据库
pDb->saveAs(fname);
//关闭数据库然后删除些对象
delete pDb;
pDb = NULL;
//释放文件名
acad_free(fname);
} |
|