by Chris Smith
Introduction
iLogic is an incredibly useful tool for any Inventor users, allowing engineers and designers to automate repeatable tasks. A lot of time can be recovered from design and manufacturing processes by automating part of, or a whole task.
When engineers adopt iLogic into their workflows, they will quickly start to build a catalogue of rules that both build skill and understanding as well as improving completion times in future iLogic projects.
Part of this time improvement comes from reusing code that we have already created in the past and modifying it to suit our new task. One way we can achieve this is with custom snippets.
Preparing for Custom Snippets
Before we can create our custom snippets, we first need to decide how they will be stored. Custom Snippet libraries are saved in XML files and can be stored anywhere on the system, be it locally or on a network. As a Vault user I like to store my snippet library in the Vault workspace. This not only allows for backing up of the XML file, but also allows other Vault users to share my snippets. To create a custom snippet library, we just need to choose the Save As New option in the custom snippets tab of the iLogic window.
We can then select the save location and provide a file name for our library XML.
Once we have our snippet library file we can start to add our custom snippets.
Creating custom snippets
There can be many reasons as to why we would want to save snippets. Generally, it is so we can save blocks of code that we want to reuse, either to save time when writing code or to reuse code blocks that we find are useful in many rules.
For this example, I have a ‘Try – Catch’ block that will export any errors to a text file. This can be very useful for logging errors in the code instead of the rule stopping if it reaches an exception point.
Try
'Insert code to test here
Catch ex As Exception
Dim exceptionFolder As String = "D:\Vault Workspace\Designs\iLogic Logging\ExportErrors"
If Not IO.Directory.Exists(exceptionFolder) Then
IO.Directory.CreateDirectory(exceptionFolder)
End If
Dim exceptionLog As String = IO.Path.Combine(exceptionFolder, "errors.txt")
IO.File.AppendAllLines(exceptionLog, {Now.ToString("dd-MM-yyyy HH:mm:ss") & " - " & ex.Message})
End Try
Once this code was created it became a good candidate for a custom snippet as it could be reused in any rule.
To create the snippet, we just need to highlight the block of code that we would like to include in our snippet, right click, and select “Capture Snippet”.
The next pop-up window allows us to give a name to our snippet, write a tool tip so that other users can understand what the snippet will do, and has an editor window to modify the code block.
You can see in my example above I have removed the “Try” from the top of the block as the catch block will usually go at the end of a code block and the try goes at the start. In this case I only need the catch block.
Once we click OK, the snippet will be added to our list.
We get the tooltip at the top in BOLD and a preview of the code.
It is important to the click the Save Custom Snippets button at the top of the iLogic Custom Snippets window if you are happy with the new custom snippet. If we do not click save before closing the window, then any edits will be reverted.
Summary
Being able to save and share custom snippets is a great benefit to the iLogic system. It allows users to quick create code as well as allowing those with a higher skill level to share their code with those that may not yet have the same grasp over the system without requiring the more skilled users to stop what they are doing to modify other users code. In summary, it is a great tool for any iLogic user to get comfortable with using.
Hozzászólások (0 hozzászólás)