If
you remember, a while back I have created an ilogic code to add item number on
drawing views (original post). Some companies like to detail parts along with assembles on same
drawing or even on same sheet.
I
was doing a drawing for a frame and because there were mostly standard shapes
that this frame was build out of I decided to document the rest of the
components on same sheet. This was not an issue for my code but on frame
generator drawings I don’t use a Parts list but rather a Material List.
The
difference is that it doesn’t have an “ITEM” column and so the code would fail
to work. You can manually add a Parts List on the drawing outside the sheet but
the code will still not work because it looks at the first PartsList on the
sheet and in this case it was my Material List.
Don’t
get confused, Parts List, Material Lists, Cut-To-Length lists are all the same,
just customized to show different info.
 |
| Parts List style |
Material
Lists contains, total length, stock number, Description, Material and individual
items are merged to show total quantity
rather than number of each item.
 |
| Cumulated lengths |
This
is setup to work with Tube and Pipe members as well. Remember that Frame
Generator use G_L as length and Pipes use PL for length and if you check the column
settings for Quantity you will see that if the member is pipe (PL parameter
exists) then it will use it as quantity.
 |
| Works with TP pipes. |
Now
that we got that out of the way and familiarized ourself with the different
type of Parts Lists let’s discuss the change in code.
The
original code was looking at the first parts list
'oPartsList =
oDrawDoc.ActiveSheet.PartsLists.Item(1)
and
we need it to search for a specific one “Parts List” so the code became
For i=1 To oPartsLists.Count
If oPartsLists.Item(i).Title = "PARTS LIST" Then
oPartsList = oPartsLists.Item(i)
Exit For
Else
'do nothing
End If
Next
-------------------------------------------------------------------------------------------------------------
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheets As Sheets
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet'Inventor.Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oPartsLists As PartsLists
Dim oPartsList As PartsList
For Each oSheet In oSheets
'declare the PartsLists
oPartsLists = oSheet.PartsLists
'try and get the parts list form the table of this sheet
Try
'this doesn't work when you have a material list like on frame drawings
'oPartsList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
'place a parts list on the drawing and search for it
'rather than using an id number
For i=1 To oPartsLists.Count
If oPartsLists.Item(i).Title = "PARTS LIST" Then
'MessageBox.Show("found table", "ilogic")
oPartsList = oPartsLists.Item(i)
Exit For
Else
'do nothing
End If
Next
Catch 'on error try and search all sheets for first found parts list
'iterate trough each sheet
Dim j As Long
For j = 1 To oDrawDoc.Sheets.Count
If oDrawDoc.Sheets.Item(j).PartsLists.Count > 0 Then Exit For
Next
'this doesn't work when you have other parts lists like a
'"material list" like on frame drawing
'oPartList = oDrawDoc.Sheets.Item(i).PartsLists.Item(1)
'place a parts list on the drawing and search for it
'rather than using an id number to locate the first
For i=1 To oPartsLists.Count
If oPartsLists.Item(i).Title = "PARTS LIST" Then
'MessageBox.Show("found table", "ilogic")
oPartsList = oPartsLists.Item(i)
Exit For
Else
'do nothing
End If
Next
'MessageBox.Show("parts list found on: " & j, "Title")
End Try
oViews = oSheet.DrawingViews
For Each oView In oViews
'Get the full filename Of the view model
Dim oModelFileName As String
oModelFileName = oView.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
'MessageBox.Show("view model name" & oModelFileName, "Title")
' Iterate through the contents of the parts list.
Dim j As Long
For j = 1 To oPartsList.PartsListRows.Count
' Get the current row.
Dim oRow As PartsListRow
oRow = oPartsList.PartsListRows.Item(j)
'get filename of model in row
Dim oRowFileName As String
Try ' try and get the full file name of the PL item
oRowFileName = oRow.ReferencedFiles.Item(1).FullFileName
Catch 'on error go to next item
' Dim oCellValue As String
' oCellValue = oRow.Item("Item").Value
' MessageBox.Show("Error Processing item: " & oCellValue, "Title")
Continue For
End Try
'compare the filenames
'Performs a text comparison, based on a case-insensitive text sort order
'If strings equal returns 0
If StrComp(oModelFileName, oRowFileName, CompareMethod.Text)=0 Then
'Get the value of Item from the Parts List
'Row name needs to be case sensitive or use 1 for first 2 for second etc.
oCell = oPartsList.PartsListRows.Item(j).Item("Item")
'Row name needs to be case sensitive or use 1 for first 2 for second etc.
'get the value of text in cell
Dim oItemValue As String
oItemValue = oCell.Value
'Show the view label
oView.ShowLabel = True
'format the text first line
oStringItem = "<StyleOverride Underline='True' FontSize='0.35'> ITEM " & oItemValue & " </StyleOverride>"
'format the text second line
oStringScale = "<Br/><StyleOverride FontSize='0.3'>(Scale <DrawingViewScale/>)</StyleOverride>"
'add to the view label
oView.Label.FormattedText = oStringItem & oStringScale
End If
Next
Next
Next
------------------------------------------------------------------------------------------------------------
EDIT: 11/08/16
if you would rather be prompted to select views rather than processing all of them , here is the code.
------------------------------------------------------------------------------------------------------------
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheets As Sheets
oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet'Inventor.Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView
Dim oPartsLists As PartsLists
Dim oPartsList As PartsList
For Each oSheet In oSheets
'declare the PartsLists
oPartsLists = oSheet.PartsLists
'try and get the parts list form the table of this sheet
Try
'this doesn't work when you have a material list like on frame drawings
'oPartsList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
'place a parts list on the drawing and search for it
'rather than using an id number
For i=1 To oPartsLists.Count
If oPartsLists.Item(i).Title = "PARTS LIST" Then
'MessageBox.Show("found table", "ilogic")
oPartsList = oPartsLists.Item(i)
Exit For
Else
'do nothing
End If
Next
Catch 'on error try and search all sheets for first found parts list
'iterate trough each sheet
Dim j As Long
For j = 1 To oDrawDoc.Sheets.Count
If oDrawDoc.Sheets.Item(j).PartsLists.Count > 0 Then Exit For
Next
'this doesn't work when you have other parts lists like a
'"material list" like on frame drawing
'oPartList = oDrawDoc.Sheets.Item(i).PartsLists.Item(1)
'place a parts list on the drawing and search for it
'rather than using an id number to locate the first
For i=1 To oPartsLists.Count
If oPartsLists.Item(i).Title = "PARTS LIST" Then
'MessageBox.Show("found table", "ilogic")
oPartsList = oPartsLists.Item(i)
Exit For
Else
'do nothing
End If
Next
'MessageBox.Show("parts list found on: " & j, "Title")
End Try
oViews = oSheet.DrawingViews
'get view from user
While True
oView = ThisApplication.CommandManager.Pick( _
SelectionFilterEnum.kDrawingViewFilter, "Select a View")
'Get the full filename Of the view model
Dim oModelFileName As String
oModelFileName = oView.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
'MessageBox.Show("view model name" & oModelFileName, "Title")
' Iterate through the contents of the parts list.
Dim j As Long
For j = 1 To oPartsList.PartsListRows.Count
' Get the current row.
Dim oRow As PartsListRow
oRow = oPartsList.PartsListRows.Item(j)
'get filename of model in row
Dim oRowFileName As String
Try ' try and get the full file name of the PL item
oRowFileName = oRow.ReferencedFiles.Item(1).FullFileName
Catch 'on error go to next item
' Dim oCellValue As String
' oCellValue = oRow.Item("Item").Value
' MessageBox.Show("Error Processing item: " & oCellValue, "Title")
Continue For
End Try
'compare the filenames
'Performs a text comparison, based on a case-insensitive text sort order
'If strings equal returns 0
If StrComp(oModelFileName, oRowFileName, CompareMethod.Text)=0 Then
'Get the value of Item from the Parts List
'Row name needs to be case sensitive or use 1 for first 2 for second etc.
oCell = oPartsList.PartsListRows.Item(j).Item("Item")
'Row name needs to be case sensitive or use 1 for first 2 for second etc.
'get the value of text in cell
Dim oItemValue As String
oItemValue = oCell.Value
'Show the view label
oView.ShowLabel = True
'format the text first line
oStringItem = "<StyleOverride Underline='True' FontSize='0.35'> ITEM " & oItemValue & " </StyleOverride>"
'format the text second line
oStringScale = "<Br/><StyleOverride FontSize='0.3'>(Scale <DrawingViewScale/>)</StyleOverride>"
'add to the view label
oView.Label.FormattedText = oStringItem & oStringScale
End If
Next
End While
Next
------------------------------------------------------------------------------------------------------------
Later,
ADS