1. 向intellicad发消息:
#include "stdafx.h"
#include "atlbase.h"
#include "IcadTLB.h" //<-- you can find this in the API directory
int _tmain(int argc, _TCHAR* argv[])
{
//init
CoInitialize(NULL);
//get the application
CComPtr
application = NULL;
CLSID applicationClass;
CLSIDFromProgID( OLESTR("icad.application"), &applicationClass );
CComPtr
unknown;
HRESULT result = GetActiveObject( applicationClass, NULL, &unknown );
if( SUCCEEDED( result ) )
{
CComQIPtr
app = unknown;
application = app;
}
else
{
printf("Failed to get Application");
return -1;
}
//get the active document.. not needed here, but left it in as an example
CComPtr
document;
HRESULT hr = application->get_ActiveDocument( &document );
if(FAILED(hr))
{
printf("Failed to get active document");
return -1;
}
//make our BSTR
BSTR bstrScript = SysAllocString(L"C:/Spline.scr");
//run the sript
hr = application->RunScript(bstrScript);
if(FAILED(hr))
{
printf("Failed to run script");
}
SysFreeString(bstrScript);
return 0;
}
以上代码由www.intellicadms.com提供。
2.autocad
#include "acad.h"
#include <comdef.h>
IAcadApplication * pApp = NULL ;
IAcadDocument *pDoc = NULL ;
CLSID clsid ;
HRESULT hr = NOERROR ;
LPUNKNOWN pUnk = NULL ;
LPDISPATCH pAcadDisp = NULL ;
BSTR BCmdStr = NULL ;
string command = "SCR " + workfolder + "//test.SCR" + "/n";
try
{
CoInitialize(NULL);
pApp = new IAcadApplication() ;
if ( FAILED(::CLSIDFromProgID( L"AutoCAD.Application",&clsid) ))
throw 1 ;
if(::GetActiveObject(clsid , NULL , &pUnk) != S_OK)
throw 2 ;
if(pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &pAcadDisp) != S_OK)
throw 3 ;
pApp ->AttachDispatch(pAcadDisp) ;
// if ( FAILED(::CLSIDFromProgID( L"AutoCAD.Application",&clsid) ))
// throw 4 ;
// if(::GetActiveObject(clsid , NULL , &pUnk) != S_OK)
// throw 5 ;
// if(pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &pAcadDisp) != S_OK)
// throw 6 ;
pDoc = new IAcadDocument() ;
LPDISPATCH pp = pApp ->GetActiveDocument() ;
pDoc ->AttachDispatch(pp) ;
// if(pop)
::BringWindowToTop(::GetParent(::GetParent((HWND)pDoc ->GetHwnd()))) ;
//BCmdStr = CComBSTR(cmdStr);
// BCmdStr = ::SysAllocString(cmdStr) ;
// pDoc ->SendCommand((LPCTSTR)BCmdStr) ;
pDoc->SendCommand("FILEDIA 0 ");
pDoc->SendCommand(command.c_str());
// pDoc ->SendCommand(cmdStr) ;
delete pDoc ;
delete pApp ;
pUnk->Release() ;
//::SysFreeString(BCmdStr) ;
CoUninitialize() ;
}
catch (_com_error * e)
{
if(pApp != NULL)
delete pApp ;
if(pDoc != NULL)
delete pDoc ;
if(BCmdStr != NULL)
::SysFreeString(BCmdStr) ;
if(pUnk != NULL)
pUnk ->Release() ;
AfxMessageBox(e ->ErrorMessage()) ;
return -1 ;
}
catch (int e)
{
if(pApp != NULL)
delete pApp ;
if(pDoc != NULL)
delete pDoc ;
if(BCmdStr != NULL)
::SysFreeString(BCmdStr) ;
if(pUnk != NULL)
pUnk ->Release() ;
// AfxMessageBox(_T("ERROR num e")) ;
return -1 ;
}
return 1 ;