用VS写一段代码,调用CAD指定路径插入块文件到具体坐标
时间: 2023-05-20 08:04:04 浏览: 171
以下是用VS写的调用CAD指定路径插入块文件到具体坐标的代码:
```c++
#include <windows.h>
#include <atlbase.h>
#include <atlcom.h>
#include <comdef.h>
#include <iostream>
using namespace std;
int main()
{
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"AutoCAD.Application", &clsid);
if (FAILED(hr))
{
cout << "Failed to get CLSID" << endl;
return 0;
}
IDispatch* pAcadApp;
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pAcadApp);
if (FAILED(hr))
{
cout << "Failed to create AutoCAD instance" << endl;
return 0;
}
VARIANT varFileName, varInsertionPoint, varScaleFactor;
VariantInit(&varFileName);
VariantInit(&varInsertionPoint);
VariantInit(&varScaleFactor);
varFileName.vt = VT_BSTR;
varFileName.bstrVal = SysAllocString(L"C:\\Block.dwg");
varInsertionPoint.vt = VT_ARRAY | VT_R8;
SAFEARRAYBOUND bounds[1];
bounds[0].lLbound = 0;
bounds[0].cElements = 3;
varInsertionPoint.parray = SafeArrayCreate(VT_R8, 1, bounds);
double* pData;
SafeArrayAccessData(varInsertionPoint.parray, (void**)&pData);
pData[0] = 10.0;
pData[1] = 20.0;
pData[2] = 0.0;
SafeArrayUnaccessData(varInsertionPoint.parray);
varScaleFactor.vt = VT_R8;
varScaleFactor.dblVal = 1.0;
DISPID dispid;
LPOLESTR szMember = L"SendCommand";
hr = pAcadApp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
cout << "Failed to get SendCommand ID" << endl;
return 0;
}
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof(DISPPARAMS));
dispparams.cArgs = 3;
dispparams.rgvarg = new VARIANT[3];
dispparams.rgvarg[2] = varFileName;
dispparams.rgvarg[1] = varInsertionPoint;
dispparams.rgvarg[0] = varScaleFactor;
EXCEPINFO excepinfo;
memset(&excepinfo, 0, sizeof(EXCEPINFO));
UINT nArgErr = (UINT)-1;
hr = pAcadApp->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparams, NULL, &excepinfo, &nArgErr);
if (FAILED(hr))
{
cout << "Failed to invoke SendCommand" << endl;
return 0;
}
VariantClear(&varFileName);
VariantClear(&varInsertionPoint);
VariantClear(&varScaleFactor);
pAcadApp->Release();
CoUninitialize();
return 0;
}
```
这段代码可以调用AutoCAD的COM接口,在指定路径下插入块文件到指定坐标。
阅读全文