Draw wires and insert blocks synchronously from PaletteSet

Draw wires and insert blocks synchronously from PaletteSet

mcoH3VZV
Advocate Advocate
826 Views
7 Replies
Message 1 of 8

Draw wires and insert blocks synchronously from PaletteSet

mcoH3VZV
Advocate
Advocate

Hi,

 

I am trying to draw full pages of drawings using pre-defined commands synchronously.

 

I have a PaletteSet that opens in AutoCAD and I have buttons to draw various pages. I cannot use editor.Command() since it is application context, and I do not wish to use async commands or sendstringtoexecute since you cannot rely on manipulating the data afterwards.

 

In order to draw wires and insert blocks the way I want, the only method I have found is to open a transaction, and insert the block that way, and for wires, I open a transaction, draw a line, change the layer to the wire layer I need, and insert a wddot if it hits an intersection. This seems to be overkill when there are great commands that do this for you in the API Help. I just cannot run any of them in synchronous application context. The commands I would like to use are as follows:

(c:wd_insym2 sym xy scl options) - For inserting Blocks

(c:wd_putwnxyfq xy newval) - (or some wire drawing equivalent) For inserting wires

 

Is there any way to do this? It seems like it shouldn't be so difficult to use pre-defined commands.

 

Thank you,

0 Likes
Accepted solutions (1)
827 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

 

It seems you're using some Vertical I'm not familiar with (Electrical?).

Anyway as you want to launch a LISP function, you shoud try with Application.Invoke method.

 

Assuming the c:wd_putwnxyfq function requires a point2d and a string as arguments, you can try something like this:

        private void button1_Click(object sender, EventArgs e)
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            if (doc != null)
            {
                var args = new ResultBuffer(
                    new TypedValue((int)LispDataType.Text, "c:wd_putwnxyfq"),
                    new TypedValue((int)LispDataType.Point2d, new Point2d(20.0, 10.0)),
                    new TypedValue((int)LispDataType.Text, "foo"));
                doc.Window.Focus();
                using (doc.LockDocument())
                {
                    AcAp.Invoke(args);
                    doc.Editor.UpdateScreen();
                }
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

mcoH3VZV
Advocate
Advocate

Yes, I am using AutoCAD Electrical. I tried that solution and it fixes the wire, but will not put "foo" as the wire number. I also tried 'c:wd_putwnxyf'.

 

But in general, most of these commands via Application.Invoke, I cannot get to work. If I try wd_wire, as shown below, I simply get the error: "Unknown (command-s) failure." I get this unknown command error for many of the API functions. Any ideas?

 

        private void button1_Click(object sender, EventArgs e)
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            if (doc != null)
            {
                var args = new ResultBuffer(
 new TypedValue((int)LispDataType.Text, "c:wd_wire"),
new TypedValue((int)LispDataType.Point2d, new Point2d(30.0, 10.0)),
new TypedValue((int)LispDataType.Point2d, new Point2d(35.0, 10.0)),
new TypedValue((int)LispDataType.Text, "WIRES")); doc.Window.Focus(); using (doc.LockDocument()) { AcAp.Invoke(args); doc.Editor.UpdateScreen(); } } }

 

0 Likes
Message 4 of 8

mcoH3VZV
Advocate
Advocate

I just entered:

 

new TypedValue((int)LispDataType.Text, "c:wd_wire"),
new TypedValue((int)LispDataType.Point2d, new Point2d(30.0, 10.0)),
new TypedValue((int)LispDataType.Point2d, new Point2d(35.0, 10.0)),
new TypedValue((int)LispDataType.Text, "WIRES"));

 

inside of a separate command not tied to the PaletteSet and it worked perfectly. Can certain API functions only work in the document context? I need these commands to somehow work in the application context so I can do multiple drawings from a PaletteSet

0 Likes
Message 5 of 8

_gile
Consultant
Consultant

Sorry but I cannot help you further.

The issue you're describing are related the Electrical specific LISP function and I do not know how work these functions nor which type of arguments they require.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 8

mcoH3VZV
Advocate
Advocate

Electrical aside, I now know I have the arguments correct. Do you know why only some of these commands are required to work only in document context? and if there is another way besides Application.Invoke to use it inside a PaletteSet?

0 Likes
Message 7 of 8

_gile
Consultant
Consultant

According to the error you got ("Unknown (command-s) failure."), this is certainly due to the fact some of these LISP functions use call to 'command' or 'command-s' built-in LISP functions which cannot run in application context.

If so, I'm afraid there's no way to run these function synchronously.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 8

mcoH3VZV
Advocate
Advocate
Accepted solution

Found a way to do this after four years..

 

1. Set this up:

    public static class MyDocExtensions
    {
        public static void Commands(this Document doc, string data)
        {
            dynamic acadDoc = doc.GetAcadDocument();
            acadDoc.SendCommand(data);
        }
    }

 

2. Then, call a command to use SendStringToExecute synchronously.

string wireCmd = "(c:wd_wire (list " + X1 + " " + Y1 + " 0.0)(list " + X2 + " " + Y2 + " 0.0) \"" + Lay + "\") ";
Application.DocumentManager.MdiActiveDocument.Commands(wireCmd);

 

0 Likes