「Inventor 2014 API 新機能 その1」「Inventor 2014 API 新機能 その2」に続き、Inventor 2014の新しいAPIについてご紹介させていただきます。
Inventor 2014 製品では、材料(マテリアル)の設定の他に、新たにCivil3D製品などと同じ「外観」というコンセプトが導入されました。
これにより、「スタイルおよび規格エディター」で設定されていた「レンダー」機能が取り除かれ、「外観」に置き換わります。
今回は、この「外観」を中心にご案内させていただきます。
1. アセンブリファイル内の「オカレンス」を規定で用意されている「外観ライブラリ」よりドキュメント毎に設定できる「ローカルな外観コレクション」に追加し、オカレンスの外観表現をオーバーライドして変更する。
' アセンブリ内のオカレンスに対し Assetオブジェクトを使用し
' 外観の表現を変更
Public Sub SetOccurrenceAppearance()
On Error Resume Next
Dim asmDoc As AssemblyDocument
Set asmDoc = ThisApplication.ActiveDocument
If asmDoc Is Nothing Then
MsgBox "AssemblyJointサンプルを動作させるか" & vbCrLf _
& "アセンブリファイルを開いてください"
Exit Sub
End If
' ドキュメントの外観を得る。
' ローカルな外観が設定されていない場合は、ドキュメントの外観ライブラリより
' 外観オブジェクトにCopyToメソッドを使ってセットする
Dim localAsset As Asset
Set localAsset = asmDoc.AppearanceAssets.Item("ACADGen-082")
On Error GoTo 0
If localAsset Is Nothing Then
' ライブラリ名でエラーの場合は、内部名が使用可能
Dim assetLib As AssetLibrary
Set assetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")
'Set assetLib = ThisApplication.AssetLibraries.Item("314DE259-5443-4621-BFBD-1730C6CC9AE9")
' ライブラリ内より「ACADGen-082」のAssetを確保
Dim libAsset As Asset
Set libAsset = assetLib.AppearanceAssets.Item("ACADGen-082")
' ライブラリからドキュメントのAppearenceAssetsコレクションにCopyする
Set localAsset = libAsset.CopyTo(asmDoc)
End If
On Error GoTo 0
' オカレンスの選択
Dim occ As ComponentOccurrence
Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select an occurrence.")
' オカレンスの外観を変更
occ.appearance = localAsset
End Sub
2. ドキュメントに「任意の外観オブジェクトを新たに作成」し、作成した任意の外観オブジェクトを用いて、フェースやオカレンスの外観表現をオーバーライド設定する。
' カスタム外観オブジェクトの作成
Public Sub CreateSimpleColorAppearance()
On Error Resume Next
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
If doc Is Nothing Then
MsgBox "AssemblyJointサンプルを動作させるか" & vbCrLf _
& "パーツファイルを開いてください"
Exit Sub
End If
' ドキュメントの外観コレクションの確保
Dim docAssets As Assets
Set docAssets = doc.Assets
' カスタム外観オブジェクトの存在チェック
Dim appearance As Asset
For Each appearance In docAssets
If appearance.DisplayName = "My Shiny Red Color" Then
Exit For
End If
Next
On Error Resume Next
If appearance Is Nothing Then
On Error GoTo 0
' 外観オブジェクト(Asset)の新規作成
Set appearance = docAssets.Add(kAssetTypeAppearance, "Generic", _
"MyShinyRed", "My Shiny Red Color")
Dim tobjs As TransientObjects
Set tobjs = ThisApplication.TransientObjects
'
Dim color As ColorAssetValue
Set color = appearance.Item("generic_diffuse")
color.value = tobjs.CreateColor(255, 15, 15)
Dim floatValue As FloatAssetValue
Set floatValue = appearance.Item("generic_reflectivity_at_0deg")
floatValue.value = 0.5
Set floatValue = appearance.Item("generic_reflectivity_at_90deg")
floatValue.value = 0.5
End If
On Error GoTo 0
' 外観を変更するオブジェクトの選択
Dim occ As Object
If doc.DocumentType = kPartDocumentObject Then
Set occ = ThisApplication.CommandManager.Pick(kAllEntitiesFilter, "Select a Face.")
ElseIf doc.DocumentType = kAssemblyDocumentObject Then
Set occ = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select an occurrence.")
End If
' オブジェクトの外観表現を変更
occ.appearance = appearance
End Sub
3. アセンブリファイル内のオーバーライドされた外観表現をマテリアルで指定された状態に戻す
Public Sub RemoveAssemblyOverrides()
' アセンブリドキュメントを得る
Dim asmDoc As AssemblyDocument
Set asmDoc = ThisApplication.ActiveDocument
' イテレートしてオーバーライドされたComponentOccurrenceオブジェクトを確保
Dim obj As ComponentOccurrence
For Each obj In asmDoc.ComponentDefinition.AppearanceOverridesObjects
' パーツのオリジナルの色に戻す
obj.AppearanceSourceType = kPartAppearance
Next
End Sub
4. パーツファイル内のオーバーライドされた外観表現を「フィーチャどおり」状態に戻す。
' パーツファイル内のオーバーライドされた外観表現を元に戻す
Public Sub RemovePartOverrides()
' アクティブなパーツファイルの確保
Dim partDoc As PartDocument
Set partDoc = ThisApplication.ActiveDocument
' イテレートしてオーバーライドされたObjectを確保
Dim obj As Object
For Each obj In partDoc.ComponentDefinition.AppearanceOverridesObjects
'それぞれのオブジェクトタイプによって、オリジナルに戻す
' サーフェスボディの場合
If TypeOf obj Is SurfaceBody Then
obj.AppearanceSourceType = kPartAppearance
' パールツィーチャーの場合
ElseIf TypeOf obj Is PartFeature Then
obj.AppearanceSourceType = kBodyAppearance
' フェースの場合
ElseIf TypeOf obj Is Face Then
obj.AppearanceSourceType = kFeatureAppearance
' その他
Else
MsgBox "Unexpected type with appearance override: " & TypeName(obj)
End If
Next
End Sub
サンプルは ここ よりダウンロードする事ができます。
(解凍後、C:\TempホルダーにInventor2014_J_Ver2.ivbとSamplePart.ipt"を配置し、「ツール」->「アプリケーションオプション」の「ファイル」タブ内「既存のVBAプロジェクト」に「C:\Temp\Inventor2014_J_Ver2.ivb」と設定する事で動作確認ができます)
By Shigekazu Saito.
コメント