Make a command with parameter

Make a command with parameter

Anonymous
Not applicable
9,418 Views
11 Replies
Message 1 of 12

Make a command with parameter

Anonymous
Not applicable

Dear All,

I want transmit a outside parameter to CAD, my thinking is like following :

//outside code

class xxxxx

{

//outside code

string parameter="ParameterToTransmit";  

 

document acdoc.SendCommand("MyCommand ",ParameterToTransmit😞

}

 

//CAD command

 [CommandMethod("MyCommand ")]
        public static void MyCommand (ParameterToTransmit)
        {

        //

         }

But i don't know how to make a command with parameter .

Is  it possible to create a command with parameter ?

Or is  it have other solution? 

 

0 Likes
Accepted solutions (1)
9,419 Views
11 Replies
Replies (11)
Message 2 of 12

fieldguy
Advisor
Advisor
0 Likes
Message 3 of 12

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You cannot define a command with a parameter (the method attributed with the CommandMethod attribute must return void).

 

You can create a command with an user prompt and call the command passing ot the input.

 

        [CommandMethod("MyCommand")]
        public void MyCommand()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var promptResult = ed.GetString("\nEnter the parameter: ");
            if (promptResult.Status != PromptStatus.OK)
                return;

            // Do what you want with the parameter
        }

 

Then, you can hard code the call to the command with your parameter:

 

 

Using AcAdDocument.SendCommand (COM API)

 

acdoc.SendCommand("MyCommand " + parameter + "\n");

 

Using Document.SendstringToExecute (.NET API)

 

doc.SendStringToExecute("MyCommand " + parameter + "\n", false, false, false);

 

Using Editor.Command()

 

ed.Command("MyCommand", parameter);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 12

Anonymous
Not applicable

Thanks Sir!

Before i have try this way, it failed, but now it sucess, thank you.

0 Likes
Message 5 of 12

Anonymous
Not applicable

Dear Sir,

May be you are right, but it too trouble for me(i'm not a Professional).

So I accepted solution from Mr _gile . Still very thank you for you help!

0 Likes
Message 6 of 12

foxever
Explorer
Explorer

hi, gile

I'm trying to load  some scripts into cad to draw some lines.

 

Autocad 2018
.NET framework 4.6.1
C#

 

 

I've tried to use (with FILEDIA set to 0)

 

 

[CommandMethod("BATCHSCR")]
public void BATCHSCR()
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = acDoc.Editor;
    ed.Command(".script","D:\\123.scr\n ");
    ed.Command(".script","D:\\234.scr\n ");
}

 

 

only commands in the second script has been executed.

 

and

 

 

[CommandMethod("BATCHSCR")]
public void BATCHSCR()
{
    string path1 = @"D:\123.scr";
    string path2 = @"D:\234.scr";
    acDoc.SendStringToExecute(".script " + path1 + "\n", false, false, false);
    acDoc.SendStringToExecute(".script " + path2 + "\n", false, false, false);
}

 

 

only commands in the first script has been executed.

Is  it possible to load scripts in one customize command ?

Or is  it have other solution? 

123.scr :

 

(setq old-osmode(getvar "osmode"))
_osmode 0
_units
2
2
3
4
0
_y
_DIMAUNIT
2
_DIMUNIT
2


_-layer
_n
220427__20220427_2_Report
_c
_red
220427__20220427_2_Report
_s
220427__20220427_2_Report

_POINT
1387480.930000,2623185.220000,0.000000
_POINT
1387602.000000,2623185.220000,0.000000
_-layer
_n
220427__20220427_2_Report
_c
_red
220427__20220427_2_Report
_s
220427__20220427_2_Report

_3DPOLY
1387480.930000,2623185.220000,0.000000
1387602.000000,2623185.220000,0.000000

(setvar "osmode" old-osmode)

 

 

234.scr

 

(setq old-osmode(getvar "osmode"))
_osmode 0
_units
2
2
3
4
0
_y
_DIMAUNIT
2
_DIMUNIT
2


_-layer
_n
220427__20220427_1_Report
_c
_red
220427__20220427_1_Report
_s
220427__20220427_1_Report

_POINT
1387525.930000,2623185.220000,0.000000
_POINT
1387550.000000,2623103.550000,0.000000
_-layer
_n
220427__20220427_1_Report
_c
_red
220427__20220427_1_Report
_s
220427__20220427_1_Report

_3DPOLY
1387525.930000,2623185.220000,0.000000
1387550.000000,2623103.550000,0.000000

(setvar "osmode" old-osmode)

 

 

0 Likes
Message 7 of 12

_gile
Consultant
Consultant

Using .NET to launch an AutoCAD script is like chasing mosquitoes with a bazooka.
That said, with the Editor.Command() method, you don't have to add a validation after the script file name.

ed.Command(".script", "D:\123.scr");


You can test arguments of Editor.Command() method more simply by using LISP command function directly from AutoCAD:

(command ".script" "D:\123.scr")

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 12

foxever
Explorer
Explorer

Using .NET to launch an AutoCAD script is like chasing mosquitoes with a bazooka.
It's a funny  anology.

 

The scenerio is Program A outputs one script file which contains commands of drawing points and lines for each project. 

Script files are placed in different subfolders named with project name.

And I wanna write a plugin to traverse the main folder  to load all the script files into one dwg file.

 

There is no need of '\n' in parameter when using ed.Command().

ed.Command(".script", "D:\\123.scr");

It works well without "\n".

 

There is another problem, when using the following codes, the second line will not be executed.

ed.Command(".script", "D:\\123.scr");
ed.Command(".zoom", "e");

 And there is even no history command (ZOOM E) in autocad command window.

The last command in command window is the last line of script file.

0 Likes
Message 9 of 12

_gile
Consultant
Consultant

Does your script file properly ends? IOW is there a validation (space or carriage return) after the (setvar "osmode" old-osmode) LISP expression.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 12

foxever
Explorer
Explorer

Uploading scr file is not supported,  file extension change to txt.

This is a  new line after the (setvar "osmode" old-osmode). 

This file seems using LF(Unix) as the line break.

0 Likes
Message 11 of 12

_gile
Consultant
Consultant

It looks like running a script from .NET is equivalent to call SendStringToExecute passing it the whole .scr file content as argument. This is probalby why it runs asynchronously.

You can try this way;

            var doc = Application.DocumentManager.MdiActiveDocument;
            string command = File.ReadAllText("D:\\123.scr");
            command += File.ReadAllText("D:\\234.scr");
            command += "_zoom\n_Extents\n";
            doc.SendStringToExecute(command, false, false, true);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 12 of 12

foxever
Explorer
Explorer

Graceful solution!

It works perfect. Thank u so much.

 

 

During the progress, unit setting window will prompt twice. Solved by adding "-" between "_" and "UNITS".

Reference:

AutoCAD help files.

A script can execute any command at the Command prompt except a command that displays a dialog box. In most cases, a command that displays a dialog box has an alternative version of the command that displays Command prompts instead of a dialog box. Most alternative versions of a command start with a hypen (-). For example, use -INSERT instead of INSERT. 

 

 

0 Likes