在命令行输入"UNDO",会看到"Enter the number of operations to undo or [Auto/Control/BEgin/End/Mark/Back]",本文就利用"BEgin/End/Mark/Back”在ARX开发中实现Undo的操作。
1. Undo 操作的结构:
2. 注意事项:
- 一组{_BEgin, _End}为一个操作单元,其内部不能再嵌套另一个{_BEgin, _End}。
- {_BEgin, _End}不能嵌在任何Transaction内部,但其内部可以嵌任意多个Transaction.
- 最好一个_Mark标识一个Transaction.
3. SampleCode
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ObjectArxNet.Test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Undo
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[CommandMethod("My_Undo")]
public void InitCommand()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Use pick first
PromptSelectionResult selectionRes = ed.SelectImplied();
bool usePickfirst = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (selectionRes.Status == PromptStatus.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// If there was a pickfirst set, clear it
ed.SetImpliedSelection(new ObjectId[0]);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ObjectId[] objIds = selectionRes.Value.GetObjectIds();
if (objIds != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
usePickfirst = objIds.Length > 0;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Selection Options
PromptSelectionOptions selOpt = new PromptSelectionOptions();
selOpt.MessageForAdding = Consts.KEY_SELECT;
selOpt.MessageForRemoval = Consts.KEY_SELECT;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//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;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
PromptResult res = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (usePickfirst)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_BEgin");
res = ed.GetKeywords(optManual);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_BEgin");
PromptSelectionResult selRes = ed.GetSelection(selOpt); //, filter
if (selRes.Status == PromptStatus.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ObjectId[] objIds = selRes.Value.GetObjectIds();
if (objIds != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
res = ed.GetKeywords(optManual);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (res != null && res.Status == PromptStatus.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (res.StringResult)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case Consts.KEY_AUTO_SELECT:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_Mark");
Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_AUTO_SELECT));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
res = ed.GetKeywords(optManual);
break;
}
case Consts.KEY_INSERT:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_Mark");
Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_INSERT));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
res = ed.GetKeywords(optManual);
break;
}
case Consts.KEY_REMOVE:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_Mark");
Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_REMOVE));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
res = ed.GetKeywords(optManual);
break;
}
case Consts.KEY_UNDO:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_Back");
Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_UNDO));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
doc.TransactionManager.QueueForGraphicsFlush();
res = ed.GetKeywords(optManual);
break;
}
case Consts.KEY_EXIT:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_End");//end the last
Tools.WriteLine(string.Format("DO: {0}", Consts.KEY_EXIT));
doc.TransactionManager.QueueForGraphicsFlush();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Tools.RunCommand("_.UNDO", "_BEgin");//begin the next
PromptSelectionResult selRes = ed.GetSelection(selOpt); //, filter
if (selRes.Status == PromptStatus.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ObjectId[] objIds = selRes.Value.GetObjectIds();
if (objIds != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
res = ed.GetKeywords(optManual);
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
break;
}
default:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
doc.TransactionManager.QueueForGraphicsFlush();
res = ed.GetKeywords(optManual);
break;
}
}
}
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Tools.RunCommand("_.UNDO", "_End");
ed.SelectionAdded -= new SelectionAddedEventHandler(callback_SelectionAdded);
}
}
}
}