Export to 3D PDF C++ Example

<?xml encoding=”UTF-8″>By Sajith Subramanian

There is a VBA example available in the Inventor API help file. Here is an example in C++.

One of the easier ways to test the below code, would be to replace the code in the SimpleExe SDK example.

The SimpleExe example is located on my system at: “C:UsersPublicDocumentsAutodeskInventor 2017SDKDeveloperToolsSamplesVC++Standalone ApplicationsInventor”.

// 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,<br>                                                          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, <br>                                                           DISPATCH_METHOD, &params, &vRes, <br>                                                           NULL, NULL);
}
}
}
return Result;
}

Comments

One response to “Export to 3D PDF C++ Example”

  1. I’ve written an iLogic version similar to the above: have a look here: https://www.cadlinecommunity.co.uk/hc/en-us/articles/213505925

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading