明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3837|回复: 3

如何在AutoCAD中加载自己的工具条

[复制链接]
发表于 2005-1-17 17:39:00 | 显示全部楼层 |阅读模式
我在编程序时,要用到很多的非模态对话框,可是一不小心关掉就再也出不来了,因此我想在用AutoCAD加载程序时也加载一个工具条,只要点击一下工具条就可以出现我所需要的对话框,可是我编的程序编译,链接都可以,就是在AutoCAD中看不到我要的工具条,下面是我的做法,希望高手指点: 1、用ARX向导建立一个USE MFC的程序框架。 2、在资源中创建一个工具条,ID为IDR_TOOLBAR_DEMO; 3、用“ObjectARX定义命令”对话框注册命令,在Internation栏填入CreateToolBar,然后点击编辑代码,并在XXXCommands.cpp中顶端加入#include"resource.h" 代码如下: void jerryCreateToolBar()
{
if(!m_MyToolBar.CreateEx(NULL,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS
|CBRS_FLYBY|CBRS_SIZE_DYNAMIC)||!m_MyToolBar.LoadToolBar(IDR_TOOLBAR_DEMO))
{
TRACE0("Failed to create toolbar\n");
return ;
}
m_MyToolBar.EnableDocking(CBRS_ALIGN_ANY);
} 请问各位高手,我错在了哪里?我的功能应该如何实现?
发表于 2005-1-24 10:22:00 | 显示全部楼层
我也想知道!
 楼主| 发表于 2005-1-24 11:24:00 | 显示全部楼层
发表于 2005-1-24 17:00:00 | 显示全部楼层

仔细看一下,稍微改一点你就可以用了.这些就在arx自带的帮助里!这要比你自己写好的多,我试过,直接拷过来就可以用!而且步骤也很清楚.

This method accesses AutoCAD ActiveX Automation through pure COM techniques. This method requires more coding, but does not rely on MFC. The sample program uses the COM ActiveX Automation interfaces to add a new shortcut menu to the AutoCAD menu bar. The following procedures demonstrate the necessary steps:

To set up a new ObjectARX project

  1. Start Visual C++ and create a new Win32 Dynamic-Link Library project called AsdkComDocSamp.
  1. Add the appropriate values to the project settings to make the project build as an ObjectARX program. This program needs to link with the following libraries:
acad.lib
rxapi.lib
acrx15.lib
acutil15.lib
acedapi.lib
  1. Add a new definitions (DEF) file named AsdkComDocSamp.def to the project.
  2. Open the new DEF file, and add the following lines to the EXPORTS section:
acrxEntryPoint								PRIVATE
acrxGetApiVersion					PRIVATE
  1. Add a new source (CPP) file named AsdkComDocSamp.cpp to the project.
  2. Open the new CPP file, and add the following code to make the program ObjectARX compatible:
#include <rxregsvc.h>
#include <aced.h>
// Used to add/remove the menu with the same command.
//
static bool bIsMenuLoaded = false;
static void initApp()
{
    acedRegCmds->addCommand(
        "ASDK_PLAIN_COM", 
        "AsdkComMenu",
        "ComMenu", 
        ACRX_CMD_MODAL, 
								addMenuThroughCom);
}
static void unloadApp()
{
    acedRegCmds->removeGroup("ASDK_PLAIN_COM");
}
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(); 
            break;
        default:
            break;
    }
    return AcRx::kRetOK;
}
  1. Stub in your new AutoCAD command handler function by adding the following code to the AsdkComDocSamp.cpp source file:
void addMenuThroughCom()
{
}

The next step is to decide which interfaces are necessary to access the AutoCAD menu bar. In this case, IAcadApplication, IAcadMenuBar, IAcadMenuGroups, IAcadMenuGroup, IAcadPopupMenus, IAcadPopupMenu, and IAcadPopupMenuItem are required. To get the definitions of these interfaces, use the AutoCAD type library (acad.tlb) as shown in the next procedure.

To import AutoCAD ActiveX interfaces

  1. Add the following line to the top of the AsdkComDocSamp.cpp file, making sure to use the path where AutoCAD is installed on your system:
import "c:\\acad\\acad.tlb" no_implementation	\
				raw_interfaces_only named_guids
  1. Add the following declarations to the addMenuThroughCom() function:
AutoCAD::IAcadApplication *pAcad;
AutoCAD::IAcadMenuBar *pMenuBar;
AutoCAD::IAcadMenuGroups *pMenuGroups;
AutoCAD::IAcadMenuGroup *pMenuGroup;
AutoCAD::IAcadPopupMenus *pPopUpMenus;
AutoCAD::IAcadPopupMenu *pPopUpMenu;
AutoCAD::IAcadPopupMenuItem *pPopUpMenuItem;

Now that your application imports the proper interfaces, you can use them to implement AutoCAD functionality. The more direct COM approach to Automation uses QueryInterface to access the IUnknown of AutoCAD and its Automation components. The next procedure shows how to accomplish this. All code examples in this procedure should be added to your addMenuThroughCOM() function in the sequence presented.

To implement AutoCAD ActiveX Automation calls

  1. In the AsdkComDocSamp.cpp file, add the following code to the empty addMenuThroughCOM() function to get AutoCAD's IUnknown:
HRESULT hr = NOERROR;
CLSID clsid;
LPUNKNOWN pUnk = NULL;
LPDISPATCH pAcadDisp = NULL; 
hr = ::CLSIDFromProgID(L"AutoCAD.Application",
        &clsid);
if (FAILED(hr))
				return;
if(::GetActiveObject(clsid, NULL, &pUnk) != S_OK)
				return;
hr = pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &pAcadDisp);
pUnk->Release();
if (FAILED(hr))
				return;
  1. Use IUnknown to get the AutoCAD application object. Also, make sure AutoCAD is visible. This is shown in the following code:
hr = pAcadDisp->QueryInterface(AutoCAD::IID_IAcadApplication,
								(void**)&pAcad);
pAcadDisp->Release();
if (FAILED(hr))
				return;
pAcad->put_Visible(true);
  1. From the AutoCAD application interface, get the menu bar and menu groups collections:
pAcad->get_MenuBar(&pMenuBar);
pAcad->get_MenuGroups(&pMenuGroups);
pAcad->Release();
  1. Determine how many menus are current on the menu bar:
long numberOfMenus;
pMenuBar->get_Count(&numberOfMenus);
pMenuBar->Release();
  1. Get the first menu from the menu groups collection. This will normally be ACAD, but could be something else:
VARIANT index;
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
pMenuGroups->Item(index, &pMenuGroup);
pMenuGroups->Release();
  1. Get the shortcut menus collection from the first menu group:
pMenuGroup->get_Menus(&pPopUpMenus);
pMenuGroup->Release();
  1. Depending on whether the menu is already created, either construct a new shortcut menu or remove the previously created one. The following code completes the example:
WCHAR wstrMenuName[256];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, 
		 "AsdkComAccess", -1, wstrMenuName, 256); 
if (!bIsMenuLoaded) {
			 pPopUpMenus->Add(wstrMenuName, &pPopUpMenu);
			 if (pPopUpMenu != NULL) {
								pPopUpMenu->put_Name(wstrMenuName);
							 WCHAR wstrMenuItemName[256];
							 MultiByteToWideChar(CP_ACP, 0,"&Add A ComCircle",
											 -1, wstrMenuItemName, 256); 
							 WCHAR wstrMenuItemMacro[256];
							 MultiByteToWideChar(CP_ACP, 0, "AsdkComCircle ",
											 -1, wstrMenuItemMacro, 256); 
							 VariantInit(&index);
							 V_VT(&index) = VT_I4;
							 V_I4(&index) = 0;
							 pPopUpMenu->AddMenuItem(index, wstrMenuItemName,
											 wstrMenuItemMacro, &pPopUpMenuItem);
								VariantInit(&index);
								V_VT(&index) = VT_I4;
								V_I4(&index) = 1;
							 pPopUpMenu->AddSeparator(index, 
											 &pPopUpMenuItem);
							 MultiByteToWideChar(CP_ACP, 0,
											 "Auto&LISP Example", -1, 
											 wstrMenuItemName, 256); 
							 MultiByteToWideChar(CP_ACP, 0,
											 "(prin1 \"Hello\") ", -1, 
											 wstrMenuItemMacro, 256); 
								VariantInit(&index);
							 V_VT(&index) = VT_I4;
								V_I4(&index) = 2;
							 pPopUpMenu->AddMenuItem(index, wstrMenuItemName,
											 wstrMenuItemMacro, &pPopUpMenuItem);
								VariantInit(&index);
							 V_VT(&index) = VT_I4;
								V_I4(&index) = numberOfMenus - 2;
							 pPopUpMenu->InsertInMenuBar(index);
							 pPopUpMenu->Release();
							 pPopUpMenuItem->Release();
							 bIsMenuLoaded = true;
				}else {
							 acutPrintf("\nMenu not created.");
				}
	}else {
			 VariantInit(&index);
			 V_VT(&index) = VT_BSTR;
			 V_BSTR(&index) = wstrMenuName;
			 pPopUpMenus->RemoveMenuFromMenuBar(index);
			 VariantClear(&index);
			 bIsMenuLoaded = false;
}
pPopUpMenus->Release();

Here is the finished function:

void
addMenuThroughCom()
{
    AutoCAD::IAcadApplication *pAcad;
    AutoCAD::IAcadMenuBar *pMenuBar;
    AutoCAD::IAcadMenuGroups *pMenuGroups;
    AutoCAD::IAcadMenuGroup *pMenuGroup;
    AutoCAD::IAcadPopupMenus *pPopUpMenus;
    AutoCAD::IAcadPopupMenu *pPopUpMenu;
    AutoCAD::IAcadPopupMenuItem *pPopUpMenuItem;
    HRESULT hr = NOERROR;
    CLSID clsid;
    LPUNKNOWN pUnk = NULL;
    LPDISPATCH pAcadDisp = NULL; 
    hr = ::CLSIDFromProgID(L"AutoCAD.Application",
        &clsid);
				if (FAILED(hr))
								return;
				if(::GetActiveObject(clsid, NULL, &pUnk) != S_OK)
								return;
				hr = pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &pAcadDisp);
    pUnk->Release();
				if (FAILED(hr))
        return;
				hr = pAcadDisp->QueryInterface(AutoCAD::IID_IAcadApplication, 												(void**)&pAcad);
    pAcadDisp->Release();
				if (FAILED(hr))
								return;
				pAcad->put_Visible(true);
				pAcad->get_MenuBar(&pMenuBar);
    pAcad->get_MenuGroups(&pMenuGroups);
    pAcad->Release();
    long numberOfMenus;
    pMenuBar->get_Count(&numberOfMenus);
    pMenuBar->Release();
    VARIANT index;
    VariantInit(&index);
    V_VT(&index) = VT_I4;
    V_I4(&index) = 0;
    pMenuGroups->Item(index, &pMenuGroup);
    pMenuGroups->Release();
    pMenuGroup->get_Menus(&pPopUpMenus);
    pMenuGroup->Release();
    WCHAR wstrMenuName[256];
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, 
        "AsdkComAccess", -1, wstrMenuName, 256); 
    if (!bIsMenuLoaded) {
        pPopUpMenus->Add(wstrMenuName, &pPopUpMenu);
        if (pPopUpMenu != NULL) {
            pPopUpMenu->put_Name(wstrMenuName);
            WCHAR wstrMenuItemName[256];
            MultiByteToWideChar(CP_ACP, 0,"&Add A ComCircle",
																-1, wstrMenuItemName, 256); 
            WCHAR wstrMenuItemMacro[256];
            MultiByteToWideChar(CP_ACP, 0, "AsdkComCircle ",
																-1, wstrMenuItemMacro, 256); 
            VariantInit(&index);
            V_VT(&index) = VT_I4;
            V_I4(&index) = 0;
            pPopUpMenu->AddMenuItem(index, wstrMenuItemName,
																wstrMenuItemMacro, &pPopUpMenuItem);
												VariantInit(&index);
            V_VT(&index) = VT_I4;
												V_I4(&index) = 1;
            pPopUpMenu->AddSeparator(index, 
																&pPopUpMenuItem);
            MultiByteToWideChar(CP_ACP, 0,
																"Auto&LISP Example", -1, 
																wstrMenuItemName, 256); 
            MultiByteToWideChar(CP_ACP, 0,
																"(prin1 \"Hello\") ", -1, 
																wstrMenuItemMacro, 256); 
												VariantInit(&index);
            V_VT(&index) = VT_I4;
												V_I4(&index) = 2;
            pPopUpMenu->AddMenuItem(index, wstrMenuItemName,
																wstrMenuItemMacro, &pPopUpMenuItem);
            VariantInit(&index);
            V_VT(&index) = VT_I4;
            V_I4(&index) = numberOfMenus - 2;;
            pPopUpMenu->InsertInMenuBar(index);
            pPopUpMenu->Release();
            pPopUpMenuItem->Release();
    
            bIsMenuLoaded = true;
        } else {
            acutPrintf("\nMenu not created.");
        }
    } else {
        VariantInit(&index);
        V_VT(&index) = VT_BSTR;
        V_BSTR(&index) = wstrMenuName;
        pPopUpMenus->RemoveMenuFromMenuBar(index);
								VariantClear(&index);
								bIsMenuLoaded = false;
    }
    pPopUpMenus->Release();
    
}

Both of these examples can be found in the ObjectARX SDK. They are located in the docsamps\comsamps directory. Each sample contains code for adding a circle and a menu using either Win32 API or MFC programming techniques. Because these methods access AutoCAD through COM interfaces, these programming techniques can be used from other C++ contexts--not just from ObjectARX. Similar techniques can also be used in other languages, such as Visual Basic.

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 08:17 , Processed in 0.187701 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表