实现功能:在CAD中增加CHANGECOLOR命令,改变选中实体颜色(默认红),另外,新建一个图层(需要用命令执行的自己加
)
本人也是菜鸟一个,刚接触ObjectARX,如果有错误的地方请各位多多指正,同时,也希望此文档能帮到一些朋友
源代码:
cpp文件
// PmBuildReportSouce.cpp : Initialization functions
#include "StdAfx.h"
#include "StdArx.h"
#include "resource.h"
#include <afxdllx.h>
#include "StdAfx.h"
#include "StdArx.h"
#include "resource.h"
#include <afxdllx.h>
HINSTANCE _hdllInstance =NULL ;
// This command registers an ARX command.
void AddCommand(const TCHAR* cmdGroup, const TCHAR* cmdInt, const TCHAR* cmdLoc,
const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal = -1);
void AddCommand(const TCHAR* cmdGroup, const TCHAR* cmdInt, const TCHAR* cmdLoc,
const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal = -1);
void InitApplication();
void UnloadApplication();
void ChangeColor();//改变颜色,默认红
void CreateNewLayer();//创建新图层,默认红
void UnloadApplication();
void ChangeColor();//改变颜色,默认红
void CreateNewLayer();//创建新图层,默认红
//
// Define the sole extension module object.
AC_IMPLEMENT_EXTENSION_MODULE(PmAzObjEntDLL);
// Now you can use the CAcModuleResourceOverride class in
// your application to switch to the correct resource instance.
// Please see the ObjectARX Documentation for more details
// your application to switch to the correct resource instance.
// Please see the ObjectARX Documentation for more details
/
// DLL Entry Point
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
_hdllInstance = hInstance;
// Extension DLL one time initialization
PmAzObjEntDLL.AttachInstance(hInstance);
InitAcUiDLL();
} else if (dwReason == DLL_PROCESS_DETACH) {
// Terminate the library before destructors are called
PmAzObjEntDLL.DetachInstance();
// DLL Entry Point
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
_hdllInstance = hInstance;
// Extension DLL one time initialization
PmAzObjEntDLL.AttachInstance(hInstance);
InitAcUiDLL();
} else if (dwReason == DLL_PROCESS_DETACH) {
// Terminate the library before destructors are called
PmAzObjEntDLL.DetachInstance();
}
return TRUE; // ok
}
return TRUE; // ok
}
/
// ObjectARX EntryPoint
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;
}
// ObjectARX EntryPoint
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()
{
AddCommand(_T("PM"), _T("CHANGECOLOR"), _T("CHANGECOLOR"), ACRX_CMD_TRANSPARENT | ACRX_CMD_USEPICKSET, ChangeColor);
}
// commands, reactors...
void InitApplication()
{
AddCommand(_T("PM"), _T("CHANGECOLOR"), _T("CHANGECOLOR"), ACRX_CMD_TRANSPARENT | ACRX_CMD_USEPICKSET, ChangeColor);
}
// Unload this application. Unregister all objects
// registered in InitApplication.
void UnloadApplication()
{
LPWSTR lpwStr = PmMfc::Fun::PM_T2W(_T("PM"));
acedRegCmds->removeGroup(lpwStr);//删除命令组
PMDelete(lpwStr);
}
// registered in InitApplication.
void UnloadApplication()
{
LPWSTR lpwStr = PmMfc::Fun::PM_T2W(_T("PM"));
acedRegCmds->removeGroup(lpwStr);//删除命令组
PMDelete(lpwStr);
}
// This functions registers an ARX command.
// It can be used to read the localized command name
// from a string table stored in the resources.
void AddCommand(const TCHAR* cmdGroup, const TCHAR* cmdInt, const TCHAR* cmdLoc,
const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal)
{
TCHAR cmdLocRes[65];
LPWSTR lpwStr = PmMfc::Fun::PM_T2W(cmdGroup);//公司封装的字符串操作库
LPWSTR lpCmdInt = PmMfc::Fun::PM_T2W(cmdInt);
LPWSTR lpCmdLoc = PmMfc::Fun::PM_T2W(cmdLoc);
// It can be used to read the localized command name
// from a string table stored in the resources.
void AddCommand(const TCHAR* cmdGroup, const TCHAR* cmdInt, const TCHAR* cmdLoc,
const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal)
{
TCHAR cmdLocRes[65];
LPWSTR lpwStr = PmMfc::Fun::PM_T2W(cmdGroup);//公司封装的字符串操作库
LPWSTR lpCmdInt = PmMfc::Fun::PM_T2W(cmdInt);
LPWSTR lpCmdLoc = PmMfc::Fun::PM_T2W(cmdLoc);
// 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(lpwStr, lpCmdInt, lpCmdLoc, cmdFlags, cmdProc);
// 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(lpwStr, lpCmdInt, lpCmdLoc, cmdFlags, cmdProc);
} else
{
// idLocal is -1, so the 'hard coded'
// localized function name is used.
acedRegCmds->addCommand(lpwStr, lpCmdInt, lpCmdLoc, cmdFlags, cmdProc);
}
PMDelete(lpwStr);
PMDelete(lpCmdInt);
PMDelete(lpCmdLoc);
}
{
// idLocal is -1, so the 'hard coded'
// localized function name is used.
acedRegCmds->addCommand(lpwStr, lpCmdInt, lpCmdLoc, cmdFlags, cmdProc);
}
PMDelete(lpwStr);
PMDelete(lpCmdInt);
PMDelete(lpCmdLoc);
}
void ChangeColor()
{
CreateNewLayer();
//用户选择一个或多个实体
ads_name sset;
acutPrintf(_T("\n选择实体:"));
if (acedSSGet(NULL, NULL, NULL, NULL, sset) != RTNORM)
return ;
long length;
acedSSLength(sset, &length);
if (length == 1)
{
AcDbObjectIdArray objIds;
ads_name ent;
acedSSName(sset, 0, ent);
AcDbObjectId objId;
acdbGetObjectId(objId, ent);//得到objectId
AcDbEntity *pEnt;
acdbOpenObject(pEnt, objId, AcDb::kForWrite);//用写的方式打开实体对象
if (pEnt->isKindOf(AcDbCircle::desc()))
{
acutPrintf(_T("您选择的是圆"));
}
if (pEnt->isKindOf(AcDbLine::desc()))
{
acutPrintf(_T("您选择的是直线"));
}
if (pEnt->isKindOf(AcDbArc::desc()))
{
acutPrintf(_T("您选择的是圆弧"));
}
pEnt->setColorIndex(1);//设置颜色
objIds.append(pEnt->objectId());
pEnt->close();
{
CreateNewLayer();
//用户选择一个或多个实体
ads_name sset;
acutPrintf(_T("\n选择实体:"));
if (acedSSGet(NULL, NULL, NULL, NULL, sset) != RTNORM)
return ;
long length;
acedSSLength(sset, &length);
if (length == 1)
{
AcDbObjectIdArray objIds;
ads_name ent;
acedSSName(sset, 0, ent);
AcDbObjectId objId;
acdbGetObjectId(objId, ent);//得到objectId
AcDbEntity *pEnt;
acdbOpenObject(pEnt, objId, AcDb::kForWrite);//用写的方式打开实体对象
if (pEnt->isKindOf(AcDbCircle::desc()))
{
acutPrintf(_T("您选择的是圆"));
}
if (pEnt->isKindOf(AcDbLine::desc()))
{
acutPrintf(_T("您选择的是直线"));
}
if (pEnt->isKindOf(AcDbArc::desc()))
{
acutPrintf(_T("您选择的是圆弧"));
}
pEnt->setColorIndex(1);//设置颜色
objIds.append(pEnt->objectId());
pEnt->close();
}else
{
AcDbObjectIdArray objIds;
for (long i = 0; i < length; i++)
{
// 获得指定元素的ObjectId
ads_name ent;
acedSSName(sset, i, ent);
AcDbObjectId objId;
acdbGetObjectId(objId, ent);
// 获得指向当前元素的指针
AcDbEntity *pEnt;
acdbOpenObject(pEnt, objId, AcDb::kForWrite);
{
AcDbObjectIdArray objIds;
for (long i = 0; i < length; i++)
{
// 获得指定元素的ObjectId
ads_name ent;
acedSSName(sset, i, ent);
AcDbObjectId objId;
acdbGetObjectId(objId, ent);
// 获得指向当前元素的指针
AcDbEntity *pEnt;
acdbOpenObject(pEnt, objId, AcDb::kForWrite);
if (pEnt->isKindOf(AcDbCircle::desc()))
{
acutPrintf(_T("您选择了圆 "));
}
if (pEnt->isKindOf(AcDbLine::desc()))
{
acutPrintf(_T("您选择了直线 "));
}
if (pEnt->isKindOf(AcDbArc::desc()))
{
acutPrintf(_T("您选择了圆弧 "));
}
//颜色设置
//int oldColor = CPmAcEntity::GetNameColor();
pEnt->setColorIndex(1);
objIds.append(pEnt->objectId());
pEnt->close();
}
}
acedSSFree(sset);
{
acutPrintf(_T("您选择了圆 "));
}
if (pEnt->isKindOf(AcDbLine::desc()))
{
acutPrintf(_T("您选择了直线 "));
}
if (pEnt->isKindOf(AcDbArc::desc()))
{
acutPrintf(_T("您选择了圆弧 "));
}
//颜色设置
//int oldColor = CPmAcEntity::GetNameColor();
pEnt->setColorIndex(1);
objIds.append(pEnt->objectId());
pEnt->close();
}
}
acedSSFree(sset);
}
//创建一个新图层
void CreateNewLayer()
{
//打开图层表
AcDbLayerTable* pLayerTable;
acdbHostApplicationServices()->workingDatabase()
->getSymbolTable(pLayerTable,AcDb::kForWrite);
void CreateNewLayer()
{
//打开图层表
AcDbLayerTable* pLayerTable;
acdbHostApplicationServices()->workingDatabase()
->getSymbolTable(pLayerTable,AcDb::kForWrite);
//初始化图层表记录对象,并设定名称
AcDbLayerTableRecord* pLayerTableRecord = new AcDbLayerTableRecord();
AcDbLayerTableRecord* pLayerTableRecord = new AcDbLayerTableRecord();
pLayerTableRecord->setName(_T("CDJ_MYLAYER"));
//设置图层颜色
AcCmColor color;
color.setColorIndex(1);
pLayerTableRecord->setColor(color);
AcCmColor color;
color.setColorIndex(1);
pLayerTableRecord->setColor(color);
//将新建的图层记录添加到图层表中,图层表打开后记得关闭,不然下次操作不了(读完可以继续读但不能写,一个对象读最多只能128次;写完必须关闭后才能再一次写入)
pLayerTable->add(pLayerTableRecord);
pLayerTable->close();
pLayerTableRecord->close();
}
pLayerTable->add(pLayerTableRecord);
pLayerTable->close();
pLayerTableRecord->close();
}
代码已贴完:最后,给大家分享一份文档,对一些和我一样的ObjectARX菜鸟应该会起点作用