CAD ObjectArx开发之可停靠窗体创建
环境:VS2010 + CAD2014
创建思路
1、创建ObjectARX项目,支持MFC
2、使用ObjectARX向导创建基类为CAdUiDockContrilBar的窗体
3、在acrxEntryPoint.cpp中编写代码
(1)、在cpp文件中定义全局窗体指针
(2)、在On_kInitAppMsg初始化函数中添加窗台构建代码
(3)、在On_kUnloadAppMsg卸载函数中添加窗体资源释放代码
编译时可能出现的问题:
(1)、构建时pArxAppMenuBar->Create(acedGetAcadFrame(),_T("DockBar"));权限问题,可直接在窗体对象中修改protected为public
(2)、修改Create (CWnd *pParent, LPTSTR lpszTitle)的参数短字符为宽字符,LPTSTR -> LPCTSTR
(3)、资源标识为识别问题:enum { IDD = IDD_ARXAPPMENUBAR};可在该头文件中添加资源头文件:#include "Resource.h"
详细步骤
1、创建ObjectARX项目,支持MFC
2、使用ObjectARX向导创建基类为CAdUiDockContrilBar的窗体
3、在acrxEntryPoint.cpp中编写代码
完整代码段
// (C) Copyright 2002-2012 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//-----------------------------------------------------------------------------
//----- acrxEntryPoint.cpp
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "resource.h"
#include "ArxAppMain.h"
//-----------------------------------------------------------------------------
#define szRDS _RXST("")
//菜单栏
ArxAppMenuBar * pArxAppMenuBar = NULL;
//-----------------------------------------------------------------------------
//----- ObjectARX EntryPoint
class CFW_ArxAppApp : public AcRxArxApp {
public:
CFW_ArxAppApp () : AcRxArxApp () {}
virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
// TODO: Load dependencies here
// You *must* call On_kInitAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ;
// TODO: Add your initialization code here
acutPrintf(_T("初始化菜单栏"));
//防止资源冲突
CAcModuleResourceOverride resOverride;
if(pArxAppMenuBar == NULL){
pArxAppMenuBar = new ArxAppMenuBar();
pArxAppMenuBar->Create(acedGetAcadFrame(),_T("DockBar"));
pArxAppMenuBar->SetWindowTextW(_T("ArxDockBar"));
pArxAppMenuBar->EnableDocking(CBRS_ALIGN_ANY);
}
acedGetAcadFrame()->FloatControlBar(pArxAppMenuBar,CPoint(100,100),CBRS_ALIGN_TOP);
acedGetAcadFrame()->ShowControlBar(pArxAppMenuBar,TRUE,TRUE);
return (retCode) ;
}
virtual AcRx::AppRetCode On_kUnloadAppMsg (void *pkt) {
// TODO: Add your code here
// You *must* call On_kUnloadAppMsg here
AcRx::AppRetCode retCode =AcRxArxApp::On_kUnloadAppMsg (pkt) ;
// TODO: Unload dependencies here
acutPrintf(_T("释放菜单栏"));
if(pArxAppMenuBar != NULL){
pArxAppMenuBar->DestroyWindow();
delete pArxAppMenuBar;
pArxAppMenuBar = NULL;
}
return (retCode) ;
}
virtual void RegisterServerComponents () {
}
// The ACED_ARXCOMMAND_ENTRY_AUTO macro can be applied to any static member
// function of the CFW_ArxAppApp class.
// The function should take no arguments and return nothing.
//
// NOTE: ACED_ARXCOMMAND_ENTRY_AUTO has overloads where you can provide resourceid and
// have arguments to define context and command mechanism.
// ACED_ARXCOMMAND_ENTRY_AUTO(classname, group, globCmd, locCmd, cmdFlags, UIContext)
// ACED_ARXCOMMAND_ENTRYBYID_AUTO(classname, group, globCmd, locCmdId, cmdFlags, UIContext)
// only differs that it creates a localized name using a string in the resource file
// locCmdId - resource ID for localized command
// Modal Command with localized name
// ACED_ARXCOMMAND_ENTRY_AUTO(CFW_ArxAppApp, MyGroup, MyCommand, MyCommandLocal, ACRX_CMD_MODAL)
static void MyGroupMyCommand () {
// Put your command code here
acutPrintf(_T("Hello Arx"));
}
// Modal Command with pickfirst selection
// ACED_ARXCOMMAND_ENTRY_AUTO(CFW_ArxAppApp, MyGroup, MyPickFirst, MyPickFirstLocal, ACRX_CMD_MODAL | ACRX_CMD_USEPICKSET)
static void MyGroupMyPickFirst () {
ads_name result ;
int iRet =acedSSGet (ACRX_T("_I"), NULL, NULL, NULL, result) ;
if ( iRet == RTNORM )
{
// There are selected entities
// Put your command using pickfirst set code here
}
else
{
// There are no selected entities
// Put your command code here
}
}
// Application Session Command with localized name
// ACED_ARXCOMMAND_ENTRY_AUTO(CFW_ArxAppApp, MyGroup, MySessionCmd, MySessionCmdLocal, ACRX_CMD_MODAL | ACRX_CMD_SESSION)
static void MyGroupMySessionCmd () {
// Put your command code here
acutPrintf(_T("Hello Arx"));
}
// The ACED_ADSFUNCTION_ENTRY_AUTO / ACED_ADSCOMMAND_ENTRY_AUTO macros can be applied to any static member
// function of the CFW_ArxAppApp class.
// The function may or may not take arguments and have to return RTNORM, RTERROR, RTCAN, RTFAIL, RTREJ to AutoCAD, but use
// acedRetNil, acedRetT, acedRetVoid, acedRetInt, acedRetReal, acedRetStr, acedRetPoint, acedRetName, acedRetList, acedRetVal to return
// a value to the Lisp interpreter.
//
// NOTE: ACED_ADSFUNCTION_ENTRY_AUTO / ACED_ADSCOMMAND_ENTRY_AUTO has overloads where you can provide resourceid.
//- ACED_ADSFUNCTION_ENTRY_AUTO(classname, name, regFunc) - this example
//- ACED_ADSSYMBOL_ENTRYBYID_AUTO(classname, name, nameId, regFunc) - only differs that it creates a localized name using a string in the resource file
//- ACED_ADSCOMMAND_ENTRY_AUTO(classname, name, regFunc) - a Lisp command (prefix C:)
//- ACED_ADSCOMMAND_ENTRYBYID_AUTO(classname, name, nameId, regFunc) - only differs that it creates a localized name using a string in the resource file
// Lisp Function is similar to ARX Command but it creates a lisp
// callable function. Many return types are supported not just string
// or integer.
// ACED_ADSFUNCTION_ENTRY_AUTO(CFW_ArxAppApp, MyLispFunction, false)
static int ads_MyLispFunction () {
//struct resbuf *args =acedGetArgs () ;
// Put your command code here
//acutRelRb (args) ;
// Return a value to the AutoCAD Lisp Interpreter
// acedRetNil, acedRetT, acedRetVoid, acedRetInt, acedRetReal, acedRetStr, acedRetPoint, acedRetName, acedRetList, acedRetVal
return (RTNORM) ;
}
} ;
//-----------------------------------------------------------------------------
IMPLEMENT_ARX_ENTRYPOINT(CFW_ArxAppApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CFW_ArxAppApp, MyGroup, MyCommand, MyCommandLocal, ACRX_CMD_MODAL, NULL)
ACED_ARXCOMMAND_ENTRY_AUTO(CFW_ArxAppApp, MyGroup, MyPickFirst, MyPickFirstLocal, ACRX_CMD_MODAL | ACRX_CMD_USEPICKSET, NULL)
ACED_ARXCOMMAND_ENTRY_AUTO(CFW_ArxAppApp, MyGroup, MySessionCmd, MySessionCmdLocal, ACRX_CMD_MODAL | ACRX_CMD_SESSION, NULL)
ACED_ADSSYMBOL_ENTRY_AUTO(CFW_ArxAppApp, MyLispFunction, false)