質問
APIでInventorを起動する際にバージョン指定する方法はありますか。
回答
AutoCAD等でGetTypeFromProgID()の引数に、"AutoCAD.Application.xx” (xxはバーンジョンを表す数値)等を渡すことでバージョンを指定して起動することが出来るため、Inventorも同様の方法で起動する方法を探している状況かと思います。
残念ながら、InventorではAutoCADとはCOMのレジストリ登録の仕組みが異なっており、CreateInstance()でバージョンを指定して起動することが出来ません。
プログラムから指定のバージョンのInventorを起動する場合、以下の2通り方法があります。
1.カスタムプログラムからexeを起動する 2.カスタムプログラムから、CreateInstance()関数の実行時に、CreateInstance()関数が参照するInventorのExeのパスが記述されている、レジストリ情報を対象バージョンのInventor.exeのパスで上書きしたのちに、GetTypeFromProgID()⇒CreateInstance()を行う。 ただし、2の方法はCLASSES_ROOT配下のレジストリ情報の書き換えを行うため、adminまたはCLASSES_ROOTの書き換えに必要なユーザ権限が必要となりるため、通常は1の方法をとることになるかと思いますので、ここでは1の方法を記載します。
以下は、1の方法でInventorを起動するヘルパークラスです。
class AdnInventorLoader
{
public enum Version
{
Inventor_2020,
Inventor_2021,
Inventor_2022,
Inventor_2023,
};
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))
{
while (true)
{
try
{
Inventor.Application app = Marshal.GetActiveObject("Inventor.Application") as Inventor.Application;
return app;
}
catch (Exception ex)
{
if (!ex.Message.Contains("MK_E_UNAVAILABLE"))
break;
System.Threading.Thread.Sleep(1000);
}
}
}
}
}
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_2020:
key = "RegistryVersion24.0";
break;
case Version.Inventor_2021:
key = "RegistryVersion25.0";
break;
case Version.Inventor_2022:
key = "RegistryVersion26.0";
break;
case Version.Inventor_2023:
key = "RegistryVersion27.0";
break;
default:
return string.Empty;
}
using (RegistryKey inventorKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64).
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;
}
}
}
}
利用側からは以下のように、起動するInventorのバージョンを指定する列挙値を指定して起動します。
Inventor.Application app =
AdnInventorLoader.CreateInstanceFromProcess(AdnInventorLoader.Version.Inventor_2023);
if (app != null)
app.Visible = true;
※この記事はブログ記事「Running programmatically a specific version of Inventor」を元に、必要な部分を抽出し、加筆修正と日本語化を行ったものとなります。
記事全体を表示