cad.net 做一個啟動cad的程式吧,通過登錄檔獲取cad安裝路徑
阿新 • • 發佈:2020-10-23
例項是用winform做的...主要是訊息機制部分...
例項程式碼:
var cadProcess = new CadProcess(); if (cadProcess.Record.Length == 0) { cadProcess.Run(CadProcess.RunCadVer.All); } if (cadProcess.Record.Length == 0) { MessageBox.Show("你沒有安裝cad嗎?"); return; }
//啟動cad
using System; using System.Diagnostics; using System.Linq;View Codeusing System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace JoinBoxCurrency { public class CadProcess { public enum RunCadVer { Minimum, Maximum, All, } public Process[] Record { get; privateset; } const string _acad = "acad"; const string _acadexe = "\\acad.exe"; string _workingDirectory; /// <summary> /// 獲取已經開啟的cad程式 /// </summary> public void GetExisting() { Process[] pros = Process.GetProcessesByName(_acad);if (pros.Length > 0) { Record = pros; } } /// <summary> /// 建構函式,初始化 /// </summary> public CadProcess() { Record = new Process[0]; _workingDirectory = Environment.GetEnvironmentVariable("TEMP"); } /// <summary> /// 啟動cad程式 /// </summary> /// <param name="runCadVer">啟動什麼版本</param> public void Run(RunCadVer runCadVer = RunCadVer.Minimum) { string progID = "AutoCAD.Application."; string exePath = null; //獲取本機cad路徑 var regedit = new AcadDirs(); if (regedit.Dirs.Count == 0) { return; } //按照版本排序 var ob = regedit.Dirs.OrderBy(cad => cad.Version).ToList(); switch (runCadVer) { case RunCadVer.Minimum: { progID += ob[0].Version; exePath = ob[0].Location + _acadexe; Run(progID, exePath); } break; case RunCadVer.Maximum: { progID += ob[ob.Count].Version; exePath = ob[ob.Count].Location + _acadexe; Run(progID, exePath); } break; case RunCadVer.All: { foreach (var reg in regedit.Dirs) { Run(progID + reg.Version, reg.Location + _acadexe); } } break; } } private void Run(string progID, string exePath) { //處理GetActiveObject在電腦睡眠之後獲取就會失敗.所以要ProcessStartInfo //https://blog.csdn.net/yuandingmao/article/details/5558763?_t_t_t=0.8027849649079144 //string progID = "AutoCAD.Application.17.1"; //string exePath = @"C:\Program Files (x86)\AutoCAD 2008\acad.exe"; object acApp = null; try { acApp = Marshal.GetActiveObject(progID); } catch { } if (acApp == null) { try { // var psi = new ProcessStartInfo(exePath, "/p myprofile");//使用cad配置,myprofile是配置名稱,預設就不寫 var psi = new ProcessStartInfo(exePath, "/nologo") { WorkingDirectory = _workingDirectory//這裡什麼路徑都可以的 }; Process pr = Process.Start(psi); pr.WaitForInputIdle(); while (acApp == null) { try { acApp = Marshal.GetActiveObject(progID); } catch { Application.DoEvents(); } Thread.Sleep(500); } } catch (Exception ex) { throw new Exception("無法建立或附加到AutoCAD物件: " + ex.Message); } } if (acApp != null) { GetExisting(); } } } }
//通過登錄檔獲取cad安裝路徑
using System.Collections.Generic; using System.IO; using Microsoft.Win32; namespace JoinBoxCurrency { public class AcadDirs { public List<AcadProductKey> Dirs { get;} /// <summary> /// 通過登錄檔獲取本機所有cad的安裝路徑 /// </summary> public AcadDirs() { Dirs = new List<AcadProductKey>(); RegistryKey Adsk = Registry.CurrentUser.OpenSubKey(@"Software\Autodesk\AutoCAD"); if (Adsk == null) { return; } foreach (string ver in Adsk.GetSubKeyNames()) { try { RegistryKey emnuAcad = Adsk.OpenSubKey(ver); var curver = emnuAcad.GetValue("CurVer"); if (curver == null) { return; } string app = curver.ToString(); string fmt = @"Software\Autodesk\AutoCAD\{0}\{1}"; emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app)); if (emnuAcad == null) { fmt = @"Software\Wow6432Node\Autodesk\AutoCAD\{0}\{1}"; emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app)); } var acadLocation = emnuAcad.GetValue("AcadLocation"); if (acadLocation == null) { return; } string location = acadLocation.ToString(); if (File.Exists(location + "\\acad.exe")) { var produ = emnuAcad.GetValue("ProductName"); if (produ == null) { return; } string productname = produ.ToString(); var release = emnuAcad.GetValue("Release"); if (release == null) { return; } string[] strVer = release.ToString().Split('.'); var pro = new AcadProductKey() { ProductKey = emnuAcad, Location = location, ProductName = productname, Version = double.Parse(strVer[0] + "." + strVer[1]), }; Dirs.Add(pro); } } catch { } } } public struct AcadProductKey { /// <summary> /// 登錄檔位置 /// </summary> public RegistryKey ProductKey; /// <summary> /// cad安裝路徑 /// </summary> public string Location; /// <summary> /// cad名稱 /// </summary> public string ProductName; /// <summary> /// cad版本號 17.1之類的 /// </summary> public double Version; } } }View Code