原文链接: http://adndevblog.typepad.com/manufacturing/2017/02/export-to-3d-pdf-c-example.html
从Inventor 2017版本开始,Inventor提供了导出3D PDF的功能。对应的VBA写的示例在帮助文档里有提供。我们同事最近写了一篇博客,用C++的方式。基于SDK中的例子进行测试:\Inventor 2017\SDK\DeveloperTools\Samples\VC++\Standalone Applications\Inventor
// SimpleExe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// Forward declarations
static HRESULT PublishPDF();
// Main. Note that all COM related activity (including the automatic 'release' within smart
// pointers) MUST take place BEFORE CoUnitialize(). Hence the function 'block' within which
// the smart-pointers construct and destruct (and AddRef and Release) keeping the CoUnitialize
// safely out of the way.
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT Result = NOERROR;
Result = CoInitialize(NULL);
if (SUCCEEDED(Result))
Result = PublishPDF();
CoUninitialize();
return 0;
}
static HRESULT PublishPDF()
{
HRESULT Result = NOERROR;
CLSID InvAppClsid;
Result = CLSIDFromProgID(L"Inventor.Application", &InvAppClsid);
if (FAILED(Result)) return Result;
/*Try and get hold of the running inventor app*/
CComPtr pInvAppUnk;
Result = ::GetActiveObject(InvAppClsid, NULL, &pInvAppUnk);
if (FAILED(Result))
{
_tprintf_s(_T("*** Could not get hold of an active Inventor application ***\n"));
return Result;
}
CComPtr pInvApp;
Result = pInvAppUnk->QueryInterface(__uuidof(Application), (void **)&pInvApp);
if (FAILED(Result)) return Result;
/*Get the active document*/
CComPtr pDoc;
Result = pInvApp->get_ActiveDocument(&pDoc);
if (FAILED(Result)) return Result;
/*Output path of the pdf*/
char filename[50];
sprintf_s(filename, "c:\\temp\\test.pdf");
/*Populate required options*/
CComPtr oOptions = NULL;
pInvApp->TransientObjects->CreateNameValueMap(&oOptions);
oOptions->put_Value(CComBSTR("FileOutputLocation"), CComVariant(filename));
oOptions->put_Value(CComBSTR("VisualizationQuality"), CComVariant((long)kHigh));
CComSafeArray sDesignViews;
sDesignViews.Add(CComBSTR("Master"));
oOptions->put_Value(CComBSTR("ExportDesignViewRepresentations"), CComVariant(sDesignViews));
/* Get the Add-Ins*/
CComPtr pAddIns;
Result = pInvApp->get_ApplicationAddIns(&pAddIns);
/*Try and get hold of the 3D PDF addin*/
CComBSTR clsidAnarkAddin = CComBSTR(_T("{3EE52B28-D6E0-4EA4-8AA6-C2A266DEBB88}"));
CComPtr pAnarkAddin = nullptr;
CComVariant vRes;
Result = pAddIns->get_ItemById(clsidAnarkAddin, &pAnarkAddin);
if (pAnarkAddin) {
CComPtr pAnarkAPI;
pAnarkAddin->get_Automation(&pAnarkAPI);
if (pAnarkAPI) {
DISPID dispid;
OLECHAR* name(OLESTR("Publish"));
Result = pAnarkAPI->GetIDsOfNames(IID_NULL, &name, 1,
LOCALE_SYSTEM_DEFAULT, &dispid);
if (SUCCEEDED(Result)) {
CComVariant vars[2];
vars[1] = pDoc;
vars[0] = oOptions;
DISPPARAMS params = { &vars[0], NULL, 2, 0 };
Result = pAnarkAPI->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT,
DISPATCH_METHOD, ¶ms, &vRes,
NULL, NULL);
}
}
}
return Result;
}