编写一ObjectARX程序,包含以下三个功能(命令):
命令Ellipse1,使之能够自动对图中的椭圆进行如下操作:面积小于等于π的椭圆,将其颜色变为红色;面积大于π的椭圆,将其颜色变为蓝色。
命令Ellipse2,使之能够让用户以逐个选取方式对椭圆进行上述操作。
命令Ellipse3,使之能够让用户以多选方式对椭圆进行上述操作。
Command.cpp 如下:
// // ObjectARX defined commands #include "StdAfx.h" #include "StdArx.h" const double PI=3.15159265;//定义常量π // This is command 'ELLIPSE1' void BaiyangEllipse1() { // TODO: Implement the command double area; AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable, AcDb::kForRead); AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord, AcDb::kForWrite); AcDbBlockTableRecordIterator *pIter; pBlockTableRecord->newIterator(pIter); AcDbEntity *pEnt; for (;!pIter->done();pIter->step())//遍历所有实体 { pIter->getEntity(pEnt,AcDb::kForWrite);//获取实体指针 if (strcmp(pEnt->isA()->name(),"AcDbEllipse")==0)//遇到椭圆就进行操作 { AcDbEllipse *pEllipse; pEllipse=(AcDbEllipse *)pEnt;//将pEnt强制转换 pEllipse->getArea(area); if (area<=PI)//面积小于π设为红色 { pEllipse->setColorIndex(1); } else//大于π设为蓝色色 { pEllipse->setColorIndex(5); } pEllipse->close();//关闭对象指针 } pEnt->close();//关闭实体 } delete pIter;//删除指针 pBlockTableRecord->close(); pBlockTable->close();//关闭所有指针 } // This is command 'ELLIPSE2' void BaiyangEllipse2()//以逐个选取方式对椭圆进行操作 { // TODO: Implement the command ads_name Ent;//记录实体名 ads_point pt;//数组 AcDbEntity *pEnt;//实体指针 AcDbObjectId EntId;//实体ID double area; while(acedEntSel("\nSelect An Entity:",Ent,pt)==RTNORM)//循环选取单个实体,直到Cancle { acdbGetObjectId(EntId,Ent);//获取实体ID acdbOpenObject(pEnt,EntId,AcDb::kForWrite);//读取实体指针 if (strcmp(pEnt->isA()->name(),"AcDbEllipse")==0)//如果实体名为椭圆,进入操作 { AcDbEllipse *pEllipse; pEllipse=(AcDbEllipse *)pEnt;//强制转换为AcDbEllipse类型的指针 pEllipse->getArea(area);//获取椭圆面积 if (area<PI) { pEllipse->setColorIndex(1); } else { pEllipse->setColorIndex(5); } pEllipse->close(); } else { acutPrintf("\nThe Selected Entity Is Not A Ellipse!");//错误提示 } pEnt->close();//关闭实体指针 } } // This is command 'ELLIPSE3' void BaiyangEllipse3()//以多选方式对椭圆进行操作 { // TODO: Implement the command AcDbEntity *pEnt;//实体指针 ads_name Ent,Ssname;//记录名 AcDbObjectId EntId; long Nss,i;//与acdeSSLength对变量的要求匹配 double area; acedSSGet(NULL,NULL,NULL,NULL,Ssname);//选择多个实体 acedSSLength(Ssname,&Nss);//得到实体个数 for (i=0;i<Nss;i++)//遍历已经选取的实体 { acedSSName(Ssname,i,Ent);//获取实体名 acdbGetObjectId(EntId,Ent);//读取实体ID acdbOpenObject(pEnt,EntId,AcDb::kForWrite);//打开实体 if (strcmp(pEnt->isA()->name(),"AcDbEllipse")==0)//如果是椭圆就进行操作 { AcDbEllipse *pEllipse; pEllipse=(AcDbEllipse *)pEnt; pEllipse->getArea(area); if (area<=PI)//小于π设为红色 { pEllipse->setColorIndex(1); } else//大于π设为蓝色 { pEllipse->setColorIndex(5); } pEllipse->close(); } else//错误提示 { acutPrintf("\nThe Selected Entity Is Not A Ellipse!"); } pEnt->close();//关闭指针 } }