Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results forย 
Showย ย onlyย  | Search instead forย 
Did you mean:ย 

Inventor crashes after running code

7 REPLIES 7
Reply
Message 1 of 8
soverdevest
596 Views, 7 Replies

Inventor crashes after running code

Dear Inventor Forum, 

 

I have a Ilogic code to publish drawings. Here I can choose to make DXF file, STP file's PDF file's etc from an existing drawing. I use this in combination with a program to draw cylindrical bushings. 

Now I wanted to expand this with thrust washers. The problem is, when i sellect the "publish button" the prgram crashes a certain point in the code. 

 

I have located the line which creates the error. 

It is inside an external code to create the PDF drawing. 

 

"

If SharedVariable.Exists("PathAndName") = False Then
MessageBox.Show("SharedVariable 'PathAndName' bestaat niet, succes anyway!", "PDF Publish", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Exit Sub
End If
oDoc = ThisApplication.ActiveDocument
If oDoc.DocumentType <> kDrawingDocumentObject Then
MessageBox.Show("PDF kan niet gemaakt worden van een 3D", "PDF Publish", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Exit Sub
End If
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
'oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
'oContext.Type = Inventor.IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
If oPDFAddIn Is Nothing Then
MessageBox.Show("Could not access PDF translator.", "PDF Publish", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Exit Sub
End If
'If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then 'aangepast voor INVENTOR 2018
oOptions.Value("All_Color_AS_Black") = False 'deze werkt
oOptions.Value("Remove_Line_Weights") = False 'deze werkt
oOptions.Value("Vector_Resolution") = 600
'oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintAllSheets 'deze werkt
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets 'deze werkt
'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet 'deze werkt
'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange 'deze werkt
'oOptions.Value("Custom_Begin_Sheet") = 1
'oOptions.Value("Custom_End_Sheet") = 100
'oOptions.Value("Display_Published_File_In_Viewer") = True 'deze werkt niet
'End If 'aangepast voor INVENTOR 2018
oDataMedium.FileName = SharedVariable("PathAndName") & ".pdf"


MessageBox.Show("Start problem")

 

oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)

 

MessageBox.Show("code run succesfully")

 

"

Above you see the code in question. 

also you see the messagbox with "start problem" and "code run succesfully" 

I have not made it to the messagebox "code run succesfully" ๐Ÿ˜†

 

Does anybody have an idea why the programs crashes at this line of code?

Tags (2)
7 REPLIES 7
Message 2 of 8
WCrihfield
in reply to: soverdevest

Hi @soverdevest.  This is just a guess based on first quick read through your posted code, but it looks like even though the 'SharedVariable' seems to be important to the functionality of this code, and you have a code near start to abort code if it does not exist, It also looks like it gets the path and file name from that shared variable to use as new file name (except for file extension).  It might be wise to check that shared variable's value before trying to use it, to ensure that even if it exists, it is not empty, or not usable value.  If the file you are saving to already exists, that may also cause problems.  I usually check if the file already exists before attempting to overwrite the file, and prompt user about any existing file before SaveAs.  I would also enclose that SaveAs line within a Try...Catch block of code, to 'handle' the potential error like this, and not cause the whole code to crash, and will let it continue, even if it did not work.  Just a couple thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8
soverdevest
in reply to: WCrihfield

hi @WCrihfield, thank you for sharing your opinionon the my isue. I do need to add, implemeting your solutions will give me a hard time as I did not write the code. I can do very basic VBA, my predecessors made the code.
Message 4 of 8
WCrihfield
in reply to: soverdevest

Hi @soverdevest.  I copied the code you posted over into my iLogic rule editor screen, then modified it a little bit, then have posted the result back here for you to try out.  I added extra precaution checks in there, and added more information feedback in there for now, to help identify any potential future problem.  If this is too much information, or too many messages, and the code is working good otherwise, you may want to 'comment out' the lines of code that show the messages you no longer want to see.  To comment out lines of code, without deleting them, just put an apostrophe (') at the beginning of the line of code.  And if that line of code wraps to a next line, you may have to comment out that next line too.  The Try...Catch...End Try block of code is a way to 'try' doing something by code, without that causing the code to crash (completely stop and show error message), then on the Catch side is where you put what you want to do if/when the Try part fails.

 

Dim sPathAndName As String
If SharedVariable.Exists("PathAndName") Then
	sPathAndName = SharedVariable.Value("PathAndName")
Else
	MessageBox.Show("SharedVariable 'PathAndName' bestaat niet, succes anyway!", "PDF Publish", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Exit Sub
End If
If String.IsNullOrEmpty(sPathAndName) Then
	MsgBox("SharedVariable 'PathAndName' found, but was empty. Exiting rule.", vbCritical, "SharedVariable Was Empty")
	Exit Sub
End If
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MessageBox.Show("The 'Active' document was not a Drawing.  Exiting rule.", "PDF Publish Wrong Doc Type", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Exit Sub
End If
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
If oPDFAddIn Is Nothing Then
	MessageBox.Show("Could not access PDF translator.", "PDF Publish", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
	Exit Sub
End If
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then 'aangepast voor INVENTOR 2018
oOptions.Value("All_Color_AS_Black") = False 'deze werkt
oOptions.Value("Remove_Line_Weights") = False 'deze werkt
oOptions.Value("Vector_Resolution") = 600
'oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintAllSheets 'deze werkt
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets 'deze werkt
'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet 'deze werkt
'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange 'deze werkt
'oOptions.Value("Custom_Begin_Sheet") = 1
'oOptions.Value("Custom_End_Sheet") = 100
'oOptions.Value("Display_Published_File_In_Viewer") = True 'deze werkt niet
'End If 'aangepast voor INVENTOR 2018
Dim sFullFileName As String = sPathAndName & ".pdf"
If System.IO.File.Exists(sFullFileName) Then
	oAns = MsgBox("A PDF file already exists with the same full path and name as you are trying to export to." _
	& vbCrLf & sFullFileName & vbCrLf & _
	"Do you want to overwrite this existing PDF file?", vbYesNo + vbQuestion, "File Already Exists")
	If oAns = vbNo Then Exit Sub
End If
oDataMedium.FileName = sFullFileName
MessageBox.Show("oDataMedium.FileName = " & oDataMedium.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Try
	oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
Catch oEx As Exception
	MsgBox("SaveCopyAs Method failed." & vbCrLf & _
	"Error Message:" & vbCrLf & _
	oEx.Message & vbCrLf & _
	"StackTrace:" & vbCrLf & _
	oEx.StackTrace, vbExclamation, "SaveCopyAs Failed")
End Try
MessageBox.Show("code run succesfully")

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) ๐Ÿ‘.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 8
soverdevest
in reply to: WCrihfield

Hi @WCrihfield,
I have placed the code in place of the other code and I receive this error:

--------------------------------------------------------------------------------------

Rule Compile Errors in Rule12, in B006299-0-v0.idw

Error on Line 38 : Local variable 'oDoc' cannot be referred to before it is declared.
Error on Line 39 : Local variable 'oDoc' cannot be referred to before it is declared.
Error on Line 49 : Local variable 'oDoc' cannot be referred to before it is declared.
Error on Line 54 : Local variable 'oDoc' cannot be referred to before it is declared.
Error on Line 55 : Local variable 'oDoc' cannot be referred to before it is declared.
Error on Line 58 : Local variable 'oDoc' cannot be referred to before it is declared.
Error on Line 71 : Local variable 'oDoc' cannot be referred to before it is declared.

--------------------------------------------------------------------------------------

Would it help If I share the total code?

Message 6 of 8
WCrihfield
in reply to: soverdevest

Hi @soverdevest.  Apparently yes.  Within the code I posted, I first declared the 'oDoc' variable on line 16, and it is not referred to before that point, so there should not be a problem.  But those line numbers mentioned in the error message you received do not seem to match the line numbers of the code I posted, which leads me to believe that you are trying to use that code within a larger block of code.  If that is the case, I would have to see the whole code to help determine how to avoid those types of errors.  We usually recognize and retain a reference to the document that a code is supposed to be working on near the start of most codes, then continue to use that single variable that represents that document throughout the rest of the entire code.  If there is more code above this point in your overall code, then we may need to continue to use an earlier, previously declared variable for the document, instead of creating a new one.  Then use Find & Replace tool to replace all those variables, so they are all the same one throughout the whole code, to avoid potential problems.

 

If it is a long code, you can copy & paste it into a simple Notepad text file, then attach that text file to your forum post response, instead of pasting it directly into a code window on the forum.  That is often easier on the eyes for the rest of us in the forum.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 8
soverdevest
in reply to: WCrihfield

See attached the code in TXT. 

Message 8 of 8

There are many reasons for Inventor to crash.
Before suspecting issues in iLogic code it is highly recommended to re-check two things:

- The latest update available for Autodesk Inventor release you use should be installed (some updates fix API and iLogic defects).

- The dataset you are using should not have hidden critical defects (use Body Sanity Tool to check the 3D-model).

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report