iLogic BOM - Part Number Row Merge Setting

iLogic BOM - Part Number Row Merge Setting

emanuel.c
Collaborator Collaborator
450 Views
4 Replies
Message 1 of 5

iLogic BOM - Part Number Row Merge Setting

emanuel.c
Collaborator
Collaborator

Hi! I have this code, largely from here https://forums.autodesk.com/t5/inventor-forum/structured-bom-total-quantity-of-loose-components/m-p/...  I write an iProperty QTY / Project in each Referenced Document of the Assembly with the total part quantity. This I then use in the Parts List on a drawing.

 

My issue is with "Part Number Row Merge" in a BOM. Does someone have a suggestion regarding this?

Can this setting be read with iLogic? In this case if the row is merged to read both quantities, add them and write this total for both, so as to be correct?

 

Thank you!

 

Sub Main ()
	
	oDoc = ThisApplication.ActiveDocument
	If oDoc.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "Error")
	Return
	End If

	oTotalQTY

End Sub

Sub oTotalQTY
	oCompDef = ThisDoc.Document.ComponentDefinition
	openDoc = ThisDoc.Document
	customPropertyName = "QTY / Project"
	
    For Each docFile In openDoc.AllReferencedDocuments         
		FNamePos = InStrRev(docFile.FullFileName, "\", -1)        
		docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos) 
		If docFile.IsModifiable = True Then                         
			assemblyDoc = openDoc         
			assemblyDef = assemblyDoc.ComponentDefinition
			partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
			Try
				If partQty.Count <>  iProperties.Value(docFName, "Custom", customPropertyName) Then                
					iProperties.Value(docFName, "Custom", customPropertyName) = partQty.Count
				End If                      
			Catch
				iProperties.Value(docFName, "Custom", customPropertyName) = partQty.Count
			End Try
		End If
	Next
	
	MessageBox.Show("Completed Successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

End Sub

merge rows.PNG

 

0 Likes
Accepted solutions (1)
451 Views
4 Replies
Replies (4)
Message 2 of 5

Andrii_Humeniuk
Advisor
Advisor

Hi @emanuel.c . If I understood you correctly. This iLogic rule should help you. Also I noticed that you only need the parts so I added that to the code and it skips all the subassemblies docs.

Sub Main ()	
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If oDoc.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "Error")
		Exit Sub
	End If
	Dim customPropertyName As String = "QTY / Project"
	Dim oAsmDoc As AssemblyDocument = oDoc
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oAsmDef.Occurrences
	Dim oDict As New Dictionary(Of String, Integer)	
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Or
			Not oRefDoc.IsModifiable Then Continue For
		Dim sNamePart As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim iCount As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		If oDict.ContainsKey(sNamePart) Then
			oDict(sNamePart) = oDict(sNamePart) + iCount
		Else
			oDict.Add(sNamePart, iCount)
		End If
	Next
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Or
			Not oRefDoc.IsModifiable Then Continue For
		Dim sNamePart As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim iCount As Integer = oDict(sNamePart)
		If iCount = 0 Then Continue For
		Dim oCustom As PropertySet = oRefDoc.PropertySets("Inventor User Defined Properties")
		Try : oCustom(customPropertyName).Value = iCount
		Catch : oCustom.Add(iCount, customPropertyName) : End Try
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 5

emanuel.c
Collaborator
Collaborator

Hi  @Andrii_Humeniuk  Thank you so much for your help!

 

I do actually need to write to both parts and assemblies and that's easy - I can take that clause out.

But maybe I wasn't clear. This is for the case in which the file names (and essentially the parts) are different but since they are geometrically the same they get the same part number and drawing.

As such I was thinking to read the iProperty "Part Number" instead of oRefDoc.FullFileName. Unfortunately when I try to modify this I get some error on line:

If oDict.ContainsKey(sNamePart) Then

I've never used "Dictionary" - not sure how that is supposed to work. Does that convert Integer to String?

 

Thank you!

 

Sub Main ()	
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If oDoc.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "Error")
		Exit Sub
	End If
	Dim customPropertyName As String = "QTY / Project"
	Dim oAsmDoc As AssemblyDocument = oDoc
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oAsmDef.Occurrences
	Dim oDict As New Dictionary(Of String, Integer)	
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.IsModifiable Then Continue For
			
    	Dim oPropSets = oRefDoc.PropertySets
    	Dim oPropSet = oPropSets.Item("Design Tracking Properties")
    	Dim oPartNumiProp = oPropSet.Item("Part Number")
        'MsgBox("The part number is: " & oPartNumiProp.Value)		
		
		Dim sNamePart = oPartNumiProp' As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim iCount As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		If oDict.ContainsKey(sNamePart) Then
			oDict(sNamePart) = oDict(sNamePart) + iCount
		Else
			oDict.Add(sNamePart, iCount)
		End If
	Next
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.IsModifiable Then Continue For
		Dim sNamePart As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim iCount As Integer = oDict(sNamePart)
		If iCount = 0 Then Continue For
		Dim oCustom As PropertySet = oRefDoc.PropertySets("Inventor User Defined Properties")
		Try : oCustom(customPropertyName).Value = iCount
		Catch : oCustom.Add(iCount, customPropertyName) : End Try
	Next
End Sub
0 Likes
Message 4 of 5

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Removed from the code the part that works only with parts and added a selection of not component names but their PartNumber. Hope this is what you need.

Sub Main ()	
	Dim oDoc As Document = ThisApplication.ActiveDocument
	If oDoc.DocumentType <> kAssemblyDocumentObject Then
		MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "Error")
		Exit Sub
	End If
	Dim customPropertyName As String = "QTY / Project"
	Dim oAsmDoc As AssemblyDocument = oDoc
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oAsmDef.Occurrences
	Dim oDict As New Dictionary(Of String, Integer)	
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.IsModifiable Then Continue For
'		Dim sNamePart As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim sPartNumb As String = oRefDoc.PropertySets("Design Tracking Properties")("Part Number").Value
		Dim iCount As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		If oDict.ContainsKey(sPartNumb) Then
			oDict(sPartNumb) = oDict(sPartNumb) + iCount
		Else
			oDict.Add(sPartNumb, iCount)
		End If
	Next
	For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
		If Not oRefDoc.IsModifiable Then Continue For
'		Dim sNamePart As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim sPartNumb As String = oRefDoc.PropertySets("Design Tracking Properties")("Part Number").Value
		Dim iCount As Integer = oDict(sPartNumb)
		If iCount = 0 Then Continue For
		Dim oCustom As PropertySet = oRefDoc.PropertySets("Inventor User Defined Properties")
		Try : oCustom(customPropertyName).Value = iCount
		Catch : oCustom.Add(iCount, customPropertyName) : End Try
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 5

emanuel.c
Collaborator
Collaborator

Such a beautiful piece of code! It works perfectly.

Thank you very much!