- UID
- 1
- 积分
- 16111
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-3
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Accessing AutoCAD Automation Interfaces Within ARX
ID 18379
Applies to: AutoCAD 2000
AutoCAD 2000I
AutoCAD 2002
Date 1/29/2002
This document is part of ObjectARX COM-ActiveX Interfaces MFC
Question
What is the correct method of accessing the AutoCAD automation interfaces from
within an ARX object?
Answer
The following code accesses the automation interfaces within an ARX object.
The third time the following function runs, an "unhandled exception" is thrown.
What is the reason?
bool testSave ()
{
// get the Active Document interface and check to see
// if the current dwg has been saved
IAcadApplication IAcadApp;
LPDISPATCH pAcadDisp = NULL;
pAcadDisp = acedGetAcadWinApp()->GetIDispatch(FALSE);
if(pAcadDisp == NULL)
return false;
IAcadApp.AttachDispatch(pAcadDisp);
IAcadDocument IAcadDoc;
pAcadDisp = NULL;
pAcadDisp = IAcadApp.GetActiveDocument();
if(pAcadDisp == NULL)
{
acutPrintf("\nFailed load template for macro.\n");
return false;
}
IAcadDoc.AttachDispatch(pAcadDisp);
if (!IAcadDoc.GetSaved())
IAcadDoc.Save();
IAcadDoc.ReleaseDispatch();
IAcadApp.ReleaseDispatch();
return true;
}
The problem with the code is that FALSE is passed to acedGetAcadWinApp()->GetIDispatch().
It means that the proper reference counting for the usage of IAcadApplication
object is not being allowed, which explains why AutoCAD terminates unexpectedly
after a few tries. Also, if you have local interface objects, when they go out
of the function scope, they will be released so you don't have to do it explicitly.
// IAcadDoc.ReleaseDispatch();
// IAcadApp.ReleaseDispatch();
The following code snippet shows the correct code to fix the problem.
void testSave () {
try
{
LPDISPATCH pDisp = acedGetAcadWinApp()->GetIDispatch(TRUE);
ASSERT(pDisp);
IAcadApplication IAcadApp(pDisp);
IAcadDocument IAcadDoc(IAcadApp.GetActiveDocument());
if (!IAcadDoc.GetSaved())
IAcadDoc.Save();
}
catch(CException e)
{
e.ReportError();
}
}
NOTE: The preceding code should reside in an ARX/MFC application. |
|