renaming occurrences in patterns

mostafamahmoudseddek94
Advocate

renaming occurrences in patterns

mostafamahmoudseddek94
Advocate
Advocate

hhh.PNGIn the master assembly, I have an associative pattern of some components. The problem is there indexes ( shown in the picture) always change whenever I configure the master assembly! and there are other rules I have created on that index

 

How can I rename those occurrences just changing their index based on the pattern index itself before I run rules? 

0 Likes
Reply
Accepted solutions (1)
960 Views
3 Replies
Replies (3)

JelteDeJong
Mentor
Mentor

you could try this rule. But there are some cases where it will fail because the new name is already used by an other occurence. For example it will fail if you have 2 or more of the same elements in your pattern. any way give it a go it might work for you.

Dim doc As AssemblyDocument = ThisDoc.Document
For Each pattern As OccurrencePattern In doc.ComponentDefinition.OccurrencePatterns
    For Each patternElement As OccurrencePatternElement In pattern.OccurrencePatternElements
        Dim elementName As String = patternElement.Name
        Dim nameParts = elementName.Split(":")
        Dim number = nameParts(nameParts.Count - 1)
        For Each occ As ComponentOccurrence In patternElement.Occurrences

            Dim orgOccName = occ.Name
            Dim occNameParts = orgOccName.Split(":")
            occNameParts(occNameParts.Count - 1) = number
            Dim newOccName = String.Join(":", occNameParts)

            Try
                occ.Name = newOccName
            Catch ex As Exception
                MsgBox(String.Format("Could not rename occurence: '{0}' to '{1}'. (Probably the name was already used)",
                    orgOccName, newOccName))
            End Try
        Next
    Next
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

mostafamahmoudseddek94
Advocate
Advocate

Hi, Thanks for your reply 

I am still a beginner I hardly can read your code, is it possible to explain it?

I write a simple code by myself, which works only for a specific pattern, and I have three different patterns in the assembly.

Sub main()
Dim oDOC As AssemblyDocument
Set oDOC = ThisApplication.ActiveDocument
 
Dim oParameter As Parameters
Set oParameter = oDOC.ComponentDefinition.Parameters

Dim oPurgeSystemPatternElement As OccurrencePatternElements
Set oPurgeSystemPatternElement = oDOC.ComponentDefinition.OccurrencePatterns.Item("Purge System Pattern").OccurrencePatternElements
    
Dim OLockingSystemPatternElement As OccurrencePatternElements
Set OLockingSystemPatternElement = oDOC.ComponentDefinition.OccurrencePatterns.Item("outer Locking System").OccurrencePatternElements

For i = 1 To oPurgeSystemPatternElement.Count
   If oParameter.Item("False_Row01_NO").Value = i Or oParameter.Item("False_Row02_NO").Value = i Or oParameter.Item("False_Row03_NO").Value = i Then
      i = i
   Else
      oPurgeSystemPatternElement.Item(i).Occurrences(1).Name = "Plenum_BlowPipe_End Plate:" & i
      oPurgeSystemPatternElement.Item(i).Occurrences(2).Name = "Plenum_BlowPipe_Staight:" & i
      oPurgeSystemPatternElement.Item(i).Occurrences(3).Name = "Tubo42.2x2.9:" & i
      oPurgeSystemPatternElement.Item(i).Occurrences(4).Name = "J-Pipe:" & i
      oPurgeSystemPatternElement.Item(i).Occurrences(5).Name = "Clamp TI-I GOST 17679-80 95:" & 2 * i - 1
      oPurgeSystemPatternElement.Item(i).Occurrences(6).Name = "Rubber Hose:" & i
      oPurgeSystemPatternElement.Item(i).Occurrences(7).Name = "Clamp TI-I GOST 17679-80 95:" & 2 * i
      oPurgeSystemPatternElement.Item(i).Occurrences(8).Name = "Pneumatic Valve:" & i
      oPurgeSystemPatternElement.Item(i).Occurrences(9).Name = "Bolted Connection:" & i
   End 

 

0 Likes

JelteDeJong
Mentor
Mentor
Accepted solution

I added comments to my original code.

 

Dim doc As AssemblyDocument = ThisDoc.Document
' the first loop is to find each pattern
For Each pattern As OccurrencePattern In doc.ComponentDefinition.OccurrencePatterns
	' in each pattern we loop over all elemetns
    For Each patternElement As OccurrencePatternElement In pattern.OccurrencePatternElements
		' get the name of the element (e.g. "Element:123")
        Dim elementName As String = patternElement.Name
		' split the name in parts. use ":" as split char
		' e.g. {"Element" , "123"}
        Dim nameParts = elementName.Split(":") ' this creates an array
		' the element number is the last part of the name.
		' e.g. "123"
        Dim number = nameParts(nameParts.Count - 1)
		' loop over all occurrences in the element.
        For Each occ As ComponentOccurrence In patternElement.Occurrences
			' get the name of the occurrence (e.g. "SomeOccurrence:456"
            Dim orgOccName = occ.Name
			' split the name in parts. use ":" as split char
			' e.g. {"SomeOccurrence" , "456"}
            Dim occNameParts = orgOccName.Split(":")' this creates an array
			' replace the last part with the number of the element.
			' e.g. the array will now be
			' {"SomeOccurrence" , "123"}
            occNameParts(occNameParts.Count - 1) = number
			' Join the parts of the array to 1 string
			' e.g. "SomeOccurrence:123"
            Dim newOccName = String.Join(":", occNameParts)

			' the try/catch block will handel exeptions that may occure
            Try
				' Try to rename the occurence with the new name.
                occ.Name = newOccName
            Catch ex As Exception
				' if an exception was thrown then its handeld by 
				' showing a message
                MsgBox(String.Format("Could not rename occurence: '{0}' to '{1}'. (Probably the name was already used)",
                    orgOccName, newOccName))
            End Try
        Next
    Next
Next

 (if you want to see an other example of the try/catch block checkout this blog post by @Anonymous )

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com