Nothing to Undo(c#)

I want to make possible using “Undo” while running this plugin.
However, whenever I press Ctrl+Z or write “Undo”. Rhino always say “Nothing to Undo.”
Is anybody know the reasons or solutions? I need your help. I’m using Rhino7. Visual studio2022, and c#.

using Eto.Forms;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Rhino.Input.Custom;
using Rhino.PlugIns;
using System;
using System.Collections.Generic;
using System.IO;

namespace MyRhinoPlugin7
{
    [Rhino.Commands.CommandStyle(Rhino.Commands.Style.ScriptRunner)]
    public class TestPluginCommand : Rhino.Commands.Command
    {
        static List<string> commandList = new List<string>();  // 사용자의 명령을 저장하는 리스트

        public override string EnglishName
        {
            get { return "Test"; }  
        }

        protected override Rhino.Commands.Result RunCommand(Rhino.RhinoDoc doc, Rhino.Commands.RunMode mode)
        {
            DateTime startTime = DateTime.Now;
            var id = doc.BeginUndoRecord("Start transformation");

            while (true)
            {
                string commandInput = string.Empty;

                GetString getString = new GetString();
                getString.SetCommandPrompt("Enter a command (or 'result' to finish):");
                getString.AcceptNothing(true);
                getString.Get();

                commandInput = getString.StringResult().Trim();

                if (commandInput.Equals("\n", System.StringComparison.OrdinalIgnoreCase))
                {
                    // 입력이 비어 있을 때 Select 명령을 실행
                    commandInput = "_Select";
                    RhinoApp.WriteLine("Select command executed.");
                }
                /*else if (commandInput.Equals("undo", System.StringComparison.OrdinalIgnoreCase))
                {
                    // "undo"가 입력되면 실행 취소 트랜잭션을 시작
                    doc.BeginUndoRecord(commandInput);
                }*/
                else if (commandInput.Equals("result", System.StringComparison.OrdinalIgnoreCase))
                {
                    // 'result'가 입력되면 로그를 파일로 저장하고 반복을 종료합니다.
                    SaveCommandsToFile();
                    doc.EndUndoRecord(id); // 루프 종료 시 실행 취소 트랜잭션을 종료
                    break;
                }

                // 명령 실행
                bool result = Rhino.RhinoApp.RunScript(commandInput, false);

                if (result)
                {
                    TimeSpan executionTime = DateTime.Now - startTime;
                    string formattedTime = $"{executionTime.Hours:D2}:{executionTime.Minutes:D2}:{executionTime.Seconds:D2}";
                    RhinoApp.WriteLine("Command result: " + result);
                    RhinoApp.WriteLine("Command execution time: " + formattedTime);
                    commandList.Add("Command: " + commandInput + " - Execution Time: " + formattedTime);
                }
            }

            return Rhino.Commands.Result.Success;
        }




        private void SaveCommandsToFile()
        {
            // 명령 리스트를 텍스트 파일로 저장합니다.
            string path = "C:\\Users\\jjang\\OneDrive\\바탕 화면\\plugin_test_1007\\test\\test1016.txt"; // 파일 경로를 여기에 지정하세요.

            using (StreamWriter sw = new StreamWriter(path, append: false))
            {
                foreach (string command in commandList)
                {
                    sw.WriteLine(command);
                }
            }

            RhinoApp.WriteLine($"Commands saved to file: {path}");
        }
    }
}

Hi @장유경

You need to end the command to have an undo record. Also, within a command, you don’t need to use BeginUndoRecord().

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

You mean if I want to use undo then the ‘test’ comand should be over?

1 Like

Yes, if you want to use the automatic command undo stack.

1 Like

Thanks for your answer.

I want to record the command names that user used with the execution time.
However, if user press Ctrl+z, then Rhino says "Nothing to undo.’
What can I do…? Do you know any solutions?