To Retrieve Parent's BOM Structure

To Retrieve Parent's BOM Structure

RoyWickrama_RWEI
Advisor Advisor
1,425 Views
10 Replies
Message 1 of 11

To Retrieve Parent's BOM Structure

RoyWickrama_RWEI
Advisor
Advisor

In my rule, I have a hard time to retrieve the BOM Structure of parent which I need as the ultimatum fix to continue.

Sample model attached here to.

I request someone help me.

2019-02-24 1339.png

Public Class RWEI_0010BOM
	Shared oTextSave As String = "C:\Users\Public\Documents\iLogicBuffer.txt"
Sub Main
	Dim oAssy_ThisDoc As AssemblyDocument 
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim iL_NO As String = "0010-BOM"
	oDelete_ex_Notepad(oTextSave, iL_NO)
	Dim HL1_Prefix As String = "FILE REFERENCE TREE RAN FROM: "
	oHL1 = HL1_Prefix & oDoc.FullFileName
	oWrite_HeaderLines(oTextSave, oHL1)
	Dim iL_Name As String = "All Occurrences with BOM Structure"
	Dim oHL2 As String = "Rule " & iL_NO & ": " & iL_Name
	oWrite_HeaderLines(oTextSave, oHL2)
	Dim oDashes As String = ""
	For i = 1 To Len(oHL2)
	oDashes = oDashes & "-"
	Next i
	Dim oHL3 As String = oDashes
	oWrite_HeaderLines(oTextSave, oHL3)
	Dim oHL4 As String = ""
	oWrite_HeaderLines(oTextSave, oHL4)

Dim oBOM As BOM
oBOM = ThisApplication.ActiveDocument.ComponentDefinition.BOM

Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Unnamed")'Accesing to Model data Column
'In English__"Unnamed"_"Structured"_"Parts Only"__In Spanish__"Sin nombre"_"Estructurado"_"Solo piezas"

  Call ListItems(oBOMView.BOMRows, 0)
Process.Start("Notepad.exe", oTextSave)
	
End Sub

Sub ListItems(Rows As BOMRowsEnumerator, indent As Integer)
    Dim oBOMRow As BOMRow
    For Each oBOMRow In Rows
		
			Dim oDef As ComponentDefinition
			oDef = oBOMRow.ComponentDefinitions(1)
			Dim oLFN  As String
			oLFN  = oDef.Document.Displayname
			
			Dim oDocFile As Document
			oDocFile = oDef.Document
			
			If oDocFile.IsModifiable = True Then 
				Dim oBOMStructnumber As String
			    oBOMStructnumber = oBOMRow.BOMStructure
				Dim BOM_Structure As String
				If oBOMStructnumber <> 51972 Then
				
				If oBOMStructnumber = 51969 Then BOM_Structure = "Default" 
				If oBOMStructnumber = 51974 Then BOM_Structure = "Inseparable" 
				If oBOMStructnumber =  51970 Then BOM_Structure = "Normal" 
				If oBOMStructnumber = 51971 Then BOM_Structure = "Phantom" 
				If oBOMStructnumber =  51973 Then BOM_Structure = "Purchased" 
				If oBOMStructnumber = 51972 Then BOM_Structure = "Reference" 
				If oBOMStructnumber = 51975 Then BOM_Structure = "The Structure Type varies amongst references" 
				Dim Parent As Document = oDocFile.ReferencingDocuments.Item(1)
			
'******************************************************************************
				'To retrieve BOM Structure of parent
				Dim oParentDocFile As Document
				oParentDocFile = Parent		'oDef.Document
				
				MessageBox.Show("Message1", "Title")

				BOM_Parent = oParentDocFile.BOMStructure		'oParentDocFile.Definition.BOMStructure

				MessageBox.Show("Message2", "Title")		
'****************************************************************************		
				
				    If Not oBOMRow.ChildRows Is Nothing Then
				        Call ListItems(oBOMRow.ChildRows, indent + 1)
				    End If
					
					Dim oPhantomPart_ParentOfPhantomPart As String = ""
					
					If Right(oLFN, 3) = "ipt" And BOM_Structure = "Phantom" Then
						'Do nothing
					Else
						If Right(oLFN, 3) = "iam" And BOM_Structure = "Phantom" Then
							'Do nothing
						Else If Right(oLFN, 3) = "ipt" And BOM_Structure = "Phantom" Then
							oPhantomPart_ParentOfPhantomPart = oLFN & "$" & Parent.DisplayName
						Else
						   	oName_Parent = BOM_Parent & "$" & Parent.DisplayName & "$" & BOM_Structure & "$" & oLFN
							oWrite_Data(oTextSave, oName_Parent)
							End If
						End If
			End If
			
		End If
    Next

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_Data(oTextSave As String, oData As String)
	oWrite = System.IO.File.AppendText(oTextSave)
	oWrite.WriteLine(oData)	
	oWrite.Flush()
	oWrite.Close
	End Sub

	Sub oDelete_ex_Notepad(oTextSave As String, iL_NO As String)		'To delete oTextSave if existing.
	If System.IO.File.Exists(oTextSave) = True Then
	System.IO.File.Delete(oTextSave)
	End If
	End Sub
	
End Class

 

0 Likes
Accepted solutions (2)
1,426 Views
10 Replies
Replies (10)
Message 2 of 11

YuhanZhang
Autodesk
Autodesk
Accepted solution

You can try to change the like below:

BOM_Parent = oParentDocFile.BOMStructure

to

BOM_Parent = oParentDocFile.ComponentDefinition.BOMStructure

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 3 of 11

RoyWickrama_RWEI
Advisor
Advisor

That is the line of code I want.

This most probably be accepted as a solution, but let me work on this. I will get back to you.

0 Likes
Message 4 of 11

RoyWickrama_RWEI
Advisor
Advisor

Thanks Yuhan.

It is a solution. But, that rule is not giving me the desired results. 

I have another nice rule (attached here to and available from within the sample assembly also attached here to) that is very good and flexible. But. I have some issues with that rules:

 

ISSUE-1: Need to get data here for the BOM Structure of Parent

ISSUE-2: Reference Bom structure assigned in assembly level is not captured.

I spent weeks without success and I request you help me get around these issues

 

2019-02-26 0724.png

0 Likes
Message 5 of 11

YuhanZhang
Autodesk
Autodesk
Accepted solution

Just modified your iLogic code, and you can try if it works as expected:

 

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 = "BOM-XX WIP"
	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 
	Dim BOM_Parent As String 
    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	


		oLFN_cwExt = 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
			BOM_Parent = Func_BOM_Structure("",oParentocc)
			
			docFile = oParentocc.Definition.Document
		    oParentFFN = System.IO.Path.GetFileNameWithoutExtension(docFile.FullFileName)
		    oParent = System.IO.Path.GetFileName(docFile.FullFileName)
		Catch
			oParentFFN = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName)
			oParent = System.IO.Path.GetFileName(oOcc.Parent.Document.DisplayName)
		End Try
		
		
'*************************** BOM_Parent BOM STRUCTURE Parent******************

		BOM_Structure_Parent = "BOM Structure of Parent: " & BOM_Parent

'***************************BOM Structure Parent**********************


		oData_X = BOM_Structure_Parent & " $ " & oParent & "$" & oLFN_cwExt & "$" & oOcc.Name & "$" & "Level-" & Level & "$" & BOM_Structure	
'		oData_X = oParent & "$" & oOcc.Name & "$" & "Level-" & Level & "$" & BOM_Structure
'		oData_X = oLFN_cwExt & "$" & 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.BOMStructure '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.
 	If System.IO.File.Exists(oTextSave) = True Then
   	System.IO.File.Delete(oTextSave)
	End If
End Sub
End Class


If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 6 of 11

RoyWickrama_RWEI
Advisor
Advisor

Thanks Yuhan Zhang. This time it is fully functioning.

You did a great help: I could never do this (at least for a year from now).

 

Again, thank you Sir.

 

Note: I was trying to do with other rule. That is also now kind of promising. I will let you know the results.

 

0 Likes
Message 7 of 11

RoyWickrama_RWEI
Advisor
Advisor

Hi Yuhan, I have a one more question:

I need to retrieve the BOM Structure of the current document (main assembly in which I run the rule), just as a formality.

Could you reply me. Thanks.

 

 

0 Likes
Message 8 of 11

RoyWickrama_RWEI
Advisor
Advisor

Hi Yuhan;

 

I think. I got it.

 

Dim doc = ThisDoc.Document

oBOM = doc.ComponentDefinition.BOMStructure
MessageBox.Show("Message: " & oBOM, "Title")
0 Likes
Message 9 of 11

RoyWickrama_RWEI
Advisor
Advisor

Hi Yuhan;

 

Thanks again. I keep doing well with your code. I came across with a situation where my document and parent-of-document relationship fails when the BOM structure of the parent is phantom.

In this case (if the parent's BOM structure is phantom), the parent of the document is the parent-of-parent.

 

I keep trying with the code shown below without success. Could you take a look at, please. Thanks. A fake model is attached.

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 = "BOM-XX WIP"
	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)
	oHL3 = ""
	oWrite_HeaderLines(oTextSave, oHL3)
	
    ' 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 
	Dim oParent_BOM_Structure As String 
    For Each oOcc In Occurrences 
		If oOcc.ReferencedDocumentDescriptor.ReferencedDocument.IsModifiable = True Then
	        ' 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 

			oOccFFN_cw_EXT = oOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName	'cw file extension	
			oOccLFN_cwExt = oFnOccc_LocalName_WExt(iL_NO, oOccFFN_cw_EXT)
			oOcc_BOM_Structure = oFn_BOM_Structure_Occ(iL_NO, oOcc)

			
	'		oParent = oOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
			Try
				Dim oParentocc As ComponentOccurrence
				oParentocc = oOcc.ParentOccurrence
				oParent_BOM_Structure = oFn_BOM_Structure_Occ("",oParentocc)
				
				docFile = oParentocc.Definition.Document
			    oParentFFN = System.IO.Path.GetFileNameWithoutExtension(docFile.FullFileName)
			    oParent = System.IO.Path.GetFileName(docFile.FullFileName)
			Catch
				oParentFFN = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName)
				oParent = System.IO.Path.GetFileName(oOcc.Parent.Document.DisplayName)
			End Try
			
			If Level = 1 Then oParent_BOM_Structure = oFn_OccLevel1_BomStructure()
			If oParent_ofParent = "" Then oParent_ofParent = "oParent_ofParent - Missing"
				
			oData_X = oParent_ofParent & " $ " & oParent_BOM_Structure & " $ " & oParent & " $ " & oOccLFN_cwExt & " $ " & oOcc.Name & " $ " & "Level-" & Level & " $ " & oOcc_BOM_Structure	
	'		oData_X = oParent & "$" & oOcc.Name & "$" & "Level-" & Level & "$" & oOcc_BOM_Structure
	'		oData_X = oOccLFN_cwExt & "$" & oOccFFN_cw_EXT & "$" & oOcc_BOM_Structure & " " & oParent & "$" & oOcc.Name & "$" & "Level-" & Level
			oWrite_Data(oTextSave, oData_X)
		End If

	Next 
End Sub

	Function oFnOccc_LocalName_WExt(iL_NO As String, oOccFFN_cw_EXT As String)
	oName_Sub = "oFnOccc_LocalName_WExt"
	oTitle = iL_NO & " $ SUB: " & oName_Sub
	oPos = Len(oOccFFN_cw_EXT) - InStrRev(oOccFFN_cw_EXT, "\", -1)
	oName_Local_cw_Ext = Right(oOccFFN_cw_EXT, oPos)
	Return oName_Local_cw_Ext 'Local file name complete with extension
	End Function


	Function oFn_BOM_Structure_Occ(iL_NO As String, oCompOcc As ComponentOccurrence)
		oName_Sub = "oOcc_BOM_Structure"
		oTitle = iL_NO & " $ SUB: " & oName_Sub
'		Dim oOccurrence As ComponentOccurrence
		BOM_X = oCompOcc.BOMStructure 'oCompOcc.Definition.BOMStructure
		If BOM_X = BOMStructureEnum.kDefaultBOMStructure Then oOcc_BOM_Structure = "Default"
		If BOM_X = BOMStructureEnum.kNormalBOMStructure Then oOcc_BOM_Structure = "Normal"
		If BOM_X = BOMStructureEnum.kReferenceBOMStructure Then oOcc_BOM_Structure = "Reference" 
		If BOM_X = BOMStructureEnum.kPhantomBOMStructure Then oOcc_BOM_Structure = "Phantom"
		If BOM_X = BOMStructureEnum.kPurchasedBOMStructure  Then oOcc_BOM_Structure = "Purchased"
		If BOM_X = BOMStructureEnum.kInseparableBOMStructure  Then oOcc_BOM_Structure = "Inseparable"
		If BOM_X = BOMStructureEnum.kVariesBOMStructure Then oOcc_BOM_Structure = "Varies" 
		'oOcc_BOM_Structure = oFnOccc_LocalName_WExt(iL_NO, oFFN_X) & " <- " & oOcc_BOM_Structure & "(BOM STATUS: Default)"
		Dim BOM_Structure_Return As String = oOcc_BOM_Structure
		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

Function oFn_OccLevel1_BomStructure()
	Dim doc = ThisDoc.Document
	oBOM = doc.ComponentDefinition.BOMStructure
	If oBOM = BOMStructureEnum.kDefaultBOMStructure Then oOcc_BOM_Structure = "Default"
	If oBOM = BOMStructureEnum.kNormalBOMStructure Then oOcc_BOM_Structure = "Normal"
	If oBOM = BOMStructureEnum.kReferenceBOMStructure Then oOcc_BOM_Structure = "Reference" 
	If oBOM = BOMStructureEnum.kPhantomBOMStructure Then oOcc_BOM_Structure = "Phantom"
	If oBOM = BOMStructureEnum.kPurchasedBOMStructure  Then oOcc_BOM_Structure = "Purchased"
	If oBOM = BOMStructureEnum.kInseparableBOMStructure  Then oOcc_BOM_Structure = "Inseparable"
	If oBOM = BOMStructureEnum.kVariesBOMStructure Then oOcc_BOM_Structure = "Varies" 
	Dim BOM_Structure_Return As String = oOcc_BOM_Structure
	Return BOM_Structure_Return
End Function




Sub oDelete_ex_Notepad(oTextSave As String, iL_NO As String)		'To delete oTextSave if existing.
 	If System.IO.File.Exists(oTextSave) = True Then
   	System.IO.File.Delete(oTextSave)
	End If
End Sub
End Class
0 Likes
Message 10 of 11

YuhanZhang
Autodesk
Autodesk

I don't see any component in your sample data has Phantom BOM structure, and also your code below will always cause to output the "oParent_ofParent - Missing" in the result, which seems not meaningful:

 

If oParent_ofParent = "" Then oParent_ofParent = "oParent_ofParent - Missing"  

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 11 of 11

RoyWickrama_RWEI
Advisor
Advisor

Thanks Yuhan for getting back to me.  (REQUEST HELP AGAIN!)

The latest rule is attached here to is fine. But, it gives me n error when I run the same in Inventor 2018 (and 2015 as well). I can't fix this issue. Could you kindly take a look at this, please.

2019-03-04 2318.png

 

2019-03-05 0718.png

 

2019-03-05 0715.png

 

 

Public Class RWEI_0001A
	Shared oTextSave As String = "C:\Users\Public\Documents\iLogicBuffer.txt"
	'https://adndevblog.typepad.com/manufacturing/2013/11/iterate-structured-bomview.html
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 = "BOM-XX WIP"
	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)
	oHL3 = ""
	oWrite_HeaderLines(oTextSave, oHL3)
	
    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 
	oAsmDoc.Save

    ' Call the function that does the recursion. 
	Dim oParent_ofParent_atL3 As String = ""
	Dim oParent_ofParent_atL4 As String = ""
	Dim oParent_ofParent_atL5 As String = ""
	Dim oLevel_Request As Integer = 1
	'oLevel_Request = InputBox("Level for Data Record: ", "USER INPUT REQUEST", oLevel_Request)
	Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, 1, oParent_ofParent_atL3, oParent_ofParent_atL4, oParent_ofParent_atL5, oLevel_Request) 
	Process.Start("Notepad.exe", oTextSave)
End Sub 

Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer, j As Integer, oParent_ofParent_atL3 As String, oParent_ofParent_atL4 As String, oParent_ofParent_atL5 As String, oLevel_Request As Integer) 
    ' Iterate through all of the occurrence in this collection.  This 
    ' represents the occurrences at the top level of an assembly. 
	Dim oParent_BOM_Structure As String 
    For Each oOcc In Occurrences
		If oOcc.ReferencedDocumentDescriptor.ReferencedDocument.IsModifiable = True Then
		If Level = 1 Then
			oParent_ofParent = "Not Applicable"
			oParent_ofParent_atL3 = "This Document"
	        If oOcc.DefinitionDocumentType = kPartDocumentObject Then 
	        Else	'If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
				oParent_ofParent_atL3 = oFn_BrowserName(oOcc)
			Call TraverseAssembly(oOcc.SubOccurrences, Level+1, j+1, oParent_ofParent_atL3, oParent_ofParent_atL4, oParent_ofParent_atL5, oLevel_Request) 
			End If 
		End If
		If Level = 2 Then 'oParent_ofParent_atL3 Processing
			oParent_ofParent = "This Document"		'for Level 2 only
	        If oOcc.DefinitionDocumentType = kPartDocumentObject Then 
		  	Else	'If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
				oParent_ofParent_atL4 = oFn_BrowserName(oOcc)
			Call TraverseAssembly(oOcc.SubOccurrences, Level+1, j+1, oParent_ofParent_atL3, oParent_ofParent_atL4, oParent_ofParent_atL5, oLevel_Request) 
			End If 
		End If 
		If Level = 3 Then
			oParent_ofParent = oParent_ofParent_atL3		'for Level 2 only
	        If oOcc.DefinitionDocumentType = kPartDocumentObject Then 
	        Else	'If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
				oParent_ofParent_atL5 = oFn_BrowserName(oOcc)
				Call TraverseAssembly(oOcc.SubOccurrences, Level+1, j+1, oParent_ofParent_atL3, oParent_ofParent_atL4, oParent_ofParent_atL5, oLevel_Request) 
			End If 
		End If 
		If Level = 4 Then
			oParent_ofParent = oParent_ofParent_atL4
	        If oOcc.DefinitionDocumentType = kPartDocumentObject Then 
	        Else	'If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
				oParent_ofParent_atL6 = oFn_BrowserName(oOcc)
				Call TraverseAssembly(oOcc.SubOccurrences, Level+1, j+1, oParent_ofParent_atL3, oParent_ofParent_atL4, oParent_ofParent_atL5, oLevel_Request) 
			End If 
		End If 
		If Level = 5 Then
			oParent_ofParent = oParent_ofParent_atL5
	        If oOcc.DefinitionDocumentType = kPartDocumentObject Then 
	        Else	'If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
				oParent_ofParent_atL7 = oFn_BrowserName(oOcc)
			Call TraverseAssembly(oOcc.SubOccurrences, Level+1, j+1, oParent_ofParent_atL3, oParent_ofParent_atL4, oParent_ofParent_atL5, oLevel_Request) 
			End If 
		End If 
		
	oOccFFN_cw_EXT = oOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName	'cw file extension	
	oOccLFN_cwExt = oFnOccc_LocalName_WExt(iL_NO, oOccFFN_cw_EXT)
	oOcc_BOM_Structure = oFn_BOM_Structure_Occ(iL_NO, oOcc)
'	oParent = oOcc.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
	Try
		Dim oParentocc As ComponentOccurrence
		oParentocc = oOcc.ParentOccurrence
		oParent_BOM_Structure = oFn_BOM_Structure_Occ("",oParentocc)
		
		docFile = oParentocc.Definition.Document
	    oParentFFN = System.IO.Path.GetFileNameWithoutExtension(docFile.FullFileName)
	    oParent = System.IO.Path.GetFileName(docFile.FullFileName)
	Catch
		oParentFFN = System.IO.Path.GetFileNameWithoutExtension(oOcc.Parent.Document.DisplayName)
		oParent = System.IO.Path.GetFileName(oOcc.Parent.Document.DisplayName)
	End Try

	If Level = 1 Then oParent_BOM_Structure = oFn_OccLevel1_BomStructure()
'	If Level = oLevel_Request Then		
		oData_X = oParent_ofParent & " $ " & oAssy_to_Next & "$" & oParent_BOM_Structure & " $ " & oParent & " $ " & oOcc.Name & " $ " & "Level-" & Level & " $ " & oOcc_BOM_Structure	
'		oData_X = oParent & "$" & oOcc.Name & "$" & "Level-" & Level & "$" & oOcc_BOM_Structure
'		oData_X = oOccLFN_cwExt & "$" & oOccFFN_cw_EXT & "$" & oOcc_BOM_Structure & " " & oParent & "$" & oOcc.Name & "$" & "Level-" & Level
		oWrite_Data(oTextSave, oData_X)
	End If
'	End If
	Next oOcc

End Sub

Function oFnOccc_LocalName_WExt(iL_NO As String, oOccFFN_cw_EXT As String)
	oName_Sub = "oFnOccc_LocalName_WExt"
	oTitle = iL_NO & " $ SUB: " & oName_Sub
	oPos = Len(oOccFFN_cw_EXT) - InStrRev(oOccFFN_cw_EXT, "\", -1)
	oName_Local_cw_Ext = Right(oOccFFN_cw_EXT, oPos)
	Return oName_Local_cw_Ext 'Local file name complete with extension
	End Function

Function oFn_BOM_Structure_Occ(iL_NO As String, oCompOcc As ComponentOccurrence)
	oName_Sub = "oOcc_BOM_Structure"
	oTitle = iL_NO & " $ SUB: " & oName_Sub
'	Dim oOccurrence As ComponentOccurrence
	BOM_X = oCompOcc.BOMStructure 'oCompOcc.Definition.BOMStructure
	If BOM_X = BOMStructureEnum.kDefaultBOMStructure Then oOcc_BOM_Structure = "Default"
	If BOM_X = BOMStructureEnum.kNormalBOMStructure Then oOcc_BOM_Structure = "Normal"
	If BOM_X = BOMStructureEnum.kReferenceBOMStructure Then oOcc_BOM_Structure = "Reference" 
	If BOM_X = BOMStructureEnum.kPhantomBOMStructure Then oOcc_BOM_Structure = "Phantom"
	If BOM_X = BOMStructureEnum.kPurchasedBOMStructure  Then oOcc_BOM_Structure = "Purchased"
	If BOM_X = BOMStructureEnum.kInseparableBOMStructure  Then oOcc_BOM_Structure = "Inseparable"
	If BOM_X = BOMStructureEnum.kVariesBOMStructure Then oOcc_BOM_Structure = "Varies" 
	'oOcc_BOM_Structure = oFnOccc_LocalName_WExt(iL_NO, oFFN_X) & " <- " & oOcc_BOM_Structure & "(BOM STATUS: Default)"
	Dim BOM_Structure_Return As String = oOcc_BOM_Structure
	Return BOM_Structure_Return
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

Function oFn_OccLevel1_BomStructure()
	Dim doc = ThisDoc.Document
	oBOM = doc.ComponentDefinition.BOMStructure
	If oBOM = BOMStructureEnum.kDefaultBOMStructure Then oOcc_BOM_Structure = "Default"
	If oBOM = BOMStructureEnum.kNormalBOMStructure Then oOcc_BOM_Structure = "Normal"
	If oBOM = BOMStructureEnum.kReferenceBOMStructure Then oOcc_BOM_Structure = "Reference" 
	If oBOM = BOMStructureEnum.kPhantomBOMStructure Then oOcc_BOM_Structure = "Phantom"
	If oBOM = BOMStructureEnum.kPurchasedBOMStructure  Then oOcc_BOM_Structure = "Purchased"
	If oBOM = BOMStructureEnum.kInseparableBOMStructure  Then oOcc_BOM_Structure = "Inseparable"
	If oBOM = BOMStructureEnum.kVariesBOMStructure Then oOcc_BOM_Structure = "Varies" 
	Dim BOM_Structure_Return As String = oOcc_BOM_Structure
	Return BOM_Structure_Return
End Function

Sub oDelete_ex_Notepad(oTextSave As String, iL_NO As String)		'To delete oTextSave if existing.
 	If System.IO.File.Exists(oTextSave) = True Then
   	System.IO.File.Delete(oTextSave)
	End If
End Sub

Function oFn_BrowserName(oOcc As ComponentOccurrence)
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 
	' Get the model browser
	Dim oPane As BrowserPane
	oPane = oAsmDoc.BrowserPanes.Item("Model")
	    Dim oSubAssyNode As BrowserNode
	    oSubAssyNode = oPane.GetBrowserNodeFromObject(oOcc)
		Dim oBrowserName_Old As String
		oBrowserName_Old = oSubAssyNode.NativeObject.Name
		Return oBrowserName_Old 
End Function
End Class

  

0 Likes