Programmatically saving the active document in C++/ARX applications

By Gopinath Taget

Use the Automation interfaces inside your ARX application. The following code samples demonstrate two approaches to consider (Make sure your ARX projects supports the AutoCAD ActiveX client interfaces. The ARX wizard makes this easy to do):

First Approach:

void saveDwg()

{

 try

{

IAcadApplicationPtr pApp =

acedGetAcadWinApp()->GetIDispatch(FALSE);

IAcadDocumentPtr pDoc;

pApp->get_ActiveDocument(&pDoc);

pDoc->Save();

}

 catch(_com_error& e)

{

acutPrintf(_T("\nCOM error: %s"), (ACHAR*)e.Description());

}

}

Second Approach:

#include <acadi_i.c>

// minimal error check for code brevity

void saveDwg()

{

CWinApp* pWinApp = acedGetAcadWinApp();

 if(!pWinApp)

  return;

CComPtr<IDispatch> pDisp = pWinApp->GetIDispatch(TRUE);

 if(!pDisp)

  return;

 

CComPtr<IAcadApplication> pComApp;

HRESULT hr = pDisp->QueryInterface(IID_IAcadApplication,

  (void**)&pComApp);

 if(FAILED(hr))

  return;

 

CComPtr<IAcadDocument> pDoc;

hr = pComApp->get_ActiveDocument(&pDoc);

 if(FAILED(hr))

  return;

 

hr = pDoc->Save();

 if(FAILED(hr))

  acutPrintf(_T("\nFailed to save current dwg file."));

}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading