Autodesk Community Tips- ADNオープン
Autodesk Community Tipsではちょっとしたコツ、やり方、ショートカット、アドバイスやヒントを共有しています。
ソート順:
質問 InventorのVBA環境で、VBAの「ツール」-「参照設定」から利用するライブラリの参照設定が行えます。 VBAを実行するクライアント端末が複数あり、各端末でライラリファイルの参照設定をすることが手間なので、ライブラリの参照設定を出力して配布することは可能でしょうか? 回答 Inventorの設定ファイルには、VBAでの参照設定を編集する機能はありません。また、InventorのAPI にも直接的に参照設定を編集する機能はありません。   一方で、InventorのAPIで、Microsoft社が提供するVBA開発環境のVBProjectオブジェクトを取得するAPIが公開されているため、VBProjectオブジェクトのReferences.AddFromFile()メソッド等を利用することで、プログラムコードから参照設定を行うことが可能です。   以下は、"Adbe Acrobat 10 Type Library”への参照を追加する場合のサンプルコードとなります。 Dim app As Application Set app = ThisApplication Dim VBProject As Object Set VBProject = app.VBAProjects.Item(1).VBProject Const RefFile As String = "<path to acrobat.tlb file>" VBProject.References.AddFromFile RefFile
記事全体を表示
質問 .Net で、Inventor API AutoCADBlock.GetPromptTextValues()でブロックの属性値が取得できない。 VBAからはAutoCADBlock.GetPromptTextValues()で値が取得できているため、データの問題ではありません。.Netからは、AutoCADBlock.GetPromptTextValues()を利用できないのでしょうか? 回答 .Net言語からAutoCADBlock.GetPromptTextValues()を利用する場合、outパラメータの引数をメソッド実行前にstring配列で初期化する必要があります。   以下はC#の場合のサンプルとなります。 object tags = new string[] { }; object attrs = new string[] { }; oAcadBlock.GetPromptTextValues(out tags, out attrs);
記事全体を表示
質問 InventorのアドインでVault接続状態を確認し、接続していない場合はログインをしてVaultのConnectionを取得したい。 回答 Vault APIのConnectionManager.Instance.Connectionにより接続中のVaultコネクションオブジェクトを取得することができます。 接続されていない場合は、InventorのVaultログインコマンド(内部名:ContentLoginCmdIntName)を実行することで、ログインダイアログを表示することが可能です。   以下は、上述のAPIを使用したサンプルコードとなります。 using VDF = Autodesk.DataManagement.Client.Framework; using VB = Connectivity.Application.VaultBase; VDF.Vault.Currency.Connections.Connection conn = VB.ConnectionManager.Instance.Connection; if (null == conn) { try { ControlDefinition oCtlDef = m_inventorApplication.CommandManager.ControlDefinitions["ContentLoginCmdIntName"]; oCtlDef.Enabled = true; oCtlDef.Execute(); conn = VB.ConnectionManager.Instance.Connection; } catch (Exception ex) { } } if (null != conn) { MessageBox.Show(String.Format("Vault = {0}, UserID = {1}, User Name = {2}",conn.Vault, conn.UserID, conn.UserName)); }   なお、Visual Stuidoプロジェクトの参照設定で、Vault SDK配下のAutodesk.DataManagement.Client.Framework.dllおよびVaultインストールフォルダ配下のConnectivity.Application.VaultBase.dllファイルの参照の追加が必要となる点にご留意ください。  
記事全体を表示
現象 C#のコンソールアプリケーションで、Inventor Apprentice Server のApprenticeServerDocument.Thumbnailプロパティにアクセスするとエラーが発生します。 回避方法はありますか。   解決策 C#のコンソールApplicationの場合、Main関数に[STAThread]属性を付加しSTAで動作するように指定することで、Thumbnailプロパティにアクセスできるようになります。   [STAThread] static void Main(string[] args) {   なお、VB.netの場合コンソールアプリケーションはデフォルトでSTAで動作する設定のため、[STAThread]属性を付加する必要はありません。 また、C#の場合でもWindows フォームアプリケーションの場合も[STAThread]属性を付加する必要はありません。
記事全体を表示
Question Inventor APIでInventorのdwgファイルとAutoCADのdwgファイルを識別する方法はありますか? Answer InventorAPIの、FileManager.IsInventorDWG()メソッドを利用することで判別が可能です。 メソッドの引数には対象のdwgファイルへのフルパスを指定します。  
記事全体を表示
Question InventorAPIでDocuments オブジェクト のCountプロパティの値が2022以降とそれより前のバージョンで異なりますが、なぜでしょうか。 Answer この動作は、Invewntor 2022以降で追加されたモデル状態機能に対応する形でのAPIの動作の変更となります。   モデル状態が存在する場合、2つのドキュメントがDocumentsプロパティから取得されます。 2つのドキュメントの相違点は、片方がファクトリドキュメントもう片方がメンバドキュメントとなり、APIでは以下のプロパティにて区別が可能です。 ・ファクトリドキュメント:ComponentDefinition.IsModelStateFactory プロパティがTrue ・メンバドキュメント:ComponentDefinition.IsModelStateMemberプロパティがTrue  
記事全体を表示
Question InventorのFileDialogで、ShowSave()実行時にInitialDirectoryプロパティで指定したパスが初期表示されない。 Answer FileDialogのShowSave()メソッド実行時には、InitialDirectoryプロパティで指定したパスが無視されます。 この問題に対応するためには、FileDialogのFileNameプロパティに、ファイル名だけでなくダイアログに初期表示をしたいフォルダを含むフルパスを指定することで回避が可能です。   以下は、現在のドキュメントが存在するフォルダを指定してShowSave()を実行する場合のサンプルとなります。 Dim oFileDlg As Inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) '.ShowSave()の場合InitialDirectoryの設定は無視される 'oFileDlg.InitialDirectory = ThisDoc.Path '.ShowSave()の場合FileNameに初期表示したいフォルダまでのフルパスを含むファイル名を指定する oFileDlg.FileName = ThisDoc.Path + "\" + ThisDoc.FileName(False) oFileDlg.CancelError = True oFileDlg.ShowSave()
記事全体を表示
質問 Inventor APIでiPropertyの質量を取得すると値がグラムで取得されます。 Inventor APIで取得する値の単位は何でしょうか。 回答 Inventorでは各ドキュメントの表示単位を設定可能ですが、内部データべースの単位は、常に同じ単位を利用しております。 InventorのAPIはこのデータベース単位を利用いたしますので(表示単位の設定にかかわらず)、データベース単位での値が取得されます。   Inventorの表示単位や内部データベースの単位の詳細については、APIリファレンスの「Inventor APIのユーザマニュアル」-「一般的なコンセプト」-「計測単位」にまとめられています。 以下は、APIリファレンスに記載のデータベース単位の表となります。   ただし、以下のコードでiPropertyの質量を取得すると、値がデータベース単位のキログラムではなく、グラムで取得されるため注意が必要です。 ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Mass").Value   質量をデータベース単位のキログラムで取得する場合は、以下のようにMassPropertesのmassプロパティから取得が可能です。 ThisApplication.ActiveDocument.ComponentDefinition.MassProperties.mass  
記事全体を表示
質問 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」を元に、必要な部分を抽出し、加筆修正と日本語化を行ったものとなります。
記事全体を表示