How to UndoRecord in wpf viewModel?

Hi,everyone.

  • When my Command inherit Rhino.Command, It worked well .
    but use wpf mvvm pattern . i can’t undoRecord.
    this is my code, where did it go wrong?
private void PickPoint()
        {
            _sn = PlugInServer.Doc.BeginUndoRecord(nameof(PutTextViewModel));//Start Record

           
            _wire = null;
            _wirePoints = new List<Point3d>();
            _isCommandRunning = true;
            PickPointCmd.NotifyCanExecuteChanged();

            var get = new GetPoint();
            get.SetCommandPrompt("Selected Location<Esc Exit>");
            get.DynamicDraw += DynamicDraw;

            while (true)
            {
                if (_constraintSurface != null)
                {
                    get.Constrain(_constraintSurface, false);
                }
                var gr = get.Get();

                if (get.CommandResult() != Rhino.Commands.Result.Success)
                {
                    break;
                }
                if (gr == Rhino.Input.GetResult.Point)
                {
                    CreateText(get.Point());
                    _wirePoints.Add(get.Point());
                    CreateWire();
                }
            }

            _isCommandRunning = false;
            PickPointCmd.NotifyCanExecuteChanged();

            if (IsCreateWire)
            {
                if (_wire == null) return;
                PlugInServer.Doc.Objects.AddCurve(_wire);
            }

            PlugInServer.Doc.Views.Redraw();
            PlugInServer.Doc.EndUndoRecord(_sn);//End Record
        }

Hi @yangf85,

Without a project we can run, it’s pretty hard to tell what’s wrong. But this is the pattern you should be following:

public class TestViewModel : Rhino.UI.ViewModel
{
  public TestViewModel(uint documentSerialNumber)
  {
    DocumentSerialNumber = documentSerialNumber;
  }

  private void DoSomething()
  {
    var doc = RhinoDoc.FromRuntimeSerialNumber(DocumentSerialNumber);
    if (null != doc)
    {
      var undoSerialNumber = doc.BeginUndoRecord("Do something");

      // TODO: make a change to the document

      if (undoSerialNumber > 0)
        doc.EndUndoRecord(undoSerialNumber);
    }
  }

  public uint DocumentSerialNumber { get; private set; }
}

Note, Rhino command do all this for you. That is, you should only use RhinoDoc.BeginUndoRecord and RhinoDoc.EndUndoRecord outside of a command and in some kind of modeless user interface.

– Dale