Using COM to Access AutoCAD ActiveX Automation [ObjectARX Developer's Guide: ARXD]

 SDK中调用COM的例子

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 sections provide procedures that demonstrate the necessary steps:

  • To set up a new ObjectARX project
  • To import AutoCAD ActiveX interfaces
  • To implement AutoCAD ActiveX Automation calls

To set up a new ObjectARX project

  1. Start Visual C++ and create a new Win32 DLL project called AsdkPlainComSamp.
  2. 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 acdb16.lib rxapi.lib acedapi.lib
  1. Add a new definitions (DEF) file named AsdkPlainComSamp.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 AsdkPlainComSamp.cpp to the project.
  2. Open the new CPP file, and add the following code to make the program ObjectARX compatible:

【转载】SDK中调用COM的例子#include <rxregsvc.h>
【转载】SDK中调用COM的例子
【转载】SDK中调用COM的例子#include 
<aced.h>
【转载】SDK中调用COM的例子
【转载】SDK中调用COM的例子
// Used to add/remove the menu with the same command.
【转载】SDK中调用COM的例子
【转载】SDK中调用COM的例子
//
【转载】SDK中调用COM的例子

【转载】SDK中调用COM的例子
static bool bIsMenuLoaded = false;
【转载】SDK中调用COM的例子
【转载】SDK中调用COM的例子
static void initApp()
【转载】SDK中调用COM的例子
}
  1. Stub in your new AutoCAD command handler function by adding the following code to the AsdkPlainComSamp.cpp source file:

【转载】SDK中调用COM的例子void addMenuThroughCom()
【转载】SDK中调用COM的例子
}

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, acax16<lang>.tlb, as shown in the next procedure.


To import AutoCAD ActiveX interfaces

  1. Add the following line to the top of the AsdkPlainComSamp.cpp file, making sure to use the path where AutoCAD is installed on your system:

【转载】SDK中调用COM的例子import "c:\\acad\\acad.tlb" no_implementation "
【转载】SDK中调用COM的例子

【转载】SDK中调用COM的例子    raw_interfaces_only named_guids
  1. Add the following declarations to the addMenuThroughCom() function:
【转载】SDK中调用COM的例子AutoCAD::IAcadApplication *pAcad;
【转载】SDK中调用COM的例子AutoCAD::IAcadMenuBar 
*pMenuBar;
【转载】SDK中调用COM的例子AutoCAD::IAcadMenuGroups 
*pMenuGroups;
【转载】SDK中调用COM的例子AutoCAD::IAcadMenuGroup 
*pMenuGroup;
【转载】SDK中调用COM的例子AutoCAD::IAcadPopupMenus 
*pPopUpMenus;
【转载】SDK中调用COM的例子AutoCAD::IAcadPopupMenu 
*pPopUpMenu;
【转载】SDK中调用COM的例子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 AsdkPlainComSamp.cpp file, add the following code to the empty addMenuThroughCOM() function to get AutoCAD's IUnknown:
【转载】SDK中调用COM的例子HRESULT hr = NOERROR;
【转载】SDK中调用COM的例子CLSID clsid;
【转载】SDK中调用COM的例子LPUNKNOWN pUnk 
= NULL;
【转载】SDK中调用COM的例子LPDISPATCH pAcadDisp 
= NULL; 
【转载】SDK中调用COM的例子hr 
= ::CLSIDFromProgID(L"AutoCAD.Application",
【转载】SDK中调用COM的例子        
&clsid);
【转载】SDK中调用COM的例子
if (FAILED(hr))
【转载】SDK中调用COM的例子    
return;
【转载】SDK中调用COM的例子
if(::GetActiveObject(clsid, NULL, &pUnk) != S_OK)
【转载】SDK中调用COM的例子    
return;
【转载】SDK中调用COM的例子hr 
= pUnk->QueryInterface(IID_IDispatch, (LPVOID*&pAcadDisp);
【转载】SDK中调用COM的例子pUnk
->Release();
【转载】SDK中调用COM的例子
if (FAILED(hr))
【转载】SDK中调用COM的例子    
return;
  1. Use IUnknown to get the AutoCAD application object. Also, make sure AutoCAD is visible. This is shown in the following code:

【转载】SDK中调用COM的例子hr = pAcadDisp->QueryInterface(AutoCAD::IID_IAcadApplication, (void**)&pAcad);
【转载】SDK中调用COM的例子pAcadDisp
->Release();
【转载】SDK中调用COM的例子
if (FAILED(hr))
【转载】SDK中调用COM的例子    
return;
【转载】SDK中调用COM的例子pAcad
->put_Visible(true);
  1. From the AutoCAD application interface, get the menu bar and menu groups collections:
【转载】SDK中调用COM的例子pAcad->get_MenuBar(&pMenuBar);
【转载】SDK中调用COM的例子pAcad
->get_MenuGroups(&pMenuGroups);
【转载】SDK中调用COM的例子pAcad
->Release();
  1. Determine how many menus are current on the menu bar:
【转载】SDK中调用COM的例子long numberOfMenus;
【转载】SDK中调用COM的例子pMenuBar
->get_Count(&numberOfMenus);
【转载】SDK中调用COM的例子pMenuBar
->Release();
  1. Get the first menu from the menu groups collection. This normally is ACAD, but could be something else:
【转载】SDK中调用COM的例子VARIANT index;
【转载】SDK中调用COM的例子VariantInit(
&index);
【转载】SDK中调用COM的例子V_VT(
&index) = VT_I4;
【转载】SDK中调用COM的例子V_I4(
&index) = 0;
【转载】SDK中调用COM的例子pMenuGroups
->Item(index, &pMenuGroup);
【转载】SDK中调用COM的例子pMenuGroups
->Release();
  1. Get the shortcut menus collection from the first menu group:
【转载】SDK中调用COM的例子pMenuGroup->get_Menus(&pPopUpMenus);
【转载】SDK中调用COM的例子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:

【转载】SDK中调用COM的例子WCHAR wstrMenuName[256];
【转载】SDK中调用COM的例子MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, 
"AsdkComAccess"-1, wstrMenuName, 256); 
Release();

Here is the finished function:


【转载】SDK中调用COM的例子void addMenuThroughCom()
}

Both of these examples can be found in the ObjectARX SDK. They are located in the samples"com 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.

相关文章: