Monday, December 1, 2014

We're going to need a bigger box...

Wouldn't it be nice if every time you reached into your toolbox you could pull out the perfect tool for the job?  You never had to use a pair of pliers to remove a spark plug?  You never had to resort to hacking through that wire with dull scissors?

The Inventor iLogic integrated development environment, (IDE), provides an awesome set of tools for quickly crafting conditional automation code.  i.e. "If this hole increases in size by x, Then thicken they webbing member by y."

This conditional logic is in fact the central theme of this blog; if this happens, then do that.  Pretty simple right?

What happens when simple conditional logic or using the provided code snippets don't measure up to the task at hand?  Fear not!  There is a solution.  A solution that's not well documented or advertised but that's been available nearly since iLogic's inception.  "...pssst, you can use, (reference), other code libraries and frameworks within the iLogic IDE.

After you've made the reference, you can call into those libraries and execute publicly exposed functions just as if they were your own.  

You can add references in two ways, AddReference "System.Drawing.dll" or through the Imports statement, as in Imports System.IO.  Inventor iLogic automatically "includes" the following .NET framework libraries or namespaces:
  • System
  • System.Math
  • System.Collections
  • Microsoft.VisualBasic
  • Autodesk.iLogic.Interfaces
  • Autodesk.iLogic.Runtime
Hmmmmm, seems that iLogic is capable of  leveraging the Microsoft .NET framework.  Well isn't that handy.  More later...



Let's take a look at a simple example that checks to see if a file exists.

Imports System.IO

Sub Main()
    Dim fName As String = "C:\Temp\rt1.ipt"
    If file.Exists(fname) Then 
        MsgBox(fName & " exists!")
    End If
End Sub

If the file is really there, we get this result:


Let's try another:

Imports System.IO

Sub Main()
    Dim path As String = "C:\temp\text.txt"
    File.AppendAllText(path, "test message" + vbCr)
End Sub



The above code snippet can be very useful for adding entries to a "log file".

I've always been a firm believer that if you intend to use a block of code more than once it deserves to me its own method or function.  Let's look at some other ways to code this in a little more efficient and professional manner.

Option 1 (Method)

Imports System.IO

Sub Main()
    Dim path As String = "C:\temp\text.txt"
    Call LogWriter(path,"Message passed as argument")
End Sub

Private Sub LogWriter(ByVal path As String, ByVal msg as String)
    File.AppendAllText(path, msg + vbCr)
End Sub

Now whenever we need to write a new line to our log file we just pass the path and the message, and the Call keyword is optional.  

LogWriter(path,"Message passed from a method")

We don't "expect" anything to go wrong because the .AppendAllText will create the file if it doesn't exist.  Oh wait, what if I typed the folder path wrong?

Let's add some error handling.


Imports System.IO

    Sub Main()
        Dim path As String = "C:\temp\text.txt"
        LogWriter(path, "Message passed as argument")
    End Sub

    Private Sub LogWriter(ByVal path As String, ByVal msg As String)
        Try
            File.AppendAllText(path, msg + vbCr)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Something has gone horribly wrong!")
        End Try

    End Sub

The Try/Catch block is your friend.  Think of it as the modern-day equivalent of the old 
On Error Resume Next.  The concept is super simple, "Try" this block of code, "Catch and handle any errors."
You may also be interested in File.Move, File.Copy, File.Exists or File.Replace.  Check out all of the File Class methods or the entire.NET Framework Class collection.
In the coming weeks/months extend this post to include "methods vs. functions", linking to data sources and expanded form functionality.
If you've learned one new thing by reading this post I've achieved my goal.

Happy Trails!