Sunday, 19 October 2014

Item Number on drawing views

 EDIT Jul 15 2016:


If you have more than one PartsList on the drawing  you might want to read this.

END EDIT Jul 15 2016

------------------------------------------------------------------------------------------------------------



 EDIT Nov 28 2014:


Question from the Autdesk Inventor Forum:
Can I have the Item Number on just the main views?
You need to look into identifying the main views. I have a new blog on how to identify the view type here.
But, if you edit the Item Number on Drawing Views code and replace:



oStringItem = "<StyleOverride Underline='True' FontSize='0.35'> ITEM " & oItemValue & " </StyleOverride>"


 with



If oView.ViewType = 10501 Then
  oStringItem = "<StyleOverride Underline='True' FontSize='0.35'> ITEM " & oItemValue & " </StyleOverride>"
Else
  oStringItem = "<StyleOverride Underline='True' FontSize='0.35'></StyleOverride>"
End If

 it will add the label on just the main views. You can also use kProjectedDrawingViewType instead of 10501.


END EDIT Nov 28 2014

------------------------------------------------------------------------------------------------------------

EDIT Nov 25 2014:

This is growing legs, and that's good. I have been told that it has an error processing virtual parts. I like to add virtual parts like grout, grease, oil, etc. so it has a problem getting the filepath of those items.

Here's a quick update that will skip rows with error (delete the comment ' symbol to see which rows have error):

Item_no_on_Drawing_Views_v1.2

I fyou replace:

oRowFileName = oRow.ReferencedFiles.Item(1).FullFileName

with

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
   Continue For
End Try

you should be fine.

END EDIT Nov 25 2014

------------------------------------------------------------------------------------------------------------

EDIT Nov 10 2014:

Thanks to Brendan Henderson that tested the code and found an error. I have updated the code.

It should work like this (for each sheet in the drawing):

Look for a parts list inside current sheet ( in case you have multiple subassemblies on same drawing, hopefully each on it's own sheet along with any detail views of components)
If found, update the all views in the current sheet with Item Number based on that Parts list
If there is no parts list in the current sheet, will search in all the sheets and use the first one it finds (for those that have main assembly and parts list on first sheet and component details on separate sheets).

If you have multiple parts lists make sure that all the views that you want updated for that parts list are on the same sheet with the parts list.
here is the code.
Item Number on Drawing Views V1.1

END EDIT Nov 10 2014.

------------------------------------------------------------------------------------------------------------

For all of you out there that like to add part views on assembly layouts this might be handy. A question was posted the other day on Inventor forum and the user there needed the Item number from the parts list to show up in the drawing view info.

Because his company has very few duplicates and lots of new parts he ended up with 10 digits part numbers that was in no way usable, either as a call out balloon or in parts list.

One way was to create a custom iproperty to hold that value and then to call it in the view label. This might work fine as long as you don't reuse the part.

There is another solution however that will give us just that, the parts list item number on the drawing view.

It's ilogic of course and here's a short description of how that works. It compares the model referenced in the view with the model of each row entry in the parts list. When match is found it gets the parts list item number, turns the view label on and adds this text with a little formatting (underline and size) in the drawing view.

The code asks you to select a views to process because you don't want to process all views (assembly views especially). We could find a value like Part Number in the table but there was no Part Number column and it could have been a problem if he had "Enable Part Row Merge" in the BOM. I decided to use filename with complete path because there might have been multiple parts with same filename but windows doesn't allow files with same name in the same place.

Here is the code, I hope it makes sense (the original code in text is attached here):

EDIT:
This code had a bug, and I have updated the code, will keep the old one in here in case someone needs just parts of the code.
END EDIT.



' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

'view selection code
Dim oSSet As SelectSet = ThisDoc.Document.SelectSet
If oSSet.count = 0 Then
MessageBox.Show("You must select a drawing view first", "iLogic")
Exit Sub
End If


'start processsing all selections
Dim i As Long
For i = 1 To oSSet.Count

'Reference to the drawing view from the current selected object
Dim oView As DrawingView = trycast(oSSet.item(i), DrawingView)
If oView IsNot Nothing Then
'Get the full filename Of the view model
Dim oModelFileName As String
oModelFileName = oView.ReferencedDocumentDescriptor.ReferencedDocument.FullFileName
' Set a reference to the first parts list on the active sheet.
' This assumes that a parts list is on the active sheet.
Dim oPartList As PartsList
oPartList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
' Iterate through the contents of the parts list.
Dim j As Long
For j = 1 To oPartList.PartsListRows.Count
' Get the current row.
Dim oRow As PartsListRow
oRow = oPartList.PartsListRows.Item(j)
'get filename of model in row
Dim oRowFileName As String
oRowFileName = oRow.ReferencedFiles.Item(1).FullFileName
'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 = oPartList.PartsListRows.Item(j).Item("Item")
'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
Else
MessageBox.Show("The selected object is not a drawing view", "iLogic")
End If
Next


NEW IMPROVED 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
Dim Sheet As Inventor.Sheet
Dim oViews As DrawingViews
Dim oView As DrawingView

For Each oSheet In oDrawDoc.Sheets
'For Each oSheet In oSheets
    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")

        Dim oPartList As PartsList
            'try and get the parts list form the table of this sheet
            Try
                oPartList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
            Catch 'on error try and search all sheets for first found parts list           
                'iterate trough each sheet
                Dim i As Long
                For i = 1 To oDrawDoc.Sheets.Count
                    If oDrawDoc.Sheets.Item(i).PartsLists.Count > 0 Then Exit For
                Next   
                oPartList = oDrawDoc.Sheets.Item(i).PartsLists.Item(1)
                'MessageBox.Show("parts list found on: " & i, "Title")
            End Try
               
            ' Iterate through the contents of the parts list.
            Dim j As Long
            For j = 1 To oPartList.PartsListRows.Count
                ' Get the current row.
                Dim oRow As PartsListRow
                oRow = oPartList.PartsListRows.Item(j)
                'get filename of model in row
                Dim oRowFileName As String
                oRowFileName = oRow.ReferencedFiles.Item(1).FullFileName
                '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  = oPartList.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