Count all sub assemblies and parts - with merged part numbers

Count all sub assemblies and parts - with merged part numbers

darrell.wcd
Enthusiast Enthusiast
617 Views
6 Replies
Message 1 of 7

Count all sub assemblies and parts - with merged part numbers

darrell.wcd
Enthusiast
Enthusiast

I have patched together some ilogic that I found on this forum..

My intent is to iterate through the assembly and take the custom iProperty (Contract Number) and basically the (item number) and the REV letter to create a stock number which I will use as a short part number. Since I found that the Item number is not accessible via iLogic.. that is not an option. Unless there is a way to do that.

 

My part number is to long as I have parts that have 30+ options that are defined in the part number in order to merge parts on the BOM. (190854-17.75-1.75-1.75-0.1-10.5-7-SS-W) my vendors software has a 12 character limit, so even removing the "-" would not shorten it enough.

 

This also needs to include all the sub assemblies and parts, and it also needs to change the stock number of all instances, due to the part number merge, otherwise i get a stock number that says varies.

I cant seem to get the "PNum" to keep counting, I just need  "PNum" to keep counting so I don't end up with the same number in either a part or sub assembly. And when I turn off the "On Error Resume Next" I get 

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at ThisRule.ListItems(BOMRowsEnumerator Rows, Int32 indent, Document oDoc)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

some pointers or rewritten illogic would be much appreciated.

 

 

 

	Sub Main()
		'On Error Resume Next
		numFiles = 0
		
		Dim oAssem As AssemblyDocument = ThisDoc.Document
		Dim oBOM As BOM = oAssem.ComponentDefinition.BOM
		Dim BoxNote As String = "Drawing Automation"
		
		
		ANum = ANum + 1
		Dim oModelREV1 As String = oAssem.PropertySets("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}").ItemByPropId(9).Value
		Dim oModelConN1 As String = oAssem.PropertySets("Inventor User Defined Properties").Item("Contract Number").Value
		oAssem.PropertySets("Design Tracking Properties").Item("Stock Number").Value = oModelConN1 & "-AS" & ANum & "-" & oModelREV1
			
		
		oBOM.StructuredViewFirstLevelOnly = False
		oBOM.StructuredViewEnabled = True
		oBOM.PartsOnlyViewEnabled = True
		oBOM.SetPartNumberMergeSettings(True, )

		For Each oDoc In oAssem.AllReferencedDocuments
			
			numFiles = numFiles + 1
			If oDoc.DocumentType = 12291 Then Call ListItems(oBOM.BOMViews.Item(1).BOMRows, 0, oDoc) 'assembly
			If oDoc.DocumentType = 12290 Then Call ListItems(oBOM.BOMViews.Item(3).BOMRows, 0, oDoc) 'part
			
			MessageBox.Show(numFiles)
		Next
		MessageBox.Show("Thanks for being efficient!" _
		& vbLf & "There are   (  " & numFiles & "  )   files that have been updated.", BoxNote, MessageBoxButtons.OK)
	End Sub

	Sub ListItems(Rows As BOMRowsEnumerator, indent As Integer, oDoc As Document)
	
	PNum = PNum + 1
		'MessageBox.Show ("PNum")
		For Each oBOMRow As BOMRow In Rows
			
			rDoc = oBOMRow.ComponentDefinitions.Item(1).Document	
				
			If rDoc IsNot oDoc Then
			
				If Not oBOMRow.ChildRows Is Nothing Then
					Call ListItems(oBOMRow.ChildRows, indent + 1, oDoc)
				End If
						
				
			Else	
				
				i = 1
				
				For Each kDoc In  oBOMRow.ComponentDefinitions					
					rDoc = oBOMRow.ComponentDefinitions.Item(i).Document
													
					Dim oModelREV As String = oDoc.PropertySets("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}").ItemByPropId(9).Value
					Dim oModelConN As String = rDoc.PropertySets("Inventor User Defined Properties").Item("Contract Number").Value
							
					rDoc.PropertySets("Design Tracking Properties").Item("Stock Number").value = oModelConN & "-" & PNum & "-" & oModelREV
					PNum = PNum + 1
					MessageBox.Show(PNum)
					i = i + 1
					
					Next
			
			End If
			
		Next
		
		
	End Sub

 

 

0 Likes
Accepted solutions (1)
618 Views
6 Replies
Replies (6)
Message 2 of 7

JelteDeJong
Mentor
Mentor

does this work for you:

Dim doc As AssemblyDocument = ThisDoc.Document
Dim pNum = 1
For Each refDoc As Document In doc.AllReferencedDocuments

    Dim oModelREV As String = refDoc.PropertySets("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}").ItemByPropId(9).Value
    Dim oModelConN As String = "<unknown>"
    Try
        oModelConN = refDoc.PropertySets("Inventor User Defined Properties").Item("Contract Number").Value
    Catch ex As Exception
    End Try


    Dim newStockNumber = String.Format("{0}-{1}-{2}",
        oModelConN, pNum, oModelREV)
    refDoc.PropertySets("Design Tracking Properties").Item("Stock Number").Value = newStockNumber
    pNum = pNum + 1
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 7

darrell.wcd
Enthusiast
Enthusiast

No it doesn't..  because it give each instance a specific incremental number, and then when I go to parts only... it says *varies* on any that are merged because they are not the same number...

0 Likes
Message 4 of 7

JelteDeJong
Mentor
Mentor

oke 2e try.

Dim doc As AssemblyDocument = ThisDoc.Document
Dim knowPartNumbers As New Dictionary(Of String, Integer)
Dim pNum = 1
For Each refDoc As Document In doc.AllReferencedDocuments
    Dim partNumber As String = refDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value
    Dim oModelREV As String = refDoc.PropertySets("Inventor Summary Information").Item("Revision Number").Value
    Dim oModelConN As String = "<unknown>"
    Try
        oModelConN = refDoc.PropertySets("Inventor User Defined Properties").Item("Contract Number").Value
    Catch ex As Exception
    End Try

    Dim newStockNumber As String
    If (knowPartNumbers.ContainsKey(partNumber)) Then
        newStockNumber = String.Format("{0}-{1}-{2}",
            oModelConN, knowPartNumbers.Item(partNumber), oModelREV)
    Else
        newStockNumber = String.Format("{0}-{1}-{2}",
            oModelConN, pNum, oModelREV)
        knowPartNumbers.Add(partNumber, pNum)
        pNum = pNum + 1
    End If
	refDoc.PropertySets("Design Tracking Properties").Item("Stock Number").Value = newStockNumber
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 7

darrell.wcd
Enthusiast
Enthusiast

The error I get

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.Property.set_Value(Object )
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes
Message 6 of 7

darrell.wcd
Enthusiast
Enthusiast

@JelteDeJong 
I think I got this to work, see code below.

But is there a way I can format the incremental number to be 01, 02,... 12, 13 etc?

 

Sub Main()
	
'zero counters	
numFiles = 0
numerros = 0
numPurchase = 0


Dim BoxNote As String = "WCD Drawing Automation"
Dim doc As AssemblyDocument = ThisDoc.Document

Dim knowPartNumbers As New Dictionary(Of String, Integer)
Dim pNum = 1
For Each refDoc As Document In doc.AllReferencedDocuments
	
	Dim partNumber As String = refDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value
	'MessageBox.Show(partNumber)
	
	numFiles = numFiles+1
	Dim oModelProcess As String = refDoc.PropertySets("Inventor User Defined Properties").Item("Process").Value
	'MessageBox.Show(oModelProcess)
	'check if file is normal on BOM
	If refDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
				
	'dont work with purhase items
	If oModelProcess = "Purchase" Then
		numPurchase = numPurchase + 1
		'MessageBox.Show(partNumber & " is purchase item")
		GoTo 2
	End If
	
	'check If File Is read only
	Dim fi As New System.IO.FileInfo(refDoc.FullFileName)
	If fi.IsReadOnly = True Then
		'MessageBox.Show(partNumber & " is read only")
		numerros = numerros+1
		GoTo 2
	End If
	
    Dim oModelREV As String = refDoc.PropertySets("Inventor Summary Information").Item("Revision Number").Value
    Dim oModelConN As String = "<unknown>"
    Try
        oModelConN = refDoc.PropertySets("Inventor User Defined Properties").Item("Contract Number").Value
    Catch ex As Exception
    End Try
	
    Dim newStockNumber As String
    If (knowPartNumbers.ContainsKey(partNumber)) Then
        newStockNumber = String.Format("{0}-{1}-{2}",oModelConN, knowPartNumbers.Item(partNumber), oModelREV)
			
    Else
        newStockNumber = String.Format("{0}-{1}-{2}",oModelConN, pNum, oModelREV)
        knowPartNumbers.Add(partNumber, pNum)
	    pNum = pNum + 1
    End If
	
	Try
	refDoc.PropertySets("Design Tracking Properties").Item("Stock Number").Value = newStockNumber
	Catch
		MessageBox.Show(partNumber & " error")
	End Try
	2 :


Next

MessageBox.Show("There were (" & numerros & ") that are read only and not updated." _
& vbLf & "There were (" & numPurchase & ") Purhase items not updated." _
& vbLf & "There were (" & numFiles & ") looked at!" _
& vbLf & "" _ 
& vbLf & "Please check the BOM and verify that all Stock Numbers are correct!", BoxNote, MessageBoxButtons.OK, MessageBoxIcon.Error)


End Sub

 

0 Likes
Message 7 of 7

JelteDeJong
Mentor
Mentor
Accepted solution

I checked your code and changed a few things. Also, I added a leading zero or the numbers.

 

'zero counters	
Dim numFiles = 0
Dim numerros = 0
Dim numPurchase = 0

Dim BoxNote As String = "WCD Drawing Automation"
Dim doc As AssemblyDocument = ThisDoc.Document

Dim knowPartNumbers As New Dictionary(Of String, Integer)
Dim pNum = 1
For Each refDoc As Document In doc.AllReferencedDocuments

    Dim partNumber As String = refDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value

    numFiles = numFiles + 1
    Dim oModelProcess As String = "<unknown>"
    Try
        oModelProcess = refDoc.PropertySets("Inventor User Defined Properties").Item("Process").Value
    Catch ex As Exception
    End Try

    'check if file is normal on BOM
    If refDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For

    'dont work with purhase items
    If oModelProcess = "Purchase" Then
        numPurchase = numPurchase + 1
        Continue For
    End If

    'check If File Is read only
    Dim fi As New System.IO.FileInfo(refDoc.FullFileName)
    If fi.IsReadOnly = True Then
        numerros = numerros + 1
        Continue For
    End If

    Dim oModelREV As String = refDoc.PropertySets("Inventor Summary Information").Item("Revision Number").Value
    Dim oModelConN As String = "<unknown>"
    Try
        oModelConN = refDoc.PropertySets("Inventor User Defined Properties").Item("Contract Number").Value
    Catch ex As Exception
    End Try

    Dim newStockNumber As String
    If (knowPartNumbers.ContainsKey(partNumber)) Then
        newStockNumber = String.Format("{0}-{1}-{2}",
                                       oModelConN,
                                       knowPartNumbers.Item(partNumber),
                                       oModelREV)
    Else
        newStockNumber = String.Format("{0}-{1}-{2}",
                                       oModelConN,
                                       String.Format("{0:00}", pNum),
                                       oModelREV)
        knowPartNumbers.Add(partNumber, pNum)
        pNum = pNum + 1
    End If

    Try
        refDoc.PropertySets("Design Tracking Properties").Item("Stock Number").Value = newStockNumber
    Catch
        MessageBox.Show(partNumber & " error")
    End Try
Next

MessageBox.Show("There were (" & numerros & ") that are read only and not updated." _
& vbLf & "There were (" & numPurchase & ") Purhase items not updated." _
& vbLf & "There were (" & numFiles & ") looked at!" _
& vbLf & "" _
& vbLf & "Please check the BOM and verify that all Stock Numbers are correct!", BoxNote, MessageBoxButtons.OK, MessageBoxIcon.Error)

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes