AddIn to export ilogic rules

AddIn to export ilogic rules

ahmed.hagi
Enthusiast Enthusiast
552 Views
2 Replies
Message 1 of 3

AddIn to export ilogic rules

ahmed.hagi
Enthusiast
Enthusiast

I have a iLogic rule that exports ilogic rules from the current file.

 

I'm trying to make this an AddIn so we can source control our rules with Git.

 

I can't seem to get the rules to export. I've got the file dialogue to select the output directory, but nothing happens when I click ok.

Dim oDoc As Document
            oDoc = g_inventorApplication.ActiveDocument

            Dim oFolderDlg As FolderBrowserDialog = New FolderBrowserDialog()

            If oFolderDlg.ShowDialog() = DialogResult.OK Then
                Dim sFolderPath As String = oFolderDlg.SelectedPath

                Dim oiLogicAddin As ApplicationAddIn
                    oiLogicAddin = g_inventorApplication.ApplicationAddIns.ItemById("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}")


                Dim oiLogicDoc = oiLogicAddin
                oiLogicDoc.Activate()
                Dim iLogicAuto = oiLogicDoc.Automation


                Dim rules As ObjectCollection
                rules = iLogicAuto.GetRules(oDoc)


                For Each oRule In rules
                    Dim iLogicVBCode As String = oRule.Text

                    Dim iLogicFilePath As String = System.IO.Path.Combine(sFolderPath, oRule.Name & ".vb")

                    System.IO.File.WriteAllText(iLogicFilePath, iLogicVBCode)

                    MsgBox("iLogic rule " & oRule.Name & " exported to " & iLogicFilePath)
                Next

            End If

 

0 Likes
Accepted solutions (1)
553 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @ahmed.hagi.  Was your code working OK when ran from within a normal external iLogic rule in Inventor?  It seems to me like you are using the wrong Type of collection object variable for holding the retrieved rules from the document.  That method is designed to return a plain 'IEnumerable' type object, not an ObjectCollection.  And you may also need to check if the variable representing the IEnumerable 'Is Nothing' after you try setting its value with that method, just in case there were no internal rules found in that document, as an error prevention step.

Attached is a text file containing the code I use for an external iLogic rule to write out all internal iLogic rules to text files.  I left my external rules with the ".txt" file extension, so you would obviously need to change that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

ahmed.hagi
Enthusiast
Enthusiast

Hello,

 

Thanks for the tip!!

When it was running in an external rule I used the built in iLogic methods to access the rules so it worked fine.

The object tree in Visual Studio doesn't seem to expose the methods available for AddIns like iLogic.

Switching to the IEnumerable fixed it!

 

Thanks!

 

Ahmed.

0 Likes