123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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
- {
- /// <summary>
- ///
- /// <para>文件名(File Name): StartCADUtil.cs</para>
- ///
- /// <para>描述(Description): 启动CAD并打开图纸</para>
- ///
- /// <para>数据表(Tables): nothing</para>
- ///
- /// <para>作者(Author): Ou Rui Song</para>
- ///
- /// <para>日期(Create Date): 2024年5月7日11:31:34</para>
- ///
- /// 修改记录(Revision History):
- /// R1:
- /// 修改作者:
- /// 修改日期:
- /// 修改理由:
- ///
- /// </summary>
- 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<AcadProductKey> 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;
- }
- }
- /// <summary>
- /// 关闭进程
- /// </summary>
- 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");
- }
- }
- }
- }
|