Can i change all of Partnumber automatically with this rule?

Can i change all of Partnumber automatically with this rule?

ssiem12
Contributor Contributor
654 Views
8 Replies
Message 1 of 9

Can i change all of Partnumber automatically with this rule?

ssiem12
Contributor
Contributor

Hello

my rule is this

Basic structure is '00-000'

 

it start from Assemblydocument

Following BOM in inventor

 

Rule for prefix

Normal - two type (As for assembly, Pr for part)

Inseparable - Wl

Purchased - St

Phantom - Pt

Reference - Rf

(Standard product is exception,No changing Part number)

 

Rule for suffix

001

002

003

004...

this sequence is ascending order of mass

as a exception if it has same mass by missing selecting material or something, ascending order of volume is okay

it should be different according to the prefix

 

for example Part number

As-001 (assembly and first heavy one)

As-002(assembly and second heavy one)

Pr-001(Part and first heavy one)

Pr-002(Part and second heavy one)

Wl-001(Welding and first heavy one)

 

I hope you give me your Wisdom

0 Likes
Accepted solutions (1)
655 Views
8 Replies
Replies (8)
Message 2 of 9

dalton98
Collaborator
Collaborator

Heres something i came up with. You will first need to export a BOM template with a row  for 'Mass' and volume

 

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
		'create a bom with a column for Mass then export it
		oBOM.ImportBOMCustomization("Z:\BOM_Style.xml")
		oBOM.StructuredViewEnabled = True
		oBOM.StructuredViewFirstLevelOnly = True
		Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
		oBOMView.Sort("Mass", False, "Volume", False)
		oBOMView.Renumber()
		For Each oRow As BOMRow In oBOMView.BOMRows
			Dim partnumber As String = PartNumberGenerator(oRow)
			oRow.ComponentDefinitions(1).Document.PropertySets(3)("Part Number").Value = partnumber
		Next
		oBOMView.Sort("Part Number", True)
		oBOMView.Renumber()
	End Sub

	Function PartNumberGenerator(oRow As BOMRow)
		Select Case oRow.BOMStructure
			Case BOMStructureEnum.kNormalBOMStructure
				If oRow.ReferencedFileDescriptor.ReferencedFileType = kAssemblyFileType
					As_count = As_Count + 1
					Return "As-" & As_count.ToString("000")
				Else
					Pr_count = Pr_count + 1
					Return "Pr-" & Pr_count.ToString("000")
				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
	End Function
End Class
0 Likes
Message 3 of 9

ssiem12
Contributor
Contributor

Thank you for your kindness

and i have a question  you mention exporting BOM first

is it mean this?

ssiem12_0-1680584314281.png

and i should put excel file on proper place right?

like "Z:" you put on code

ssiem12_1-1680584408655.png

and change file name right?

0 Likes
Message 4 of 9

dalton98
Collaborator
Collaborator

This one. It saves a custom BOM template for columns, spacing, etc. You will probably have to change the property names from english aswell.

daltonparrish_0-1680610605863.png

 

0 Likes
Message 5 of 9

ssiem12
Contributor
Contributor

Thank you and could you please help me one more?

I success by 30row

and there is error like below

ssiem12_1-1680688863631.png

ssiem12_0-1680688853043.png

so i check 30 row and what i found is kAssemblyFileType is not defined i guess

ssiem12_2-1680688937347.png

 

because when i put cursor on this function speech bubble doesn't pops out but other function do

could it be right?

0 Likes
Message 6 of 9

dalton98
Collaborator
Collaborator

Try changing these 3 values. You will probably need to change the text "Part Number" too

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
		'create a bom with a column for Mass then export it
		oBOM.ImportBOMCustomization("Z:\BOM_Style.xml")
		oBOM.StructuredViewEnabled = True
		oBOM.StructuredViewFirstLevelOnly = True
		oBOM.SetPartNumberMergeSettings(False)
		Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
		oBOMView.Sort("Mass", False, "Volume", False)
		oBOMView.Renumber()
		For Each oRow As BOMRow In oBOMView.BOMRows
			Dim partnumber As String = PartNumberGenerator(oRow)
			oRow.ComponentDefinitions(1).Document.PropertySets(3)(2).Value = partnumber
		Next
		oBOMView.Sort("Part Number", True)
		oBOMView.Renumber()
	End Sub

	Function PartNumberGenerator(oRow As BOMRow)
		Select Case oRow.BOMStructure
			Case BOMStructureEnum.kNormalBOMStructure
				If oRow.ReferencedFileDescriptor.ReferencedFileType = 56323
					As_count = As_count + 1
					Return "As-" & As_count.ToString("000")
				Else
					Pr_count = Pr_count + 1
					Return "Pr-" & Pr_count.ToString("000")
				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
	End Function
End Class
0 Likes
Message 7 of 9

ssiem12
Contributor
Contributor

hello i finally ran this rule thank you

and it's modified 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:\test.xml")
		oBOM.StructuredViewEnabled = True
		oBOM.StructuredViewFirstLevelOnly = True
		oBOM.SetPartNumberMergeSettings(False)
		Dim oBOMView As BOMView = oBOM.BOMViews.Item("구조적")
		oBOMView.Sort("질량", False, "체적", False)
		oBOMView.Renumber()
		For Each oRow As BOMRow In oBOMView.BOMRows
			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 
		Next
		oBOMView.Sort("Part Number", True)
		oBOMView.Renumber()
	End Sub

	Function PartNumberGenerator(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
	
	Function IsStandardContentCenterRow(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
	
	Function GetDocumentType(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

 

one thing i'd like to ask is can i change Partnumber every class not first class in BOM

 

Message 8 of 9

dalton98
Collaborator
Collaborator
Accepted solution

Do you mean running this rule from a top level assembly and changing the part numbers in the sub level assemblies? If so, this would be harder and require a lot more testing.  It would involve using a recursive sub routine ex.

oBOM.StructuredViewFirstLevelOnly = False

Sub RecursiveBOM(oRows As BOMRowsEnumerator)
For Each oRow As BOMRow in oRows
If oRow.ChildRows.Count > 1
RecursiveBOM(oRow)
End If
Next
End Sub

 

If you mean the class in the code, You do this so you don't have to redefine the part number count every sub. If you are wanting to do this for every assembly in your main assembly file you will have to do this. By adding ByRef ex.

Function PartNumberGenerator(ByRef As_count as Integer, ByRef Pr_count as Integer, oRow As BOMRow) As String

 

0 Likes
Message 9 of 9

ssiem12
Contributor
Contributor

What i want is first one you mention and recursive function uhmm

i got C- in C++ class cuz of this

0 Likes