Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Replace line in rule

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Joos_T
575 Views, 6 Replies

Replace line in rule

Hey all,

 

Is it posible to replace a single line in a ilogic rule when this rule contains a certain text?

For example:

If ThisDoc.Document.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	Param_1 = "Test_1"
	Param_2 = "Test_2"
	Param_3 = "Test_3"
ElseIf
	Param_1 = "Test_4"
	Param_2 = "Test_5"
	Param_3 = "Test_6"
End If 

 I want to replace the line with Param_2 = "Test_2" for the a new line called Param_10 = "Test_10"  by searching only on "Test_2".

 

Also this has to be executable by an External rule because it has to run over multiple files.

 

Labels (2)
6 REPLIES 6
Message 2 of 7
WCrihfield
in reply to: Joos_T

Hi @Joos_T.  Yes, this sounds possible.  Is this rule named exactly the same in every one of those documents?  If so, what is that rule's name (capitalization is likely important)?  If it is not always exactly the same, then is there another way to identify which rule (if multiple rules) it will be?  Do those terms ("Param_1") represent local parameters (do they turn blue, due to recognition)?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7
Joos_T
in reply to: WCrihfield

Thank you for your reply @WCrihfield.

 

Yes, the rule name is always te same.  Just call it Rule_1 for now. 

The variables in "Param" are different from each other.

Also the line length may differ from each other theirfore I need to delete the whole line and create a new one.

Message 4 of 7
WCrihfield
in reply to: Joos_T

Hi @Joos_T.  Here is an example code I just created for you, as a test of this functionality.  As a test, I created a new document, created an internal rule in it named "Rule_1", then pasted your code into it.  (I corrected "ElseIf" to "Else", though.)  Then I created this other rule to manipulate the contents of that first rule, and it works, as expected.  Below is that iLogic rule code.  This is set up for a single, simple application, but it could easily be adaptable for processing a whole directory of Inventor documents, with some added error checking.

Dim oDoc As Document = ThisDoc.Document
oAuto = iLogicVb.Automation
'Dim oRules = oAuto.Rules(oDoc)
Dim oRule As iLogicRule = oAuto.GetRule(oDoc, "Rule_1")
If IsNothing(oRule) Then Exit Sub 'or Continue For, if used in a For loop
Dim oText As String = oRule.Text
If oText.Contains("Test_2") Then
	Dim oAllLines() As String = oText.Split(vbCrLf)
	For i As Integer = 0 To UBound(oAllLines)
		Dim oLine As String = oAllLines(i)
		If oLine.Contains("Test_2") Then
			oLine = "Param_10 = " & Chr(34) & "Test_10" & Chr(34)
			oAllLines(i) = vbCrLf & vbTab & oLine
			Exit For
		End If
	Next
	oText = String.Join("", oAllLines)
	oRule.Text = oText
End If

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

EESignature

(Not an Autodesk Employee)

Message 5 of 7
Joos_T
in reply to: WCrihfield

This is what I was looking for! Thanks a lot!

Message 6 of 7
WCrihfield
in reply to: Joos_T

Hi @Joos_T.  I'm glad I was able to help out.  By the way, from the sounds of it, I believe I have another tip about these types of situations that you may find helpful.  What I often do, to avoid having to edit a whole bunch of local rules later, when changes may be needed, is simplify the 'internal rule' (rule saved within a document, vs external rule), to only include a few 'dummy' variables, which are set to local parameter values (they do not do anything, just used to cause triggering), then after that, use a single line of code to run an external rule.  Below is an example of a rule like this:

oTrigger = Param_1
oTrigger = Param_2
oTrigger = Param_3
iLogicVb.Automation.RunExternalRule(ThisDoc.Document, "RuleToProcessParams")

This could be the only rule in all those documents, instead of the 'real code'.  This internal rule will be triggered to run every time any of those parameter values change, but it will only run the external rule, instead of its own code.  That way, if any specific changes are needed, they can be done to the one external rule.  Another idea would be to put that external rule into the Event Triggers dialog, under an event that makes sense, if that would work for your situation.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 7
Joos_T
in reply to: Joos_T

Hi @WCrihfield ,

 

I've build a new code with your script unfortunatly the script fails. The code works fine when you are using the script ones but if I want to run it twice it fails and deletes a random part of the rule. 

Do you maybe have a solution?

 

The results are in the attachment.

 

Code I'm using:

 

Sub Main()
	
	Dim oRuleName As String
	Dim oDoc As Document = ThisApplication.ActiveDocument
	Dim oAuto As IiLogicAutomation = iLogicVb.Automation
	Dim oRule As iLogicRule

	oRuleName = CheckDoc(oDoc)
	Replace(oAuto, oDoc, oRuleName)
	
	iProperties.Material = "Generic"
	iLogicVb.RunRule(oRuleName) 

End Sub

'[ SUBS AND FUNCTIONS
Function CheckDoc(oDoc As Document) As String
	Dim Result As String = Nothing
	
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		Result = "Rule_1"
	ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Result = "Rule_2"
	End If
Return Result	
End Function

Sub Replace (oAuto As IiLogicAutomation, oDoc As Document, oRuleName As String)

'Dim oRules = oAuto.Rules(oDoc)
Dim oRule As iLogicRule = oAuto.GetRule(oDoc, oRuleName)

If IsNothing(oRule) Then Exit Sub
	
Dim oText As String = oRule.Text

If oText.Contains("Test_1") Then
	Dim oAllLines() As String = oText.Split(vbCrLf)
	For i As Integer = 0 To UBound(oAllLines)
		Dim oLine As String = oAllLines(i)
		If oLine.Contains("Test_1") Then
			oLine = ""
			oAllLines(i) = vbCrLf & vbTab & oLine 
			Exit For
		End If
	Next
	oText = String.Join("", oAllLines)
	oRule.Text = oText
End If


If oText.Contains("TEST_RULE") Then
	Dim oAllLines() As String = oText.Split(vbCrLf)
	For i As Integer = 0 To UBound(oAllLines)
		Dim oLine As String = oAllLines(i)
		If oLine.Contains("TEST_RULE") Then
			oLine = "iLogicVb.RunExternalRule(" & Chr(34) & "TEST_RULE_1" & Chr(34) & ")"
			oAllLines(i) =  vbCrLf & vbTab & oLine 
			Exit For
		End If
	Next
	oText = String.Join("", oAllLines)
	oRule.Text = oText
End If
End Sub
']

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report