I added your PartList/Item number portion of the posted code to the "FindAssemblyItemNumber" Function of my original post. See complete code below:
'Anything Outside The 'main' routine only runs when called
Sub Main
'Try to set Active Document to a DrawingDocument Object
Dim dDoc As DrawingDocument = TryCast(ThisApplication.ActiveDocument, DrawingDocument) 'This Fails to Nothing
If IsNothing(dDoc) Then Logger.Debug("Not A DrawingDocuemnt") : Exit Sub
'We know we have a drawing document open
'Get User to select a view
Dim PickView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a View")
If IsNothing(PickView) Then Exit Sub 'If nothing gets selected then we're done
'We now have a drawing view
'Call routine for the selected view.
Call ViewInfo(PickView)
'Breaking up code like this helps for multipurposing.
'If you want you can loop through all the views on a sheet
End Sub
'Sub Routine: Processes Custom Options for a given DrawingView Object
Sub ViewInfo(dView As DrawingView)
'Define some Objects
Dim ModelDoc As Document = dView.ReferencedDocumentDescriptor.ReferencedDocument
Dim ItemNumber As String 'I will need more information to make this work
Dim mPartNumber As String 'ModelDocument PartNumber
Dim smThickness As String 'SheetMetal Thickness As a number
Dim mMaterial As String 'ModelDocument Material
'Try to set the model document to a PartDocument Object
Dim pDoc As PartDocument = TryCast(ModelDoc, PartDocument)
If Not IsNothing(pDoc) 'As long as it is anything/not nothing
ItemNumber = FindAssemblyItemNumber(pDoc, dView.Parent.Parent) 'Call Custom Function to get Item Number
mPartNumber = pDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value 'Get PartNumber iProperty
smThickness = GetSheetMetalThickness(pDoc) 'Call Custom Function to get Sheet Metal Thickness [I'm going to return mm]
mMaterial = pDoc.ActiveMaterial.DisplayName
'Now I am sending the gathered information to the drawingview label.
'If you want to put it somehwere else, let me know
Call SetLabel(dView, ItemNumber, mPartNumber, smThickness, mMaterial) 'Custom Sub Routine
End If
End Sub
'Sub Routine: Sets the DrawingView Label to a custom string format, centered & bottom of view for a given DrawingView Object
Sub SetLabel(dView As DrawingView, s1 As String, s2 As String, s3 As String, s4 As String)
Dim ThisSheet As Sheet = dView.Parent 'Get The Sheet the view is on
'Set Text to dsiplay
Dim FormattedText As String = "<StyleOverride Underline='True'>" & s1 & " - " & s2 & "</StyleOverride><Br/>" & s3 & s4
'SetThe DrawingView Label & make sure it is visible & @ Bottom Center
dView.ShowLabel = True
Dim dvLabel As DrawingViewLabel = dView.Label
dvLabel.FormattedText = FormattedText
dvLabel.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
dvLabel.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextLower
End Sub
'A Function sends something back after being called. It gets defined like any other Object
'Function: Return {AS String} Item number of given part document in Assembly. {assembly currently unknown}
Function FindAssemblyItemNumber(pDoc As PartDocument, oDrawDoc As DrawingDocument) As String
Dim Result As String = "Assembly Info Not Found" 'Defualt Result
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
oPartList = oDrawDoc.Sheets.Item(i).PartsLists.Item(1)
Exit For
End If
Next
'MessageBox.Show("parts list found on: " & i, "Title")
End Try
If IsNothing(oPartList) Then Logger.Debug("No PartsList found in drawing document") : Return Result
' 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 = oPartList.PartsListRows.Item(j)
'get filename of model in row
Dim oRowFileName As String = 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(pDoc.FullFileName, oRowFileName, CompareMethod.Text)=0
'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
Result = oCell.Value
Return Result
End If
Next
Return Result
End Function
'Function: Return {AS String} SheetMetal Thickness for given PartDocument Object
Function GetSheetMetalThickness(pDoc As PartDocument) As String
Dim Result As String = "" 'Defualt Result
Dim ResultValue As Double 'Set Object For Double Handling
'Try to set a SheetMetalComponentDefinition Object
Dim smDef As SheetMetalComponentDefinition = TryCast(pDoc.ComponentDefinition, SheetMetalComponentDefinition)
If IsNothing(smDef) Then Return Result 'If The PartDocument is not a sheetmetal part return our 'nothing' default
'We know we have a view of a sheetmetal Part and can call Specific Properties and methods
ResultValue = smDef.Thickness.Value 'Database units for Length is cm. We have to convert to our desired unit type
ResultValue *= 10 'I'm converting the value from cm to mm by multiplying itself by 10
'I'm going to return mm with 3 decimals
Result = Math.Round(ResultValue, 3).ToString & " mm "
Return Result
End Function
I changed the PartsList Try/Catch so it doesn't error out if no PartsList exists in the drawing, but left the rest of the formatting as you wrote it.
Let me know if you have any questions, or if this is not working as intended.