启动特定版本的Inventor

大多数情况下,一台机器上可能会装有几个版本的Inventor,尤其开发者的机器。我同事写了篇文章,介绍如何启动特定版本的Inventor。原文地址:

Running programmatically a specific version of Inventor

大家知道,从独立应用程序(EXE)启动Inventor,基本步骤如下。从注册表找到Inventor Application组件信息,然后调用CreateInstance启动。

Type inventorAppType =
     System.Type.GetTypeFromProgID(
     "Inventor.Application");
 
Inventor.Application app =  
    System.Activator.CreateInstance(
      inventorAppType)                          
           as Inventor.Application;

若你做过AutoCAD二次开发,可能知道,AutoCAD也有COM API,也能实现启动AutoCAD进程,而且能够指定版本,形如 “AutoCAD.Application.19.1”,将启动AutoCAD 2014. 但Inventor无此机制。那么要启动特定版本的Inventor,如何做呢?以下是个我们提供的工具类AdnInventorLoader, 提供了两种方式:
AdnInventorLoader.CreateInstanceFromProcess
该方法将读取特定版本Inventor注册信息,然后获取到对应Inventor.exe所在路径,最后启动该exe。
AdnInventorLoader.CreateInstanceFromCLSID
该方法也是先读取特定版本Inventor注册信息,然后获取到对应Inventor.exe所在路径。但接着它并不是直接启动exe,而是去改变最后一次启动的Inventor版本。当然,该方法可能需要管理员权限或特别的用户权限。
因此,可能方法1更适合你。

// Utility Class to launch specific version
// of Inventor Application
// By Philippe Leefsma, May 2013 – Devtech
class AdnInventorLoader
{
    public enum Version
    {
        Inventor_2010,
        Inventor_2011,
        Inventor_2012,
        Inventor_2013,
        Inventor_2014
    };
 
    public static Inventor.Application
        CreateInstanceFromProcess(Version version)
    {
        try
        {
            string exePath = GetExePath(version);
 
            if (exePath != string.Empty)
            {
                CleanUpRegistry();
 
                System.Diagnostics.Process process =
                    System.Diagnostics.Process.Start(
                        exePath);
 
                if (process != null)
                {
                    //Wait for 5 Mins
                    if (process.WaitForInputIdle(
                        300000))
                    {
                        Inventor.Application app =
                            Marshal.GetActiveObject(
                               "Inventor.Application")
                              as Inventor.Application;
 
                        return app;
                    }
                }
            }
 
            return null;
        }
        catch
        {
            return null;
        }
    }
 
    public static Inventor.Application
        CreateInstanceFromCLSID(Version version)
    {
        try
        {
            string exePath = GetExePath(version);
 
            if (exePath != string.Empty)
            {
                CleanUpRegistry();
 
                using (RegistryKey inventorKey =
                    Registry.ClassesRoot.
                        OpenSubKey("CLSID").
                        OpenSubKey("").
                        OpenSubKey("LocalServer32",
                            true))
                {
                    string[] names =
                         inventorKey.GetValueNames();
 
                    inventorKey.SetValue(
                        names[0],
                        exePath + " /Automation");
 
                    inventorKey.Close();
 
                    Type inventorAppType =
                        System.Type.GetTypeFromProgID(
                            "Inventor.Application");
 
                    Inventor.Application app =
                      System.Activator.CreateInstance(
                          inventorAppType)
                            as Inventor.Application;
 
                    return app;
                }
            }
 
            return null;
        }
        catch
        {
            return null;
        }
    }
 
    // Clean up registry to prevent
    // "Re-registration" dialog
    // http://tinyurl.com/dx4tsnu
    private static bool CleanUpRegistry()
    {
        try
        {
            using (RegistryKey inventorKey =
                Registry.CurrentUser.
                    OpenSubKey("Software").
                    OpenSubKey("Autodesk").
                    OpenSubKey("Inventor").
                    OpenSubKey("Current Version", 
                       true))
            {
                if (inventorKey == null)
                    return false;
 
                inventorKey.DeleteValue(
                    "Executable");
                inventorKey.DeleteValue(
                    "LastVersionRun");
                inventorKey.DeleteValue(
                    "Registered");
                inventorKey.DeleteValue(
                    "RegistryVersion");
                inventorKey.DeleteValue(
                    "SilentMode");
                inventorKey.DeleteValue(
                    "UBI");
 
                inventorKey.Close();
 
                return true;
            }
        }
        catch
        {
            return false;
        }
    }
 
    // Retrieve Inventor.exe fullpath based on version
    private static string GetExePath(Version version)
    {
        try
        {
            string key = string.Empty;
 
            switch (version)
            {
                case Version.Inventor_2010:
                    key = "RegistryVersion14.0";
                    break;
                case Version.Inventor_2011:
                    key = "RegistryVersion15.0";
                    break;
                case Version.Inventor_2012:
                    key = "RegistryVersion16.0";
                    break;
                case Version.Inventor_2013:
                    key = "RegistryVersion17.0";
                    break;
                case Version.Inventor_2014:
                    key = "RegistryVersion18.0";
                    break;
                default:
                    return string.Empty;
            }
 
            using (RegistryKey inventorKey =
                Registry.LocalMachine.
                    OpenSubKey("SOFTWARE").
                    OpenSubKey("Autodesk").
                    OpenSubKey("Inventor").
                    OpenSubKey(key))
            {
 
                if (inventorKey == null)
                    return string.Empty;
 
                string path = inventorKey.GetValue(
                  "InventorLocation")
                    as string;
 
                inventorKey.Close();
 
                path += "\\Inventor.exe";
 
                return (System.IO.File.Exists(path) ?
                    path :
                    string.Empty);
            }
        }
        catch
        {
            return string.Empty;
        }
    }
}
 
Here are examples of use for both methods:
 
Inventor.Application app =
    AdnInventorLoader.CreateInstanceFromProcess(
        AdnInventorLoader.Version.Inventor_2013);
 
if (app != null)
   app.Visible = true;
 
             -------------------------
 
Inventor.Application app =
    AdnInventorLoader.CreateInstanceFromCLSID(
        AdnInventorLoader.Version.Inventor_2014);
   
if (app != null)
   app.Visible = true;



资源下载链接为: https://pan.quark.cn/s/0c983733fad2 本文主要回顾了2021年之前及2021年中国科学技术大学软件学院(简称“中科大软院”)高级软件工程(MN)专业的考试情况,重点聚焦于编程题。编程题在考试中的占比不断提高,因此考生需要深入理解这些题目及其解题方法。 中科大软院的高级软件工程专业致力于培养具备深厚理论基础和强大实践能力的高级软件人才。课程设计注重理论与实践相结合,以满足软件行业对高素质工程师的需求。考试内容通常涵盖计算机基础知识、软件工程理论、编程语言、数据结构与算法、操作系统、数据库系统等多个领域。2021年的考试中,编程题的比重进一步提升,这体现了学院对学生实际编程能力和问题解决能力的重视。 编程题通常涉及常见的编程问题,例如字符串处理、数组操作、递归算法、图论问题等,也可能包括网络编程、数据库查询或系统设计等特定领域的应用。考生需要熟练掌握至少一种编程语言,如C++、Java、Python等,并具备较强的算法分析和实现能力。在解题过程中,考生需要注意以下几点:一是准确理解题目要求,避免因误解而导致错误;二是合理选择并设计算法,考虑时间复杂度和空间复杂度,追求高效性;三是遵循良好的编程规范,注重代码的可读性和可维护性;四是考虑边界条件和异常情况,编写健壮的代码;五是编写测试用例,对代码进行充分测试,及时发现并修复问题。 对于备考的同学,建议多做历年试题,尤其是编程题,以熟悉题型和解题思路。同时,可以参加编程竞赛或在在线编程平台(如LeetCode、HackerRank)进行实战训练,提升编程和问题解决能力。此外,关注PPT中的编程代码也很关键,因为这些代码可能是老师给出的示例或解题思路,能够帮助学生更好地理解和掌握编程题的解法。因此,考生需要深入学习PPT内容,理解代码逻辑,并学会将其应用到实际编程题目中。 总之,对于
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值