0

After defining a user command, when entering a user command in autocad, if the user command entered using the CommandWillStart event handler is the same as the dictionary key value, the GetDistanceBetweenTwoPoints() method is called, but there is no response in the CommandWillStart event handler part. I don't know what the problem is

using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Newtonsoft.Json;

namespace Autocad_SetShortcutKey_005
{
public class CommandConfig
{
public string Name { get; set; }
public string Subname { get; set; }
public string Method { get; set; }
}

    public class CommandMappings
    {
        public List<CommandConfig> Commands { get; set; }
    }
    
    public class SetShortcutKeyClass : IExtensionApplication
    {
        private Dictionary<string, Action> commandDictionary;
        Document doc;
        Editor ed;
    
        public SetShortcutKeyClass()
        {
            doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            ed = doc.Editor;
        }
        public void Initialize()
        {
            // Load commands from JSON
            LoadCommands();
    
            // Register for the CommandWillStart event
           
            doc.CommandWillStart += new CommandEventHandler(OnCommandWillStart);
            
        }
    
        public void Terminate()
        {
            // Unsubscribe from the CommandWillStart event
    
            doc.CommandWillStart += new CommandEventHandler(OnCommandWillStart);
            
        }
    
        private void LoadCommands()
        {
            commandDictionary = new Dictionary<string, Action>();
    
            // Read JSON file
            string jsonFilePath = "commands.json";
            if (File.Exists(jsonFilePath))
            {
                
                string jsonContent = File.ReadAllText(jsonFilePath);
                var commandMappings = JsonConvert.DeserializeObject<CommandMappings>(jsonContent);
    
                foreach (var commandConfig in commandMappings.Commands)
                {
                    e
                    if (commandConfig.Method == nameof(GetDistanceBetweenTwoPoints))
                    {
                        commandDictionary[commandConfig.Name.ToUpper()] = GetDistanceBetweenTwoPoints;
                        commandDictionary[commandConfig.Subname.ToUpper()] = GetDistanceBetweenTwoPoints;
                    }
                }
            }
            else
            {
                ed.WriteMessage("\n No Json file.");
                return;
            }
        }
    
        private void OnCommandWillStart(object sender, CommandEventArgs e)
        {
            
            string commandName = e.GlobalCommandName.ToUpper();
            
            if (commandDictionary.ContainsKey(commandName))
            {
                
              
                // Execute the custom method
                commandDictionary[commandName].Invoke();
                return;
            }
        }
    
        public  void GetDistanceBetweenTwoPoints()
        {
            PromptPointOptions ppo1 = new PromptPointOptions("\nPick the first point: ");
            PromptPointResult ppr1 = ed.GetPoint(ppo1);
            if (ppr1.Status != PromptStatus.OK) return;
    
            PromptPointOptions ppo2 = new PromptPointOptions("\nPick the second point: ");
            PromptPointResult ppr2 = ed.GetPoint(ppo2);
            if (ppr2.Status != PromptStatus.OK) return;
    
            double distance = ppr1.Value.DistanceTo(ppr2.Value);
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog($"\nDistance between points: {distance}");
        }
    }

}

commands.json // file contents

{
"commands": \[
{
"name": "TPX",
"subname": "TP1",
"method": "GetDistanceBetweenTwoPoints"
}
\]
}

When I type TPX orTP1 in the autocad command line, I want the GetDistanceBetweenTwoPoints() method to be called using an event handler.

I don't want it this way

\[CommandMethod("TPX")\]
\[CommandMethod("TP1")\]
public  void GetDistanceBetweenTwoPoints()
{
// Code content to enter
}
5
  • Where are you registering the event with '+=' Commented Jun 23, 2024 at 11:31
  • @jdweng Register at Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Commented Jun 23, 2024 at 15:36
  • That line does not register. Where is the '+='? Are you getting to the line of code where event is being registered? Commented Jun 23, 2024 at 20:15
  • public void Initialize() { // Load commands from JSON LoadCommands(); // Register for the CommandWillStart event doc.CommandWillStart += new CommandEventHandler(OnCommandWillStart); } When netloading a code file, isn't the CommandWillStart event triggered by the Initialize() method? Commented Jun 24, 2024 at 1:25
  • I was looking at you first comment "Register at ......". Set break points and verify you are getting to the code to register. Commented Jun 24, 2024 at 8:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.