明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4318|回复: 7

事件处理过程中读取全局内容

[复制链接]
发表于 2012-2-13 19:24 | 显示全部楼层 |阅读模式
本帖最后由 20033535 于 2012-2-13 19:32 编辑

做了个块浏览器,带paletteset的那种,主要实现以下:
1、netload加载后即可用,无需其他启动命令。
2、编辑、删除、创建数据库对象后,自动更新paletteset里面的gridview内容。
3、切换文档后,自动更新paletteset里面的gridview内容。

目前遇到一个困难:在向数据库级的ObjectModified、ObjectErased、ObjectAppended事件中写更新gridview内容时CAD会报错;文档切换事件中更新gridview就没有问题。
我认为问题出现在这里:在数据库级的ObjectModified、ObjectErased、ObjectAppended事件处理过程中又读取了全局DataBase,所以报错。但是该怎么实现在删除/创建/改变事件中读取全局DataBase呢?

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Autodesk.AutoCAD.Geometry;
  10. using Autodesk.AutoCAD.Runtime;
  11. using Autodesk.AutoCAD.Windows;
  12. using cadapp = Autodesk.AutoCAD.ApplicationServices;

  13. [assembly: ExtensionApplication(typeof(CADBlockBrowse.CAD))]
  14. [assembly: CommandClass(typeof(CADBlockBrowse.CAD))]

  15. namespace CADBlockBrowse
  16. {
  17.     public class CAD : IExtensionApplication
  18.     {
  19.         private cadapp.Document CurDoc = cadapp.Application.DocumentManager.MdiActiveDocument;
  20.         private Database CurDB = cadapp.Application.DocumentManager.MdiActiveDocument.Database;
  21.         private Editor ed = cadapp.Application.DocumentManager.MdiActiveDocument.Editor;

  22.         private PaletteSet ps = new PaletteSet("块选择器");
  23.         private Hashtable BlockName_Count = new Hashtable();
  24.         private UserControl1 mycontrol = new UserControl1();

  25.         public CAD()
  26.         {
  27.         }

  28.         /// <summary>
  29.         /// 读取当前活动文档的所有块名、数量到Hashtable中
  30.         /// </summary>
  31.         /// <returns>返回Hashtable</returns>
  32.         public static Hashtable ReloadBlockNames()
  33.         {
  34.             Hashtable hs = new Hashtable();
  35.             Database db = cadapp.Application.DocumentManager.MdiActiveDocument.Database;
  36.             using (Transaction trans = db.TransactionManager.StartTransaction())
  37.             {
  38.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  39.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);

  40.                 foreach (ObjectId obj in btr)
  41.                 {
  42.                     Entity ent = (Entity)trans.GetObject(obj, OpenMode.ForRead);
  43.                     if (ent.GetType() == typeof(BlockReference))
  44.                     {
  45.                         BlockReference brf = ent as BlockReference;
  46.                         if (hs.ContainsKey(brf.Name))
  47.                         {
  48.                             int oc = Convert.ToInt32(hs[brf.Name]);
  49.                             hs[brf.Name] = oc + 1;
  50.                         }
  51.                         else
  52.                         {
  53.                             hs.Add(brf.Name, 1);
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.             return hs;
  59.         }

  60.         /// <summary>
  61.         /// 炸开所有块
  62.         /// </summary>
  63.         /// <param name="b_name">块名称</param>
  64.         public static void BlockSelectAndExplode(string b_name)
  65.         {
  66.             cadapp.Document doc = cadapp.Application.DocumentManager.MdiActiveDocument;
  67.             Database db = doc.Database;
  68.             Editor ed = doc.Editor;
  69.             Transaction transaction = db.TransactionManager.StartTransaction();
  70.             using (transaction)
  71.             {
  72.                 Entity entity = null;
  73.                 DBObjectCollection EntityCollection = new DBObjectCollection();
  74.                 BlockTable bt = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForRead);
  75.                 BlockTableRecord btr = (BlockTableRecord)transaction.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
  76.                 cadapp.DocumentLock documentLock = doc.LockDocument();

  77.                 foreach (ObjectId id in btr)
  78.                 {
  79.                     entity = (Entity)transaction.GetObject(id, OpenMode.ForWrite);
  80.                     if (entity is BlockReference)
  81.                     {
  82.                         BlockReference br = (BlockReference)entity;
  83.                         if (br.Name == b_name)
  84.                         {
  85.                             entity.Explode(EntityCollection);
  86.                             entity.UpgradeOpen();
  87.                             entity.Erase();
  88.                         }
  89.                     }
  90.                 }

  91.                 AddEntityCollection(EntityCollection, doc);
  92.                 transaction.Commit();
  93.                 documentLock.Dispose();
  94.             }
  95.         }

  96.         private static void AddEntityCollection(DBObjectCollection dbos, cadapp.Document doc)
  97.         {
  98.             Database db = doc.Database;
  99.             using (Transaction tran = db.TransactionManager.StartTransaction())
  100.             {
  101.                 BlockTableRecord btr = (BlockTableRecord)tran.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  102.                 foreach (DBObject obj in dbos)
  103.                 {
  104.                     Entity ent = (Entity)obj;
  105.                     btr.AppendEntity(ent);
  106.                     tran.AddNewlyCreatedDBObject(ent, true);
  107.                 }
  108.                 tran.Commit();
  109.             }
  110.         }

  111.         public void Initialize()
  112.         {
  113.             ed.WriteMessage("\nInitialize函数中运行\n");
  114.             ps.Style = PaletteSetStyles.ShowAutoHideButton | PaletteSetStyles.ShowPropertiesMenu;
  115.             //ps.Dock = DockSides.None;
  116.             ps.DockEnabled = DockSides.Left | DockSides.Right | DockSides.None;
  117.             ps.MinimumSize = new System.Drawing.Size(150, 100);
  118.             ps.Size = new System.Drawing.Size(150, 100);
  119.             ps.Add("Palette1", mycontrol);
  120.             ps.Visible = true;

  121.             //注册文档切换事件
  122.             cadapp.Application.DocumentManager.DocumentActivated += new cadapp.DocumentCollectionEventHandler(DocumentManager_DocumentActivated);
  123.             //注册文档打开、创建事件
  124.             cadapp.Application.DocumentManager.DocumentCreated += new cadapp.DocumentCollectionEventHandler(DocumentManager_DocumentCreated);
  125.             //注册文档对象删除事件
  126.             cadapp.Application.DocumentManager.MdiActiveDocument.Database.ObjectErased += new ObjectErasedEventHandler(Database_ObjectErased);
  127.             //注册文档对象创建事件
  128.             //cadapp.Application.DocumentManager.MdiActiveDocument.Database.ObjectAppended += new ObjectEventHandler(Database_ObjectAppended);


  129.             UpdateGV();
  130.         }

  131.         void DocumentManager_DocumentCreated(object sender, cadapp.DocumentCollectionEventArgs e)
  132.         {
  133.             try
  134.             {
  135.                 //MessageBox.Show("DocumentCreated Event:" + e.Document.Name);
  136.                 //注册文档对象修改事件
  137.                 //e.Document.Database.ObjectModified += new ObjectEventHandler(Database_ObjectModified);
  138.                 //注册文档对象删除事件
  139.                 e.Document.Database.ObjectErased += new ObjectErasedEventHandler(Database_ObjectErased);
  140.                 //注册文档对象创建事件
  141.                 //e.Document.Database.ObjectAppended += new ObjectEventHandler(Database_ObjectAppended);
  142.                 UpdateGV();
  143.             }
  144.             catch (System.Exception ee)
  145.             {
  146.                 cadapp.Application.ShowAlertDialog(ee.Message);
  147.             }
  148.         }

  149.         //void Database_ObjectAppended(object sender, ObjectEventArgs e)
  150.         //{
  151.         //    //MessageBox.Show("ObjectAppended Event:" + e.DBObject.GetType().ToString());
  152.         //    try
  153.         //    {
  154.         //        UpdateGV();
  155.         //    }
  156.         //    catch (System.Exception ee)
  157.         //    {
  158.         //        MessageBox.Show("ObjectAppended Error:" + ee.Message);
  159.         //    }
  160.         //}

  161.         //private void Database_ObjectModified(object sender, ObjectEventArgs e)
  162.         //{
  163.         //    //MessageBox.Show("ObjectModified Event:" + e.DBObject.GetType().ToString());
  164.         //    UpdateGV();
  165.         //}

  166.         void Database_ObjectErased(object sender, ObjectErasedEventArgs e)
  167.         {
  168.             try
  169.             {
  170.                 if (e.Erased)
  171.                 {
  172.                     UpdateGV();  //我认为问题出现在这里:在事件处理过程中又读取了全局DataBase。但是该怎么实现在删除/创建/改变事件中读取全局DataBase呢?
  173.                 }

  174.             }
  175.             catch (System.Exception ee)
  176.             {
  177.                 MessageBox.Show("ObjectErased Error:" + ee.Message);
  178.             }
  179.         }

  180.         private void DocumentManager_DocumentActivated(object sender, cadapp.DocumentCollectionEventArgs e)
  181.         {
  182.             UpdateGV();
  183.         }

  184.         /// <summary>
  185.         /// 更新GridView的内容
  186.         /// </summary>
  187.         private void UpdateGV()
  188.         {
  189.             Hashtable bts = ReloadBlockNames();
  190.             if (bts.Count == 0)
  191.             {
  192.                 mycontrol.bt_explode.Text = "无块";
  193.                 mycontrol.bt_explode.Enabled = false;
  194.             }
  195.             else
  196.             {
  197.                 mycontrol.bt_explode.Text = "炸开";
  198.                 mycontrol.bt_explode.Enabled = true;
  199.                 mycontrol.setdgvDataSource(bts);
  200.             }
  201.         }

  202.         public void Terminate()
  203.         {
  204.         }
  205.     }
  206. }


发表于 2012-2-14 15:30 | 显示全部楼层
标记一下,正在使用这些方法
发表于 2012-2-14 16:37 | 显示全部楼层
非常复杂!为什么不把报什么错贴出来了,既然怀疑某个地方错,怎么不再那里继续检查,至少可以把那行代码干掉看看还会不会报错呀
 楼主| 发表于 2012-2-14 18:23 | 显示全部楼层
本帖最后由 20033535 于 2012-2-14 18:25 编辑

其实问题也很简单。就是 Database.ObjectErased 这个数据库级的事件处理时,要读取全部的Database。这时会报错误。
如果不用注册事件,那就简单了,用命令启动。但是就没那么完美了。
有点类似这个http://bbs.mjtd.com/forum.php?mod=viewthread&tid=86739&extra=page%3D1%26filter%3Dtypeid%26typeid%3D81%26typeid%3D81

发表于 2012-2-17 18:20 | 显示全部楼层
无需其他启动命令,好牛

更新paletteset里面的gridview内容,我正要学学怎么把块放到palette上呢

切换文档后,自动更新paletteset里面的gridview内容,我不想更新。
发表于 2012-4-18 18:33 | 显示全部楼层
很好的方法。
发表于 2012-5-8 10:20 | 显示全部楼层
楼主很牛嘛
发表于 2012-5-20 19:33 | 显示全部楼层
楼主你好。我在做基于C#的CAD二次开发,想在自己做的dll里面调用sql数据库 可以吗???
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-6 10:35 , Processed in 0.279720 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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