iLogic: Using Try – Catch to Open Drawings

I realise that I don’t often explain how specific iLogic code works, so I thought I’d offer a bit more of an explanation in this post. I’ve written a simple iLogic rule that opens the drawing of a model in Inventor.

This iLogic utility uses a nested “Try, Catch, End Try” statement. If you are not familiar with a “Try, Catch, End Try” statement, I’ll explain how they work below, the structure is always Try, Catch, End Try, as laid out below.

Try = Try to run this code

Catch = Catch any errors – and then do

End Try = End of “Try, Catch, End Try”

A simple version of the code looks like this:

	Try 'Try open a drawing with the same name as this model
		ThisDoc.Launch(ThisDoc.PathAndFileName(False) + ".dwg")
	Catch 'If no model is found show a message box
		MessageBox.Show("No Drawing found for this model", "@ClintBrown3D")
	End Try

The “nested” version I was talking about above, is shown below, The code first tries to open a DWG file, if it can’t find a DWG, it tries to find an IDW, and if it can’t find the IDW, it shows a message box.

Try
	ThisDoc.Launch(ThisDoc.PathAndFileName(False) + ".dwg")
Catch
	Try
		ThisDoc.Launch(ThisDoc.PathAndFileName(False) + ".idw")
	Catch
		MessageBox.Show("No Drawing found", "@ClintBrown3D")
	End Try
End Try

Using these basic principles, you should be able to build some pretty neat iLogic utilities yourself!

My final code has a bit of user input at the start, it looks like this:

i = MessageBox.Show("Would you like to open the Drawing now?", "@ClintBrown3D", MessageBoxButtons.YesNo)
If i = vbYes Then : launchviewer = 1 : Else : launchviewer = 0 : End If
If launchviewer = 1 Then
Try
	ThisDoc.Launch(ThisDoc.PathAndFileName(False) + ".dwg")
Catch
	Try
		ThisDoc.Launch(ThisDoc.PathAndFileName(False) + ".idw")
	Catch
		MessageBox.Show("No Drawing found for this model", "@ClintBrown3D")
	End Try
End Try
End If

I hope you find this useful, happy coding!