zoukankan      html  css  js  c++  java
  • cad.net 获取所有已经安装的cad版本信息并制作一个外部启动程序

    所有版本将存放在这个注册表位置

    计算机HKEY_LOCAL_MACHINESOFTWAREAutodeskHardcopy

    var ackey = Registry.LocalMachine.OpenSubKey(@"SOFTWAREAutodeskHardcopy", false);
    var values = ackey.GetValueNames();
    

    img

    启动cad的外部程序

    这样一来,我们拥有了这个所有的cad版本信息,然后我们便可以制作一个启动cad的外部程序.

    我的实现工程是x64的,否则读64位注册表是空的.

    然后是用winform做的.主要是消息机制部分...当然,你也可以用WPF制作,只是没有什么必要.

    调用方式

    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;
    using 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; private set; }
    
            const string _acad = "acad";
            const string _acadexe = "\acad.exe";
            string _workingDirectory;
    
            /// <summary>
            /// 获取已经打开的cad程序
            /// </summary>
            public void GetExisting()
            {
                Record = Process.GetProcessesByName(_acad);
            }
    
            /// <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 - 1].Version;
                            exePath = ob[ob.Count - 1].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 2008acad.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(@"SoftwareAutodeskAutoCAD");
                if (Adsk == null)
                {
                    return;
                }
                foreach (string ver in Adsk.GetSubKeyNames())
                {
                    try
                    {
                        RegistryKey emnuAcad = Adsk.OpenSubKey(ver);
                        var curver = emnuAcad.GetValue("CurVer");
                        if (curver == null)
                        {
                            continue;
                        }
                        string app = curver.ToString();
                        string fmt = @"SoftwareAutodeskAutoCAD{0}{1}";
                        emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
                        if (emnuAcad == null)
                        {
                            fmt = @"SoftwareWow6432NodeAutodeskAutoCAD{0}{1}";
                            emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
                        }
    
                        var acadLocation = emnuAcad.GetValue("AcadLocation");
                        if (acadLocation == null)
                        {
                            continue;
                        }
                        string location = acadLocation.ToString();
                        if (File.Exists(location + "\acad.exe"))
                        {
                            var produ = emnuAcad.GetValue("ProductName");
                            if (produ == null)
                            {
                                continue;
                            }
                            string productname = produ.ToString();
                            var release = emnuAcad.GetValue("Release");
                            if (release == null)
                            {
                                continue;
                            }
                            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;
            }
        }
    }
    

    (完)

  • 相关阅读:
    面向对象的继承关系体现在数据结构上时,如何表示
    codeforces 584C Marina and Vasya
    codeforces 602A Two Bases
    LA 4329 PingPong
    codeforces 584B Kolya and Tanya
    codeforces 584A Olesya and Rodion
    codeforces 583B Robot's Task
    codeforces 583A Asphalting Roads
    codeforces 581C Developing Skills
    codeforces 581A Vasya the Hipster
  • 原文地址:https://www.cnblogs.com/JJBox/p/11381254.html
Copyright © 2011-2022 走看看