1. 程式人生 > 實用技巧 >cad.net 直接在exe傳送繪圖資訊載入資訊到cad

cad.net 直接在exe傳送繪圖資訊載入資訊到cad

程式碼來自於南勝

除了以下這個的方法還可以用埠通訊,不過埠通訊比這個還麻煩...

雖然例子是傳送繪圖資訊,但是也可以傳送載入資訊....

重要的事這份程式碼解決了com通訊只能處理相同的兩個cad,例如你打開了兩個cad08,導致了只能處理第一個開啟的....不詳談了

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    public class exe傳送到cad
    {
        static void Main(string
[] args) { //程序控制代碼 var pros = Process.GetProcessesByName("acad"); //通過程序控制代碼獲取主視窗控制代碼 foreach (var item in pros) { SendCad.SendCommandToAutoCAD("_circle 0,0,0 300 ", item.MainWindowHandle); } } }
public class SendCad { [DllImport("user32.dll", EntryPoint = "SendMessage")] static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam); /// <summary> /// 傳送資訊到cad /// </summary> /// <param name="toSend"></param>
/// <param name="hwnd">cad控制代碼</param> public static void SendCommandToAutoCAD(string toSend, IntPtr hwnd) { const int WM_COPYDATA = 0x4A; var cds = new COPYDATASTRUCT { dwData = new IntPtr(1) }; string data = toSend + "\0"; cds.cbData = data.Length * Marshal.SystemDefaultCharSize; cds.lpData = Marshal.StringToCoTaskMemAuto(data); SendMessage(hwnd, WM_COPYDATA, IntPtr.Zero, ref cds); Marshal.FreeCoTaskMem(cds.lpData); } private struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; public IntPtr lpData; } } }
View Code