IDW View Label Automation

IDW View Label Automation

smanionDV2GJ
Contributor Contributor
1,109 Views
12 Replies
Message 1 of 13

IDW View Label Automation

smanionDV2GJ
Contributor
Contributor

Hi, I am a novice at using Ilogic. I would like to create a code that will do the following: 

 

     1. Place a custom iproperty from my ipt. template into the view label. This iproperty is called "SheetMetalName".           2. I would also like it to write the name of the file below that. 

     3. Lastly,  I would like it to say ITEM # X with X being the item number from the IDW's parts list. 

  

               I have attached an image below of an IDW where I manually created the view labels the way I would like them to appear. If anyone could write a code that will do this or give me a clue as to how I can achieve these functions, that would be great. Initially I attempted to do this using the style editor but was unable to as I could not place a custom iProperty in the styles editor and I am unsure of how to access variables from the Parts list. I would like for this label to appear and populate as soon as I place the view in the drawing. Thanks in advance for any help.

0 Likes
Accepted solutions (3)
1,110 Views
12 Replies
Replies (12)
Message 2 of 13

yuzeaa
Advocate
Advocate

First you need manually create view label and insert your customproperty 

微信图片_20230523094453.png

Then , you need check your label formattedtext .

oWrite = System.IO.File.CreateText("C:\Users\Public\Documents\Formattedtext.txt")
oWrite.WriteLine(ThisApplication.ActiveDocument.ActiveSheet.DrawingViews(1).Label.Formattedtext)
oWrite.Close()
ThisDoc.Launch("C:\Users\Public\Documents\Formattedtext.txt")

 

 you can set label formattedtext by following code(input your  formattedtext)

Dim oDrawingView As DrawingView = ThisApplication.ActiveDocument.ActiveSheet.DrawingViews(1)
Dim olabel As DrawingViewLabel = oDrawingView.Label
oDrawingView.ShowLabel = True
olabel.Color = ThisApplication.TransientObjects.CreateColor(200, 0, 0)
olabel.FormattedText = "<StyleOverride Font='@Microsoft JhengHei UI' FontSize='0.35' " &
"Bold='True' Underline='True'><Property Document='model' PropertySet=" &
" 'Design Tracking Properties' Property='Part Number' FormatID= "  &
" '{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>PartNumber</Property></StyleOverride><Br/> " &
" <StyleOverride Font='ᣐ!' FontSize='0.3' Bold='True'>(</StyleOverride><StyleOverride Font='@Microsoft JhengHei UI' " &
" FontSize='0.35' Bold='True'><DrawingViewScale/></StyleOverride><StyleOverride Font='ᣐ!' FontSize='0.35' Bold='True'>)</StyleOverride>" 

 

 

Message 3 of 13

smanionDV2GJ
Contributor
Contributor

Hi. Thank you for putting this together. I really appreciate your time and knowledge on the subject. Unfortunately, this code does not quite suit my needs. This is likely due to the way I asked the question. Let me try to present what I need more clearly. Here are the goals I would like the code to achieve.  

     I would like for:

1. The view label to automatically pop up when I add a base view. 

2. The first line of the view label to show as the name of the part. 

3. The second line of the view label to show as the name of the current sheet metal rule. (Initially this was a custom iproperty from the ipt. file but this does not work for iam files that are split into ipt's, so any way to achieve this would be great.)

4. The third line of the view label to show the word "ITEM #" along with the item number value pulled from the parts list. If there is no parts list, I would like this line to not show up at all.  

 

     Thanks again for your time and efforts. Any help with this would be greatly appreciated.

 

 

 

 

0 Likes
Message 4 of 13

yuzeaa
Advocate
Advocate

Because I don't have much time to code, let's slowly solve all your problems. The following is a response to the first question ("The view label to automatically pop up when I add a base view").

Class ThisRule
	Private WithEvents oUserInputEvents As UserInputEvents
	Dim DrawingView_List As New List(Of DrawingView)
	Sub main
		If SharedVariable.Exists("UserInputEvent") Then Exit Sub
		oUserInputEvents = ThisApplication.CommandManager.UserInputEvents
		SharedVariable("UserInputEvent") = oUserInputEvents
	End Sub
	Private Sub oUserInputEvents_Onactivecommand(CommandName As String, ByVal 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 setLabel(DrawingViews As List(Of DrawingView))
		For Each view As DrawingView In DrawingViews
			View.showLabel = True
			View.Label.FormattedText = "<StyleOverride Font='@Microsoft JhengHei UI' FontSize='0.35' " &
            "Bold='True' Underline='True'><Property Document='model' PropertySet=" &
            " 'Design Tracking Properties' Property='Part Number' FormatID= "  &
            " '{32853F0F-3444-11D1-9E93-0060B03C1CA6}' PropertyID='5'>PartNumber</Property></StyleOverride><Br/> " &
            " <StyleOverride Font='ᣐ!' FontSize='0.3' Bold='True'>(</StyleOverride><StyleOverride Font='@Microsoft JhengHei UI' " &
            " FontSize='0.35' Bold='True'><DrawingViewScale/></StyleOverride><StyleOverride Font='ᣐ!' FontSize='0.35' Bold='True'>)</StyleOverride>" 
		Next
	End Sub
End Class
Message 5 of 13

smanionDV2GJ
Contributor
Contributor

Sounds great. I appreciate the help. With that bit of code added, goals 1 and 2 are complete. It does however show up with an underline as well as a scale with boxes on each side that I would like to eliminate. I attached an image of it below.

0 Likes
Message 6 of 13

yuzeaa
Advocate
Advocate
Accepted solution

Try it first,explain later.Need to sleep now.

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)
		If oview.parent.partslists.count = 0 Then 
		 oview.Label.FormattedText =   Formattext("model","Design Tracking Properties","Part Number") _
                                       & "<Br/>" _
					                   & Formattext("model","User Defined Properties","SheetMetalName") 
		Else
		oview.Label.FormattedText =   Formattext("model","Design Tracking Properties","Part Number") _
                                    & "<Br/>" _
                                    & Formattext("model","User Defined Properties","SheetMetalName") _
                                    & "<Br/>" _
                                    & "Item # " & Get_index(oview.ReferencedFile.FullFileName,oview.parent.partslists(1))
		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
			If osheet.PartsLists.Count = 0  Then Continue For
			For Each view As DrawingView In osheet.DrawingViews
				Dim labeltext As String = View.Label.FormattedText
				If labeltext.Contains("SheetMetalName") Then
				   setViewLabel(View)
			    End If
			Next 
	    Next
	End Sub
End Class
Message 7 of 13

smanionDV2GJ
Contributor
Contributor

Thank you! This does exactly what I asked for. One more question for you. When I create components from an assembly, my custom I-property "SheetMetalName" does not show up. Is there a way for me to either get it to show up or write the name of my custom sheet metal rule to the view label without the use of the custom I-property? 

0 Likes
Message 8 of 13

Frederick_Law
Mentor
Mentor
Accepted solution

Copy your template and replace Standard.*

Lots of commands use Standard.* templates.

Message 9 of 13

smanionDV2GJ
Contributor
Contributor

That was my initial thought as well but when I use the make components tool, then open the separate parts, the custom iproperty from my template does not show up.

0 Likes
Message 10 of 13

smanionDV2GJ
Contributor
Contributor

You were absolutely correct. I needed to replace the sheet metal standard as well was the only reason it wasn't working. Thank you.

0 Likes
Message 11 of 13

yuzeaa
Advocate
Advocate

replace sub setViewlabel,the property will not show up on label if not exist in model.

Private Sub setViewLabel(oview As DrawingView)
	Formattext1 = Formattext("model", "Design Tracking Properties", "Part Number")
	Formattext2 = Formattext("model","User Defined Properties","SheetMetalName")
	
	Dim odoc As Document = oview.ReferencedDocumentDescriptor.ReferencedDocument
	If odoc.PropertySets("User Defined Properties").Cast(Of Object).Any(Function(X) X.DisplayName = "SheetMetalName") Then
		If oview.parent.partslists.count = 0 Then 
		   oview.Label.FormattedText =   Formattext1 & "<Br/>" &  Formattext2
	    Else 
		   oview.Label.FormattedText =  Formattext1 & "<Br/>" &  Formattext2 & "<Br/>" _
                                      & "Item # " & Get_index(oview.ReferencedFile.FullFileName,oview.parent.partslists(1))
		End If 
	Else
		If oview.parent.partslists.count = 0 Then 
		   oview.Label.FormattedText =   Formattext1 
	    Else 
		  oview.Label.FormattedText =  Formattext1 & "<Br/>"  _
                                      & "Item # " & Get_index(oview.ReferencedFile.FullFileName,oview.parent.partslists(1))
		End If 
		
	End If 
End Sub 

 

Message 12 of 13

smanionDV2GJ
Contributor
Contributor

Awesome. Thank you so much. I appreciate all of your help. I have one last question for you. This current code only displays the Item # if the parts list is on the same sheet. Many times, I start the parts list on a sheet then have several sheets of parts that I would like to also pull from that parts list. I can always add a parts list in space on multiple sheets then delete it out, but is there perhaps a better method of dealing with this?

0 Likes
Message 13 of 13

yuzeaa
Advocate
Advocate
Accepted solution

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