Lock item BOM list

Lock item BOM list

J.Classens
Enthusiast Enthusiast
1,375 Views
9 Replies
Message 1 of 10

Lock item BOM list

J.Classens
Enthusiast
Enthusiast

Hello,

 

To organisize our BOM before making a 2D drawing we will do a sort and renumber with the following ilogic. 

Sub Main()

Dim oAsmDoc As AssemblyDocument
Dim oAsmCompDef As AssemblyComponentDefinition
Dim oBOMView As BOMView

oAsmDoc = ThisDoc.Document
oAsmCompDef = oAsmDoc.ComponentDefinition
oBOMView = oAsmCompDef.BOM.BOMViews.Item("Structured")
oBOMView.Sort("Vendor", 1, "Part Number", 1)
oBOMView.Renumber(001)

End Sub

I would like to add a lock items rule in this Ilogic.

This way it protects the document that a renumber only can be done once. 

 

Is there a way to implement this function in a Ilogic?

 

JClassens_1-1629183736472.png

 

 

0 Likes
Accepted solutions (2)
1,376 Views
9 Replies
Replies (9)
Message 2 of 10

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @J.Classens 

Are you trying to lock item numbers in all rows after the sorting has finished? Something like this? 🙂

Sub Main()

Dim oAsmDoc As AssemblyDocument
Dim oAsmCompDef As AssemblyComponentDefinition
Dim oBOMView As BOMView

oAsmDoc = ThisDoc.Document
oAsmCompDef = oAsmDoc.ComponentDefinition
oBOMView = oAsmCompDef.BOM.BOMViews.Item("Structured")
oBOMView.Sort("Vendor", 1, "Part Number", 1)
oBOMView.Renumber(001)

For Each oRow As BOMRow In oBOMView.BOMRows
	oRow.ItemNumberLocked = True
Next
End Sub
Message 3 of 10

WCrihfield
Mentor
Mentor

Hi @J.Classens.  If @JhoelForshav 's solution isn't what you were looking for then maybe something like this would work for you.  Basically, when you run it the first time, it creates a custom iProperty called "BOMLocked", with the value set to True (Boolean).  Then when you try to run it again, it checks for that custom iProperty, and if found, if it is True, it will then exit the rule, without going any further.  Therefore it will only sort and renumber the BOM that first time.

 

Sub Main()
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
	Dim oLocked As Boolean = False
	Try
		oLocked = oAsmDoc.PropertySets(4).Item("BOMLocked").Value
	Catch
	End Try
	If oLocked Then Exit Sub
	Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oBOMView As BOMView = oAsmCompDef.BOM.BOMViews.Item("Structured")
	oBOMView.Sort("Vendor", 1, "Part Number", 1)
	oBOMView.Renumber(001)
	Try
		oAsmDoc.PropertySets(4).Item("BOMLocked").Value = True
	Catch
		oAsmDoc.PropertySets(4).Add(True, "BOMLocked")
	End Try
End Sub

 

You could also use a question there to inform the user that it has already been sorted/renumbered before, are you sure you want to sort/renumber again?  If No, exit the rule, if Yes, continue.

 

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 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 10

J.Classens
Enthusiast
Enthusiast

@JhoelForshav thanks that worked perfect.

 

I only want to add a warning messages that checks if ItemNumberLocked = true it should provide a message.

I have tried the following but that doesn't do the trick. Suggestions? 

Sub Main()


If ItemNumberLocked = True Then
	MessageBox.Show("Renumbering can only be done once", "Warning")

End If

Dim oAsmDoc As AssemblyDocument
Dim oAsmCompDef As AssemblyComponentDefinition
Dim oBOMView As BOMView

oAsmDoc = ThisDoc.Document
oAsmCompDef = oAsmDoc.ComponentDefinition
oBOMView = oAsmCompDef.BOM.BOMViews.Item("Structured")
oBOMView.Sort("Vendor", 1, "Part Number", 1)
oBOMView.Renumber(001)

For Each oRow As BOMRow In oBOMView.BOMRows
	oRow.ItemNumberLocked = True
Next

 

0 Likes
Message 5 of 10

J.Classens
Enthusiast
Enthusiast

Good suggestion. 

 

The only thing i tried not to do is adding an extra property. 

I want the change only on assembly level not the part level. 

 

 

0 Likes
Message 6 of 10

vizual.prof
Contributor
Contributor
This rule only blocks first-level items. How to make to block elements of all levels and under the levels?
0 Likes
Message 7 of 10

JhoelForshav
Mentor
Mentor
Accepted solution

@J.Classens  - The code below shows the warning message if any row is already locked.

@vizual.prof  - I also updated it to handle all levels of the BOMView.

 

Sub Main()
	
Dim oAsmDoc As AssemblyDocument
Dim oAsmCompDef As AssemblyComponentDefinition
Dim oBOMView As BOMView

oAsmDoc = ThisDoc.Document
oAsmCompDef = oAsmDoc.ComponentDefinition
oBOMView = oAsmCompDef.BOM.BOMViews.Item("Structured")

If oBOMView.BOMRows.OfType(Of BOMRow).Where(Function(t As BOMRow) t.ItemNumberLocked = True).Count > 0
MessageBox.Show("Renumbering can only be done once", "Warning")
Exit Sub
End If

oBOMView.Sort("Vendor", 1, "Part Number", 1)
oBOMView.Renumber(001)

For Each oRow As BOMRow In oBOMView.BOMRows
	LockItemNumbers(oRow)
Next
End Sub

Sub LockItemNumbers(oRow As BOMRow)
	oRow.ItemNumberLocked = True
	If oRow.ChildRows IsNot Nothing
	For Each cRow As BOMRow In oRow.ChildRows
		LockItemNumbers(cRow)
	Next
End If
End Sub
Message 8 of 10

xiaoming910p
Explorer
Explorer

Hi Guys,

 

I would like to lock only Normal/Purchased item. How can it be done? Please help!

 

'01_Load BOM template_Sort BOM
Dim doc As AssemblyDocument = ThisDoc.Document
Dim oBom As BOM = doc.ComponentDefinition.BOM
oBom.ImportBOMCustomization("D:\OneDrive - Eagle Technology As\Personal document\01_Inventor Templates\BOM template.XML") ' edit this line to point to the exported XML

If Not oBom.StructuredViewEnabled Then oBom.StructuredViewEnabled = True

For Each viewX As BOMView In oBom.BOMViews
    If viewX.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
        viewX.Sort2("BOM Structure", False, "Description", True, "Material", True) ' edit this line to match the properties to sort as needed
		viewX.Renumber(101, 1) 'renumber the BOM
		For Each oRow As BOMRow In viewX.BOMRows
		oRow.ItemNumberLocked = True
		Next
Exit For ' we are done now. Exit the loop.
End If
Next  

 Thanks

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor

Hi @xiaoming910p.  If you only want to lock the BOMRow Item Number if it is 'Normal or Purchased', then you can test for that condition using the BOMRow.BOMStructure property, which will have a variation of the BOMStructureEnum as its value, when iterating through the BOMRows.

For Each oRow As BOMRow In viewX.BOMRows
	If oRow.BOMStructure = BOMStructureEnum.kNormalBOMStructure Or _
		oRow.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure Then
		oRow.ItemNumberLocked = True
	End If
Next

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 10

xiaoming910p
Explorer
Explorer
Hi @WCrihfield.
Thanks so much for the code. It works great for me.
0 Likes