After looking at this again I tried working directly with the leaf occurrences. It has the benefit of filtering out derived base component documents(derived part parent). This will bring back more than one occurrence of a part so an easy way to just process one document is to add the full file name to an arraylist and then transfer to another to filter out these extra occurrences.
Dim AssyDoc As AssemblyDocument
AssyDoc = ThisAssembly.Document
Dim AssyDef As AssemblyComponentDefinition
AssyDef = AssyDoc.ComponentDefinition
Dim New_Folder_Path As String
New_Folder_Path = "C:\Temp"
Dim SMFiles As New ArrayList 'Define array lists
Dim FlatPatternFiles As New ArrayList
Dim MaterialAndThickness As New ArrayList
For Each Leafocc In AssyDef.Occurrences.AllLeafOccurrences
Dim LeafDoc As Document
LeafDoc = Leafocc.Definition.Document
If LeafDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 'Filter for Sheetmetal Documents
SMFiles.Add(LeafDoc.FullFileName)
If Not FlatPatternFiles.Contains(LeafDoc.FullFileName) Then FlatPatternFiles.Add(LeafDoc.FullFileName)'Remove Duplicates using string.contains method
End If
Next
d0 = InputListBox("Prompt", SMFiles, d0, Title := "AllFiles", ListName := "List")'Quick Check of list created
d0 = InputListBox("Prompt", FlatPatternFiles, d0, Title := "FlatPatternFiles", ListName := "List")'Quick Check of list created
Check = MessageBox.Show("Do You want to create the Directories?", "iLogic", MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation)
If Check = vbOK Then
For Each oFile In FlatPatternFiles 'Iterate through valid files and look for Material and Thickness
oPartDoc = ThisApplication.Documents.Open(oFile, False)'Open Part
MyStringValues = New String(){oPartDoc.ComponentDefinition.Material.Name, oPartDoc.ComponentDefinition.Thickness.value*10}
MaterialAndThickness.Add(MyStringValues)
Next
For i = 0 To MaterialAndThickness.Count - 1 'Iterate thrrough and create directories
'MessageBox.Show(MaterialAndThickness(i)(0), MaterialAndThickness(i)(1))
Material_Path = New_Folder_Path & "\" & MaterialAndThickness(i)(0) & " - " & MaterialAndThickness(i)(1) & "mm"
System.IO.Directory.CreateDirectory(Material_Path)
Next
MessageBox.Show("Directory Creation Completed", "iLogic")
ElseIf Check = vbCancel Then
MessageBox.Show("Canceled", "iLogic")
End If
Another method is to work with the referenced documents and check for referenced documents containing a collection of occurrences using the ComponentOccurrencesEnumerator object. This will allow us to retrieve the first occurrence and then either work directly with this or retrieve the document reference and work with that. It has the benefit also of filtering out the derived part parent as it is not an occurrence. Here is a nice post where its use is clear to see. And here is the API help for source relationships.
Dim AssyDoc As AssemblyDocument
AssyDoc = ThisAssembly.Document
Dim AssyDef As AssemblyComponentDefinition
AssyDef = AssyDoc.ComponentDefinition
Dim New_Folder_Path As String
New_Folder_Path = "C:\Temp"
Dim SMFiles As New ArrayList 'Define array lists
Dim FlatPatternFiles As New ArrayList 'Define array list of files containing flatpatterns to process
Dim MaterialAndThickness As New ArrayList
For Each RefDoc As Document In AssyDoc.AllReferencedDocuments
If RefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 'Filter for Sheetmetal Documents
SMFiles.Add(RefDoc.FullFileName)
Dim RefOccs As ComponentOccurrencesEnumerator
RefOccs = AssyDef.Occurrences.AllReferencedOccurrences(RefDoc)'Reference the specific document in the assembly and check if it contains occurrences
If RefOccs.Count >0 Then 'Check the qty of occurrences there
Dim RefOcc As ComponentOccurrence
RefOcc = RefOccs(1)'Get reference to the 1st occurrence of this document in this assembly
'Do some work here or just work with document below
Dim RefOccDoc As PartDocument
RefOccDoc = RefOcc.Definition.Document
FlatPatternFiles.Add(RefOccDoc.FullFileName)
End If
End If
Next
d0 = InputListBox("Prompt", SMFiles, d0, Title := "FlatPatternFiles", ListName := "List")'Quick Check of list created
d0 = InputListBox("Prompt", FlatPatternFiles, d0, Title := "FlatPatternFiles", ListName := "List")'Quick Check of list created
Check = MessageBox.Show("Do You want to create the Directories?", "iLogic", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
If Check = vbOK Then
For Each oFile In FlatPatternFiles 'Iterate through valid files and look for Material and Thickness
oPartDoc = ThisApplication.Documents.Open(oFile, False)'Open Part
MyStringValues = New String(){oPartDoc.ComponentDefinition.Material.Name, oPartDoc.ComponentDefinition.Thickness.value*10}
MaterialAndThickness.Add(MyStringValues)
Next
For i = 0 To MaterialAndThickness.Count - 1 'Iterate thrrough and create directories
'MessageBox.Show(MaterialAndThickness(i)(0), MaterialAndThickness(i)(1))
Material_Path = New_Folder_Path & "\" & MaterialAndThickness(i)(0) & " - " & MaterialAndThickness(i)(1) & "mm"
System.IO.Directory.CreateDirectory(Material_Path)
Next
MessageBox.Show("Directory Creation Completed", "iLogic")
ElseIf Check = vbCancel Then
MessageBox.Show("Canceled", "iLogic")
End If
If this solved a problem, please click (accept) as solution.
Or if this helped you, please, click (like)
Regards
Alan