Rule to backup all rules in active document

Rule to backup all rules in active document

daniel.puchta
Enthusiast Enthusiast
381 Views
1 Reply
Message 1 of 2

Rule to backup all rules in active document

daniel.puchta
Enthusiast
Enthusiast

I have created a rule, which  every rule in active document saves as Word document.

A lot of times it happens that when recoding rule, it does not work so you have to return to old code (or compare it with old code). This rule makes backup creation really easy.

I think that this would be interesting for others. Let me know if you used this rule and if it helped you 🙂


Imports System.Threading.Tasks
Class Create_backup
'Rule, which for each rule in active document copies the rule code and saves it as word document (by rule name) in defined folder

Sub Main()
	
    Dim iLogic As New Object
    iLogic = GetiLogicAddin(ThisApplication) 'Calls function defined bellow
    If (iLogic Is Nothing) Then Exit Sub 'If ilogic is not returned, exit sub
    Dim oDoc As Document
	Dim Text2 As String
	Dim RuleName As String
    oDoc = ThisApplication.ActiveDocument

	Dim ruleCol As System.Collections.IEnumerable = iLogic.Automation.Rules(oDoc) 'gets ilogic rules in document
	i = 0
	Dim objWord

	Dim objDoc

	objWord = CreateObject("Word.Application") 'create Word object (opens word)


	For Each iLogRule As iLogicRule In ruleCol
		objDoc = objWord.Documents.Add  'create new Word document
		Text2 = iLogRule.Text	'save rule code to variable Text2
		objDoc.Content.InsertAfter (Text:=Text2) 'insert Text2 to word
		RuleName = iLogRule.Name 'get ilogic rule name
		objDoc.SaveAs("C:\Rules\" & RuleName) 'save word document which contains rule code
		objDoc.close 'close word document
	Next

	
	objWord.Visible = True 'make word visible
End Sub

Function GetiLogicAddin(oApplication As Inventor.Application) As Object 'function to get reference of iLogic object
    addIns = oApplication.ApplicationAddIns
    Dim addIn As ApplicationAddIn
    addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}") 'load ilogic addin
    If (addIn Is Nothing) Then Exit Function 'if ilogic addin not found, exit function
    addIn.Activate 'activate ilogic addin
    Return addIn 
    Exit Function 
    NotFound:
End Function
End Class

 

382 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor

Nice work @daniel.puchta.  I gave it a try (with a few personal edits), and it works OK.  Using MS Word is certainly one way to do it.  But if you want to keep it really simple and make it process faster, you could just use plain text documents (.txt), instead of invoking MS Word.  Here is a fairly simple iLogic rule that I've used in the past.

Sub Main
	Dim oSourceDoc As Document = ThisApplication.ActiveDocument
	'could use folder browser dialog for this part too
	Dim oOutputFolder As String = "C:\Temp\iLogic Rules\"
	If Not System.IO.Directory.Exists(oOutputFolder) Then
		System.IO.Directory.CreateDirectory(oOutputFolder)
	End If
	Dim oAuto As IiLogicAutomation = iLogicVb.Automation
	Dim oSourceRule As iLogicRule
	For Each oSourceRule In oAuto.Rules(oSourceDoc)
		Dim oWriter As System.IO.StreamWriter = System.IO.File.CreateText(oOutputFolder & oSourceRule.Name & ".txt")
		oWriter.Write(oSourceRule.Text)
		oWriter.Close
	Next
End Sub

And here are a few other links (within my contribution posts) you may find useful, which have a similar idea of backing up (or copying) iLogic rules.

Copy All Rules From One Document To Another Using An Exteral iLogic Rule 

Copy All iLogic Rules & Event Triggers From One Document To Another Using An External iLogic Rule 

External iLogic Rule (or VBA Macro) To Create Local iLogic Rule   (You can use one of the solutions here to sort of reverse this process, and start with a text file, and create a local rule based on it.)

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes