Create Command with Call options

Create Command with Call options

jan_tappenbeck
Collaborator Collaborator
2,552 Views
8 Replies
Message 1 of 9

Create Command with Call options

jan_tappenbeck
Collaborator
Collaborator

hi !

 

i want to create functions in vb.net to make some aktion and there should be different ways in use - control by optionalparameters.

 

i know the way to call a function direct:

 <Autodesk.AutoCAD.Runtime.CommandMethod("TEST", CType(2097155, Autodesk.AutoCAD.Runtime.CommandFlags))> _
    Public Shared Sub TEST()

 

my idea is to put following into commandline - like Lisp-call

 

test Option1 Option2

 

or

 

(test Option1 Option2)

 

Could someone help me?

 

regards Jan

0 Likes
Accepted solutions (1)
2,553 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

I think the key is to understand that once you press 'TEST ' (with the trailing space) the command is launched and you are already inside your function.

From there you must mimic the options management.

If your command will have a graphics interaction, like getting a point, the best solution is to set keyword options in the same prompt, because it will work exactly as AutoCAD native commands.

For Example, here I want to select a polyline, but also allow for keyword actions:

 

            // set defaults
            PromptDoubleOptions optVal = new PromptDoubleOptions("");
            PromptDoubleResult res_double;
            PromptEntityResult res;

            while (true)
            {
                String szMessage = String.Format("\nSelect polyline or [Radius={0}/Width={1}/Xhatch={2}/Keep={3}]:",
                                                 dRad, dThick, (bDrawHatch ? "S" : "N"), (bKeepPline ? "S" : "N"));

                // get Polyline or keywords
                PromptEntityOptions opt = new PromptEntityOptions(szMessage, "Radius Width Xhatch Keep");
                opt.SetRejectMessage("** not a polyline **");
                opt.AddAllowedClass(typeof(Polyline), true);
                res = ed.GetEntity(opt);

                if (res.Status == PromptStatus.OK)
                {
                    // execute the command with current parameters
                    if (DoLamiere(res.ObjectId, dRad, dThick, bDrawHatch, false, bKeepPline) == true)
                    {
                        csProfili.SaveCurrentParameters(csProfili.SECT_LAMIERE, dRad, dThick, 0.0, bDrawHatch, bKeepPline, 0);
                    }
                }
                else if (res.Status == PromptStatus.Keyword)
                {
                    switch (res.StringResult.ToUpperInvariant())
                    {
                        case "RADIUS":
                            // ask for new fillet radius
                            optVal.Message = "\nInternal bending radius";
                            optVal.AllowZero = true;
                            optVal.DefaultValue = dRad;
                            optVal.UseDefaultValue = true;
                            res_double = ed.GetDouble(optVal);
                            if (res_double.Status == PromptStatus.OK) dRad = res_double.Value;

                            break;

                        case "WIDTH":
                            // ask for new width
                            optVal.Message = "\nWidth";
                            optVal.AllowZero = false;
                            optVal.DefaultValue = dThick;
                            optVal.UseDefaultValue = true;
                            res_double = ed.GetDouble(optVal);
                            if (res_double.Status == PromptStatus.OK) dThick = res_double.Value;

                            break;

                        case "XHATCH":
                            // Flag: draw xh or not
                            bDrawHatch = !bDrawHatch;
                            break;

                        case "KEEP":
                            // Flag: keep original polyline or discard
                            bKeepPline = !bKeepPline;
                            break;
                    }
                }
                else
                    break;  // command aborted, exit main loop
            }

If you use JIG function, keywords are also available, see JigPromptPointOptions.SetMessageAndKeywords().

If you don't have an interactive session, I'm afraid you should mimic a text input and process it. The only not-standard behavior is that for empty parameters you need an additional space or enter to actually run the command. Maybe you may display default values to let the user know he/she needs another Enter to actually start the command.

 

 

0 Likes
Message 3 of 9

jan_tappenbeck
Collaborator
Collaborator

Hi!

 

thanks for answer.

 

Your idea is to allow options in the process of a function!

 

i search a way to call a funktion with options - here a example by lisp:

 

(defun myfunktion ( option1 option2 / )

.....

this will be call in commandline by

 

 

(myfunktion 15 52)

 

did you understand?

 

regards Jan

0 Likes
Message 4 of 9

Anonymous
Not applicable

So, you have fixed number of options and fixed position? Something like '_MYCMD 100 50 ' ?

 

With NET I would just add a sequence of inputs as soon as my command starts, something like:

 

            // ask for mandatory parameters
            PromptDoubleOptions optVal = new PromptDoubleOptions();

            // ask for length
            optVal.Message = "\nLength"
            optVal.AllowZero = false;
            optVal.DefaultValue = dWidth;
            optVal.UseDefaultValue = true;
            PromptDoubleResult res_wdt = ed.GetDouble(optVal);
            if (res_wdt.Status != PromptStatus.OK) return false;

            // ask for Height
            optVal.Message = "\nHeight";
            optVal.AllowZero = false;
            optVal.DefaultValue = dHeight;
            optVal.UseDefaultValue = true;
            PromptDoubleResult res_hgt = ed.GetDouble(optVal);
            if (res_wdt.Status != PromptStatus.OK) return false;

            // here res_wdt and res_hgt are valid and contain a value

 

0 Likes
Message 5 of 9

jan_tappenbeck
Collaborator
Collaborator

Hi !

 

yes, you ask the two parameter by userinput  - but i want to transfer the parameters by call function.

 

reagards Jan

0 Likes
Message 6 of 9

_gile
Consultant
Consultant

Hi

 

A CommandMethod attributed method (i.e. a custom AutoCAD command) cannot have parameters.

But you can use a LispFunction attributed method (i.e. a custom AutoLISP function) which requires a ResultBuffer as parameter, the ResultBuffer may contain the LISP function parameters.

You can google for 'AutoCAD .NET LispFunction'



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 9

Anonymous
Not applicable
Accepted solution

Sorry Jan,

maybe I did not explained myself in my first message: you cannot let AutoCAD to handle all the input and THEN call your function with all parameters. As soon as you type 'TEST ' (and the following space), the control is handled to your function, that MUST handle the following user interaction.

 

With .NET this is a normal scenario, but even if you handle the input by yourself, the whole command will works as a standard command, by manual typing, by script, by UI buttons, whatever!

So, actually is a better behavior, since you have complete control over it.

 

0 Likes
Message 8 of 9

fieldguy
Advisor
Advisor

You can simulate a .net command call with arguments if you use an autocad macro.

 

macro

^C^Cdotnetcommandmethod;PARAM1,PARAM2; 

 

.net

[CommandMethod("dotnetcommandmethod")

public void dotnetcommandmethod()

{

acapp.Document doc = acapp.Core.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptStringOptions pso = new PromptStringOptions("");
PromptResult pr = ed.GetString(pso);
>> pr.StringResult will contain "PARAM1,PARAM2"

<edit>

string[] myparams = pr.StringResult.Split(',');

</edit>

  

Perhaps you could use something like that?  I have macros assigned to autocad ribbon buttons that implement this method.

 

0 Likes
Message 9 of 9

jan_tappenbeck
Collaborator
Collaborator

hi !

 

thanks for this other options - i will try.

 

regards Jan

0 Likes