Design Automation API for Inventor – iLogicを利用したDesign Automationの実行方法

無題

これまでのDesign Automation API for Inventorの解説記事では、Inventor Pluginを用いてDesing Automation for Inventorを実行する方法について解説をしてきました。

Pluginの開発には、.Net を用いて開発を行いますので、.Net Frameworkが提供する多くの機能が利用できるため、柔軟でパワフルかつメンテナンス性に優れたプログラムを開発しやすいといった利点がある一方で、Visual Studioを用いてのプログラム開発に、敷居が高さを感じていらっしゃる方もいらっしゃるのではないかと思います。

 

一方で、InventorにはiLogicというドキュメント内・外にルールを記述する仕組みがあります。iLogicは、Inventorに組み込みのエディタで、Visual Basic  (VB.NET)を用いて小さなプログラムを開発することができ、開発がしやすく簡単に利用することができます。このため、すでにご活用されているiLogic、または新規にiLogicを開発してDesing Automation for Inventorで利用したいと考えている方もいらっしゃるのではないかと思います。

 

そこで、今回の記事では、InventorのiLogicを利用してDesing Automationを実行する方法について解説をしたいと思います。

Design Automation についてのおさらい

InventorのiLogicをDesing Automationで実行する方法の解説の前に、Design Automationについて簡単におさらいをしたいと思います。

 

Design Automationを実行するにあたって、以下の3つのDesign Automationのコンポーネントについて理解をする必要があります。

  1. AppBundle
  2. Activity
  3. WorkItem

 

AppBundleとは、Desing Automationで実行させたい「処理」が記述されたプラグインを、Desing Automationの実行系が利用できるようにパッケージ化したものとなります。

Activityとは、Desing Automationで実行するタスクの入出力および、実行するAppBundleを定義したものとなります。

WorkItemは、Activityに具体的な入出力値を指定して、実行するものとなります。

 

この3つは、よくプログラムにおける関数の宣言、定義、呼び出しに例えて説明がされますが、ActivityがC/C++言語でのヘッダファイルでの関数の宣言に、AppBundleが関数の定義に、そしてWorkItemが、関数の呼び出しに相当いたします。

Design Automation APIには、AppBundle、Activity、WorkItemそれぞれに作成、問い合わせなどのAPI(エンドポイント)が存在します。

 

また、Design Automationはクラウド上のサーバで実行されることから、以下のような点を理解する必要があります。

  1. 処理の対象となるファイル(Inventorのパートファイルやアセンブリ等)はクラウドストレージ上に配置する必要がある
  2. 処理はDesign Automation実行毎に、クラウドサーバ上の一時領域で実行され、処理結果はクラウドストレージを介して取得する必要がある
  3. 処理はサーバ上で実行されるため、GUIでのインタラクションを伴う処理を実行することは出来ない

 

Design Automationの仕組みについては、以下のForge Online動画記事 「Design Automation API の理解」でも解説をしておりますのでご参照ください

 

また、Design Automation Inventorについては、Forge Online動画記事にて詳細を解説しておりますので、是非ご参照ください。

 

Design AutomationでInventor iLogicの実行

Design Automationについての理解をおさらいしましたので、実際にDesign AutomationでiLogicを使う方法について解説をしたいと思います。

 

iLogicを実行する方法には、大きく分けて以下の2つの方法があります。

  1. 設計データ内(.prtや.iam等)のiLogicを実行する方法
  2. 外部ファイルのiLogicを指定して実行する方法

 

また、2の外部ファイルのiLogicを指定して実行する場合、外部ファイルのiLogicをどこに置くかおよび、iLogicファイルを指定する方法で、以下の3つのパターンがあります。

2-1.外部ファイルのiLogicをクラウドストレージに格納し、WorkItemのParameterで指定する方法

2-2.外部ファイルのiLogicをクラウドストレージに格納し、ActivityのSettingで指定する方法

2-3.外部ファイルのiLogicをAppBundleに含める方法

 

それぞれの場合に共通することは、ActivityでDesing Automation for Inventorの実行エンジンである、InventorCoreConsole.exeの実行コマンドラインに、/s オプションで、実行するiLogicのスクリプトを指定する点にあります。

InventorCoreConsole.exeのコマンドラインについては、以下のコマンドラインリファレンスをご参照ください。

 

それでは、具体的にそれぞれの場合についてどのように指定するかを見ていきましょう。

 

1.設計データ内(.prtや.iam等)のiLogicを実行する方法

Activity のcommandLineの/s オプションを指定して実行するiLogicスクリプトを指定します。iLogicスクリプトは、settings配下に定義しており、実行するInventor iLogicのルール名を、iLogicVb.RunRule(“MyRule”)の形で記述し、から参照します。以下の例では、MyRuleという名前のiLogicが実行されることになります。

実行するiLogicルール名は、”(ダブルクオーテーション)で囲む必要があり、エスケープ文字列を追加して、”MyRule”としています。

 

  • Activity
{
    "id": "{ActivityId}",
    "commandLine": [
        "$(engine.path)\InventorCoreConsole.exe /i "$(args[InventorDoc].path)" /s $(settings[script].path)"
    ],
    "parameters": {
      "InventorDoc": {
        "verb": "get",
        "localName": "Input.ipt"
      },
      "OutputIpt": {
        "localName": "Input.ipt",
        "verb": "post"
      }
    },
    "engine": "Autodesk.Inventor+2021",
    "settings": {
        "script":{
            "value":"iLogicVb.RunRule("MyRule")"
        }
    },
    "description": "Execute iLogic rule within a part file."
}
 

この場合のWorkItemの指定は特別なことはなく、入力となるInventorファイルと、Desing Automation実行結果のInventorファイルを格納するOSSのURLを指定するだけとなります。

  • WorkItem
{
   "activityId": "{specify activity id + alias}",
   "arguments": {
        "InventorDoc": {
            "url": "{specify input file url}"
        },
        "OutputIpt": {
            "url": "{specify output file url}",
            "verb": "put"
        }
    }
}

 

また、上述の例では、ActivityにiLogicルール名のMyRuleを固定で指定しましたが、WorkItem毎に実行するiLogicルール名を変更することも可能で、この場合は以下のようになります。

  • Activity
{
"id": "{ActivityId}",
"commandLine": [
"$(engine.path)\InventorCoreConsole.exe /i "$(args[InventorDoc].path)" /s  $(args[iLogicName].path)"
],
"parameters": {
"InventorDoc": {
"verb": "get",
"localName": "Input.ipt"
},
"iLogicName": {
"verb": "read"
},
"OutputIpt": {
"localName": "Input.ipt",
"verb": "post"
}
},
"engine": "Autodesk.Inventor+2021",
"settings": {},
"description": "Execute iLogic rule within a part file."
}
  • WorkItem

{
   "activityId": "{specify activity id + alias}",
   "arguments": {
       "InventorDoc": {
           "url": "{specify input file url}"        }, "iLogicName": {     "value": "iLogicVb.RunRule("MyRule")" }, "OutputIpt": {    "url": "{specify output file url}",    "verb": "put" }     } }

 

ここまででの指定を見て、お気づきの方がいらっしゃるかもしれませんがMyRuleを実行している、iLogicVb.RunRule()というのは、i引数で指定された名前をもつiLogicルールを実行するLogicの組み込み関数です。

 

つまりWorkItem、ActivityでiLogicVb.RunRule()を記載していた箇所に ( n は改行): “Trace.TraceInformation(“Start of MyScript”)nTrace.TraceInformation(“Setting height to 2 cm”)nParameter(“height”) = “2 cm”nTrace.TraceInformation(“Updating document”)nInventorVb.DocumentUpdate()nTrace.TraceInformation(“Saving changes”)nThisDoc.Save()nTrace.TraceInformation(“End of MyScript”)”といった形で、直接iLogicの処理を記述して実行させることも可能です。 

ただし、この方法は実行するiLogicの見通しが非常に悪いためあまりお勧めできる方法ではありません。

 

2-1.外部ファイルのiLogicをクラウドストレージに格納し、WorkItemのParameterで指定する方法

それでは、外部ファイルのiLogicを指定する方法について解説をしていきます。

基本的には、設計データ内のiLogicを実行する方法で説明をした方法とほぼ同じで、/s オプションに外部ファイルのURLを指定する形となります。

入力ファイル中のiLogicルールを実行する場合と異なり、iLogicが記載されたファイルもクラウドストレージにアップロードしてURLを指定する必要がある点にご注意ください。

 

  • Activity
{
"id": "{ActivityId}",
"commandLine": [
"$(engine.path)\InventorCoreConsole.exe /i "$(args[InventorDoc].path)" /s  $(args[iLogicVb].path)"
],
"parameters": {
"InventorDoc": {
"verb": "get",
"localName": "Input.ipt"
},
"iLogicVb": {
"verb": "get"
"localName": "input.iLogicVb" }, "OutputIpt": { "localName": "Input.ipt", "verb": "post" } }, "engine": "Autodesk.Inventor+2021", "settings": {}, "description": "Execute specified iLogic file." }
  • WorkItem

{
   "activityId": "{specify activity id + alias}",
   "arguments": {
       "InventorDoc": {
           "url": "{specify input file url}"        }, "iLogicVb": {     "url": "{specify input iLogic file url}" }, "OutputIpt": {    "url": "{specify output file url}",    "verb": "put" }     } }

 

2-2.外部ファイルのiLogicをクラウドストレージに格納し、ActivityのSettingで指定する方法

2-1では、外部ファイルをWorkItemで指定しましたが、WorkItem実行毎に同じiLogicを実行したい場合など毎回WorkItemでiLogicファイルのURLを指定するのは面倒です。

このような場合には、ActivityにiLogicファイルを指定することも可能です。

この場合の指定は以下のようになります。parameterではなくsettingsセクションでiLogicファイルを指定していることにご注意ください。

  • Activity
{
    "id": "{ActivityId}",
    "commandLine": [
        "$(engine.path)\InventorCoreConsole.exe /i "$(args[InventorDoc].path)" /s $(settings[iLogicVb].path)"
    ],
    "parameters": {
      "InventorDoc": {
        "verb": "get",
        "localName": "Input.ipt"
      },
      "OutputIpt": {
        "localName": "Input.ipt",
        "verb": "post"
      }
    },
    "engine": "Autodesk.Inventor+2021",
    "settings": {
        "iLogicVb":{
            "url":"{specify input iLogic file url}",
           "verb": "get"         }     },     "description": "Execute specified iLogic file" }
 

この場合WorkItemではiLogicファイルのURLを指定は不要となります。

  • WorkItem
{
   "activityId": "{specify activity id + alias}",
   "arguments": {
        "InventorDoc": {
            "url": "{specify input file url}"
        },
        "OutputIpt": {
            "url": "{specify output file url}",
            "verb": "put"
        }
    }
}

 

 

2-3.外部ファイルのiLogicをAppBundleに含める方法

AppBundleにアップロードするzipファイル内にiLogicファイルを含めて、iLogicを実行する方法です。

  • AppBundle

以下のスクリーンショット例のようにAppBundle内にiLogicVbファイルを含めて、AppBundleを作成します。

Tempsnip

 

  • Activity

ActivityでAppbundle内のiLogicVbファイルを/sオプションに指定します。

この時、 AppBundleを /al オプションで指定してしまった場合、 /s オプションで指定したiLogicコードは無視され読み込まれないことに注意をしてください。

{
    "id": "{ActivityId}",
    "commandLine": [
        "$(engine.path)\InventorCoreConsole.exe /i "$(args[InventorDoc].path)" /s "$(appbundles[{AppBundle Name}].path)\iLogicPlugin.bundle\Contents\input.iLogicVb""
    ],
    "parameters": {
      "InventorDoc": {
        "verb": "get",
        "localName": "Input.ipt"
      },
      "OutputIpt": {
        "localName": "Input.ipt",
        "verb": "post"
      }
    },
    "engine": "Autodesk.Inventor+2021",
    "settings": {}
    },
   "appbundles": [{AppBundleId + AppBundleAlias}],     "description": "Execute iLogic file inside AppBundle" }
 

WorkItemからは、入力のiptファイルおよび出力先のURLを指定するだけとなります。

  • WorkItem
{
   "activityId": "{specify activity id + alias}",
   "arguments": {
        "InventorDoc": {
            "url": "{specify input file url}"
        },
        "OutputIpt": {
            "url": "{specify output file url}",
            "verb": "put"
        }
    }
}

 

Design AutomationでInventor iLogicにパラメータ指定して実行する方法

さて、ここまでDesing AutomationでiLogicを実行する方法について説明をしてきました。

これまでの説明では、iLogicはすべて、固定のロジックを実行する形となっていましたが、実際には、DesingAutomation実行時にiLogicに値を指定したい場合があるかと思います。ここでは、iLogicに値を渡す方法とiLogicからどのように値を取得するのかを解説をしたいと思います。

 

実は、iLogicに値を渡す方法はとても簡単でActivity でInventorCoreConsole.exeのコマンドラインに、既定のオプション以外の任意の名前を指定して値を渡すだけとなります。

 

以下はheightというオプションで値を渡すサンプルとなります。

 

  • Activity
{
    "id": "{ActivityId}",
    "commandLine": [
        "$(engine.path)\InventorCoreConsole.exe /i "$(args[InventorDoc].path)" /s $(settings[iLogicVb].path) /height $args[height].value"
    ],
    "parameters": {
      "InventorDoc": {
        "verb": "get",
        "localName": "Input.ipt"
      },
     "height": {         "verb": "read"       }       "OutputIpt": {         "localName": "Input.ipt",         "verb": "post"       }     },     "engine": "Autodesk.Inventor+2021",     "settings": {         "iLogicVb":{             "url":"{specify input iLogic file url}",
           "verb": "get"         }     },     "description": "Execute specified iLogic file" }
 

次に、WorkItemでは作成したパラメータのheightの値(以下の例では10.5)を指定します。

  • WorkItem
{
   "activityId": "{specify activity id + alias}",
   "arguments": {
        "InventorDoc": {
            "url": "{specify input file url}"
        },
       height{
           "value": "10.5"
       },        "OutputIpt": {            "url": "{specify output file url}",            "verb": "put"        }     } }

 

このWorkItemを実行するとiLgoic実行時に10.5という値が渡されますが、iLogic側からアクセスするには、iLogicのRuleArguments()関数を使用します。

例えば、次のようなコードでheightパラメータにアクセスをして、指定された値を元にパートのパラメータを変更し、ドキュメントを保存することができます。

 

  • iLogic
Trace.TraceInformation("Start of MyScript")
Trace.TraceInformation("Setting height to 2 in")
Parameter("height") = RuleArguments("height")
Trace.TraceInformation("Updating document")
InventorVb.DocumentUpdate()
Trace.TraceInformation("Saving changes")
ThisDoc.Save()
Trace.TraceInformation("End of MyScript")

 

RuleArguments関数については、以下に詳細の解説がありますのでご参照ください。

 

まとめ

今回の記事では、InventorのiLogicを利用したDesing Automation for Inventorについて、Design Automationの基本的なおさらい、Design AutomationでiLogicを実行する方法、iLogicにパラメータ値を指定して実行する方法について解説をしました。

 

非常に簡単に、Design AutomationからiLogicを利用できることをご理解いただけたのではないかと思います。

皆様も是非、iLogicを用いたDesing Automation for Inventorのご活用を検討いただければと存じます。

 

次回の記事では、Desing Automation for InventorでiLogicを使用する場合の注意点について解説をしたいと思います。

 

By Takehiro Kato


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading