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: 

Export to PDF rule partially works but has a bug that I can't figure out

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Shag_Bore
209 Views, 4 Replies

Export to PDF rule partially works but has a bug that I can't figure out

Hello, I have a rule that does to things, first it exports entire .idw to pdf, this works perfectly fine. The second part of the rule searches thru all the sheet names and if it finds one named "BASE PLATE POLE CAP", it activate that drawing sheet, then exports the sheet to it's own pdf so that I have it ready to forward to where we would get them cut. 

 

Our workflow isn't perfect and sometimes I don't have a Job Number or Project number available so I created an If/Then/Else statement to create this second pdf. If the JobNumber custom iProperty exists, create a pdf named "this", if it doesn't exist, the just create a generic pdf name "Base Plate Pole Cap".

 

I can't get the Base Plate Pole Cap pdf export to function properly, it only exports a pdf named "BASE PLATE POLE CAP", and before that export occurs and the pdf appears in windows explorer, I get the error message box where I have to press OK first. But no one has it open, it isn't even created yet.

  

Shag_Bore_0-1718287107869.png

 

Not sure where I have gone wrong with this. Any ideas, thanks!

 

rule below:

'EXPORT ENTIRE PDF
path_and_namePDF = ThisDoc.PathAndFileName(False)
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("All_Color_AS_Black") = 0
'oOptions.Value("Custom_End_Sheet") = 4
End If
'Set the destination file name
oDataMedium.FileName = path_and_namePDF & ".pdf" 'SET DESTINATION, FILE LOCATION WITH FILE NAME AND EXTENSION
On Error Goto handlePDFLock
Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) ' EXPORT DOCUMENT
'CHECK FOR BASE PLATE POLE CAP SHEET, IF EXIST THEN EXPORT TO PDF
Dim oSheet As Sheet  ' Set up an indicator to track progress.
Dim bCheck as Boolean = False
For Each oSheet In ThisApplication.ActiveDocument.Sheets
Dim sSheetName As String = oSheet.Name.Split(":")(0)
        If oSheet.Name.StartsWith("BASE PLATE POLE CAP") = True Then
           bCheck = True
           ActiveSheet = ThisDrawing.Sheet(oSheet.Name)
           Exit For
        End If
Next
If bCheck = False Then ' Checking we actually found the sheet being looked for.
Return ' No Sheet found with given name so exit. 
End If

'if BASE PLATE POLE CAP sheet is found, it should be activated to current sheet, then export current sheet only to pdf
pathPDF = ThisDoc.Path
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument2 = ThisApplication.ActiveDocument
oContext2 = ThisApplication.TransientObjects.CreateTranslationContext
oContext2.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions2 = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium2 = ThisApplication.TransientObjects.CreateDataMedium
If PDFAddIn.HasSaveCopyAsOptions(oDataMedium2, oContext2, oOptions2) Then
oOptions2.Value("Remove_Line_Weights") = 1
oOptions2.Value("Vector_Resolution") = 4800
oOptions2.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("All_Color_AS_Black") = 0
'oOptions.Value("Custom_End_Sheet") = 4
End If

'Set the destination file name
If iProperties.Value("Custom", "JobNumber") = False Then
oDataMedium2.FileName = pathPDF & "\" & "BASE PLATE POLE CAP" & ".pdf"
'MessageBox.Show(oDataMedium2.FileName, "Title")
Else
oDataMedium2.FileName = pathPDF & "\" & iProperties.Value("Custom", "JobNumber") & "_BASE PLATE POLE CAP" & ".pdf"
'MessageBox.Show(oDataMedium2.FileName, "Title")
End If

On Error Goto handlePDFLock
'Publish document.
Call PDFAddIn.SaveCopyAs(oDocument2, oContext2, oOptions2, oDataMedium2)
Exit Sub
handlePDFLock:
MessageBox.Show("PDF could not be saved, most likely someone else has it open.", _
"No PDF for you " & ThisApplication.GeneralOptions.UserName & "!")
Resume Next
Sean Farr
Product Designer at Teksign Inc.
Inventor 2016 SP1
Dell Precision 3660
i7-12700 @ 2.40GHz-4.90GHz
32GB DDR5 4400MHz RAM
NIVDIA RTX A2000 6GB
4 REPLIES 4
Message 2 of 5
A.Acheson
in reply to: Shag_Bore

Hi @Shag_Bore 

You will first need to remove the in error resume next line. That's a catch all method that doesn't allow you find what's going wrong. Try using try catch instead and position it around a block of code. You can then keep moving it to a smaller and smaller block of code untill you find the line erroring out. Alternatively just remove the error trap and let the code editor return a line that is failing and address why it could be.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 5

Hi @Shag_Bore 

 

This line will error if the property does not exist. I think this is likely the source of your error.

If iProperties.Value("Custom", "JobNumber") = False Then

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Message 4 of 5

I think this will work for you

 

oErrorMsg = "No PDF for you " & ThisApplication.GeneralOptions.UserName & "!" _
& vbLf & "PDF could not be saved, maybe someone else has it open?"

'[PDF setup
PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("All_Color_AS_Black") = 0
'oOptions.Value("Custom_End_Sheet") = 4
']

'[EXPORT ENTIRE PDF
'set sheet range option
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'set name
oDataMedium.FileName = ThisDoc.PathAndFileName(False) & ".pdf" 'SET DESTINATION, FILE LOCATION WITH FILE NAME AND EXTENSION

Try 
	Call PDFAddIn.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oDataMedium) ' EXPORT DOCUMENT
Catch
	MsgBox(oErrorMsg, , "Problem Creating 1st PDF")
End Try
']

'[CHECK FOR BASE PLATE POLE CAP SHEET, IF EXIST THEN EXPORT TO PDF
Dim bCheck As Boolean = False
For Each oSheet As Sheet In ThisApplication.ActiveDocument.Sheets
	Dim sSheetName As String = oSheet.Name.Split(":")(0)
	If oSheet.Name.StartsWith("BASE PLATE POLE CAP") = True Then
		bCheck = True
		ActiveSheet = ThisDrawing.Sheet(oSheet.Name)
		Exit For
	End If
Next	

If bCheck = False Then ' Checking we actually found the sheet being looked for.
	Return ' No Sheet found with given name so exit. 
End If

Try
	oJNum = iProperties.Value("Custom", "JobNumber") & "_"
Catch
	oJNum = ""
End Try

'update sheet range option
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet
'set name
oDataMedium.FileName = ThisDoc.Path & "\" & oJNum & "BASE PLATE POLE CAP" & ".pdf"

Try 
	Call PDFAddIn.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oDataMedium) ' EXPORT DOCUMENT
Catch
	MsgBox(oErrorMsg, , "Problem Creating 2nd PDF")
End Try
']
Message 5 of 5

Thanks @Curtis_Waguespack  and @A.Acheson 

 

I am getting a better understanding now with the Try and catch's,  

 

interesting in this specific case how the try looks for the jobnumber and if it exists, uses it, if it doesn't then the code moves onto the catch which is essentially then blank. very cool!!

 

thanks again, have a good weekend!

 

Sean 

Sean Farr
Product Designer at Teksign Inc.
Inventor 2016 SP1
Dell Precision 3660
i7-12700 @ 2.40GHz-4.90GHz
32GB DDR5 4400MHz RAM
NIVDIA RTX A2000 6GB

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report