Can my rule changing part number based on BOM structure be fixed?

Can my rule changing part number based on BOM structure be fixed?

ssiem
Contributor Contributor
528 Views
5 Replies
Message 1 of 6

Can my rule changing part number based on BOM structure be fixed?

ssiem
Contributor
Contributor

Hello

 

I'm using this rule and i found there is some point need to be fixed

 

this is my rule

 

Option Explicit On

Class ThisRule
	Dim As_count As Integer = 0
	Dim Pr_count As Integer = 0
	Dim Wl_count As Integer = 0
	Dim St_count As Integer = 0
	Dim Pt_count As Integer = 0
	Dim Rf_count As Integer = 0
	Sub Main
		ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd").Execute
		Dim oADoc As AssemblyDocument = ThisDoc.Document
		Dim oBOM As BOM = oADoc.ComponentDefinition.BOM
		'BOM Export 후 xml파일과 아래 경로를 일치할것
		oBOM.ImportBOMCustomization("D:\UC BOM.xml")
		oBOM.StructuredViewEnabled = True
		oBOM.StructuredViewFirstLevelOnly = False
		oBOM.SetPartNumberMergeSettings(False)
		Dim oBOMView As BOMView = oBOM.BOMViews.Item("구조적")
		oBOMView.Sort("질량", False, "체적", False)
		oBOMView.Renumber()
		WritePartMumb(oBOMView.BOMRows)
		oBOMView.Sort("Part Number", True)
		oBOMView.Renumber()
	End Sub
	
	Private Sub WritePartMumb(ByVal oBOMRows As BOMRowsEnumerator)
		For Each oRow As BOMRow In oBOMRows
			If Not IsStandardContentCenterRow(oRow) Then
				Dim partnumber As String = PartNumberGenerator(oRow)
				If partnumber <> "" Then
					oRow.ComponentDefinitions(1).Document.PropertySets(3)(2).Value = partnumber
				End If
			End If
			If oRow.ChildRows IsNot Nothing Then
				WritePartMumb(oRow.ChildRows)
			End If
		Next		
	End Sub

	Private Function PartNumberGenerator(ByVal oRow As BOMRow) As String
		Select Case oRow.BOMStructure
			Case BOMStructureEnum.kNormalBOMStructure
				Dim rowDocType = GetDocumentType(oRow)
				If rowDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
					As_count = As_count + 1
					Return "As-" & As_count.ToString("000")
				ElseIf rowDocType = DocumentTypeEnum.kPartDocumentObject Then
					Pr_count = Pr_count + 1
					Return "Pr-" & Pr_count.ToString("000")
				Else 
					Return ""
				End If
			Case BOMStructureEnum.kInseparableBOMStructure
				Wl_count = Wl_count + 1
				Return "Wl-" & Wl_count.ToString("000")
			Case BOMStructureEnum.kPurchasedBOMStructure
				St_count = St_count + 1
				Return "St-" & St_count.ToString("000")
			Case BOMStructureEnum.kPhantomBOMStructure
				Pt_count = Pt_count + 1
				Return "Pt-" & Pt_count.ToString("000")
			Case BOMStructureEnum.kReferenceBOMStructure
				Rf_count = Rf_count + 1
				Return "Rf-" & Rf_count.ToString("000")
		End Select
		Return "" ' default case if we can't get a part number
	End Function
	
	Private Function IsStandardContentCenterRow(ByVal oRow As BOMRow) As Boolean
		For Each compDef As ComponentDefinition In oRow.ComponentDefinitions
			Dim partCompDef As PartComponentDefinition = TryCast(compDef, PartComponentDefinition)
			If partCompDef IsNot Nothing AndAlso partCompDef.IsContentMember Then
				Return True
			End If
		Next 
		Return False
	End Function
	
	Private Function GetDocumentType(ByVal oRow As BOMRow) As DocumentTypeEnum
		For Each compDef As ComponentDefinition In oRow.ComponentDefinitions
			Dim partCompDef As PartComponentDefinition = TryCast(compDef, PartComponentDefinition)
			If partCompDef IsNot Nothing Then
				Return DocumentTypeEnum.kPartDocumentObject
			End If
			Dim assemCompDef As AssemblyComponentDefinition = TryCast(compDef, AssemblyComponentDefinition)
			If assemCompDef IsNot Nothing Then
				Return DocumentTypeEnum.kAssemblyDocumentObject
			End If
		Next 
		Return DocumentTypeEnum.kUnknownDocumentObject
	End Function
End Class

 

other function work well but it's not working  if it is Phantom or Reference structure like this

ssiem_0-1693011725162.png

Could you help me please?

0 Likes
Accepted solutions (1)
529 Views
5 Replies
Replies (5)
Message 2 of 6

A.Acheson
Mentor
Mentor

Hi @ssiem 

You can use index 1 to get to model tab so you can affect the Bom Structure before it is filtered into the Structured Bom View.

 Dim oModelBOMView As BOMView = oBOM.BOMViews.Item(1)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 6

ssiem
Contributor
Contributor

Sorry you mean like this?

Option Explicit On

Class ThisRule
	Dim As_count As Integer = 0
	Dim Pr_count As Integer = 0
	Dim Wl_count As Integer = 0
	Dim St_count As Integer = 0
	Dim Pt_count As Integer = 0
	Dim Rf_count As Integer = 0
	Sub Main
		ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd").Execute
		Dim oADoc As AssemblyDocument = ThisDoc.Document
		Dim oBOM As BOM = oADoc.ComponentDefinition.BOM
		Dim oModelBOMView As BOMView = oBOM.BOMViews.Item(1)
		'BOM Export 후 xml파일과 아래 경로를 일치할것
		oBOM.ImportBOMCustomization("D:\STCRECH.xml")
		oBOM.StructuredViewEnabled = True
		oBOM.StructuredViewFirstLevelOnly = False
		oBOM.SetPartNumberMergeSettings(False)
		Dim oBOMView As BOMView = oBOM.BOMViews.Item("구조적")
		oBOMView.Sort("질량", False, "체적", False)
		oBOMView.Renumber()
		WritePartMumb(oBOMView.BOMRows)
		oBOMView.Sort("Part Number", True)
		oBOMView.Renumber()
	End Sub
	
	Private Sub WritePartMumb(ByVal oBOMRows As BOMRowsEnumerator)
		For Each oRow As BOMRow In oBOMRows
			If Not IsStandardContentCenterRow(oRow) Then
				Dim partnumber As String = PartNumberGenerator(oRow)
				If partnumber <> "" Then
					oRow.ComponentDefinitions(1).Document.PropertySets(3)(2).Value = partnumber
				End If
			End If
			If oRow.ChildRows IsNot Nothing Then
				WritePartMumb(oRow.ChildRows)
			End If
		Next		
	End Sub

	Private Function PartNumberGenerator(ByVal oRow As BOMRow) As String
		Select Case oRow.BOMStructure
			Case BOMStructureEnum.kNormalBOMStructure
				Dim rowDocType = GetDocumentType(oRow)
				If rowDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
					As_count = As_count + 1
					Return "As-" & As_count.ToString("000")
				ElseIf rowDocType = DocumentTypeEnum.kPartDocumentObject Then
					Pr_count = Pr_count + 1
					Return "Pr-" & Pr_count.ToString("000")
				Else 
					Return ""
				End If
			Case BOMStructureEnum.kInseparableBOMStructure
				Wl_count = Wl_count + 1
				Return "Wl-" & Wl_count.ToString("000")
			Case BOMStructureEnum.kPurchasedBOMStructure
				St_count = St_count + 1
				Return "St-" & St_count.ToString("000")
			Case BOMStructureEnum.kPhantomBOMStructure
				Pt_count = Pt_count + 1
				Return "Pt-" & Pt_count.ToString("000")
			Case BOMStructureEnum.kReferenceBOMStructure
				Rf_count = Rf_count + 1
				Return "Rf-" & Rf_count.ToString("000")
		End Select
		Return "" ' default case if we can't get a part number
	End Function
	
	Private Function IsStandardContentCenterRow(ByVal oRow As BOMRow) As Boolean
		For Each compDef As ComponentDefinition In oRow.ComponentDefinitions
			Dim partCompDef As PartComponentDefinition = TryCast(compDef, PartComponentDefinition)
			If partCompDef IsNot Nothing AndAlso partCompDef.IsContentMember Then
				Return True
			End If
		Next 
		Return False
	End Function
	
	Private Function GetDocumentType(ByVal oRow As BOMRow) As DocumentTypeEnum
		For Each compDef As ComponentDefinition In oRow.ComponentDefinitions
			Dim partCompDef As PartComponentDefinition = TryCast(compDef, PartComponentDefinition)
			If partCompDef IsNot Nothing Then
				Return DocumentTypeEnum.kPartDocumentObject
			End If
			Dim assemCompDef As AssemblyComponentDefinition = TryCast(compDef, AssemblyComponentDefinition)
			If assemCompDef IsNot Nothing Then
				Return DocumentTypeEnum.kAssemblyDocumentObject
			End If
		Next 
		Return DocumentTypeEnum.kUnknownDocumentObject
	End Function
End Class

 

0 Likes
Message 4 of 6

A.Acheson
Mentor
Mentor
Accepted solution

Yes but you also need to pass in the bomview to the sub routine argument.

WritePartMumb(oModelBOMView.BOMRows)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 5 of 6

ssiem
Contributor
Contributor

WoW it works thank you so much!

ssiem_0-1693027260068.png

Option Explicit On

Class ThisRule
	Dim As_count As Integer = 0
	Dim Pr_count As Integer = 0
	Dim Wl_count As Integer = 0
	Dim St_count As Integer = 0
	Dim Pt_count As Integer = 0
	Dim Rf_count As Integer = 0
	Sub Main
		ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd").Execute
		Dim oADoc As AssemblyDocument = ThisDoc.Document
		Dim oBOM As BOM = oADoc.ComponentDefinition.BOM
		Dim oModelBOMView As BOMView = oBOM.BOMViews.Item(1)
		WritePartMumb(oModelBOMView.BOMRows)
		'BOM Export 후 xml파일과 아래 경로를 일치할것
		oBOM.ImportBOMCustomization("D:\STCRECH.xml")
		oBOM.StructuredViewEnabled = True
		oBOM.StructuredViewFirstLevelOnly = False
		oBOM.SetPartNumberMergeSettings(False)
		Dim oBOMView As BOMView = oBOM.BOMViews.Item("구조적")
		oBOMView.Sort("질량", False, "체적", False)
		oBOMView.Renumber()
		WritePartMumb(oBOMView.BOMRows)
		oBOMView.Sort("Part Number", True)
		oBOMView.Renumber()
	End Sub
	
	Private Sub WritePartMumb(ByVal oBOMRows As BOMRowsEnumerator)
		For Each oRow As BOMRow In oBOMRows
			If Not IsStandardContentCenterRow(oRow) Then
				Dim partnumber As String = PartNumberGenerator(oRow)
				If partnumber <> "" Then
					oRow.ComponentDefinitions(1).Document.PropertySets(3)(2).Value = partnumber
				End If
			End If
			If oRow.ChildRows IsNot Nothing Then
				WritePartMumb(oRow.ChildRows)
			End If
		Next		
	End Sub

	Private Function PartNumberGenerator(ByVal oRow As BOMRow) As String
		Select Case oRow.BOMStructure
			Case BOMStructureEnum.kNormalBOMStructure
				Dim rowDocType = GetDocumentType(oRow)
				If rowDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
					As_count = As_count + 1
					Return "As-" & As_count.ToString("000")
				ElseIf rowDocType = DocumentTypeEnum.kPartDocumentObject Then
					Pr_count = Pr_count + 1
					Return "Pr-" & Pr_count.ToString("000")
				Else 
					Return ""
				End If
			Case BOMStructureEnum.kInseparableBOMStructure
				Wl_count = Wl_count + 1
				Return "Wl-" & Wl_count.ToString("000")
			Case BOMStructureEnum.kPurchasedBOMStructure
				St_count = St_count + 1
				Return "St-" & St_count.ToString("000")
			Case BOMStructureEnum.kPhantomBOMStructure
				Pt_count = Pt_count + 1
				Return "Pt-" & Pt_count.ToString("000")
			Case BOMStructureEnum.kReferenceBOMStructure
				Rf_count = Rf_count + 1
				Return "Rf-" & Rf_count.ToString("000")
		End Select
		Return "" ' default case if we can't get a part number
	End Function
	
	Private Function IsStandardContentCenterRow(ByVal oRow As BOMRow) As Boolean
		For Each compDef As ComponentDefinition In oRow.ComponentDefinitions
			Dim partCompDef As PartComponentDefinition = TryCast(compDef, PartComponentDefinition)
			If partCompDef IsNot Nothing AndAlso partCompDef.IsContentMember Then
				Return True
			End If
		Next 
		Return False
	End Function
	
	Private Function GetDocumentType(ByVal oRow As BOMRow) As DocumentTypeEnum
		For Each compDef As ComponentDefinition In oRow.ComponentDefinitions
			Dim partCompDef As PartComponentDefinition = TryCast(compDef, PartComponentDefinition)
			If partCompDef IsNot Nothing Then
				Return DocumentTypeEnum.kPartDocumentObject
			End If
			Dim assemCompDef As AssemblyComponentDefinition = TryCast(compDef, AssemblyComponentDefinition)
			If assemCompDef IsNot Nothing Then
				Return DocumentTypeEnum.kAssemblyDocumentObject
			End If
		Next 
		Return DocumentTypeEnum.kUnknownDocumentObject
	End Function
End Class

 

0 Likes
Message 6 of 6

ssiem
Contributor
Contributor

oh i think it invalidate independent sequence by BOM structure

 

it give number as partnumber together

 

* no it seems not to give number together it think some number is ignored

ssiem_0-1693031209899.png

 

 

for example pt-001 pt-002 are missing

and same symtom with other like St, Wl

0 Likes