Hi all,
I have got an Ilogic rule partially shown below.
It is meant to create a folder for each sheet metal thickness present in assembly.
The only problem is that it's creating empty folders for any possible part's parent.
I think the cause is in that "AllReferencedDocuments" but I don't know how to change it.
I have tried with "ComponentOccurrence" but it looks doesn't work with the SubType
For Each doc As Document In oAsmDoc.AllReferencedDocuments
If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
Try
R_Part = doc.DisplayName
oMaterial = doc.ComponentDefinition.Material.Name
oThickness = doc.ComponentDefinition.Thickness.value*10
Material_Path = New_Folder_Path & "\" & oMaterial & " - " & oThickness & "mm"
Solved! Go to Solution.
Solved by A.Acheson. Go to Solution.
Give this a shot. It's not elegant, but it works.
Dim MaterialAndThickness As New ArrayList
For Each doc As Document In oAsmDoc.AllReferencedDocuments
If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
R_Part = doc.DisplayName
MyStringValues = New String(){doc.ComponentDefinition.Material.Name, doc.ComponentDefinition.Thickness.value*10}
MaterialAndThickness.Add(MyStringValues)
End If
Next
For i = 0 To MaterialAndThickness.Count - 1
'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
Hi @aelqabbany
sorry for the late reply. I was on holiday and could not try it.
It is not doing what I need: basically is creating the folders but in the same way as the original rule. It is creating an empty folder with the material an thickness of the parent (multibody) and I would like to avoid it. I would like the rule to create folders only for the materials of the parts of the assembly.
Thanks.
Hi @aurel_e. If I understand the situation correctly, then you have given up on trying to include material thickness in the new folder names, because the sheet metal parts are multi-body parts, and the thickness retrieved is always the parent part's main thickness, and not necessarily the thickness you are looking for. Now you just want to create a new folder for each unique material found among the sheet metal parts in the assembly, correct? If so, then this should do that for you. This worked for a fairly simple example assembly I tested it on.
oAsmDoc = ThisAssembly.Document
Dim oMaterialNames As New List(Of String)
For Each oRefDoc As Document In oAsmDoc.ReferencedDocuments
If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
Dim oPDoc As PartDocument = oRefDoc
If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Continue For
Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
'oMaterialName = oSMDef.Material.Name
oMaterialName = oPDoc.ActiveMaterial.DisplayName
'oMaterialName = oPDoc.ActiveMaterial.Name
'MsgBox("oMaterialName = " & oMaterialName, , "")
If Not oMaterialNames.Contains(oMaterialName) Then
oMaterialNames.Add(oMaterialName)
End If
Next
'just to show them in a list
'a = InputListBox("", oMaterialNames,"", "Material Names", "List Of Material Names")
Dim oBasePath As String = "C:\Temp\"
For Each oName In oMaterialNames
oNewPath = oBasePath & oName
If Not System.IO.Directory.Exists(oNewPath) Then
System.IO.Directory.CreateDirectory(oNewPath)
End If
Next
I usually avoid using the SubType and those long unreadable strings, in favor of the several other readable ways of checking that, which usually just means a bit more code, but that's just my personal preference.
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb: or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)
Hi @WCrihfield
Thank you very much
It is not what I was looking but it helped me to find the solution.
Your code is creating exactly the folders of existing sheetmetal components, while mine was creating additional empty folders referring to the parent multibody material.
I have been analyzing for hours your code (I am quite ignorant in ilogic syntax 😀) and at the end the problem was in this line:
For Each doc As Document In oAsmDoc.ReferencedDocuments
Mine was AllReferencedDocuments
I was not spotting that, looked exactly the same but was making the difference.
Thanks again.
@aurel_e wrote:
I have been analyzing for hours your code (I am quite ignorant in ilogic syntax 😀) and at the end the problem was in this line:
For Each doc As Document In oAsmDoc.ReferencedDocumentsMine was AllReferencedDocuments
I was not spotting that, looked exactly the same but was making the difference.
Thanks again.
I thought that was the solution, but now I am realizing now the rule is not looking inside the subassemblies and not exporting their sheetmetal flat patterns.
Any idea?
Here is hopefully what you were looking for. The key is to exclude the reference to the base component of the derived files. This post here gave me the filter (ReferencedFileDescriptors) to look for that reference and once we have that reference in this case the full file name we can remove it from the list of full file names retrieved in the first loop of all referenced documents. .
oAsmDoc = ThisAssembly.Document Dim New_Folder_Path As String New_Folder_Path = "C:\Temp" Dim AllFiles As New ArrayList 'Define array lists Dim FlatPatternFiles As New ArrayList Dim MaterialAndThickness As New ArrayList For Each doc As Document In oAsmDoc.AllReferencedDocuments 'Look at all Referenced Documents If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then 'Filter for Sheetmetal Documents AllFiles.Add(doc.FullFileName)'Add file name to list FlatPatternFiles.Add(doc.FullFileName)'Add file name to list '[Check each part for referenced file indicating a Base Component is present Dim oFile As File oFile = doc.File Dim oFileDescriptor As FileDescriptor If oFile.ReferencedFileDescriptors.Count = 0 Then''Check for further Referenced files ( Derived Parts)>0 indicates derived part Else For Each oFileDescriptor In oFile.ReferencedFileDescriptors sDerivePart = oFileDescriptor.FullFileName If FlatPatternFiles.Contains(sDerivePart) Then' Check for reference file FlatPatternFiles.Remove(sDerivePart)'Remove if found End If Next End If End If Next '] d0 = InputListBox("Prompt", AllFiles, 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
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
Thank you @A.Acheson
It looks that adding the occurrence qty check solved it.
It is a long, more complex rule. For the moment I can't spot any problem. If I do, I will ask again.
Thanks to @WCrihfield and @aelqabbany as well for the support.
Can't find what you're looking for? Ask the community or share your knowledge.