明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1048|回复: 4

请教老大们关于取得对象扩展数据引起的CAD崩溃

[复制链接]
发表于 2014-9-12 17:32 | 显示全部楼层 |阅读模式
本帖最后由 cheng5276 于 2014-9-12 18:05 编辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using System.Windows.Forms;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using Document = Autodesk.AutoCAD.ApplicationServices.Document;
[assembly: CommandClass(typeof(Cheng5276_CAD.Class1))]
namespace Cheng5276_CAD
{

    public class Class1
    {
        public void doc_ImpliedSelectionChanged(object sender, EventArgs e)
        {
            Autodesk.AutoCAD.EditorInput.Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptSelectionResult pkf = ed.SelectImplied();
            if (pkf.Status != PromptStatus.OK)
            { //取消选择状态
                return;
            }
            else
            { //对象选择状态
ObjectId[] objIds = pkf.Value.GetObjectIds();
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Transaction trans = doc.TransactionManager.StartTransaction();
                foreach (ObjectId objId  in objIds)
                {
                    
                    try
                    {
                        DBObject obj = trans.GetObject(objId, OpenMode.ForRead);
                        ResultBuffer rb = obj.XData;
                        if (rb != null)
                        {

                            foreach (TypedValue tv in rb)
                            {
                                if (tv.TypeCode == 1000)
                                {
                                    string zg_t = tv.Value.ToString();
                                    break;
                                }

                            }
                            rb.Dispose();
                        }

                        trans.Commit();
                        obj.Dispose();

                    }
                    catch { }
                }

                doc.Dispose();
                trans.Dispose();
            }
        }

        [CommandMethod("FF")]
        public void Initialize()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            acDoc.ImpliedSelectionChanged += new EventHandler(doc_ImpliedSelectionChanged); //定义对象选择事件
       }
    }
}


NET加载后,执行FF启用事件。
但是只要选择对象后,随便干什么,都会引起CAD崩溃,实在不明原因,请老大们指点


发表于 2014-9-12 17:57 来自手机 | 显示全部楼层
文档相关的操作先加锁试试 另外你获取实体的代码呢 如果事务外调用实体会崩溃 因为实体已释放
 楼主| 发表于 2014-9-15 18:19 | 显示全部楼层
老大,不好意思,能请您说的更详细些么?谢谢了
发表于 2014-9-15 18:58 | 显示全部楼层
把所有的Dispose()都去掉 事务完成后会自动调用的
 楼主| 发表于 2014-9-16 13:36 | 显示全部楼层
爱死您了,我的版主大人
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-5 17:30 , Processed in 0.445638 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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