Adding Total Mass to the Parts List

Adding Total Mass to the Parts List

ThomasSwanson
Advocate Advocate
790 Views
2 Replies
Message 1 of 3

Adding Total Mass to the Parts List

ThomasSwanson
Advocate
Advocate

Hello,

 

I've got a working Ilogic code here that will read the title of the active document and the weight and add it to the existing parts list. 

It works well for a drawing that has its main parts list on the first sheet.

Working Code:

 

oDocument = ThisDoc.Document
Try
	'look for the first parts list on the drawing
	Dim oPartsList As PartsList
	oPartsList = oDocument.ActiveSheet.PartsLists.Item(1)
	oPartsList.Title = "B.O.M. / " & iProperties.Value("Custom", "TITLE I") &" / "&iProperties.Value("Project", "Part Number") &"-"&ActiveSheet.Size
 
    Dim oPLCol As Inventor.PartsListColumn
    Dim currIndex As Integer
	Dim weightColumnIndex As Integer
 	
    currIndex = 1
       
    For Each oPLCol In oPartsList.PartsListColumns
        If Trim(UCase(oPLCol.Title)) = Trim(UCase("QTY. WT.")) Then
            weightColumnIndex = currIndex
        End If
        currIndex = currIndex + 1
    Next
    
 		
Dim chkItemNoCell As Inventor.PartsListCell
		
Dim newRow As Inventor.PartsListRow
            newRow = oPartslist.PartsListRows.Add(oPartslist.PartsListRows.Count, False)
            newRow.Height = myRowHeight
        
		
'Remove any existing TOTAL lines

	Dim chkTotCell As Inventor.PartsListCell
        For Each PLRow In oPartslist.PartsListRows
            If PLRow.Visible Then
                chkTotCell = PLRow.Item(1)
                If chkTotCell.Value = "TOTAL" Then
                    PLRow.Remove
                End If
            End If
        Next
            
		
		
            myCell = newRow.Item(1)
           myCell.Value = "TOTAL"

 myCell = newRow.Item(weightColumnIndex)
 
 
 'Define the open document
		Dim openDoc As Document
		openDoc = ThisDoc.Document

'Look at the model file referenced in the open document
		Dim docFile As Document
		docFile = ThisDoc.ModelDocument

'format model file name                   
		Dim FNamePos As Long
		FNamePos = InStrRev(docFile.FullFileName, "\", -1)                        
		Dim docFName As String 
		docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos) 
'Reads mass value from part file
		kgmass = iProperties.Mass(docFName)
'Converts mass value to LB
		mass = kgmass * 2.20462
'Writes Value of Mass to Parts List   
		myCell.Value = mass
	
  
 Catch
	'inform user no parts list was found
	MessageBox.Show("No Parts List Found", "iLogic")
End Try
'Forces Inventor to update
InventorVb.DocumentUpdate()

 

 

 

So now I am trying to expand this ilogic rule to accomplish a few more things.

1. It will loop through all sheets of a drawing looking for the parts list.

2. If it finds a parts list it will update the title of the parts list to show which assembly it came from .

3. It will update the total mass of the parts list with that of the referneced assembly.

 

Currently the title and the mass are incorrect as it keeps referencing back to the active document and not the assembly each parts list is made from.

 

I was able to enclose it in a for loop for the number of sheets and get it to loop through all sheets in a drawing set.

Edited code with looping:

Sub Main()
	oDocument = ThisDoc.Document
	Dim oDrawingDoc as Inventor.DrawingDocument
	oDrawingDoc = ThisApplication.ActiveDocument
	
	'Cycle Through all Sheets
	For SheetNumber = 1 To oDrawingDoc.Sheets.Count
		oDrawingDoc.Sheets(SheetNumber).Activate
		
		Try
		'look for the first parts list on the drawing
		Dim oPartsList As PartsList
		'Need to loop through each sheet looking for parts list 
		oPartsList = oDocument.ActiveSheet.PartsLists.Item(1)
		'Need to refernece the title of the part or assembly not the drawing file 
		oPartsList.Title = "Parts List : " &iProperties.Value("Project", "Part Number")
 
    	Dim oPLCol As Inventor.PartsListColumn
    	Dim currIndex As Integer
		Dim weightColumnIndex As Integer
 	
    	currIndex = 1
       
    	For Each oPLCol In oPartsList.PartsListColumns
		'Parts List must have a column called QTY. WT. for the code to run.  Or adjust weight column name to match what the code is looking for. 
        If Trim(UCase(oPLCol.Title)) = Trim(UCase("QTY MASS")) Then
            weightColumnIndex = currIndex
        End If
        currIndex = currIndex + 1
    	Next
    
 		
		Dim chkItemNoCell As Inventor.PartsListCell
		
		Dim newRow As Inventor.PartsListRow
            newRow = oPartslist.PartsListRows.Add(oPartslist.PartsListRows.Count, False)
            newRow.Height = myRowHeight
        
		
			'Remove any existing TOTAL lines

			Dim chkTotCell As Inventor.PartsListCell
        	For Each PLRow In oPartslist.PartsListRows
            If PLRow.Visible Then
                chkTotCell = PLRow.Item(1)
                If chkTotCell.Value = "TOTAL" Then
                    PLRow.Remove
                End If
            End If
        Next
            
		
		
            myCell = newRow.Item(1)
           myCell.Value = "TOTAL"

 		myCell = newRow.Item(weightColumnIndex)
 
 
 		'Define the open document
		Dim openDoc As Document
		openDoc = ThisDoc.Document

		'Look at the model file referenced in the open document
		Dim docFile As Document
		docFile = ThisDoc.ModelDocument

		'format model file name                   
		Dim FNamePos As Long
		FNamePos = InStrRev(docFile.FullFileName, "\", -1)                        
		Dim docFName As String 
		docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos) 
		'Reads mass value from part file
		kgmass = iProperties.Mass(docFName)
		'Converts mass value to LB
		mass = kgmass * 2.20462
		'Writes Value of Mass to Parts List   
		myCell.Value = mass
	
  
 		Catch
		'inform user no parts list was found
		MessageBox.Show("No Parts List Found", "iLogic")
		End Try
		
	Next


'Forces Inventor to update
InventorVb.DocumentUpdate()
End Sub

 

 

I know its a long post, Sorry.

 

Summary.   How do you refernece the the assembly a parts list comes from?

0 Likes
791 Views
2 Replies
Replies (2)
Message 2 of 3

ThomasSwanson
Advocate
Advocate

Ok, I've made some progress.  I've managed to solve the three issues I wanted. I have it looping through sheets, and referencing the name and weight of the assembly the parts list comes from.

 

Two more issues have appeared.

Multiple different sub assemblies and multiple parts list may appear on a single page.

Therefore the code now needs to:

1.Find all parts list on a drawing, not just the first one.

2.Only process certain parts list with a given style. 

 

Here is my code so far with the two issues above not working.

Sub Main()
	oDocument = ThisDoc.Document
	Dim oDrawingDoc As Inventor.DrawingDocument
	oDrawingDoc = ThisApplication.ActiveDocument
	
	'Need to loop through each sheet looking for parts list 
	'Cycle Through all Sheets
	For SheetNumber = 1 To oDrawingDoc.Sheets.Count
		oDrawingDoc.Sheets(SheetNumber).Activate
		
		Try
		'look for the first parts list on the drawing
		Dim oPartsList As PartsList
		
		oPartsList = oDocument.ActiveSheet.PartsLists.Item(1)
		'Need to reference the file that the parts list looks at to build the parts list
		Dim PLREF as String
		PLREF = oPartsList.ReferencedDocumentDescriptor.DisplayName
		'MessageBox.Show(PLREF, "Title")

		'This removed the file extension (.ipt, iam) from the file name
		PartsListName = Mid(PLREF, 1, Len(PLREF)-4)
		
		'Adding name of parts list file to the title of the parts list. 
		oPartsList.Title = "Parts List : " & PartsListName
'		oPartsList.Title = "Parts List : " &iProperties.Value("Project", "Part Number")
 
    	Dim oPLCol As Inventor.PartsListColumn
    	Dim currIndex As Integer
		Dim weightColumnIndex As Integer
 	
    	currIndex = 1
		MessageBox.Show("Message1", "Title")

       
    	For Each oPLCol In oPartsList.PartsListColumns
		'Parts List must have a column called QTY. WT. for the code to run.  Or adjust weight column name to match what the code is looking for. 
        If Trim(UCase(oPLCol.Title)) = Trim(UCase("MASS")) Then
            weightColumnIndex = currIndex
			MessageBox.Show("Message2", "Title")

        End If
        currIndex = currIndex + 1
    	Next
    MessageBox.Show("Message3", "Title")

 		
		Dim chkItemNoCell As Inventor.PartsListCell
		
		Dim newRow As Inventor.PartsListRow
            newRow = oPartslist.PartsListRows.Add(oPartslist.PartsListRows.Count, False)
            newRow.Height = myRowHeight
        
		
			'Remove any existing TOTAL lines

			Dim chkTotCell As Inventor.PartsListCell
        For Each PLRow In oPartslist.PartsListRows
            If PLRow.Visible Then
                chkTotCell = PLRow.Item(1)
                If chkTotCell.Value = "TOTAL" Then
                    PLRow.Remove
					MessageBox.Show("Message4", "Title")

                End If
            End If
        Next
            
		
		
        myCell = newRow.Item(1)
        myCell.Value = "TOTAL"
		myCell = newRow.Item(weightColumnIndex)
		   
		Dim kgmass as String
		'Reading Mass from Parts List file
		kgmass = iProperties.Mass(PLREF)
 
 
		'Converts mass value to LB
		mass = kgmass * 2.20462
		MessageBox.Show(mass, "Title")

		'Writes Value of Mass to Parts List   
		myCell.Value = mass
	
  
 		Catch
		'inform user no parts list was found
		MessageBox.Show("No Parts List Found", "iLogic")
		End Try
		
	Next


'Forces Inventor to update
InventorVb.DocumentUpdate()
End Sub
0 Likes
Message 3 of 3

adam.nagy
Autodesk Support
Autodesk Support

Hi Thomas,

 

The same way you are iterating through the sheets you can also iterate through the parts lists on the sheet.

 

Also, just a tiny improvement: you can use For Each to iterate through a collection which is a bit more efficient.

So instead of 

For SheetNumber = 1 To oDrawingDoc.Sheets.Count
  oDrawingDoc.Sheets(SheetNumber).Activate
  ' etc

... you could write: 

For Each oSheet As Sheet In oDrawingDoc.Sheets
  oSheet.Activate
  ' etc

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes