API 提供了全面的材质库相关对象和方法。首先看看对象层次结构。
在Application下有AssetLibraries 集合,表示的所有材质库(AssetLibrary),包括自定义的材质库。每个库中有类别集合,叫做AssetCategories, 每个类别叫做AssetCategory,每类中的具体是具体的材质(材料或外观),都叫做Asset。Asset的属性是AssetValue的集合, 所有asset 是以下类型值的集合:
- Boolean
- Choice
- Color
- Filename
- Float
- Integer
- Reference
- String
- Texture
这里有段代码,用来访问文档所有Asset的信息。
Public Sub DumpDocumentAppearances()
' 当前文档是装配或零件.
If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject And ThisApplication.ActiveDocumentType <> kPartDocumentObject Then
MsgBox "A part or assembly must be active."
Exit Sub
End If
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
' 信息写入一个文本文件
Open "C:\Temp\DocumentAppearanceDump.txt" For Output As #1
' 遍历文档中每个材质
Dim appearance As Asset
For Each appearance In doc.AppearanceAssets
Print #1, " Appearance"
Print #1, " DisplayName: " & appearance.DisplayName
Print #1, " HasTexture: " & appearance.HasTexture
Print #1, " IsReadOnly: " & appearance.IsReadOnly
Print #1, " Name: " & appearance.Name
' 遍历材质所有属性
Dim value As AssetValue
For Each value In appearance
Call PrintAssetValue(value, 8)
Next
Next
Close #1
MsgBox "Finished writing output to ""C:\Temp\DocumentAppearanceDump.txt"""
End Sub
' 打印材质Value的具体信息
Private Sub PrintAssetValue(InValue As AssetValue, Indent As Integer)
Dim indentChars As String
indentChars = Space(Indent)
Print #1, indentChars & "Value"
Print #1, indentCh