Error Handling if printer not found

Error Handling if printer not found

karthur1
Mentor Mentor
1,046 Views
11 Replies
Message 1 of 12

Error Handling if printer not found

karthur1
Mentor
Mentor

I have made a "one-click print" iLogic rule to make printing simplier.  I would like to add some error handling if it is not able to find the printer.   The printer name is hard-coded in the iLogic. The way it is now, I get the error below. 

 

Is there a way to catch this and just display a message that makes it obvious to the user as to what is wrong?

 

Thanks

 

karthur1_1-1694519283112.png

 

0 Likes
Accepted solutions (2)
1,047 Views
11 Replies
Replies (11)
Message 2 of 12

Andrii_Humeniuk
Advisor
Advisor

Hi @karthur1 . In your rule, the document is specified as a drawing:

Dim oDrgDoc As DrawingDocument
oDrgDoc = ThisApplication.ActiveDocument

Based on your error, you are trying to run a rule in an assembly (871319.iam):

ErrorAsm.png

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 12

karthur1
Mentor
Mentor

Not what I originally meant, but can we catch that situation too?

 

 Below is the message I get when I run it on an IDW.  I know the problem is because the printer name in my code is not the same as the system printer (IT changed the name on me).

 

karthur1_0-1694520268822.png

 

0 Likes
Message 4 of 12

Andrii_Humeniuk
Advisor
Advisor

Is the Try-Catch method right for you?

 

 

Try
	oDrgPrintMgr.Printer = "\\SRV-VM-RUS-DC2\FMI Engineering - BW Ricoh MP C4504EX PCL6"
Catch
	MessageBox.Show("Printer not found - FMI Engineering - BW Ricoh MP C4504EX PCL6",
					"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Exit Sub
End Try

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 12

karthur1
Mentor
Mentor

Yes, That works for me.

 

How do I test to make sure that it is being ran on an IDW document?  I tried this, but it doesn't work.

 

karthur1_0-1694524056903.png

 

 

 

0 Likes
Message 6 of 12

Andrii_Humeniuk
Advisor
Advisor

Please try this code:

 

Dim oDrgDoc As DrawingDocument
oDrgDoc = ThisApplication.ActiveDocument
If System.IO.Path.GetExtension(oDrgDoc.FullDocumentName) = ".idw" Then
End If

 

Or:

Dim oDrgDoc As DrawingDocument
oDrgDoc = ThisApplication.ActiveDocument
If oDrgDoc.IsInventorDWG = False Then
End If

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 7 of 12

karthur1
Mentor
Mentor

 It gets by the error check it when I run it on a IDW, but when I run it on a IAM, it fails on line 2.

 

Dim oDrgDoc As DrawingDocument
oDrgDoc = ThisApplication.ActiveDocument
If System.IO.Path.GetExtension(oDrgDoc.FullDocumentName) <> ".idw" Then
	MessageBox.Show("Please run this rule from an IDW file.", "iLogic")
		Exit Sub
End If

 

I get this error when I run it on an IAM.  It never gets to the IF statement.

 

karthur1_0-1694525475096.png

 

 

0 Likes
Message 8 of 12

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

If I were writing this rule, I would do it like this:

Dim oInvApp As Inventor.Application = ThisApplication
Dim oDoc As Document = oInvApp.ActiveDocument
If Not TypeOf oDoc Is DrawingDocument Then
	MessageBox.Show("Active document is not drawing!",
					"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Exit Sub
End If
Dim oDrgDoc As DrawingDocument = oDoc
If oDrgDoc.IsInventorDWG = False Then
	MessageBox.Show("Active drawing is not .idw!",
					"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Exit Sub
End If

' Set reference to drawing print manager' DrawingPrintManager has more options than PrintManager' as it's specific to drawing document
Dim oPrintMgr As DrawingPrintManager
oPrintMgr = oInvApp.ActiveDocument.PrintManager

'Set the printer name. Either Change this to match the system printer name, or change the system printer to this name
'(comment this line to use Default printer Or assign another one).
Try
	oDrgPrintMgr.Printer = "\\SRV-VM-RUS-DC2\FMI Engineering - BW Ricoh MP C4504EX PCL6"
Catch
	MessageBox.Show("Printer not found - FMI Engineering - BW Ricoh MP C4504EX PCL6",
					"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Exit Sub
End Try

'Set Color Mode
oPrintMgr.ColorMode = kPrintGrayScale
'oPrintMgr.ColorMode = kPrintColorPalette

'Set Orientation
'oPrintMgr.Orientation = kPortraitOrientation
oPrintMgr.Orientation = kLandscapeOrientation

'Set Paper Size. Height x Width units are in centimeters.
oPrintMgr.PaperSize = kPaperSize11x17
'oPrintMgr.PaperHeight = 17
'oPrintMgr.PaperWidth = 11

oPrintMgr.ScaleMode = kPrintBestFitScale

' Set to print all sheets.
	oPrintMgr.PrintRange = kPrintAllSheets
'	oPrintMgr.PrintRange = kPrintCurrentSheet
	
oPrintMgr.SubmitPrint

 

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 9 of 12

Frederick_Law
Mentor
Mentor
Accepted solution

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-DF62A491-8743-492B-B2FB-BBF30C77B34E

 

Dim oDoc As Document
oDoc = ThisDoc.Document
If oDoc.DocumentType = kDrawingDocumentObject Then
'Do Printing here
End if

 

Message 10 of 12

WCrihfield
Mentor
Mentor

Basically, any time a rule is for a specific type of document, and it is possible for that rule to be ran on any other type of document, you should always check the Type of document you are getting a reference to, before trying to set that document reference to any of the 'specific' document Type variables (AssemblyDocument, DrawingDocument, PartDocument).  If you try to check document type afterwards, the error will have usually already happened.  If you try to set an AssemblyDocument as the value of a DrawingDocument type variable, it will throw an error.  There are several popular ways to deal with that situation, depending on the situation, and your individual coding style.

Below are a few starting lines examples I often use:

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document

or

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument

or

If Not TypeOf ThisDoc.Document Is AssemblyDocument Then Exit Sub
Dim oADoc As AssemblyDocument = ThisDoc.Document

There are lots of other similar examples too.  Whichever suits your needs & style best.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 12

karthur1
Mentor
Mentor

I got it to work!!!  Changed these lines to this.  It works for either IDW or DWG format.

 

Many Thanks

 

karthur1_0-1694527357745.png

 

0 Likes
Message 12 of 12

Frederick_Law
Mentor
Mentor

I don't like "exit sub", "goto".

Structured Programming.

I'll use:

If oDoc.DocumentType = kDrawingDocumentObject Then
'Do Printing here
else
  MessageBox.Show("Please run this rule from an IDW file.", "iLogic")
End if

 

0 Likes