08-09-2023
07:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
08-09-2023
07:05 AM
Thank you for your reply.
I've taken the snippet you've provided and added it to my document loop of my assembly and modified it a bit to match what I'm wanting.
I do want the user running the code to select the material library and material name in the assembly environment through the input list boxes created by the block.
The only issue I'm running into is getting the user's selection from the assembly to the part.
However, when I run the newly modified code I'm presented with an error.
Please see attached for the error.
I've also pasted the modified code below.
I feel like I'm close now, thanks to your contributions (I realized that some of the code I've copied is actually some of your older works).
Sub Main()
oADOC = ThisDoc.ModelDocument
If oADOC.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("This rule cannot be executed in this file type.", "iLogic")
Exit Sub
End If
Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oTransaction As Transaction = ThisApplication.TransactionManager.StartTransaction(oAsm, "FG Member Material Changer") 'Make this a single transaction
Dim oAL_List As New ArrayList
Dim lib_List As New ArrayList
For Each oAL As AssetLibrary In ThisApplication.AssetLibraries
If InStr(oAL.DisplayName , "Material") <> 0 Then
oAL_List.Add(oAL.DisplayName)
End If
Next
Dim oALSelect As String = InputListBox("Select Asset Library", oAL_List, oAL_List(0), "Change Part Material", "Available Selections")
If oALSelect Is Nothing Then Exit Sub
Dim oLib As AssetLibrary =ThisApplication.AssetLibraries(oALSelect)
For Each libAsset In oLib.MaterialAssets
lib_List.Add(libAsset.DisplayName)
Next
lib_List.Sort
Dim oLibAssetSel As String = InputListBox("Select Material", lib_List, lib_List(0), "Change Part Material", "Available Selections")
If oLibAssetSel Is Nothing Then Exit Sub
For Each oDoc As Document In oAsm.AllReferencedDocuments 'Traverse all referenced documents
If oDoc.DocumentInterests.HasInterest("{AC211AE0-A7A5-4589-916D-81C529DA6D17}") _ 'Frame generator component
AndAlso oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject _'Part
AndAlso oDoc.IsModifiable _ 'Modifiable (not reference skeleton)
AndAlso oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0 'Exists in assembly (not derived base component)
Dim oDoc1 As PartDocument = ThisApplication.ActiveDocument
Dim assetLib As AssetLibrary = ThisApplication.AssetLibraries.Item(oLib)
ThisApplication.ActiveMaterialLibrary = assetLib
Dim MaterialName As String = oLibAssetSel
Try
Dim localMaterial As MaterialAsset = oDoc1.MaterialAssets.Item(MaterialName)
Catch
Dim libMaterial As MaterialAsset = assetLib.MaterialAssets.Item(MaterialName)
libMaterial.CopyTo(oDoc1)
End Try
oDoc1.ActiveMaterial = oDoc1.MaterialAssets.Item(MaterialName)
iLogicVb.UpdateWhenDone = True
End If
Next
oTransaction.End
End Sub