- 积分
- 252
- 明经币
- 个
- 注册时间
- 2005-2-23
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2005-3-11 10:55:00
|
显示全部楼层
//Entities.cpp //AutoCAD数据库初级操作实例——创建实钵、层、新组词典等, //在AutoCAD命令行中运行MakeNewEnts即可 //包含头文件 #include <string.h> #include <aced.h> #include <dbents.h> #include <dbsymtb.h> #include <dbgroup.h> #include <dbapserv.h>
//函数声明 AcDbObjectId createNewLine(); AcDbObjectId createNewCircle(); AcDbObjectId createNewLayer(); void createNewGroup(); Acad::ErrorStatus changeColor(AcDbObjectId,Adesk::UInt16);
//创建直线对象,并返回对象ID AcDbObjectId CreateNewLine() { //定义直线的起点、终点 AcGePoint3d startPoint(5.0,4.0,0.0); AcGePoint3d endPoint(15.0,10.0,0.0); //初始化直线对象实例 AcDbLine* pLine=new AcDbLine(startPoint,endPoint); //打开块表 AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,AcDb::kForRead); //打开模型空间块表段,获得当前块表记录,并关闭块表 AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite); pBlockTable->close(); //向当前块表记录中添加直线对象 AcDbObjectId lineId; pBlockTableRecord->appendAcDbEntity(lineId,pLine); //关闭块表记录及直线对象 pBlockTableRecord->close(); pLine->close(); //返回直线对象ID return lineId; }
//创建圆对象,并返回对象ID AcDbObjectId CreateNewCircle() { //定义圆心、法向向量 AcGePoint3d center(10.0,7.0,0.0); AcGeVector3d normal(0.0,0.0,1.0); //初始化圆对象实例 AcDbCircle *pCirc=new AcDbCircle(center,normal,4.5); //打开块表 AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,AcDb::kForRead); //打开模型空间块表,获得当前块表记录,并关闭块表 AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite); pBlockTable->close(); //向当前块表记录中添加圆对象 AcDbObjectId CircleId; pBlockTableRecord->appendAcDbEntity(CircleId,pCirc); //关闭块表记录及圆对象 pBlockTableRecord->close(); pCirc->close(); //返回圆对象ID return CircleId; }
//建立名为“MYNewLayer”的新层 AcDbObjectId createNewLayer() { //打开层表,打开方式为只写 AcDbLayerTable *pLayerTable; acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTable,AcDb::kForWrite); //初始化层表记录对象,并设定层表记录的名称“MYNewLayer” AcDbLayerTableRecord *pLayerTableRecord=new AcDbLayerTableRecord; //层的其它属性(例如颜色、线形等)都用缺省但, //如:颜色为white、线形为Continuous等 //将新建的层表记录添加到层表中,并将层表记录的ID //保存到PLayerId作为函数的返回值 AcDbObjectId pLayerId; pLayerTable->add(pLayerId,pLayerTableRecord); //关闭层表和层表记录对象 pLayerTable->close(); pLayerTableRecord->close(); return pLayerId; }
//建立新组词典条目,利用第三个参数可以改变组的层属性 void createNewGroup(AcDbObjectIdArray& objIds,char* pGroupName,AcDbObjectId pLayerId) { //初始化组对象实例,以参数pGroupName为组名 AcDbGroup *pGroup= new AcDbGroup(pGroupName); //向组对象中加入实体 for(int i=0;i<objIds.length();i++) pGroup->append(objIds); //打开组词典(存放于对象词典中) AcDbDictionary* pGroupDict; acdbHostApplicationServices()->workingDatabase()->getGroupDictionary(pGroupDict,AcDb::kForWrite); //将新建的组词典条的R入到组词典中 AcDbObjectId pGroupId; pGroupDict->setAt(pGroupName,pGroup,pGroupId); //将组pGroup的层属性修改为PLayerld所代表的层 pGroup->setLayer(pLayerId); //关闭组词典和组词典对象 pGroupDict->close(); pGroup->close(); } //修改实体对象颜色,并返回一个状态码 Acad::ErrorStatus changeColor(AcDbObjectId endId,Adesk::UInt16 newColor) { //定义一个实体对象,并利用参数endId打开这个实体对象 AcDbEntity *pEntity; acdbOpenObject(pEntity,endId,AcDb::kForWrite); //修改实体对象的颜色 pEntity->setColorIndex(newColor); //关闭实体对象 pEntity->close(); //返回Acad::eOK,对于有容错处理的程序,可能在函数当中就返回另外的状态码 return Acad::eOk; }
//命令MakeNewEnts的执行函数 void runIt() { //创建新层 AcDbObjectId newLayerId=createNewLayer(); //定义一个对象Id序列idArray AcDbObjectIdArray idArray; //创建一条直线和一个圆 //将新建的直线、圆对象加入idArray中 idArray.append(createNewLine()); idArray.append(createNewCircle()); //由于新建的两个对象均在0层,他们的缺省颜色为ByLayer //将直线对象的颜色该为红色(1) //将圆对象的颜色该为绿色(3) changeColor(idArray.first(),1); changeColor(idArray.last(),3); //将直线和圆对象放到组“MyNewGroup”中, //并将组的层属性改为“MyewGroup”层, //组中的两个实体对象的层属性也相应的改为“MyNewGroup”层 //创建新层的函数也可以写成带名称参数 createNewGroup(idArray,"MyNewGroup",newLayerId); }
//初始化函数 void initApp() { acedRegCmds->addCommand("ASDK_MAKE_ENTITIES", "ASDK_MakeNewEnts","MakeNewEnts",ACRX_CMD_MODAL,runIt); }
//卸载函数 void unloadApp() { acedRegCmds->removeGroup("ASDK_MAKE_ENTITIES"); }
//应用程序主函数 extern "C" AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* appId) { switch (msg) { case AcRx::kInitAppMsg: acrxDynamicLinker->unlockApplication(appId); acrxDynamicLinker->registerAppMDIAware(appId); //调用initApp()函数 initApp(); break; case AcRx::kUnloadAppMsg: //调用unloadApp()函数 unloadApp(); break; } return AcRx::kRetOK; } |
|