CommandWillStart handler query in c# autocad api

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
}

New contributor

Yongtae Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật