http://adndevblog.typepad.com/autocad/2012/07 ... h-escape-characters-cc.html I'd like to start some commands from my modeless dialog's buttons. I already found that in case of SendStringToExecute I should use '\x03' characters instead of ^C. My only problem is that in this case if no command is running then I end up with two *Cancel* strings in the Command Window, whereas a menu's ^C^C does not cause *Cancel* to appear if no command is running when the menu item is clicked.
How could I achieve the same?
Solution
You could check the CMDNAMES system variable and depending on how many commands are currently running you could add that many escape characters to the beginning of your command string.
Here is a sample that demonstrates this:
using System;
using System.Windows.Forms;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
namespace CsMgdAcad1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      string esc = "";
      string cmds = (string)acApp.GetSystemVariable("CMDNAMES");
      if (cmds.Length > 0)
      {
        int cmdNum = cmds.Split(new char[] { '\'' }).Length;
        for (int i = 0; i < cmdNum; i++)
          esc += '\x03';
      }
      Document doc = acApp.DocumentManager.MdiActiveDocument;
      doc.SendStringToExecute(esc + "_.LINE ", true, false, true);
    }
  }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim esc As String = ""
        Dim cmds As String = CType(acApp.GetSystemVariable("CMDNAMES"), String)
        If cmds.Length > 0 Then
            Dim cmdNum As Integer = cmds.Split(New Char() {"'"c}).Length
            Dim i As Integer = 0
            While i < cmdNum
                esc += ChrW(3)
                System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
            End While
        End If
        Dim doc As Document = acApp.DocumentManager.MdiActiveDocument
        doc.SendStringToExecute(esc + "_.LINE ", True, False, True)
    End Sub
acDoc.SendStringToExecute(ChrW(3) & ChrW(3), False, False, False) '发送两个ESC