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
}
Yongtae Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.