Calling translator add-in from external application C++

By Adam Nagy

This is the C++ version of this blog post: http://adndevblog.typepad.com/manufacturing/2013/01/calling-translator-add-in-from-external-application.html

static HRESULT SaveToIges()
{
  HRESULT Result = NOERROR;
 
  // Access Inventor
 
  CLSID InvAppClsid;
  Result = CLSIDFromProgID (L"Inventor.Application", &InvAppClsid);
  if (FAILED(Result)) return Result;
 
  CComPtr pInvAppUnk;
  Result = ::GetActiveObject (InvAppClsid, NULL, &pInvAppUnk);
  if (FAILED (Result))
    _tprintf_s(_T("*** Could not get hold of an active Inventor application ***n"));
  if (FAILED(Result)) return Result;
 
  CComPtr pInvApp;
  Result = pInvAppUnk->QueryInterface(
    __uuidof(Application), (void **) &pInvApp);
  if (FAILED(Result)) return Result;
 
  // Get current document
 
  CComPtr pDoc;
  pInvApp->get_ActiveDocument(&pDoc);
 
  // Find the translator addin
 
  CComPtr pAddIns;
  pInvApp->get_ApplicationAddIns(&pAddIns);
 
  CComPtr pAddIn;
  pAddIns->get_ItemById(
    _bstr_t("{90AF7F44-0C01-11D5-8E83-0010B541CD80}"), 
    (ApplicationAddIn**)&pAddIn);
 
  if (pAddIn == NULL)
  {
    _tprintf_s(_T("*** Could not find the translator addin ***n"));
    return E_FAIL;
  }
 
  // Set it up
 
  CComPtr pTr;
  pInvApp->get_TransientObjects(&pTr);
 
  CComPtr pContext;
  pTr->CreateTranslationContext(&pContext);
  pContext->put_Type(IOMechanismEnum::kFileBrowseIOMechanism);
 
  CComPtr pMedium;
  pTr->CreateDataMedium(&pMedium);
  pMedium->put_FileName(_bstr_t("c:\temp\file.igs"));
 
  CComPtr pOptions;
  pTr->CreateNameValueMap(&pOptions);
 
  VARIANT_BOOL hasOptions;
  pAddIn->get_HasSaveCopyAsOptions(
    pDoc, pContext, pOptions, &hasOptions);
  if (hasOptions == VARIANT_TRUE)
  {
    CComVariant num1(1, VT_I4);
    pOptions->put_Value(_bstr_t("GeometryType"), num1);
    CComVariant num0(0, VT_I4);
    pOptions->put_Value(_bstr_t("SolidFaceType"), num0);
    pOptions->put_Value(_bstr_t("SurfaceType"), num0);
  }
 
  pAddIn->SaveCopyAs(pDoc, pContext, pOptions, pMedium);
 
  return Result;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
  HRESULT Result = NOERROR;
 
  Result = CoInitialize (NULL);
 
  if (SUCCEEDED(Result))
    Result = SaveToIges();
 
  CoUninitialize(); 
 
  return 0;
}

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading