- UID
- 15750
- 积分
- 69
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-11-17
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- [font=courier new]
- // Ch4_1.cpp : Initialization functions
- // CH4_1.cpp
- // by Charles Mc Auley
- // "Programming AutoCAD 2000 with ObjectARX"
- //
- // This application demonstrates how to add
- // new layer table records to the layer table.
- // It also demonstrates how to use a layer table
- // iterator
- #include "StdAfx.h"
- #include "StdArx.h"
- #include "resource.h"
- //定义实例对象_hdllInstance
- HINSTANCE _hdllInstance =NULL ;
- /////函数原型说明
- /////
- // This command registers an ARX command.
- void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
- const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal = -1);
- // NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_MSG
- //初始化
- void InitApplication();
- //卸载arx程序
- void UnloadApplication();
- //}}AFX_ARX_MSG
- // NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_ADDIN_FUNCS
- //}}AFX_ARX_ADDIN_FUNCS
- /////////////////////////////////////////////////////////////////////////////
- // DLL Entry Point
- ///定义dll入口函数
- ////////////////////////////////////////////
- ///功能:dll入口函数
- ///参数:HINSTANCE hInstance, DWORD dwReason, LPVOID
- ///
- extern "C"//声明函数为外部函数
- BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
- {
- if (dwReason == DLL_PROCESS_ATTACH)
- {
- _hdllInstance = hInstance;
- }
- else if (dwReason == DLL_PROCESS_DETACH)
- {
- }
- return TRUE; // ok
- }
- ////////////////////////////////////////////
- ///功能:arx入口函数
- ///参数:AcRx::AppMsgCode msg, void* pkt
- ///
- extern "C" AcRx::AppRetCode
- acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
- {
- switch (msg) {
- case AcRx::kInitAppMsg:
- // Comment out the following line if your
- // application should be locked into memory
- acrxDynamicLinker->unlockApplication(pkt);
- acrxDynamicLinker->registerAppMDIAware(pkt);
- InitApplication();
- break;
- case AcRx::kUnloadAppMsg:
- UnloadApplication();
- break;
- }
- return AcRx::kRetOK;
- }
- // Init this application. Register your commands, reactors...
- //功能:初始化
- void InitApplication()
- {
- // NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_INIT
- AddCommand("CH4_APPS", "CNL", "CNL", ACRX_CMD_MODAL, cnl);
- //}}AFX_ARX_INIT
- // TODO: add your initialization functions
- acutPrintf("Enter "CNL" to create a new layer.\n");
- }
- // Unload this application. Unregister all objectsregistered in InitApplication.
- // 卸载arx程序
- void UnloadApplication()
- {
- // NOTE: DO NOT edit the following lines.
- //{{AFX_ARX_EXIT
- acedRegCmds->removeGroup("CH4_APPS");
- //}}AFX_ARX_EXIT
- acutPrintf("%s%s", "Goodbye\n", "Removing command group "CH4_APPS"\n");
- }
- // This functions registers an ARX command.
- // 注册命令
- void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
- const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal)
- {
- char cmdLocRes[65];
- // If idLocal is not -1, it's treated as an ID for
- // a string stored in the resources.
- if (idLocal != -1) {
- // Load strings from the string table and register the command.
- ::LoadString(_hdllInstance, idLocal, cmdLocRes, 64);
- acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLocRes, cmdFlags, cmdProc);
- } else
- // idLocal is -1, so the 'hard coded'
- // localized function name is used.
- acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLoc, cmdFlags, cmdProc);
- }
- ////////////////////////////////////////////////
- //功能:获取新层的linetype
- Adesk::Boolean getNewLyrLtId(AcDbObjectId& ltypeId)
- {
- //关键字
- char kw[20];
- //线型
- char linType[50];
- //线型指针
- char *pLtName;
- //图形数据库
- AcDbDatabase *pCurDb = NULL;
- //线型表
- AcDbLinetypeTable *pltTable;
- //线型表纪录
- AcDbLinetypeTableRecord *pLtTableRcd;
- //遍历
- AcDbLinetypeTableIterator *pLtIterator;
- int rc;//返回码
- //获取当前dwg文件的图形数据库
- pCurDb = acdbHostApplicationServices()->workingDatabase();
-
- acedInitGet(NULL, "Name List Continuous");
- //获取关键字
- rc = acedGetKword("\nLinetype - [Name/List/Continuous]<Continuous>: ", kw);
-
- switch(rc)
- {
- /*
- Return values for user-input functions Code Description
- RTNORM User entered a valid value
- RTERROR The function call failed
- RTCAN User entered ESC
- RTNONE User entered only ENTER
- RTREJ AutoCAD rejected the request as invalid
- RTKWORD User entered a keyword or arbitrary text
- */
- case RTCAN://取消命令
- acutPrintf("\nUser canceled.");
- return Adesk::kFalse;
- case RTERROR:
- acutPrintf("\nFailed in getNewLyrLtId() function. ");
- return Adesk::kFalse;
- break;
- case RTNONE://回车响应
- //获取线型表
- pCurDb->getLinetypeTable(pltTable, AcDb::kForRead);
- //在线型表中搜索"CONTINUOUS",返回ltypeId(AcDbObjectId )
- pltTable->getAt("CONTINUOUS", ltypeId);
- //关闭线型表
- pltTable->close();
- return Adesk::kTrue;
- break;
- case RTNORM:
- if(strcmp(kw, "Name") == 0)
- {
- rc = acedGetString(0, "\nEnter linetype string name: ", linType);
- switch(rc)
- {
- case RTCAN:
- case RTERROR:
- acutPrintf("\nError in retreiving linetype name.");
- return Adesk::kFalse;
- break;
- case RTNORM:
- if(linType[0] == '\0')
- {
- acutPrintf("\nNo layer name given.");
- return Adesk::kFalse;
- }
- // Check to see if the linetype exists
- //获取线型表
- pCurDb->getLinetypeTable(pltTable, AcDb::kForRead);
- if(pltTable->has(linType))
- {
- // Retreive the AcDbObjecId of the layer table record
- pltTable->getAt(linType, ltypeId);
- pltTable->close();
- return Adesk::kTrue;
- }
- acutPrintf("\nLinetype '%s' does not exist using CONTINUOUS. ",
- linType);
- pltTable->getAt("CONTINUOUS", ltypeId);
- pltTable->close();
- return Adesk::kTrue;
- break;
- }// switch
- }// if
- else if(strcmp(kw, "List") == 0)
- {
- // Here we will use a Linetype Table Iteraror
- // to list out all the available linetype
- // and then ask the user to enter a
- // valid name for one of the layers
- acutPrintf("\nThe following linetypes are available. ");
- pCurDb->getLinetypeTable(pltTable, AcDb::kForRead);
- pltTable->newIterator(pLtIterator);
- for(; !pLtIterator->done(); pLtIterator->step())
- {
-
- // get the current linetype record from the iterator,
- // opened for read
- pLtIterator->getRecord(pLtTableRcd, AcDb::kForRead);
- // get the linetype name from the linetype record
- // and store it in pLtName
- pLtTableRcd->getName(pLtName);
- // close the linetype record
- pLtTableRcd->close();
-
- acutPrintf("\nLinetype name: %s", pLtName);
- delete [] pLtName;
- }// for
- // Don't forget to delete the iterator
- // that's your responsibility
- delete pLtIterator;
- rc = acedGetString(0, "\n\nEnter linetype string name: ", linType);
- switch(rc)
- {
- case RTCAN:
- case RTERROR:
- acutPrintf("\nError in retreiving linetype name.");
- return Adesk::kFalse;
- break;
- case RTNORM:
- if(linType[0] == '\0')
- {
- acutPrintf("\nNo layer name given.");
- return Adesk::kFalse;
- }
- // Check to see if the linetype
- // exists
- pCurDb->getLinetypeTable(pltTable, AcDb::kForRead);
- if(pltTable->has(linType))
- {
- // Retreive the AcDbObjecId of the layer table record
- pltTable->getAt(linType, ltypeId);
- pltTable->close();
- return Adesk::kTrue;
- }
- acutPrintf("\nLinetype '%s' does not exist using CONTINUOUS. ",
- linType);
- pltTable->getAt("CONTINUOUS", ltypeId);
- pltTable->close();
- return Adesk::kTrue;
- break;
- }// switch
- }
- else if(strcmp(kw, "Continuous") == 0)
- {
- // Remember the CONTINUOUS linetype always
- // exists in a drawing file.
- pCurDb->getLinetypeTable(pltTable, AcDb::kForRead);
- pltTable->getAt("CONTINUOUS", ltypeId);
- pltTable->close();
- return Adesk::kTrue;
- }
- else
- {
- return Adesk::kFalse;
- }
- break;
- }// switch
- return Adesk::kFalse;
- }
- ///////////////////////////////////////////////////////////////
- ///功能:创建新层
- ///参数:char* lyrname, Adesk::UInt16 clr,AcDbObjectId ltypeId, Adesk::Boolean current
-
- void createNewLayer(char* lyrname, Adesk::UInt16 clr,
- AcDbObjectId ltypeId, Adesk::Boolean current)
- {
- // 首先判断层表中是否存在名称为lyrname的层,若是则设置该层颜色等,否则创建新层
- //生成层表对象指针
- AcDbLayerTable *pLyrTable;
- //生成层表纪录对象指针
- AcDbLayerTableRecord *pLyrTblRecord;
- ///生成实体ID
- AcDbObjectId recId;
- //颜色
- AcCmColor color;
- //设置颜色
- color.setColorIndex(clr); // set color to parameter clr
- //图形数据库指针
- AcDbDatabase *pCurDb = NULL;
- //获取当前图形数据库
- pCurDb = acdbHostApplicationServices()->workingDatabase();
- //获取当前数据库层表
- pCurDb->getLayerTable(pLyrTable, AcDb::kForRead);
- //检查层是否存在
- if(pLyrTable->has(lyrname))
- {
- //从获取层
- pLyrTable->getAt(lyrname, pLyrTblRecord, AcDb::kForWrite, Adesk::kFalse);
- //解冻
- pLyrTblRecord->setIsFrozen(Adesk::kFalse);
- //设置颜色
- pLyrTblRecord->setColor(color);
- //设置线形
- pLyrTblRecord->setLinetypeObjectId(ltypeId);
- }
- else
- {
- //创建新层(创建层纪录)
- pLyrTable->upgradeOpen();//
- pLyrTblRecord = new AcDbLayerTableRecord;
- //设置层名
- pLyrTblRecord->setName(lyrname);
- //设置颜色
- pLyrTblRecord->setColor(color);
- //设置线形
- pLyrTblRecord->setLinetypeObjectId(ltypeId);
- //添加层记录与层表
- pLyrTable->add(pLyrTblRecord);
- }
- // Get the layer Table ObjectId
- //获取层表记录id
- recId = pLyrTblRecord->objectId();
- //关闭层表记录,一定要关闭,否则导致autocad系统错误
- pLyrTblRecord->close();
- //关闭层表
- pLyrTable->close();
- // Set the layer current if current is equal to Adesk::kTrue
- // pCurDb is point to the current drawing database
- // The database AcDbDatabase has a number of query and edit functions for the header variables
- if(current)
- {
- //设置当前层
- pCurDb->setClayer(recId);
- }
- }
- [/font]
|
|