ObjectARX简单功能实现

实现功能:在CAD中增加CHANGECOLOR命令,改变选中实体颜色(默认红),另外,新建一个图层(需要用命令执行的自己加微笑

本人也是菜鸟一个,刚接触ObjectARX,如果有错误的地方请各位多多指正,同时,也希望此文档能帮到一些朋友

源代码:

cpp文件


// PmBuildReportSouce.cpp : Initialization functions
#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 InitApplication();
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

/
// 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
}



/
// 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);
}

// Unload this application. Unregister all objects
// 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);

 // 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);

 } 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);
}

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();

 }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);

   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);

}

//创建一个新图层
void CreateNewLayer()
{
 //打开图层表
 AcDbLayerTable* pLayerTable;
 acdbHostApplicationServices()->workingDatabase()
  ->getSymbolTable(pLayerTable,AcDb::kForWrite);

 //初始化图层表记录对象,并设定名称
 AcDbLayerTableRecord* pLayerTableRecord = new AcDbLayerTableRecord();

 pLayerTableRecord->setName(_T("CDJ_MYLAYER"));

 //设置图层颜色
 AcCmColor color;
 color.setColorIndex(1);
 pLayerTableRecord->setColor(color);

 //将新建的图层记录添加到图层表中,图层表打开后记得关闭,不然下次操作不了(读完可以继续读但不能写,一个对象读最多只能128次;写完必须关闭后才能再一次写入)
 pLayerTable->add(pLayerTableRecord);
 pLayerTable->close();
 pLayerTableRecord->close();
}

代码已贴完:最后,给大家分享一份文档,对一些和我一样的ObjectARX菜鸟应该会起点作用



ObjectARX 是 AutoCAD 的编程接口,可以使用 C++ 编程语言来扩展 AutoCAD 的功能。要实现对称函数,可以使用 ObjectARX 提供的实体对象类 AcDbEntity 和 AcDbMirrorEntityPE 类。 首先,创建一个派生自 AcDbEntity 的实体类,例如 MyEntity。然后在 MyEntity 类中重载 AcDbEntity 类的 mirror() 函数,实现对称的操作。mirror() 函数的定义如下: ``` virtual Acad::ErrorStatus mirror(const AcGePlane& plane, AcDbObject*& pMirrorObj) override; ``` 在 mirror() 函数中,可以使用 AcDbMirrorEntityPE 类的成员函数 mirror() 来实现对称操作。mirror() 函数的定义如下: ``` virtual Acad::ErrorStatus mirror(AcDbEntity* pEnt, const AcGePlane& plane) override; ``` 其中,pEnt 是需要对称的实体对象指针,plane 是对称的平面。mirror() 函数将对称后的实体对象保存在 pMirrorObj 中,并返回 Acad::eOk 表示操作成功。 下面是一个简单的示例代码: ``` class MyEntity : public AcDbEntity { public: ACDB_DECLARE_MEMBERS(MyEntity); virtual Acad::ErrorStatus mirror(const AcGePlane& plane, AcDbObject*& pMirrorObj) override { AcDbMirrorEntityPE* pMirrorPE = AcDbMirrorEntityPE::cast(this); if (pMirrorPE == nullptr) return Acad::eNotImplementedYet; AcDbEntity* pMirrorEnt = nullptr; Acad::ErrorStatus es = pMirrorPE->mirror(this, plane, pMirrorEnt); if (es != Acad::eOk) return es; pMirrorObj = pMirrorEnt; return Acad::eOk; } }; ACDB_REGISTER_OBJECT_ENTRY_AUTO(MyEntity) ``` 在上面的示例中,MyEntity 类派生自 AcDbEntity,重载了 mirror() 函数。在 mirror() 函数中,首先使用 AcDbMirrorEntityPE::cast() 函数获取 AcDbMirrorEntityPE 类的指针。如果获取失败,说明当前实体对象不支持对称操作,返回 Acad::eNotImplementedYet。否则,调用 pMirrorPE->mirror() 函数实现对称操作,将对称后的实体对象保存在 pMirrorEnt 中,并将 pMirrorEnt 的指针保存在 pMirrorObj 中,最后返回 Acad::eOk 表示操作成功。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值