Thursday, July 26, 2018

Custom Command Showing Or Not Showing Dialog Box - 2 of 2

This is the second post on the topic of showing or not showing dialog box within custom command execution. The part 1 is here.

While an AutoCAD operation started with a command often needs to deal with files, thus needs file path/name to be inputted into the operation process with File Open/Save dialog box or at command line, even more often, the operation needs other data inputs than file path/name. Depending on the data input requirements, dialog box is used as more user-friendly UI and better input quality control means. However, in order to make a command script-able, a version of the same command often exists that collects all inputs from command line. The convention is to prefix "-" the regular command name as that command's command-line-only version. So, when we do create our own custom command with CommandMethod class, we should also try to follow this convention, if we want our custom command to be script-able.

Below is the code example:

[CommandMethod("DoThis")]
[CommandMethod("-DoThis")]
[CommandMethod("DiaDoThis")]
public static void DoSomething()
{
    var dwg = CadApp.DocumentManager.MdiActiveDocument;
    var ed = dwg.Editor;
 
    var cmd = CadApp.GetSystemVariable("CMDNAMES").ToString().ToUpper();
    var showDialog = !cmd.StartsWith("-") && !cmd.StartsWith("DIA");
 
    var userInput = showDialog ?
        GetInputFromDialog() :
        GetInputFromCommandLine();
 
    string msg;
    if (userInput=="YES")
        msg = $"The command proceeded {(showDialog?"with dialog box input":"with command line input")}.";
    else
        msg = $"The command cancelled {(showDialog ? "with dialog box input" : "with command line input")}.";
 
    CadApp.ShowAlertDialog(msg);
}
 
private static string GetInputFromDialog()
{
    var msg = "The process needs you to answer:\n\n" +
        "YES to continue\n" +
        "NO to caancel\n\n" +
        "Do you want to continue?";
    var res = System.Windows.Forms.MessageBox.Show(
        msg, "My Dialog Box",
        System.Windows.Forms.MessageBoxButtons.YesNo,
        System.Windows.Forms.MessageBoxIcon.Question,
        System.Windows.Forms.MessageBoxDefaultButton.Button1);
 
    return res == System.Windows.Forms.DialogResult.Yes ? "YES" : "NO";
}
 
private static string GetInputFromCommandLine()
{
    var ed = CadApp.DocumentManager.MdiActiveDocument.Editor;
    var input = "NO";
 
    var opt = new PromptKeywordOptions(
        "\nContinue the process?");
    opt.AppendKeywordsToMessage = true;
    opt.Keywords.Add("Yes");
    opt.Keywords.Add("No");
    opt.Keywords.Default = "Yes";
 
    var res = ed.GetKeywords(opt);
    if (res.Status== PromptStatus.OK)
    {
        input = res.StringResult.ToUpper();
    }
 
    return input;
}

The code is really simple, where I just used a message box, as a dialog box, to collect user input, but the interesting part is the multiple CommandMethodAttribute used to decorate the same methods as multiple custom commands. And notice that I also prefixed one of the command name with "Dia", implying it is dialog box version of the command, instead of prefixing the command with "-". This is just meant to show how I can use the different command name to run different version of the command, dialog box version, or command line version.

See this video clip for the code in action.

3 comments:

Unknown said...

SW 2D drawings convert readily to several 2D formats including .dwg & .dwf (AC); while the 3D models also convert to several different formats like .stp & .igs files. My advise to you is to do as I did learn both a 2D & 3D software. As a side note solidworks is NOT very popular in automotive design.

An Introduction to the Profession of Social Work Becoming a Change Agent Segal 3 test bank & solutions manual

iron said...

INTERESTING!!
GOOD CONTENT
CAD to BIM conversion in Uk

Abhi said...

Thanks for info
autocad drafting












Followers

About Me

My photo
After graduating from university, I worked as civil engineer for more than 10 years. It was AutoCAD use that led me to the path of computer programming. Although I now do more generic business software development, such as enterprise system, timesheet, billing, web services..., AutoCAD related programming is always interesting me and I still get AutoCAD programming tasks assigned to me from time to time. So, AutoCAD goes, I go.