AutoCAD.net: 实现AutoCAD Undo操作
在命令行输入"UNDO",会看到"Enter the number of operations to undo or [Auto/Control/BEgin/End/Mark/Back]",本文就利用"BEgin/End/Mark/Back”在ARX开发中实现Undo的操作。
1. Undo 操作的结构:
- _BEgin
- _Mark
- _Undo
- _End
2. 注意事项:
- 一组{_BEgin, _End}为一个操作单元,其内部不能再嵌套另一个{_BEgin, _End}。
- {_BEgin, _End}不能嵌在任何Transaction内部,但其内部可以嵌任意多个Transaction.
- 最好一个_Mark标识一个Transaction.
View Code
Code using System; using System.Collections.Generic; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.GraphicsInterface; using Autodesk.AutoCAD.Runtime; using ArxApp = Autodesk.AutoCAD.ApplicationServices.Application; using ObjectArxNet.Commons; namespace ObjectArxNet.Test { public class Undo { [CommandMethod("My_Undo")] public void InitCommand() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { //Use pick first PromptSelectionResult selectionRes = ed.SelectImplied(); bool usePickfirst = false; if (selectionRes.Status == PromptStatus.OK) { // If there was a pickfirst set, clear it ed.SetImpliedSelection(new ObjectId[0]); ObjectId[] objIds = selectionRes.Value.GetObjectIds(); if (objIds != null) { usePickfirst = objIds.Length > 0; } } //Selection Options PromptSelectionOptions selOpt = new PromptSelectionOptions(); selOpt.MessageForAdding = Consts.KEY_SELECT; selOpt.MessageForRemoval = Consts.KEY_SELECT; //Keyword Options PromptKeywordOptions optManual = new PromptKeywordOptions("\nSelect aligning mode"); optManual.Keywords.Add(Consts.KEY_AUTO_SELECT, Consts.KEY_AUTO_SELECT, Consts.KEY_AUTO_SELECT_DIS); optManual.Keywords.Add(Consts.KEY_INSERT, Consts.KEY_INSERT, Consts.KEY_INSERT_DIS); optManual.Keywords.Add(Consts.KEY_UNDO, Consts.KEY_UNDO, Consts.KEY_UNDO); optManual.Keywords.Add(Consts.KEY_EXIT, Consts.KEY_EXIT, Consts.KEY_EXIT); optManual.AllowNone = false; optManual.Keywords.Default = Consts.KEY_AUTO_SELECT; PromptResult res = null; if (usePickfirst) { Tools.RunCommand("_.UNDO", "_BEgin"); res = ed.GetKeywords(optManual); } else { Tools.RunCommand("_.UNDO", "_BEgin"); PromptSelectionResult selRes = ed.GetSelection(selOpt); //, filter if (selRes.Status == PromptStatus.OK) { ObjectId[] objIds = selRes.Value.GetObjectIds(); if (objIds != null) { res = ed.GetKeywords(optManual); } } } while (res != null && res.Status == PromptStatus.OK) { switch (res.StringResult) { case Consts.KEY_AUTO_SELECT: { Tools.RunCommand("_.UNDO", "_Mark"); Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_AUTO_SELECT)); res = ed.GetKeywords(optManual); break; } case Consts.KEY_INSERT: { Tools.RunCommand("_.UNDO", "_Mark"); Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_INSERT)); res = ed.GetKeywords(optManual); break; } case Consts.KEY_REMOVE: { Tools.RunCommand("_.UNDO", "_Mark"); Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_REMOVE)); res = ed.GetKeywords(optManual); break; } case Consts.KEY_UNDO: { Tools.RunCommand("_.UNDO", "_Back"); Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_UNDO)); doc.TransactionManager.QueueForGraphicsFlush(); res = ed.GetKeywords(optManual); break; } case Consts.KEY_EXIT: { Tools.RunCommand("_.UNDO", "_End");//end the last Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_EXIT)); doc.TransactionManager.QueueForGraphicsFlush(); Tools.RunCommand("_.UNDO", "_BEgin");//begin the next PromptSelectionResult selRes = ed.GetSelection(selOpt); //, filter if (selRes.Status == PromptStatus.OK) { ObjectId[] objIds = selRes.Value.GetObjectIds(); if (objIds != null) { res = ed.GetKeywords(optManual); } } else { return; } break; } default: { doc.TransactionManager.QueueForGraphicsFlush(); res = ed.GetKeywords(optManual); break; } } } } finally { Tools.RunCommand("_.UNDO", "_End"); ed.SelectionAdded -= new SelectionAddedEventHandler(callback_SelectionAdded); } } } }