Doesn't pick up virtual Parts

Doesn't pick up virtual Parts

Fazlur_Rahaman
Advocate Advocate
627 Views
10 Replies
Message 1 of 11

Doesn't pick up virtual Parts

Fazlur_Rahaman
Advocate
Advocate

Hi there,

Can someone look at my code below and able to tell me why it doesn't recognize the Virtual Part Numbers?

 

Dim oBOM As BOM = ThisAssembly.Document.ComponentDefinition.BOM
'Make sure the views are enabled
oBOM.StructuredViewEnabled = True


Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues("C:\Users\XXXXX\Documents\Inventor iLogic External Rules\5066 Part Numbers.xlsx", "Sheet1", "A1","B2175")



oRowStart = 2 'first row check
oRowEnd = 2175 'lat row check
'get info from the XLS file
For MyRow = oRowStart To oRowEnd 'index row 2 through 200

'Primary Head
partNUMBER = GoExcel.CellValue("B" & MyRow)	
	
itemNUMBER1 = GoExcel.CellValue("A" & MyRow)


For Each oBOMRow As BOMRow In oBOM.BOMViews.Item("Structured").BOMRows


If oBOMRow.ComponentDefinitions(1).Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value.Contains(partNUMBER) Then
	oBOMRow.ItemNumber = itemNUMBER1
End If



Next
Next

'[
Dim doc As Document = ThisDoc.Document
Dim partslist As PartsList
Dim aDoc As AssemblyDocument

If (doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject) Then
    Dim dDoc As DrawingDocument = ThisDoc.Document
    partslist = dDoc.ActiveSheet.PartsLists.Item(1)
    aDoc = partslist.ReferencedDocumentDescriptor.ReferencedDocument
Else If (doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject) Then
    aDoc = doc
Else
	Return
End If

If (aDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject)
	Return
End If

Dim BOM As BOM = aDoc.ComponentDefinition.BOM
BOM.StructuredViewEnabled = True

Dim oBOMView As BOMView = BOM.BOMViews("Structured")



oBOMView.Sort("Item", True,)
oBOMView.Renumber(1, 1)
']
0 Likes
Accepted solutions (3)
628 Views
10 Replies
Replies (10)
Message 2 of 11

A.Acheson
Mentor
Mentor

Hi @Fazlur_Rahaman 

I suspect this is because the document attached to the component definition will be an assembly when filtering for partnumber iproperties, virtual parts do not have sn individual document outsidevof an assembly. Virtual parts have the property sets inside the component definition as they live only inside an assembly. See virtualcomponentdefinition help page here

Try this for virtual parts. 

 

If oBOMRow.ComponentDefinitions(1).PropertySets.Item("Design Tracking Properties").Item("Part Number").Value.Contains(partNUMBER) Then
	oBOMRow.ItemNumber = itemNUMBER1
End If

 

 I also notice you will not be able to get to nested virtual parts inside nested sub assemblies as the rule is not iterating through the children. See an example of BOM children iteration  in the API help here  

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 11

WCrihfield
Mentor
Mentor

Hi @Fazlur_Rahaman.  In addition to what @A.Acheson mentioned above, I see another place in your code (lines 6 & 7) that seems like it might not be right.  It looks like you are attempting to gather the values from a rectangular range of cells in Excel to a single ArrayList.  I do not think that works as intended.  Have you reviewed the contents of that ArrayList after that line of code in that iLogic rule, to see what it got?  You can normally only get multiple cell values of a single row or a single column.  The exception to that is when retrieving a named range from Excel using the IGoExcel.NamedRangeValue.  If the named range in Excel is a rectangular range containing multiple rows and multiple columns, you can receive a 2-dimensional Array of Object.  That is not a single list...it is more like multiple parallel lists, similar to how it was in Excel.

 

Edit:  After posting that, I looked closer, and it looks like you are not even using the data you retrieved in those two lines, and are just iterating through the Excel row directly after that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 11

Fazlur_Rahaman
Advocate
Advocate

Hi @A.Acheson , thank you for the suggestion. Can you reattach the link again? I am still a novice when it comes to coding.

 

I tried coping your code into mine, but keep getting following error Public member 'PropertySets' on type 'PartComponentDefinition' not found.

 

Hi @WCrihfield , the code does seem to work (except virtual component). The purpose is to iterate though the Excel rows and if matching Part number found, then assign 'Item Number' in the assembly.

 

The part numbers are listed in Excel column 'B' and Item number listed in Excel column 'A'. The code is to check the part number from excel against the Assembly BOM (Level one) and if match found, assign the corresponding Item number from Excel column A.

0 Likes
Message 5 of 11

A.Acheson
Mentor
Mentor

See virtualcomponentdefinition help page here

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

A.Acheson
Mentor
Mentor

You will likely need a try catch or filter the component definition and then set the propertysets. Propertysets are not contained in part definition but are in virtual definition. 

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

Fazlur_Rahaman
Advocate
Advocate

Thank you for the suggestion @A.Acheson 

 

I am able to manipulate the Item number of my virtual components. I am currently stuck at the part where i need to check if the Part number of this virtual component matches one of the rows in excel, you have any suggestion?

 

Here is my code:

 

Dim oBOM As BOM = ThisAssembly.Document.ComponentDefinition.BOM
'Make sure the views are enabled
oBOM.StructuredViewEnabled = True


Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues("C:\Users\fazlur.rahaman\Documents\Inventor iLogic External Rules\5066 Part Numbers.xlsx", "Sheet1", "A1", "B2176")



oRowStart = 2 'first row check
oRowEnd = 2176 'lat row check
'get info from the XLS file
For MyRow = oRowStart To oRowEnd 'index row 2 through XXXX


partNUMBER1 = GoExcel.CellValue("B" & MyRow)	

itemNUMBER1 = GoExcel.CellValue("A" & MyRow)



Dim oAsm As AssemblyDocument
    Dim oBomRows As BOMRowsEnumerator
    Dim oBomRow As BOMRow
    Dim oBomView As BOMView
    Dim oDef As ComponentDefinition
    Dim ItemNumber As String
	Dim PartNumber As String
    
    oAsm = ThisDoc.Document
    oBOM = oAsm.ComponentDefinition.BOM
    oBOM.StructuredViewEnabled = True
    oBOM.StructuredViewFirstLevelOnly = False
    
    oBomView = oBOM.BOMViews("Structured")
    oBomRows = oBomView.BOMRows
    
    For Each oBomRow In oBomRows
        oDef = oBomRow.ComponentDefinitions(1)
		

If TypeOf oDef Is VirtualComponentDefinition Then

 'NEED TO CHECK IF THE PART NUMBER OF THIS VIRTUAL COMPONENT MATCHES ONE OF THE ROWS IN EXCEL

			oBomRow.ItemNumber = itemNUMBER1
			
			
		Else If oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value.Contains(partNUMBER1) Then
	oBomRow.ItemNumber = itemNUMBER1

			
        End If
        
    Next   


Next



 
0 Likes
Message 8 of 11

A.Acheson
Mentor
Mentor
Accepted solution

Maybe something like this.

If TypeOf oDef Is VirtualComponentDefinition Then		
   If oDef.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = partNUMBER1 Then
     'DO SOMETHING
    End If
End If
        

 

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

Fazlur_Rahaman
Advocate
Advocate

Thank you for all your help again @A.Acheson. I got the code to work as expected. However i have run into another issue. I have some parts that have quantity of '0', this is from suppressing Model State assemblies. I know this issue of BOM quantity of '0' was fixed in the Inventor 2023, but my organization is using 2022 and will do so for another year. So the parts that have '0' quantity are throwing up errors, its throwing error on line 38 in my code, where defined:

      oDef = oBomRow.ComponentDefinitions(1)

 

Do you have any suggestion on how to solve this?

 

Here is the full code:

 

Dim oBOM As BOM = ThisAssembly.Document.ComponentDefinition.BOM
'Make sure the views are enabled
oBOM.StructuredViewEnabled = True
	Dim oAsm As AssemblyDocument
    Dim oBomRows As BOMRowsEnumerator
    Dim oBomRow As BOMRow
    Dim oBomView As BOMView
    Dim oDef As ComponentDefinition
    Dim ItemNumber As String
	Dim PartNumber As String


    
    oBomView = oBOM.BOMViews("Structured")
    oBomRows = oBomView.BOMRows
	
	'organize the inital BOM from 3000
	oBomView.Sort("Item", True,)
	oBomView.Renumber(3000, 1)
    
Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues("C:\Users\fazlur.rahaman\Documents\Inventor iLogic External Rules\5066 Part Numbers.xlsx", "Sheet1", "A1", "B2176")



oRowStart = 2 'first row check
oRowEnd = 2176 'last row check
'get info from the XLS file
For MyRow = oRowStart To oRowEnd 'index row 2 through XXXX


partNUMBER1 = GoExcel.CellValue("B" & MyRow)	

itemNUMBER1 = GoExcel.CellValue("A" & MyRow)


   For Each oBomRow In oBomRows
        oDef = oBomRow.ComponentDefinitions(1)


		 If TypeOf oDef Is VirtualComponentDefinition Then
			If oDef.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = partNUMBER1 Then	
	 	 		 oBomRow.ItemNumber = itemNUMBER1
    		End If
						
			
		Else If oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value.Contains(partNUMBER1) Then
				oBomRow.ItemNumber = itemNUMBER1

		Else If oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value="" Then 
				oBomRow.ItemNumber = 3100

		End If	
    Next   

Next

'sort and renumber
oBomView.Sort("Item", True,)
oBomView.Renumber(1, 1)
0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor
Accepted solution

Try editing those first couple lines of your loop like the following:

 

For Each oBomRow In oBomRows
	oDef = Nothing
	Try : oDef = oBomRow.ComponentDefinitions(1) : Catch : End Try
	If oDef Is Nothing Then Continue For

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 11

Fazlur_Rahaman
Advocate
Advocate
Accepted solution

That worked perfectly, thank you @WCrihfield  & @A.Acheson for all your help.

 

Here is the final working code if anyone interested.

 

Dim oBOM As BOM = ThisAssembly.Document.ComponentDefinition.BOM
	
	'Make sure the views are enabled
	oBOM.StructuredViewEnabled = True
	Dim oAsm As AssemblyDocument
    Dim oBomRows As BOMRowsEnumerator
    Dim oBomRow As BOMRow
    Dim oBomView As BOMView
    Dim oDef As ComponentDefinition
    Dim ItemNumber As String
	Dim PartNumber As String


    
    oBomView = oBOM.BOMViews("Structured")
    oBomRows = oBomView.BOMRows
	
	'organize the inital BOM from 3000
	oBomView.Sort("Item", True,)
	oBomView.Renumber(3000, 1)
    
Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues("C:\Users\fazlur.rahaman\Documents\Inventor iLogic External Rules\5066 Part Numbers.xlsx", "Sheet1", "A1", "B2176")



oRowStart = 2 'first row check
oRowEnd = 2176 'last row check
'get info from the XLS file
For MyRow = oRowStart To oRowEnd 'index row 2 through XXXX


partNUMBER1 = GoExcel.CellValue("B" & MyRow)	

itemNUMBER1 = GoExcel.CellValue("A" & MyRow)


   For Each oBomRow In oBomRows
	   oDef = Nothing
        Try : oDef = oBomRow.ComponentDefinitions(1) : Catch : End Try 
			If oDef Is Nothing Then Continue For


		 If TypeOf oDef Is VirtualComponentDefinition Then
			If oDef.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = partNUMBER1 Then	
	 	 		 oBomRow.ItemNumber = itemNUMBER1
    		End If	
			
		Else If oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value.Contains(partNUMBER1) Then
				oBomRow.ItemNumber = itemNUMBER1

		Else If oDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value="" Then 
				oBomRow.ItemNumber = 3100

		End If	
    Next   

Next

'sort and renumber
oBomView.Sort("Item", True,)
oBomView.Renumber(1, 1)

 

0 Likes