Inventor API:用C++导出3D PDF

原文链接: 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;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值