Replacing text in rules

Replacing text in rules

Anonymous
Not applicable
2,257 Views
6 Replies
Message 1 of 7

Replacing text in rules

Anonymous
Not applicable

Hi All,

 

I have a question on Inventor ilogic. 

 

I have a assembly with about 2000 parts with all parts having ilogic rules. There is common text "H:\CADsupport\Inventor"" in all these part rules and I need to change this text to "ASVICM\Inventor" in all rules of all parts. I can open all parts individually and change the text but it will take huge time.

Is there any way to write a rule to change the text in all rules of all parts? 

 

Any help will be greatly appreciated

 

Thanks

Rudresh 

0 Likes
Accepted solutions (1)
2,258 Views
6 Replies
Replies (6)
Message 2 of 7

pball
Mentor
Mentor
Accepted solution

The code below loops through every component in the assembly and replaces any text inside of any local ilogic rules. I'm including both vba and ilogic code for this as I work in vba mostly and then port to ilogic.

 

I would suggest trying this on a small assembly first to verify it works for you. Also if you use the iLogic code do NOT add and run it as a local rule or it will update itself, so please run it as an external rule.

 

Hope this helps out.

 

'iLogic
Public Sub Main() original_text = "test\path" replacement_text = "path\real" Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument Call rilogic(oAsmDoc, original_text, replacement_text) Dim item As Document For Each item In oAsmDoc.AllReferencedDocuments Call rilogic(item, original_text, replacement_text) Next item MsgBox ("Done") End Sub Public Sub rilogic(docfile As Document, str1 as String, str2 as String) Try Dim iLogicObject As Object = iLogicVb.Automation Dim rules As Object rules = iLogicObject.rules(docfile) If (Not rules Is Nothing) Then Dim rule As Object For Each rule In rules rule.Text = Replace(rule.Text, str1, str2) Next rule End If Catch 'error getting ilogic addin End Try End Sub
'VBA
Public Sub ReplaceiLogic() original_text = "test\path" replacement_text = "path\real" Dim oAsmDoc As AssemblyDocument Set oAsmDoc = ThisApplication.ActiveDocument Call rilogic(oAsmDoc, original_text, replacement_text) Dim item As Document For Each item In oAsmDoc.AllReferencedDocuments Call rilogic(item, original_text, replacement_text) Next item MsgBox ("Done") End Sub Public Sub rilogic(docfile As Document, str1 as string, str2 as string) Dim iLogicObject As Object On Error GoTo error Set iLogicObject = ThisApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}").Automation Dim rules As Object Set rules = iLogicObject.rules(docfile) If (Not rules Is Nothing) Then Dim rule As Object For Each rule In rules rule.Text = Replace(rule.Text, str1, str2) Next rule End If Exit Sub error: 'error getting ilogic addin End Sub
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
Message 3 of 7

Anonymous
Not applicable

Hi,

 

Thank you very much for your solution. It worked great. 

I used the ilogic code. 

 

I am looking for one more help for the same assembly

 

I want to add a parameter to the all rules of all parts of a assembly (of thousand parts) 

 

Again thanks for your help 

 

Thanks

Rudresh

0 Likes
Message 4 of 7

Jonathanspies
Explorer
Explorer

Hi,

its a older post, at least i try to get an answer...

iam working on this and i cannot make it happen to replace existing text if there are more lines.

Iam able to have multiple lines in the Replacement-text by using 

& Chr(10) &

at the end of the line.

But if I use that in the original_text field it is not working, any idea?

 

Jonathan 

0 Likes
Message 5 of 7

pball
Mentor
Mentor

Provide an example of the iLogic code you are trying to replace and what it is being replaced with. Then someone might be able to help.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 6 of 7

Jonathanspies
Explorer
Explorer

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

 

0 Likes
Message 7 of 7

lohithna
Enthusiast
Enthusiast

can you help me or give a code ilogic rule to repalce the particular text inside ilogic rule in part file indiviually 

ASTM A-36 OR EQUIVALENT to
ASTM A36 OR EQUIVALENT
0 Likes