Autodesk Inventorの自動化ツール「iLogic」。設計作業を劇的に効率化できる機能だけど、プログラミング初心者にとってはハードルが高く感じることも多い。そこで役立つのがChatGPTだ。
ChatGPTでiLogicコードを解読しよう
iLogicはVisual Basicベースのスクリプトを活用する仕組み。コードを読めば理解できるとはいえ、最初は意味が分からないことも多い。そんなときは、コードをChatGPTに読み込ませれば、直感的に理解できる解説をしてくれる。
例えば、Autodeskのサポートページで提供されているサンプルファイル「BrewMain-Drawing-ModelState.zip」内のiLogicコードを見てみよう。
「MakeBoiloverTray」ルール
Using New ActiveDocumentFixer(ThisApplication, ThisDoc.Document)
ThisAssembly.BeginManage
Dim triggers = Tiers & kettleSize
If (boilOverTrays) Then
Dim newComp = Components.Add("BoilOverTray", "Boil Over Tray.ipt")
Constraints.AddMate("Mate:27", newComp, "Face2", "LowerHorizontalTube", "Right")
Constraints.AddFlush("Flush:23", newComp, "Face0", "LowerHorizontalTube", "Front")
Constraints.AddMate("Mate:26", newComp, "Face3", "RightTube","Left")
Patterns.AddRectangular("TrayPattern", "BoilOverTray", 3 ul,
dimSectionWidth, "", "X Axis",
columnNaturalDirection := False)
End If
ThisAssembly.EndManage
このコードをChatGPTに入力すると、例えばこんな解説をしてくれる。
ChatGPTの解説:
このiLogicコードは、Inventorのアセンブリ内で「Boil Over Tray」というコンポーネントを追加し、それに対して拘束(Constraints)とパターン(Patterns)を適用する処理を行っています。以下、コードの解説をしていきます。
コードの解説
Using New ActiveDocumentFixer(ThisApplication, ThisDoc.Document)
ActiveDocumentFixerを使用して、現在のInventorのドキュメントを適切に管理する。ThisApplicationは現在のInventorインスタンス、ThisDoc.Documentは現在のアクティブなドキュメントを指す。
ThisAssembly.BeginManage
- アセンブリの管理処理を開始。
Dim triggers = Tiers & kettleSize
Tiers(段数?)とkettleSize(ケトルのサイズ)を連結して、triggers変数を作成。
If (boilOverTrays) Then
boilOverTraysがTrueの場合に以下の処理を実行。
Dim newComp = Components.Add("BoilOverTray", "Boil Over Tray.ipt")
- 新しいコンポーネント
Boil Over Tray.iptをアセンブリに追加し、newCompに格納。
- 拘束(Constraints)
Constraints.AddMate("Mate:27", newComp, "Face2", "LowerHorizontalTube", "Right")newCompの"Face2"を"LowerHorizontalTube"の"Right"面にメイト拘束(Mate)を追加。Constraints.AddFlush("Flush:23", newComp, "Face0", "LowerHorizontalTube", "Front")newCompの"Face0"を"LowerHorizontalTube"の"Front"面にフラッシュ拘束(Flush)を追加。Constraints.AddMate("Mate:26", newComp, "Face3", "RightTube", "Left")newCompの"Face3"を"RightTube"の"Left"面にメイト拘束(Mate)を追加。
- パターンの適用(Patterns)
Patterns.AddRectangular("TrayPattern", "BoilOverTray", 3, dimSectionWidth, "", "X Axis", columnNaturalDirection := False)BoilOverTrayを 3 回、dimSectionWidthの間隔でX軸方向に直線配列(Rectangular Pattern)。columnNaturalDirection := Falseにより、列の方向を指定。
ThisAssembly.EndManage
- アセンブリの管理を終了。
End Using
Usingステートメントを終了し、ActiveDocumentFixerによる管理を解放。
さらに、もう一つのコード例を見てみよう。
「MakeFeet」ルール
Dim triggers = Tiers & kettleSize
Using New ActiveDocumentFixer(ThisApplication, ThisDoc.Document)
ThisAssembly.BeginManage
Dim Pad_1Pos = ThisAssembly.Geometry.Point(0,0,0)
Dim Pad_1 = Components.Add("Foot", "Pad.ipt", Pad_1Pos)
Constraints.AddMate("FootOnGround", "Foot", "Bottom", "", "XY Plane")
Constraints.AddFlush("SideFlush", "Foot", "YZ Plane", "", "YZ Plane")
Constraints.AddMate("FootOtherSide", "", "XZ Plane", "Foot", "XY Plane")
Patterns.AddRectangular("FeetPattern", "Foot",
2 ul, dimSectionWidth * 3 ul, "", "X Axis", columnNaturalDirection := False,
rowCount := 2 ul,
rowOffset := dimDepth - dimTubingSize,
rowComponent := "",
rowEntityName := "Y Axis")
ThisAssembly.EndManage
End Using
これをChatGPTに渡すと・・・
このiLogicコードの解説
このコードは、Inventorのアセンブリ内で「Foot(Pad.ipt)」というコンポーネントを追加し、それに対して拘束(Constraints)とパターン(Patterns)を適用する処理を行っています。特に、フットパーツをXY面に配置し、それをX方向とY方向にパターン配置するスクリプトになっています。
コードの分解と解説
1. Dim triggers = Tiers & kettleSize
Tiers(段数)とkettleSize(ケトルのサイズ)を連結して、triggers変数に格納。- これは条件分岐やトリガー設定に使われる可能性がある。
2. Using New ActiveDocumentFixer(ThisApplication, ThisDoc.Document)
ActiveDocumentFixerを使って現在のInventorのドキュメントを適切に管理。ThisApplicationは現在のInventorインスタンス、ThisDoc.Documentは現在のアクティブなドキュメントを指す。
3. ThisAssembly.BeginManage
- アセンブリの変更管理を開始。
4. フットパッドの配置
フットの挿入
vb
コピーする編集する
Dim Pad_1Pos = ThisAssembly.Geometry.Point(0,0,0)
Dim Pad_1 = Components.Add("Foot", "Pad.ipt", Pad_1Pos)
Pad_1Posに(0,0,0)の位置を設定し、新しいフットパーツ (Pad.ipt) を追加。
5. 拘束の追加
vb
コピーする編集する
Constraints.AddMate("FootOnGround", "Foot", "Bottom", "", "XY Plane")
"Foot"の"Bottom"面を"XY Plane"にメイト拘束(Mate)。- これによりフットパーツがXY面(床)に固定される。
vb
コピーする編集する
Constraints.AddFlush("SideFlush", "Foot", "YZ Plane", "", "YZ Plane")
"Foot"の"YZ Plane"をアセンブリの"YZ Plane"にフラッシュ拘束(Flush)。- これにより左右の位置がそろう。
vb
コピーする編集する
Constraints.AddMate("FootOtherSide", "", "XZ Plane", "Foot", "XY Plane")
"XZ Plane"に"Foot"の"XY Plane"をメイト拘束(Mate)。- これにより、フットの正しい方向が保たれる。
6. 矩形パターンの適用
vb
コピーする編集する
Patterns.AddRectangular("FeetPattern", "Foot",
2 ul, dimSectionWidth * 3 ul, "", "X Axis", columnNaturalDirection := False,
rowCount := 2 ul,
rowOffset := dimDepth - dimTubingSize,
rowComponent := "",
rowEntityName := "Y Axis")
"Foot"を X軸方向に2回 配置(間隔dimSectionWidth * 3)。"Y Axis"にも 2回 配置(間隔dimDepth - dimTubingSize)。columnNaturalDirection := Falseに設定し、自然な方向と逆向きの配置を指定。
7. ThisAssembly.EndManage
- アセンブリの変更管理を終了。
8. End Using
Usingステートメントを終了し、ActiveDocumentFixerによる管理を解放。
ChatGPTを活用すればiLogic初心者でも大丈夫
iLogicの最大の課題は、コードの理解に時間がかかること。でも、ChatGPTを使えば、すぐにコードの意味を把握できる。
また、「このコードをもっと最適化できる?」と聞くことで、より効率的な書き方やエラーハンドリングの方法を教えてくれる。iLogicの学習をスムーズに進めるために、ぜひChatGPTを活用してみよう!
