做了个块浏览器,带paletteset的那种,主要实现以下:
1、netload加载后即可用,无需其他启动命令。
2、编辑、删除、创建数据库对象后,自动更新paletteset里面的gridview内容。
3、切换文档后,自动更新paletteset里面的gridview内容。
目前遇到一个困难:在向数据库级的ObjectModified、ObjectErased、ObjectAppended事件中写更新gridview内容时CAD会报错;文档切换事件中更新gridview就没有问题。
我认为问题出现在这里:在数据库级的ObjectModified、ObjectErased、ObjectAppended事件处理过程中又读取了全局DataBase,所以报错。但是该怎么实现在删除/创建/改变事件中读取全局DataBase呢?
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Autodesk..DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Windows;
- using cadapp = Autodesk.AutoCAD.ApplicationServices;
- [assembly: ExtensionApplication(typeof(CADBlockBrowse.CAD))]
- [assembly: CommandClass(typeof(CADBlockBrowse.CAD))]
- namespace CADBlockBrowse
- {
- public class CAD : IExtensionApplication
- {
- private cadapp.Document CurDoc = cadapp.Application.DocumentManager.MdiActiveDocument;
- private Database CurDB = cadapp.Application.DocumentManager.MdiActiveDocument.Database;
- private Editor ed = cadapp.Application.DocumentManager.MdiActiveDocument.Editor;
- private PaletteSet ps = new PaletteSet("块选择器");
- private Hashtable BlockName_Count = new Hashtable();
- private UserControl1 mycontrol = new UserControl1();
- public CAD()
- {
- }
- ///
- /// 读取当前活动文档的所有块名、数量到Hashtable中
- ///
- /// 返回Hashtable
- public static Hashtable ReloadBlockNames()
- {
- Hashtable hs = new Hashtable();
- Database db = cadapp.Application.DocumentManager.MdiActiveDocument.Database;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
- foreach (ObjectId obj in btr)
- {
- Entity ent = (Entity)trans.GetObject(obj, OpenMode.ForRead);
- if (ent.GetType() == typeof(BlockReference))
- {
- BlockReference brf = ent as BlockReference;
- if (hs.ContainsKey(brf.Name))
- {
- int oc = Convert.ToInt32(hs[brf.Name]);
- hs[brf.Name] = oc + 1;
- }
- else
- {
- hs.Add(brf.Name, 1);
- }
- }
- }
- }
- return hs;
- }
- ///
- /// 炸开所有块
- ///
- /// 块名称
- public static void BlockSelectAndExplode(string b_name)
- {
- cadapp.Document doc = cadapp.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Transaction transaction = db.TransactionManager.StartTransaction();
- using (transaction)
- {
- Entity entity = null;
- DBObjectCollection EntityCollection = new DBObjectCollection();
- BlockTable bt = (BlockTable)transaction.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)transaction.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
- cadapp.DocumentLock documentLock = doc.LockDocument();
- foreach (ObjectId id in btr)
- {
- entity = (Entity)transaction.GetObject(id, OpenMode.ForWrite);
- if (entity is BlockReference)
- {
- BlockReference br = (BlockReference)entity;
- if (br.Name == b_name)
- {
- entity.Explode(EntityCollection);
- entity.UpgradeOpen();
- entity.Erase();
- }
- }
- }
- AddEntityCollection(EntityCollection, doc);
- transaction.Commit();
- documentLock.Dispose();
- }
- }
- private static void AddEntityCollection(DBObjectCollection dbos, cadapp.Document doc)
- {
- Database db = doc.Database;
- using (Transaction tran = db.TransactionManager.StartTransaction())
- {
- BlockTableRecord btr = (BlockTableRecord)tran.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- foreach (DBObject obj in dbos)
- {
- Entity ent = (Entity)obj;
- btr.AppendEntity(ent);
- tran.AddNewlyCreatedDBObject(ent, true);
- }
- tran.Commit();
- }
- }
- public void Initialize()
- {
- ed.WriteMessage("\nInitialize函数中运行\n");
- ps.Style = PaletteSetStyles.ShowAutoHideButton | PaletteSetStyles.ShowPropertiesMenu;
- //ps.Dock = DockSides.None;
- ps.DockEnabled = DockSides.Left | DockSides.Right | DockSides.None;
- ps.MinimumSize = new System.Drawing.Size(150, 100);
|