在CAD里可以加载,但是输入命令之后CAD就弹出窗口说致命错误,我是初学者,谢谢你们了 .cpp文件 //包含头文件 #include "stdafx.h" #include <string.h> #include <aced.h> #include <dbents.h> #include <dbsymtb.h> #include <dbgroup.h> #include <dbapserv.h> #include <dbid.h> #include <gepnt3d.h > #include <rxdlinkr.h > #include <rxregsvc.h> //函数声明 AcDbObjectId createNewLine(); AcDbObjectId createNewCricle(); AcDbObjectId createNewLayer(); void createNewGroup(); Acad::ErrorStatus changeColor(AcDbObjectId,Adesk::UInt16); //创建直线对象,并返回对象ID AcDbObjectId createNewLine() { //定义直线起点,终点 AcGePoint3d ptStart(5, 4, 0); AcGePoint3d ptEnd(15, 10, 0); AcDbLine *pLine = new AcDbLine(ptStart, ptEnd); // 获得指向块表的指针 AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pBlockTable, AcDb::kForRead); // 获得指向特定的块表记录(模型空间)的指针 AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite); // 将AcDbLine类的对象添加到块表记录中 AcDbObjectId lineId; pBlockTableRecord->appendAcDbEntity(lineId, pLine); // 关闭图形数据库的各种对象 pBlockTable->close(); pBlockTableRecord->close(); pLine->close(); return lineId; } AcDbObjectId createNewCricle() { AcGePoint3d center(10.0, 7.0, 0.0); AcGeVector3d normal(0.0, 0.0, 5.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); // 将AcDbLine类的对象添加到块表记录中 AcDbObjectId circleId; pBlockTableRecord->appendAcDbEntity(circleId, pCirc); // 关闭图形数据库的各种对象 pBlockTable->close(); pBlockTableRecord->close(); pCirc->close(); return circleId; } AcDbObjectId createNewLayer() { AcDbLayerTable *pLayerTable; acdbHostApplicationServices()->workingDatabase() ->getSymbolTable(pLayerTable, AcDb::kForWrite); // 获得指向特定的块表记录(模型空间)的指针 AcDbLayerTableRecord *pLayerTableRecord; pLayerTableRecord->setName("MYNewLayer"); //层表记录的其他属性(例如颜色,线形等)若未设置都取缺省值 //如:颜色为white,线形为Continous等 //将新建的层表记录添加到层表中 //并将表层记录的ID保存到pLayerId作为函数的返回值 AcDbObjectId pLayerId; pLayerTable->add(pLayerId,pLayerTableRecord); //关闭层表和层表记录对象 pLayerTable->close(); pLayerTableRecord->close(); //返回新建层表记录对象的ID 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); //将新建的组词典条目加入到组词典中 AcDbObjectId pGroupId; pGroupDict->setAt(pGroupName,pGroup,pGroupId); //将组pGroup的层属性修改为pLayerId所代表的层 pGroup->setLayer(pGroupId); pGroupDict->close(); pGroup->close(); } //修改实体对象颜色,并返回一个状态码 Acad::ErrorStatus changeColor(AcDbObjectId endId,Adesk::UInt16 newColor) { //定义一个实体对象,并利用参数entId打开这个实体对象 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(createNewCricle()); //由于新建的两个对象均在0层,他们的缺省颜色值为ByLayer //将直线对象的颜色改为红色(1) //将圆对象的颜色改为绿色(3) changeColor(idArray.first(),1); changeColor(idArray.last(),3); //将直线和圆对象放到组"MyNewGroup"中, //并将组的层属性改为"MyNewGroup"层, //组中的两个实体对象的层属性也相应的改为"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(); break; case AcRx::kUnloadAppMsg: unloadApp(); } return AcRx::kRetOK; } .def文件 LIBRARY line EXPORTS acrxEntryPoint PRIVATE acrxGetApiVersion PRIVATE
|