Making Rhino Plugin for recording commands

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 TrackCommandsPluginCommand : Rhino.Commands.Command
    {
        static List<string> commandList = new List<string>();  // 사용자의 명령을 저장하는 리스트

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

        protected override Rhino.Commands.Result RunCommand(Rhino.RhinoDoc doc, Rhino.Commands.RunMode mode)
        {
            DateTime StartTime = DateTime.Now;
            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();

                bool result = Rhino.RhinoApp.RunScript(commandInput, false);
                DateTime endTime = DateTime.Now;

                if (string.IsNullOrEmpty(commandInput))
                {
                    // 입력이 비어 있을 때 Select 명령을 실행
                    commandInput = "_Select";
                    RhinoApp.WriteLine("Select command executed.");
                }

                if (commandInput.Equals("result", System.StringComparison.OrdinalIgnoreCase))
                {
                    // 'result'가 입력되면 로그를 파일로 저장하고 반복을 종료합니다.
                    SaveCommandsToFile();
                    break;
                }


                    // 명령 실행 결과 출력
                    if (result)
                {
                    TimeSpan executionTime = endTime - 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}");
        }
    }
}

If the user does not enter ‘command’ and selects an object, I want to make ‘select’ run. But that’s not how it works. And, why won’t ‘undo’ run while running this plug-in? Does anyone know a solution?

The goal of this plug-in is to record the command you run and the time you run the command. I’m using c#, Visual Studio 2022, and Rhino 7.

Please help…

1 Like

There are a couple events you can add handlers to, to be notified when a Command Starts/Ends in Rhino.

I would recommend you wire to these events, and maybe create 2 commands, one to wire up, and one to disconnect from the events to start/stop tracking of commands.

image

We use this event to track our internal commands, to get a usage count of builders.

Your command above is running in a loop, Instead your command to start/stop tracking should do just that and end, so that users can run other “commands”.

I’d also recommand moving your logic into a static class and set a flag on command end when you want to trigger a save of the commands.

You can also wire to the Rhino Idle event, check your flag, and if needed do the save there. Using the Idle event will keep Rhino feeling responsive, and not interrupt users while running a command.

image

1 Like