Hi @ikilic1461. Besides what I mentioned on your other current forum topic, below is another possible workaround way to 'fix' the rule afterwards, if needed, but this one can be pretty tricky to implement correctly. This would be something you would need to run after the design copy process has completed, to help fix the contents of the iLogic rule to reflect the new component names.
It is not 'perfect', but may work OK if working from a Template type situation (copying a template assembly files to new files). This is because it is expecting the names of the components in the rule to not already have a 'prefix' in their names. If they do already have a prefix in their names, such as when copying one 'copied' assembly to make another, then the 'find & replace' task will not be working with the correct 'find' text. If that is the case, then you may need to redefine how the 'find' text is dictated by replacing old/previous prefix with new/current prefix, instead of just removing prefix.
Sub Main
Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
If oADoc Is Nothing Then Logger.Debug(iLogicVb.RuleName & " exited (Wrong DocumentType)") : Return
Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
If oOccs.Count = 0 Then Return
Dim sPrefix As String = InputBox("Enter Prefix Text.", "Prefix", "")
If sPrefix = "" Then Return
'<<< a Dictionary to hold each 'old/previous' and 'new/current' component BrowserNode name, in pairs, in a list >>>
'<<< each 'entry' has a 'Key' and a 'Value' (Key = old or previous name, Value = new or current name) >>>
Dim oDict As New Dictionary(Of String, String)
'<<< a transaction bundles multiple actions into one item in the UNDO list >>>
Dim oTrans As Inventor.Transaction
oTrans = ThisApplication.TransactionManager.StartTransaction(oADoc, "Update iLogic Rule Component Names")
For Each oOcc As ComponentOccurrence In oOccs
'<<< get name of component BrowserNode without the 'Prefix' >>>
Dim sOccNameWithoutPrefix As String = oOcc.Name.Replace(sPrefix, "")
'<<< if sOccNameWithoutPrefix is not already in dictionary, then add it, along with current name >>>
If Not oDict.ContainsKey(sOccNameWithoutPrefix) Then
oDict.Add(sOccNameWithoutPrefix, oOcc.Name)
End If
Next 'oOcc
If oDict.Count = 0 Then : oTrans.Abort : Return : End If
'<<< run the custom Sub routine below with following line of code >>>
'<<< IMPORTANT: EDIT RULE NAME IN NEXT LINE >>>
FindReplaceTextInRule(oADoc, "RuleName", oDict)
oTrans.End
End Sub
Sub FindReplaceTextInRule(oDoc As Inventor.Document, sRuleName As String, _
oOldAndNewPairs As Dictionary(Of String, String))
If oDoc Is Nothing OrElse oDoc.IsModifiable = False Then Return
If sRuleName = "" Then Return
If oOldAndNewPairs Is Nothing OrElse oOldAndNewPairs.Count = 0 Then Return
Dim oRule As iLogicRule = iLogicVb.Automation.GetRule(oDoc, sRuleName)
If oRule Is Nothing Then
Logger.Debug("iLogic Rule named '" & sRuleName & "' not found!")
Return
End If
Dim sRuleText As String = oRule.Text
If sRuleText = "" Then Return
For Each oEntry As KeyValuePair(Of String, String) In oOldAndNewPairs
Try
sRuleText = sRuleText.Replace(oEntry.Key, oEntry.Value)
Catch
Logger.Error("Error editing iLogic Rule named " & sRuleName & vbCrLf & _
"while replacing: " & oEntry.Key & vbCrLf & _
"with: " & oEntry.Value)
End Try
Next 'oEntry
oRule.Text = sRuleText
End Sub
I also have an alternative custom Sub routine with the same name, but which requests a single 'text to find' and 'text to replace the with', instead of a Dictionary(Of String, String), for when you only need to replace one term in a rule.
Sub FindReplaceTextInRule(oDoc As Inventor.Document, sRuleName As String, _
sTextToFind As String, sReplacementText As String)
If oDoc Is Nothing OrElse oDoc.IsModifiable = False Then Return
If sRuleName = "" OrElse sTextToFind = "" Then Return
Dim oRule As iLogicRule = iLogicVb.Automation.GetRule(oDoc, sRuleName)
If oRule Is Nothing Then
Logger.Debug("iLogic Rule named '" & sRuleName & "' not found!")
Return
End If
Dim sRuleText As String = oRule.Text
If sRuleText = "" Then Return
Try
sRuleText = sRuleText.Replace(sTextToFind, sReplacementText)
Catch
Logger.Error("Error editing iLogic Rule named " & sRuleName & vbCrLf & _
"while trying to replace: " & sTextToFind & vbCrLf & _
"with the following: " & sReplacementText)
End Try
oRule.Text = sRuleText
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)