Finally I found another solution, with this rule I can just replace the full content of the Rule, basiclly I overwrite the internal rule, using an external rule. Using & Chr(34) & for the different " and & Chr(10) & for next line.
It works perfect.
Before that I wanted to use the rule from Above to replace for example this text:
'DisplayName
ThisDoc.Document.DisplayName= iProperties.Value("Custom", "Title-EN CAD")
with this text:
'Removed function
I was able to replace on line with multiple lines but iam not able to find/locate a text with multiple lines and replace it with only one line.
Below is my rule to write or overwrite an internal rule, using an external rule.
Sub Main()
'CreateRule(ByVal RuleName As String)
If ThisDoc.Document.DocumentType = kPartDocumentObject Then
RuleName = "Rule-Trigger"
Else
RuleName = "Rule-iTrigger0"
End If
Dim i As Object
i = GetiLogicAddin(ThisApplication)
Dim oDoc As Inventor.Document
oDoc = ThisApplication.ActiveDocument
'we will create a rule in the current document, that has a name, but doesn't have any text
'this way the rule will fire with nothing to do, meaning that it does nothing
Dim exRule As Object
'i will try and open that rule to see if it exists, if it does, then we will overwrite it
On Error Resume Next
exRule = i.GetRule(oDoc, RuleName)
If exRule Is Nothing Then
Call i.AddRule(oDoc, RuleName, "")
exRule = i.GetRule(oDoc, RuleName)
Else
exRule.Text = ""
End If
'now create the rule text, be careful here with double quotes and line breaks that you use the correct chr()
Dim ruleText As String
If ThisDoc.Document.DocumentType = kDrawingDocumentObject Then
ruleText = "iTrigger = iTrigger0" & Chr(10) & _
"FILEPATH_RULES = " & Chr(34) & "C:\Vault_WORK\TC_VAULT\Templates\Rules\" & Chr(34) & "" & Chr(10) & _
"If System.IO.File.Exists(ThisDoc.PathAndFileName(True)) Then" & Chr(10) & _
"iLogicForm.ShowGlobal("& Chr(34) & "ENG_Category Type-EN" & Chr(34) & ", FormMode.Modal)" & Chr(10) & _
"iProperties.Value("& Chr(34) & "Custom" & Chr(34) & "," & Chr(34) & "Category Type-ENG-PRJ" & Chr(34) & ") = Parameter(" & Chr(34) & "Category_type_ENG_PRJ" & Chr(34) & ")" & Chr(10) & _
"If iProperties.Value("& Chr(34) & "Custom" & Chr(34) & "," & Chr(34) & "Category Type-ENG-PRJ" & Chr(34) & ") = " & Chr(34) & "Customer drawing" & Chr(34) & "Then" & Chr(10) & _
"iLogicForm.ShowGlobal(" & Chr(34) &"02_Customer drawing" & Chr(34) & ",FormMode.Modal)" & Chr(10) & _
"ElseIf iProperties.Value("& Chr(34) &"Custom"& Chr(34) &","& Chr(34) & "Category Type-ENG-PRJ"& Chr(34) &") = "& Chr(34) &"Internal drawing"& Chr(34) &"Then" & Chr(10) & _
"iLogicForm.ShowGlobal("& Chr(34) &"01_Internal Drawing"& Chr(34) &", FormMode.Modal)" & Chr(10) & _
"Else" & Chr(10) & _
"End If" & Chr(10) & _
"iLogicVb.RunExternalRule(FILEPATH_RULES + " & Chr(34) & "02_BeforeSave_MustProperties" & Chr(34) & ")" & Chr(10) & _
"Else" & Chr(10) & _
"MessageBox.Show(" & Chr(34) & "Please save the File first!" & Chr(34) & "," & Chr(34) & "Title" & Chr(34) & ")" & Chr(10) & _
"End If"
Else
ruleText = "iTrigger = iTrigger0" & Chr(10) & _
"FILEPATH_RULES = " & Chr(34) & "C:\Vault_WORK\TC_VAULT\Templates\Rules\" & Chr(34) & "" & Chr(10) & _
"iLogicForm.ShowGlobal(" & Chr(34) & "01_Internal Drawing" & Chr(34) & ", FormMode.Modal)" & Chr(10) & _
"iLogicVb.RunExternalRule(FILEPATH_RULES + " & Chr(34) & "02_BeforeSave_MustProperties" & Chr(34) & ")"
End If
'now we can assign the users rule text to that rule, without it actually running
exRule.Text = ruleText
Debug.Print (exRule.Text)
End Sub
Function GetiLogicAddin(oApplication As Inventor.Application) As Object
addIns = oApplication.ApplicationAddIns
'Find the add-in you are looking for
Dim addIn As ApplicationAddIn
On Error GoTo NotFound
addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
If (addIn Is Nothing) Then Exit Function
addIn.Activate
GetiLogicAddin = addIn.Automation
Exit Function
NotFound:
End Function