iLogic Rule to Modify iLogic Rule Text

iLogic Rule to Modify iLogic Rule Text

layochim
Enthusiast Enthusiast
380 Views
7 Replies
Message 1 of 8

iLogic Rule to Modify iLogic Rule Text

layochim
Enthusiast
Enthusiast

Is there a way to create a rule to find and replace text in another rule?

 

I've got a configured assembly that uses "generic" part numbers, once this assembly is copied and given a defined assembly number along with its parts I need the code to now recognize the new part numbers without changing each line of code manually.

0 Likes
Accepted solutions (1)
381 Views
7 Replies
Replies (7)
Message 2 of 8

ryan.rittenhouse
Advocate
Advocate

This is a problem I've dealt with before (code below). All you need to do is pass in the rule name, the old text, and the new text and it'll do the replacing for you. If you want to do it in something other than the current document, you can also pass in a document to replace rules in other components.

 

Sub Main()
	
	'Replace rule "Update" in the current document
	PatchRule("Update", "PARTNUMBER", "123456")
	
	'Replace rule "Update" in component "Plate"
	Dim comp As ComponentOccurrence = Component.InventorComponent("Plate")
	PatchRule("Update", "PARTNUMBER", "123456", comp.Definition.Document)
	
End Sub 'Main

Sub PatchRule(ruleName As String, oldText As String, newText As String, Optional doc As Document = Nothing)

	If doc Is Nothing Then doc = ThisDoc.Document
	Dim rule As iLogicRule = iLogicVb.Automation.GetRule(doc, ruleName)
	
	If rule Is Nothing Then
		Logger.Error("Error patching rule {0} in {1}. Rule not found!", ruleName, doc.DisplayName)
	Else
		If rule.Text.ToString.Contains(oldText) Then
			Logger.Info("Patching rule {0} in {1}", ruleName, doc.DisplayName)
			rule.Text = rule.Text.ToString.Replace(oldText, newText)
		End If
	End If
	
End Sub
If this solved your problem, or answered your question, please click Accept Solution.
Message 3 of 8

Curtis_Waguespack
Consultant
Consultant

Hi @layochim, welcome to the forum.

 

If this is really just about browser names ( that happen to be the part numbers) then you will want to do something like this:

https://clintbrown.co.uk/2020/07/29/ilogic-normalise-browser-nodes-with-code/

 

If after understanding the practice to stabilize browser names, and determine you truly need to modify the code in your rules, it can be done, but it would be highly irregular, and maybe a bit complicated to just swap out just parts of the rule.

 

In any case if that's what you truly need, here is the basics of modifying the text of an internal rule ( note in this example we're really we're just deleting it and recreating it with specified text).

 

edit: oops I just saw @ryan.rittenhouse's reply... I might like his version for updating a rule better 😀

 

' Create the rule content
Dim ruleContent As String = "'Hello world" & vbCrLf & _
"'And Hello Again" & vbCrLf & _
"'And Hello a 3rd Time"

RuleName = "Hello Rule"
Try
	oRules = iLogicVb.Automation.Rules(ThisDoc.Document)
	For Each oRule In oRules
		If oRule.name = RuleName Then iLogicVb.Automation.DeleteRule(ThisDoc.Document, RuleName)
	Next
Catch
End Try

' Add the rule to the document
iLogicVb.Automation.AddRule(ThisDoc.Document, RuleName, ruleContent)

 

Hope that helps,

Curtis

EESignature

Message 4 of 8

layochim
Enthusiast
Enthusiast

Thanks for the help! Appreciate it.

0 Likes
Message 5 of 8

layochim
Enthusiast
Enthusiast

Thank you for this!

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi guys.  Nice example codes.  Since this is a relatively popular topic for me also, due to using techniques like these relatively often in my own rules, I decided to drop one of my example codes in there also.  This one is specifically for working with 'external' iLogic rules though, because that's where 99% of my rules are, and because I have so many of them, in so many different sub folders (categories, all under one 'primary folder').  Sometimes, if I rename an external rule, or change the name of something important, that is being called for across multiple other external rules, I use this tool to find the previous text, and replace it with the new text, across all existing external rules.  It just directly edits the text files themselves, without involving the usual iLogic snippets.  It does use iLogic for its 'Logger' functionality, and to access the external rule directories listing though.

I will attach it as a text file, for convenience, because it is considerably longer, and includes the use of an Inventor.ProgressBar, so the process can be canceled, if needed.  It could likely be improved upon &/or further developed, because I haven't revisited it in a while, but it serves my needs pretty well, and processes hundreds of files pretty quickly, even with the small dialog pop-ups here and there (by design).  Sometimes, when a term is widely used, I may not actually want that term replaced in every single instance that it might be found throughout all rules, so I have it set-up to prompt me with the name of the rule where the text is found, giving me the chance to say Yes or No to that instance replacement.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 8

layochim
Enthusiast
Enthusiast

This is very helpful, thank you!

0 Likes
Message 8 of 8

bradeneuropeArthur
Mentor
Mentor

Maybe considering to create an addin instead of using built in code.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes