using DFBIM.CADNETCommon.XmlConfig; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using static CollaborativePlatformMain.CADStartUtil.AcadDirs; namespace CollaborativePlatformMain.CADStartUtil { /// /// /// 文件名(File Name): StartCADUtil.cs /// /// 描述(Description): 启动CAD并打开图纸 /// /// 数据表(Tables): nothing /// /// 作者(Author): Ou Rui Song /// /// 日期(Create Date): 2024年5月7日11:31:34 /// /// 修改记录(Revision History): /// R1: /// 修改作者: /// 修改日期: /// 修改理由: /// /// public class StartCADUtil { public static void StartCADMath(bool isFloor = true, OpenDrawPathData openDrawPathData = null) { try { //获取本机cad路径 AcadDirs regedits = new AcadDirs(); //未安装CAD if (regedits.Dirs.Count == 0) { MessageBox.Show(DateTime.Now + "当前电脑未安装 AutoCAD "); return; } //未安装2019版CAD List cadPathpks = regedits.Dirs.Where(x => x.ProductName.Equals("AutoCAD 2019 - 简体中文 (Simplified Chinese)")).ToList(); if (cadPathpks.Count == 0) { Console.WriteLine(DateTime.Now + "当前电脑未安装 AutoCAD 2019 - 简体中文 (Simplified Chinese)"); Console.ReadLine(); return; } var pros = Process.GetProcessesByName("acad"); if (pros.Length != 0) { string dPath = Global.GetDriveLetterExceptC(); //已打开cad,写入一个文件,让cad监听文件 if (isFloor) { TwoOpenCADConfigUtil twoOpenCADConfigUtil = new TwoOpenCADConfigUtil(); twoOpenCADConfigUtil.Save(new TwoOpenData(true), string.Format(@"{0}\DFBIM\CMTwoOpenFloor.config", dPath)); } else { OpenDrawConfigUtil openDrawConfigUtil = new OpenDrawConfigUtil(); openDrawConfigUtil.Save(openDrawPathData, string.Format(@"{0}\DFBIM\OpenDrawPathData.config", dPath)); } } else { string location = cadPathpks[0].Location; // 创建一个脚本文件并写入要执行的命令 string scriptFile = location + "\\autocad_script.scr"; if (isFloor) File.WriteAllText(scriptFile, "CMOPENCADFLOOR\n"); else { string dPath = Global.GetDriveLetterExceptC(); OpenDrawConfigUtil openDrawConfigUtil = new OpenDrawConfigUtil(); openDrawConfigUtil.Save(openDrawPathData, string.Format(@"{0}\DFBIM\OpenDrawPathData.config", dPath)); File.WriteAllText(scriptFile, "CMOPENCADDRAW\n"); } // 启动AutoCAD进程并运行脚本文件 ProcessStartInfo startInfo = new ProcessStartInfo { FileName = location + "\\acad.exe", Arguments = $"/b \"{scriptFile}\"", UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, CreateNoWindow = true }; //创建新的线程 using (Process autocadProcess = new Process()) { autocadProcess.StartInfo = startInfo; autocadProcess.Start(); // 等待AutoCAD进程完成 autocadProcess.WaitForInputIdle(); // 删除脚本文件 //File.Delete(scriptFile); } } } catch (Exception) { return; } } /// /// 关闭进程 /// private static void KillProcess(int currentProcessId = -1) { if (currentProcessId == -1) currentProcessId = Process.GetCurrentProcess().Id; Process cadProcess = Process.GetProcessById(currentProcessId); if (!cadProcess.HasExited) { cadProcess.Kill(); //log.Info($"成功关闭CAD应用程序 (PID: {cadProcess.Id})"); } else { //log.Info($"CAD应用程序 (PID: {cadProcess.Id}) 已经退出。"); } //kill infoer Process[] processes = Process.GetProcessesByName("WSCommCntr4"); if (processes.Length != 0) { processes[0].Kill(); //log.Info("kill infoer"); } } } }