- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to retrieve the parent(s) of each occurrence in the master assembly. The rule I have is not giving the correct parent data. I request someone help. Thanks.
Public Class RWEI_0001A Shared oTextSave As String = "C:\Users\Public\Documents\iLogicBuffer.txt" Sub Main() 'Accessing Assembly Components @ https://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html Dim oDashes As String = "" Dim iL_NO As String = "0001A-BOM" Dim iL_Name As String = "All Occurrences with BOM Structure" Dim iL_FullName As String = "Rule " & iL_NO & ": " & iL_Name Dim oDoc As Document = ThisApplication.ActiveDocument oDelete_ex_Notepad(oTextSave, iL_NO) oHL1 = "FILE REFERENCE TREE RAN FROM: " & oDoc.FullFileName oWrite_HeaderLines(oTextSave, oHL1) oHL2 = iL_FullName oWrite_HeaderLines(oTextSave, oHL2) For i = 1 To Len(iL_FullName) oDashes = oDashes & "-" Next i oWrite_DashLine(oTextSave, oDashes) ' Get the active assembly. Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument ' Call the function that does the recursion. Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1) Process.Start("Notepad.exe", oTextSave) End Sub Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) ' Iterate through all of the occurrence in this collection. This ' represents the occurrences at the top level of an assembly. Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences ' Print the name of the current occurrence. ' Check to see if this occurrence represents a subassembly ' and recursively call this function to traverse through it. If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) End If oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) oData_X = oParent & "@" & oOcc.Name & "@" & "Level - " & Level oWrite_Data(oTextSave, oData_X) ' MessageBox.Show(oOcc.Parent.Document.DisplayName) Next End Sub Function GetOccurrenceParent(oOcc As ComponentOccurrence, oOccName As String) Return oOcc.Parent.Document.DisplayName End Function 'Process.Start("Notepad.exe", oTextSave) Sub oWrite_Data(oTextSave As String, oData As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oData) oWrite.Flush() oWrite.Close End Sub Sub oWrite_HeaderLines(oTextSave As String, oHeaderLine As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oHeaderLine) oWrite.Flush() oWrite.Close End Sub Sub oWrite_DashLine(oTextSave As String, oDashes As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oDashes) oWrite.Flush() oWrite.Close End Sub Sub oDelete_ex_Notepad(oTextSave As String, iL_NO As String) 'To delete oTextSave if existing. ' Dim FileDelete As String ' FileDelete = "C:\testDelete.txt" If System.IO.File.Exists(oTextSave) = True Then System.IO.File.Delete(oTextSave) MessageBox.Show("Existing (if any) File Deleted", iL_NO) End If End Sub End Class 'Function GetOccurrenceParent(oOcc As ComponentOccurrence, oOccName As String) ' For Each occ In oSubOcc.SubOccurrences ' If occ.SubOccurrences.Count <> 0 Then ' If occ.Name = PartNameToLookFor Then ' MessageBox.Show(occ.Name & " is inside " & occ.ParentOccurrence.Name, "Title") ' Else ' GetOccurrenceParent(occ, PartNameToLookFor) ' End If ' Else ' If occ.Name = PartNameToLookFor Then ' MessageBox.Show(occ.Name & " is inside " & occ.ParentOccurrence.Name, "Title") ' End If ' End If ' Next ' Return oParent 'End Function
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I tweak your code a bit, I think it's now what you're looking for. Please check it out and see if we can improve it. regards
Public Class RWEI_0001A Shared oTextSave As String = "C:\Users\Public\Documents\iLogicBuffer.txt" Sub Main() 'Accessing Assembly Components @ https://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html Dim oDashes As String = "" Dim iL_NO As String = "0001A-BOM" Dim iL_Name As String = "All Occurrences with BOM Structure" Dim iL_FullName As String = "Rule " & iL_NO & ": " & iL_Name Dim oDoc As Document = ThisApplication.ActiveDocument oDelete_ex_Notepad(oTextSave, iL_NO) oHL1 = "FILE REFERENCE TREE RAN FROM: " & oDoc.FullFileName oWrite_HeaderLines(oTextSave, oHL1) oHL2 = iL_FullName oWrite_HeaderLines(oTextSave, oHL2) For i = 1 To Len(iL_FullName) oDashes = oDashes & "-" Next i oWrite_DashLine(oTextSave, oDashes) ' Get the active assembly. Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument ' Call the function that does the recursion. Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1) Process.Start("Notepad.exe", oTextSave) End Sub Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) ' Iterate through all of the occurrence in this collection. This ' represents the occurrences at the top level of an assembly. Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences ' Print the name of the current occurrence. ' Check to see if this occurrence represents a subassembly ' and recursively call this function to traverse through it. If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) End If Try Dim oParentocc As ComponentOccurrence oParentocc = oOcc.ParentOccurrence oParent = oParentocc.Name Catch oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) End Try oData_X = oParent & "@" & oOcc.Name & "@" & "Level - " & Level oWrite_Data(oTextSave, oData_X) Next End Sub Function GetOccurrenceParent(oOcc As ComponentOccurrence, oOccName As String) Return oOcc.Parent.Document.DisplayName End Function 'Process.Start("Notepad.exe", oTextSave) Sub oWrite_Data(oTextSave As String, oData As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oData) oWrite.Flush() oWrite.Close End Sub Sub oWrite_HeaderLines(oTextSave As String, oHeaderLine As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oHeaderLine) oWrite.Flush() oWrite.Close End Sub Sub oWrite_DashLine(oTextSave As String, oDashes As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oDashes) oWrite.Flush() oWrite.Close End Sub Sub oDelete_ex_Notepad(oTextSave As String, iL_NO As String) 'To delete oTextSave if existing. ' Dim FileDelete As String ' FileDelete = "C:\testDelete.txt" If System.IO.File.Exists(oTextSave) = True Then System.IO.File.Delete(oTextSave) MessageBox.Show("Existing (if any) File Deleted", iL_NO) End If End Sub End Class
Please accept as solution and give likes if applicable.
I am attaching my Upwork profile for specific queries.
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Oh. Very nice. It is working.
Thanks a lot.
Let me try with bigger assemblies. I hope, you would not mind if you revert back to you for further help.
Once again, thanks a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Based on your idea, I further explored: I found the following works well except it does not carry the file extension although it is the full file name.
' Try ' Dim oParentocc As ComponentOccurrence ' oParentocc = oOcc.ParentOccurrence ' oParent = oParentocc.Name ' Catch ' MessageBox.Show("@ Catch", "Title") ' oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) ' End Try docFile = oOcc.Definition.Document oParent = System.IO.Path.GetFileNameWithoutExtension(docFile.FullFileName)
Do you think can I have the file extension as well: Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
How about changing the lines to get parent like this:
If Level >1 Then oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.ParentOccurrence.Name ) Else oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) End If

Jane Fan
Inventor QA Engineer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
waaa !! that simple and summarized method, I can hardly prove it
Please accept as solution and give likes if applicable.
I am attaching my Upwork profile for specific queries.
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi Jane Fan;
Thanks for responding to my request. It did not retrieve the parent children. See Figure - 1.
I modified as follows,
If Level > 1 Then oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.ParentOccurrence.Definition.Document.FullFileName) Else oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) End If
and it is working. Pl. see Figure - 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
It is a smart tweek but not quite right (see the figure below) . It led me to tweek as follows and it is fine.
Thanks again for your help.
Try Dim oParentocc As ComponentOccurrence oParentocc = oOcc.ParentOccurrence docFile = oParentocc.Definition.Document oParent = System.IO.Path.GetFileNameWithoutExtension(docFile.FullFileName) oParent = System.IO.Path.GetFileName(docFile.FullFileName) Catch oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) oParent = System.IO.Path.GetFileName(oOcc.Parent.Document.DisplayName) End Try
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Final solution is:
Public Class RWEI_0001A Shared oTextSave As String = "C:\Users\Public\Documents\iLogicBuffer.txt" Sub Main() 'Accessing Assembly Components @ https://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html Dim oDashes As String = "" Dim iL_NO As String = "0009-BOM" Dim iL_Name As String = "All Occurrences with BOM Structure" Dim iL_FullName As String = "Rule " & iL_NO & ": " & iL_Name Dim oDoc As Document = ThisApplication.ActiveDocument oDelete_ex_Notepad(oTextSave, iL_NO) oHL1 = "FILE REFERENCE TREE RAN FROM: " & oDoc.FullFileName oWrite_HeaderLines(oTextSave, oHL1) oHL2 = iL_FullName oWrite_HeaderLines(oTextSave, oHL2) For i = 1 To Len(iL_FullName) oDashes = oDashes & "-" Next i oWrite_DashLine(oTextSave, oDashes) ' Get the active assembly. Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument ' Call the function that does the recursion. Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1) Process.Start("Notepad.exe", oTextSave) End Sub Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) ' Iterate through all of the occurrence in this collection. This ' represents the occurrences at the top level of an assembly. Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences ' Print the name of the current occurrence. ' Check to see if this occurrence represents a subassembly ' and recursively call this function to traverse through it. If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) End If oFFN_cw_EXT = oOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName 'cw file extension oStr = oDoc_LocalName_WExt(iL_NO, oFFN_cw_EXT) BOM_Structure_Return = Func_BOM_Structure(iL_NO, oOcc) 'specify word splitting characters "space" and "dash" Dim Separators() As Char = {"*"c} Sentence = BOM_Structure_Return Words = Sentence.Split(Separators) i = 0 For Each wrd In Words 'MessageBox.Show("Word Index #" & i & " = " & Words(i)) i += 1 Next BOM_Structure = Words(0) oOccurrence_FFN = Words(1) ' oParent = oOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName Try Dim oParentocc As ComponentOccurrence oParentocc = oOcc.ParentOccurrence docFile = oParentocc.Definition.Document oParent = System.IO.Path.GetFileNameWithoutExtension(docFile.FullFileName) oParent = System.IO.Path.GetFileName(docFile.FullFileName) Catch oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) oParent = System.IO.Path.GetFileName(oOcc.Parent.Document.DisplayName) End Try ' If Level > 1 Then ' oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.ParentOccurrence.Definition.Document.FullFileName) ' Else ' oParent = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName) ' End If oData_X = oParent & "@" & oStr & "@" & "Level-" & Level & "@" & BOM_Structure ' oData_X = oParent & "@" & oOcc.Name & "@" & "Level-" & Level & "@" & BOM_Structure ' oData_X = oStr & "@" & oFFN_cw_EXT & "@" & BOM_Structure & " " & oParent & "@" & oOcc.Name & "@" & "Level-" & Level oWrite_Data(oTextSave, oData_X) Next End Sub Function oDoc_LocalName_WExt(iL_NO As String, oFFN_cw_EXT As String) oName_Sub = "oDoc_LocalName_WExt" oTitle = iL_NO & " @ SUB: " & oName_Sub oPos = Len(oFFN_cw_EXT) - InStrRev(oFFN_cw_EXT, "\", -1) oName_Local_cw_Ext = Right(oFFN_cw_EXT, oPos) Return oName_Local_cw_Ext 'Local file name complete with extension End Function Function Func_BOM_Structure(iL_NO As String, oCompOcc As ComponentOccurrence) oName_Sub = "BOM_Structure" oTitle = iL_NO & " @ SUB: " & oName_Sub Dim oOccurrence As ComponentOccurrence oFFN_X = oCompOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName BOM_X = oCompOcc.Definition.BOMStructure If BOM_X = BOMStructureEnum.kDefaultBOMStructure Then BOM_Structure = "Default" If BOM_X = BOMStructureEnum.kNormalBOMStructure Then BOM_Structure = "Normal" If BOM_X = BOMStructureEnum.kReferenceBOMStructure Then BOM_Structure = "Reference" If BOM_X = BOMStructureEnum.kPhantomBOMStructure Then BOM_Structure = "Phantom" If BOM_X = BOMStructureEnum.kPurchasedBOMStructure Then BOM_Structure = "Purchased" If BOM_X = BOMStructureEnum.kInseparableBOMStructure Then BOM_Structure = "Inseparable" If BOM_X = BOMStructureEnum.kVariesBOMStructure Then BOM_Structure = "Varies" oDoc_LocalName_WExt(iL_NO, oFFN_X) 'BOM_Structure = oDoc_LocalName_WExt(iL_NO, oFFN_X) & " <- " & BOM_Structure & "(BOM STATUS: Default)" Dim BOM_Structure_Return As String = BOM_Structure & "*" & oFFN_X Return BOM_Structure_Return End Function Function GetOccurrenceParent(oOcc As ComponentOccurrence, oOccName As String) Return oOcc.Parent.Document.DisplayName End Function 'Process.Start("Notepad.exe", oTextSave) Sub oWrite_Data(oTextSave As String, oData As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oData) oWrite.Flush() oWrite.Close End Sub Sub oWrite_HeaderLines(oTextSave As String, oHeaderLine As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oHeaderLine) oWrite.Flush() oWrite.Close End Sub Sub oWrite_DashLine(oTextSave As String, oDashes As String) oWrite = System.IO.File.AppendText(oTextSave) oWrite.WriteLine(oDashes) oWrite.Flush() oWrite.Close End Sub Sub oDelete_ex_Notepad(oTextSave As String, iL_NO As String) 'To delete oTextSave if existing. ' Dim FileDelete As String ' FileDelete = "C:\testDelete.txt" If System.IO.File.Exists(oTextSave) = True Then System.IO.File.Delete(oTextSave) ' MessageBox.Show("Existing (if any) File Deleted", iL_NO) End If End Sub End Class