1.The item will show up if any partlist exist in sheets.
2.Ensure that all parts in this drawing document come from same partlist.
3.You can only modify the first partslist to refresh item number in label.
4.The label will always show up even when you don't want them.
5.The label will always show up whether standard ipt or sheetmetal ipt.
6.You can add formattext in code to change font size.
Class ThisRule
Private WithEvents oUserInputEvents As UserInputEvents
Private WithEvents oApplicationEvents As ApplicationEvents
Dim DrawingView_List As New List(Of DrawingView)
Sub main
If SharedVariable.Exists("UserInputEvent") Then Exit Sub
oUserInputEvents = ThisApplication.CommandManager.UserInputEvents
oApplicationEvents = ThisApplication.ApplicationEvents
SharedVariable("UserInputEvent") = oUserInputEvents
End Sub
Private Sub oUserInputEvents_Onactivecommand(CommandName As String, Context As NameValueMap) _
Handles oUserInputEvents.OnActivateCommand
If CommandName = "DrawingBaseViewCmd" OrElse CommandName = "DrawingProjectedViewCmd" Then
Dim DrawingDoc As DrawingDocument = ThisApplication.ActiveDocument
DrawingView_List = DrawingDoc.ActiveSheet.DrawingViews.OfType(Of DrawingView).ToList()
End If
End Sub
Private Sub oUserInputEvents_Ondeactivecommand(CommandName As String, Context As NameValueMap) _
Handles oUserInputEvents.OnTerminateCommand
If CommandName = "DrawingBaseViewCmd" OrElse CommandName = "DrawingProjectedViewCmd" Then
Dim itemcount = DrawingView_List.count
Dim DrawingDoc As DrawingDocument = ThisApplication.ActiveDocument
DrawingView_List = DrawingDoc.ActiveSheet.DrawingViews.OfType(Of DrawingView).ToList()
DrawingView_List.RemoveRange(0, itemcount)
setLabel(DrawingView_List)
End If
End Sub
Private Sub partListEditEvent( DocumentObject As _Document,
BeforeOrAfter As EventTimingEnum,
ReasonsForChange As CommandTypesEnum,
Context As NameValueMap,
ByRef HandlingCode As HandlingCodeEnum) _
Handles oApplicationEvents.OnDocumentChange
HandlingCode = HandlingCodeEnum.kEventNotHandled
If BeforeOrAfter = EventTimingEnum.kAfter Then
If Context(1) = "EditPartList" Then
refreshView()
Else If Context(1) = "CreatePartListLocalBOM"
If DocumentObject.ActiveSheet.PartsLists.Count = 1 Then
refreshView()
End If
End If
End If
End Sub
Private Sub setLabel(DrawingViews As List(Of DrawingView))
For Each view As DrawingView In DrawingViews
If Not View.ReferencedDocumentDescriptor.ReferencedDocumentType = kPartDocumentObject Then Continue For
If Not View.ParentView Is Nothing Then Continue For
If Not View.InheritSlice = False Then Continue For
View.showLabel = True
setViewLabel(View)
Next
End Sub
Function Get_index(FileName As String,PartsList As PartsList) As Integer
Dim PartsListColumns = PartsList.PartsListColumns.OfType(Of PartsListColumn).ToList()
Dim itemColumn = PartsListColumns.Where(Function(x) x.PropertyType = PropertyTypeEnum.kItemPartsListProperty).First()
Dim itemindex = PartsListColumns.IndexOf(itemColumn) + 1
Dim index = PartsList.PartsListRows.Cast(Of PartsListRow).Where(Function(m) m.ReferencedFiles(1).FullFileName = FileName).First().item(itemindex).value
Return index
End Function
Private Sub setViewLabel(oview As DrawingView)
Formattext1 = Formattext("model", "Design Tracking Properties", "Part Number")
Formattext2 = Formattext("model","User Defined Properties","SheetMetalName")
Dim osheets As Sheets = oview.Parent.Parent.sheets
Dim osheet = osheets.Cast(Of Sheet).where(Function(p) p.Partslists.count <> 0).FirstorDefault()
Dim oPartsList As PartsList
If osheet Is Nothing Then
oPartsList = Nothing
Else
oPartsList = osheet.PartsLists(1)
End If
Dim odoc As Document = oview.ReferencedDocumentDescriptor.ReferencedDocument
If odoc.PropertySets("User Defined Properties").Cast(Of Object).Any(Function(X) X.DisplayName = "SheetMetalName") Then
If oPartsList Is Nothing Then
oview.Label.FormattedText = Formattext1 & "<Br/>" & Formattext2
Else
oview.Label.FormattedText = Formattext1 & "<Br/>" & Formattext2 & "<Br/>" _
& "Item # " & Get_index(oview.ReferencedFile.FullFileName,oPartsList)
End If
Else
If oPartsList Is Nothing Then
oview.Label.FormattedText = Formattext1
Else
oview.Label.FormattedText = Formattext1 & "<Br/>" _
& "Item # " & Get_index(oview.ReferencedFile.FullFileName,oPartsList)
End If
End If
End Sub
Function Formattext(Document As String, PropertyID As String, PropertyName As String) As String
Document = "'" & Document & "' "
PropertyID = ThisApplication.ActiveDocument.propertysets(PropertyID).InternalName
PropertyID = "'" & PropertyID & "' "
PropertyName = "'" & PropertyName & "'"
Return "<Property Document=" & Document & "FormatID=" & PropertyID & "Property=" & PropertyName & "></Property>"
End Function
Private Sub refreshView()
For Each osheet As Sheet In ThisApplication.ActiveDocument.Sheets
For Each view As DrawingView In osheet.DrawingViews
If View.ShowLabel Then setViewLabel(View)
Next
Next
End Sub
End Class