LoginSignup
2
0

More than 1 year has passed since last update.

Autodesk Inventor API Hacking (Out processからの操作)

Last updated at Posted at 2021-06-17

0. はじめに

Freeradicalの中の人、yamarahです。
APIをハックする際に、「AddInを使うのは手間なので、VBAでやるか・・・」という状況を打破する方法を見つけました。
なぜもっと早く気付かなかったんだ・・・

1. 実行中のInventorに外部から接続する

予めInventorを起動しておいて、後から接続して操作すれば、色々な邪魔くさいことは関係なくなるのです。

1.1 Console Applicationを使う

新規のConsole Applicationを作成して、コードを書いてみましょう。

using Inventor;
using System;

namespace InventorAccessor
{
    class Program
    {
        static void Main(string[] args)
        {
            var invApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
            Console.WriteLine($"Version {invApp.SoftwareVersion.DisplayName}");

            var document = invApp.ActiveEditDocument;
            if (document is null)
            {
                Console.WriteLine($"document is null.");
            }
            else
            {
                Console.WriteLine($"SelectSet.Count : {document.SelectSet.Count}");
            }

            Console.WriteLine("Hit any key.");
            _ = Console.ReadKey();
        }
    }
}
実行結果
Version 2020.4 (Build 244396000, 396)
SelectSet.Count : 1
Hit any key.

再コンパイルもし放題ですし、ブレークポインタを使ったデバッグも出来ます。デバッグを停止しても、Inventorごと終了することもありません。まさにVBAのルーズさが、今ここに!!
注意する点は、GetActiveObjectはなぜか.NetCoreにはないので、.NetFrameworkを使うということです。
それと、当然ですがInteropのdllを参照しておく必要があります。

1.2 C# Interactiveを使う

もっと簡便には、C# Interactiveを使う手もあります。
Visual Studioのメニューから、表示その他のウィンドウC# Interactiveを選択して、以下のように操作します。

C# Interactive
Microsoft (R) Visual C# インタラクティブ コンパイラ バージョン 3.10.0-4.21269.26 ()
'CSharpInteractive.rsp' からコンテキストを読み込んでいます。
詳細については、「#help」と入力します。
> #r "Autodesk.Inventor.Interop"
> using Inventor;
> var app = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
> app.SoftwareVersion.DisplayVersion
"2020.4"
> 

このように、VBAのイミディエイトウィンドウのように操作することが出来ます。

99. 親の記事に戻る

Autodesk Inventor API Hacking (概略)

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0