- UID
- 3388
- 积分
- 3292
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-3-28
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
提供了四个命令:
HIDENT:隐藏所选对象
ISOLENT:隔离选定的对象(隐藏所有其他对象)
INVIZ:反转对象可见性
UNISOLENT:取消隐藏所有对象
这些命令被添加到默认上下文菜单中,而HIDENT和ISOLENT也被添加到对象上下文菜单中。
由于Visible属性仅以编程方式可用,因此出现一个消息框,要求用户在保存时取消隐藏隐藏的对象(如果有)。 相同的消息警告用户打开新图形。
提示在法语AutoCAD版本上以法语显示,否则以英语显示。
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Windows;
- using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
- [assembly: ExtensionApplication(typeof(IsolateObjects.Commands))]
- namespace IsolateObjects
- {
- public class Commands : IExtensionApplication
- {
- private bool fra = (string)acadApp.GetSystemVariable("LOCALE") == "FRA";
- private DocumentCollection docMan = acadApp.DocumentManager;
- // Abonnement à l'évènement "DocumentCreated"
- // Abonnement de tous les documents ouverts à l'évènement "BeginSave"
- // Ajouts aux menus contextuel
- public void Initialize()
- {
- docMan.DocumentCreated += new DocumentCollectionEventHandler(OnDocumentCreated);
- foreach (Document doc in docMan)
- {
- doc.Database.BeginSave += new DatabaseIOEventHandler(OnSaveAlert);
- }
- ContextMenu.AddDefaultContextMenu();
- ContextMenu.AddObjectContextMenu();
- ShowMessage();
- }
- // Suppression des ajouts aux menus contextuels
- public void Terminate()
- {
- ContextMenu.RemoveDefaultContextMenu();
- ContextMenu.RemoveObjectContextMenu();
- }
- // Réaction à l'ouverture d'un document => abonnement à l'évènement "BeginSave"
- private void OnDocumentCreated(object sender, DocumentCollectionEventArgs e)
- {
- e.Document.Database.BeginSave += new DatabaseIOEventHandler(OnSaveAlert);
- ShowMessage();
- }
- // Réaction à l'évènement "Database.BeginSave"
- private void OnSaveAlert(object sender, DatabaseIOEventArgs e)
- {
- ShowMessage();
- }
- // MessageBox
- private void ShowMessage()
- {
- int num = Dictionary.GetHiddenNumber();
- if (num != 0)
- {
- System.Windows.Forms.DialogResult dr =
- System.Windows.Forms.MessageBox.Show(
- num.ToString() + (fra ?
- " objets invisibles\nRestaurer la visibilite ?" :
- " invisible objects\nRestore visibility ?"),
- fra ? "Visibilité des entités" : "Objects visibility",
- System.Windows.Forms.MessageBoxButtons.YesNo,
- System.Windows.Forms.MessageBoxIcon.Question);
- if (dr == System.Windows.Forms.DialogResult.Yes)
- UnisolateObjects();
- }
- }
- // Commande HIDENT : rendre invisibles les entités sélectionnées
- [CommandMethod("HIDENT", CommandFlags.Modal | CommandFlags.UsePickSet)]
- public void HideObjects()
- {
- Document doc = docMan.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptSelectionOptions pso = new PromptSelectionOptions();
- pso.MessageForAdding = fra ? "Sélectionnez les objets à cacher: " : "\nSelect objects to hide: ";
- PromptSelectionResult psr = ed.GetSelection(pso);
- if (psr.Status == PromptStatus.OK)
- {
- try
- {
- int cnt = 0;
- ObjectId[] selSet = psr.Value.GetObjectIds();
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- foreach (ObjectId id in selSet)
- {
- Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite, false, true);
- ent.Visible = false;
- cnt++;
- }
- tr.Commit();
- }
- ed.WriteMessage(
- "\n{0} {1} (total : {2})",
- cnt.ToString(),
- fra ? "entités rendues invisibles" : "objects have been hidden",
- Dictionary.SetHiddenNumber(cnt).ToString());
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage((fra ? "\nErreur: " : "\nError: ") + ex.Message);
- }
- }
- }
- // Commande ISOLENT : isoler les entités sélectionnées (rendre invisibles les autres)
- [CommandMethod("ISOLENT", CommandFlags.Modal | CommandFlags.UsePickSet)]
- public void IsolateObjects()
- {
- Document doc = docMan.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptSelectionOptions pso = new PromptSelectionOptions();
- pso.MessageForAdding = fra ? "Sélectionnez les objets à isoler: " : "\nSelect objects to isolate";
- PromptSelectionResult psr = ed.GetSelection(pso);
- if (psr.Status == PromptStatus.OK)
- {
- try
- {
- int cnt = 0;
- ObjectId[] selSet = psr.Value.GetObjectIds();
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- foreach (ObjectId id in btr)
- {
- Entity ent = tr.GetObject(id, OpenMode.ForWrite, false, true) as Entity;
- if (ent != null)
- {
- ent.Visible = false;
- cnt++;
- }
- }
- foreach (ObjectId id in selSet)
- {
- Entity ent = tr.GetObject(id, OpenMode.ForWrite, false, true) as Entity;
- ent.Visible = true;
- cnt--;
- }
- tr.Commit();
- }
- ed.WriteMessage(
- "\n{0} {1} (total : {2})",
- cnt.ToString(),
- fra ? "entités rendues invisibles" : "objects have been hidden",
- Dictionary.SetHiddenNumber(cnt).ToString());
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage((fra ? "\nErreur: " : "\nError: ") + ex.Message);
- }
- }
- }
- // Commande : UNISOLENT : rendre visibles toutes les entités
- [CommandMethod("UNISOLENT")]
- public void UnisolateObjects()
- {
- Document doc = docMan.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- try
- {
- int cnt = 0;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- foreach (ObjectId id in btr)
- {
- Entity ent = tr.GetObject(id, OpenMode.ForWrite, false, true) as Entity;
- if ((ent != null) && (ent.Visible == false))
- {
- ent.Visible = true;
- cnt++;
- }
- }
- Dictionary.Erase();
- tr.Commit();
- }
- ed.WriteMessage(
- "\n{0} {1}",
- cnt.ToString(),
- fra ? "entités rendues visibles" : "objects have been unhidden");
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage((fra ? "\nErreur: " : "\nError: ") + ex.Message);
- }
- }
- // Commande INVIZ : inverser la visibilité des entités
- [CommandMethod("INVIZ")]
- public void InverseVisibility()
- {
- Document doc = docMan.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- try
- {
- int cnt1 = 0, cnt2 = 0;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- foreach (ObjectId id in btr)
- {
- Entity ent = tr.GetObject(id, OpenMode.ForWrite, false, true) as Entity;
- if (ent != null)
- {
- if (ent.Visible == true)
- {
- ent.Visible = false;
- cnt1++;
- }
- else
- {
- ent.Visible = true;
- cnt2++;
- }
- }
- }
- tr.Commit();
- }
- Dictionary.SetHiddenNumber(cnt1 - cnt2);
- ed.WriteMessage(
- "\n{0} {1} {2} {3})",
- cnt2.ToString(),
- fra ? "entités rendues visibles," : "objects have been unhidden,",
- cnt1.ToString(),
- fra ? "rendues invisibles" : "have been hidden");
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage((fra ? "\nErreur: " : "\nError: ") + ex.Message);
- }
- }
- }
- public class ContextMenu
- {
- private static ContextMenuExtension ocme, dcme;
- private static bool fra = (string)acadApp.GetSystemVariable("LOCALE") == "FRA";
- // Ajouts au menu contextuel objets
- internal static void AddObjectContextMenu()
- {
- ocme = new ContextMenuExtension();
- MenuItem hide = new MenuItem(fra ? "Cacher les objets" : "Hide objects");
- MenuItem isolate = new MenuItem(fra ? "Isoler les objets" : "Isolate objects");
- ocme.MenuItems.Add(hide);
- ocme.MenuItems.Add(isolate);
- hide.Click += new EventHandler(hide_Click);
- isolate.Click += new EventHandler(isolate_Click);
- RXClass rxc = Entity.GetClass(typeof(Entity));
- acadApp.AddObjectContextMenuExtension(rxc, ocme);
- }
- // Ajouts au menu contextuel par défaut
- internal static void AddDefaultContextMenu()
- {
- dcme = new ContextMenuExtension();
- dcme.Title = (fra ? "Visibilité des objets" : "Objects visibility");
- MenuItem hide = new MenuItem(fra ? "Cacher" : "Hide");
- MenuItem isolate = new MenuItem(fra ? "Isoler" : "Isolate");
- MenuItem inverse = new MenuItem(fra ? "Inverser" : "Toggle");
- MenuItem unisolate = new MenuItem(fra ? "Afficher tout" : "Unhide all");
- dcme.MenuItems.Add(hide);
- dcme.MenuItems.Add(isolate);
- dcme.MenuItems.Add(inverse);
- dcme.MenuItems.Add(unisolate);
- hide.Click += new EventHandler(hide_Click);
- isolate.Click += new EventHandler(isolate_Click);
- inverse.Click += new EventHandler(inverse_Click);
- unisolate.Click += new EventHandler(unisolate_Click);
- acadApp.AddDefaultContextMenuExtension(dcme);
- }
- // Suppression du menu contextuel par défaut
- internal static void RemoveDefaultContextMenu()
- {
- acadApp.RemoveDefaultContextMenuExtension(dcme);
- }
- // Suppression du menu contextuel objets
- internal static void RemoveObjectContextMenu()
- {
- RXClass rxc = Entity.GetClass(typeof(Entity));
- acadApp.RemoveObjectContextMenuExtension(rxc, ocme);
- }
- // Réaction au clic sur "Afficher tout"
- private static void unisolate_Click(object sender, EventArgs e)
- {
- Document doc = acadApp.DocumentManager.MdiActiveDocument;
- doc.SendStringToExecute("UNISOLENT ", true, false, true);
- }
- // Réaction au clic sur "Inverser"
- private static void inverse_Click(object sender, EventArgs e)
- {
- Document doc = acadApp.DocumentManager.MdiActiveDocument;
- doc.SendStringToExecute("INVIZ ", true, false, true);
- }
- // Réaction au clic sur "Isoler"
- private static void isolate_Click(object sender, EventArgs e)
- {
- Document doc = acadApp.DocumentManager.MdiActiveDocument;
- doc.SendStringToExecute("ISOLENT ", true, false, true);
- }
- // Réaction au clic sur "Cacher"
- private static void hide_Click(object sender, EventArgs e)
- {
- Document doc = acadApp.DocumentManager.MdiActiveDocument;
- doc.SendStringToExecute("HIDENT ", true, false, true);
- }
- }
- public class Dictionary
- {
- private const string kDict = "GILE_HIDENTS";
- private const string kXrec = "HiddenEntitiesNumber";
- // Ajout ou modification de l'entrée "HiddenEntitiesNumber" du dictionnaire "GILE_HIDENTS"
- internal static int SetHiddenNumber(int num)
- {
- Database db = acadApp.DocumentManager.MdiActiveDocument.Database;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
- DBDictionary dict;
- Xrecord xrec;
- ResultBuffer data;
- if (NOD.Contains(kDict))
- {
- dict = (DBDictionary)tr.GetObject(NOD.GetAt(kDict), OpenMode.ForRead);
- }
- else
- {
- NOD.UpgradeOpen();
- dict = new DBDictionary();
- NOD.SetAt(kDict, dict);
- tr.AddNewlyCreatedDBObject(dict, true);
- }
- if (dict.Contains(kXrec))
- {
- xrec = (Xrecord)tr.GetObject(dict.GetAt(kXrec), OpenMode.ForWrite);
- data = xrec.Data;
- num += (int)(data.AsArray())[0].Value;
- TypedValue[] tva = { new TypedValue((int)DxfCode.Int32, num) };
- data = new ResultBuffer(tva);
- }
- else
- {
- dict.UpgradeOpen();
- xrec = new Xrecord();
- dict.SetAt(kXrec, xrec);
- tr.AddNewlyCreatedDBObject(xrec, true);
- TypedValue[] tva = { new TypedValue((int)DxfCode.Int32, num) };
- data = new ResultBuffer(tva);
- }
- xrec.Data = data;
- tr.Commit();
- }
- return num;
- }
- // Récupération la valeur de l'entrée "HiddenEntitiesNumber" du dictionnaire "GILE_HIDENTS"
- internal static int GetHiddenNumber()
- {
- Database db = acadApp.DocumentManager.MdiActiveDocument.Database;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
- try
- {
- DBDictionary dict = (DBDictionary)tr.GetObject(NOD.GetAt(kDict), OpenMode.ForRead);
- Xrecord xrec = (Xrecord)tr.GetObject(dict.GetAt(kXrec), OpenMode.ForRead);
- ResultBuffer data = xrec.Data;
- return (int)(data.AsArray())[0].Value;
- }
- catch (System.Exception)
- {
- return 0;
- }
- }
- }
- // Suppression du dictionnaire "GILE_HIDENTS"
- internal static void Erase()
- {
- Database db = acadApp.DocumentManager.MdiActiveDocument.Database;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
- if (NOD.Contains(kDict))
- {
- DBDictionary dict = (DBDictionary)tr.GetObject(NOD.GetAt(kDict), OpenMode.ForWrite);
- dict.Erase(true);
- }
- tr.Commit();
- }
- }
- }
- }
|
|