找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 725|回复: 0

[ARX程序]:Accessing AutoCAD Automation Interfaces Within ARX

[复制链接]

已领礼包: 145个

财富等级: 日进斗金

发表于 2002-2-13 02:52:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
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.
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-5 13:31 , Processed in 0.339236 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表