系统相关
首页 > 系统相关> > c# autocad 通过进程向cad发送command

c# autocad 通过进程向cad发送command

作者:互联网

主要的过程,日后操作autocad lt也应该可以用此方法
1 var pros = Process.GetProcessesByName("acad");
2             if (pros.Length == 0)
3             {
4                 MessageBox.Show("没有找到acad进程,请检查后再运行本程序!");
5                 return;
6             }
7             SendCommandToAutoCAD("_circle 0,0,0 300 ", pros.First().MainWindowHandle);
8             SetForegroundWindow(pros.First().MainWindowHandle);

 

导入win32的api的函数
1      [DllImport("user32.dll")]
2         public static extern bool SetForegroundWindow(IntPtr hWnd);
3 
4         [DllImport("user32.dll", EntryPoint = "FindWindow")]
5         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
6 
7         [DllImport("user32.dll")]
8         private static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
1  private struct COPYDATASTRUCT
2         {
3             public IntPtr dwData;
4 
5             public int cbData;
6 
7             public IntPtr lpData;
8 
9         }

kean大神提供的向autocad 命令行发送命令的函数

 1 private void SendCommandToAutoCAD(string toSend, IntPtr hwnd)
 2         {
 3             const int WM_COPYDATA = 0x4A;
 4             COPYDATASTRUCT cds = new COPYDATASTRUCT();
 5             cds.dwData = new IntPtr(1);
 6             string data = toSend + "\0";
 7             cds.cbData = data.Length * Marshal.SystemDefaultCharSize;
 8             cds.lpData = Marshal.StringToCoTaskMemAuto(data);
 9             SendMessageW(hwnd, WM_COPYDATA, IntPtr.Zero, ref cds);
10             Marshal.FreeCoTaskMem(cds.lpData);
11         }

标签:IntPtr,autocad,string,c#,pros,command,COPYDATASTRUCT,cds,public
来源: https://www.cnblogs.com/NanShengBlogs/p/13767036.html