PDF iLogic

ASchlaack
Collaborator

PDF iLogic

ASchlaack
Collaborator
Collaborator

I had this iLogic rule which when ran would export a PDF of the current .idw. We just upgraded to 2018 and it no longer works. I can find no errors with the code as to why it will no longer fire.

 

When I run it I get two pop ups...

Pop up one:

error1.PNG

 

Pop up two:

error 2.PNG

 

I hit ok to both pop ups and nothing ends up happening.

 

 

Can someone else try to run it in 2018 and tell me what results or solutions you're able to find?

 

Thanks!

 

 

SyntaxEditor Code Snippet

Sub Main()
    Dim oDoc As Document
    Dim oPDFAddIn As TranslatorAddIn
    Try
        Dim ThisApp = ThisApplication
        Dim TransObj As TransientObjects = ThisApp.TransientObjects
        oDoc = ThisDoc.Document
        If oDoc.DocumentType <> 12292 Then '12292 = kDrawingDocument object
            MessageBox.Show("This Rule must be run from within a Drawing Document.", "Incorrect Document Type")
    Exit Sub
End If

oPDFAddIn = ThisApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = TransObj.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = TransObj.CreateNameValueMap
oDataMedium = TransObj.CreateDataMedium

If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
    oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
    oOptions.Value("Remove_Line_Weights") = 1 ' 0 = False, 1 = True
    oOptions.Value("Vector_Resolution") = 400 ' DPI
    oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
    'oOptions.Value("Custom_Begin_Sheet") = 2
    'oOptions.Value("Custom_End_Sheet") = 4
End If

'Set the PDF target file name
oDataMedium.FileName = ThisDoc.Path & "\" & ThisDoc.FileName(False) & ".pdf"
Catch
MessageBox.Show("Error Getting Document Information" & vbCr & "Is the current Document Saved?", "Document Error")
End Try
'Publish document

Try
oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
Catch
MessageBox.Show("Unable to Save PDF" & vbCr & "Is the file open or read only?", "Error Saving Document")
End Try
End Sub 

 

Thanks,
Aaron Schlaack
---------------------------------------------------------------------------------
Autodesk Inventor 2018
Dell Windows 8.1 64 bit Intel(R) Xeon(R) @ 3.50GHz 32GB Ram
0 Likes
Reply
Accepted solutions (1)
4,153 Views
10 Replies
Replies (10)

MechMachineMan
Advisor
Advisor
Accepted solution

1. Added explicit typing to a couple objects (probably not ESSENTIAL).

2. Replaced DataMedium with oDoc in the HasSaveCopyAs options line.

 

Sub Main()
    Dim oDoc As Document
    Dim oPDFAddIn As TranslatorAddIn
	Dim oContext As TranslationContext
	Dim oOptions As NameValueMap
	Dim oDataMedium As DataMedium
	
	Try
		Dim ThisApp = ThisApplication
		Dim TransObj As TransientObjects = ThisApp.TransientObjects
		oDoc = ThisDoc.Document
		If oDoc.DocumentType <> 12292 Then '12292 = kDrawingDocument object
			MessageBox.Show("This Rule must be run from within a Drawing Document.", "Incorrect Document Type")
			Exit Sub
		End If
		
		oPDFAddIn = ThisApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
		
		oContext = TransObj.CreateTranslationContext
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		oOptions = TransObj.CreateNameValueMap
		oDataMedium = TransObj.CreateDataMedium
		
		If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
			oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
			oOptions.Value("Remove_Line_Weights") = 1 ' 0 = False, 1 = True
			oOptions.Value("Vector_Resolution") = 400 ' DPI
			oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
			'oOptions.Value("Custom_Begin_Sheet") = 2
			'oOptions.Value("Custom_End_Sheet") = 4
		End If
		
		'Set the PDF target file name
		oDataMedium.FileName = ThisDoc.Path & "\" & ThisDoc.FileName(False) & ".pdf"
	Catch
		MessageBox.Show("Error Getting Document Information" & vbCr & "Is the current Document Saved?", "Document Error")
	End Try
'Publish document

	Try
		oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
	Catch
		MessageBox.Show("Unable to Save PDF" & vbCr & "Is the file open or read only?", "Error Saving Document")
	End Try
End Sub 

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type

Anonymous
Not applicable

Hello - I have the same issue. Would greatly appreciate any help. Thank you!

 

 

 



'this rule outputs all drawing sheets to PDF 'this rule outputs all drawing sheets to DWF, 3D models of first sheet included Public Sub Main() 'Ask to print file printerName = "\\JLF-PRINT01\Canon Meadow Lighting_Dept PS3" askprint = MessageBox.Show("Print to " & printerName & "?", "Print",MessageBoxButtons.YesNoCancel) If askprint = vbYes Dim oDrawDoc As Document oDrawDoc = ThisApplication.ActiveDocument Dim oPrintMgr As PrintManager oPrintMgr = oDrawDoc.PrintManager 'specify your printer name '\\JLF-PRINT01\Canon Meadow Lighting_Dept PS3 oPrintMgr.Printer = printerName oPrintMgr.PrintRange = Inventor.PrintRangeEnum.kPrintAllSheets oPrintMgr.ColorMode = PrintColorModeEnum.kPrintGrayScale oPrintMgr.AllColorsAsBlack = False oPrintMgr.Orientation = PrintOrientationEnum.kPortraitOrientation oPrintMgr.PaperSize = SizeActiveSheet oPrintMgr.SubmitPrint Else If askprint = vbCancel Goto ErrorHandle End If 'Set-Up declarations for DPF and DWF add-ins PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}") DWFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}") oDocument = ThisApplication.ActiveDocument oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism PDFOptions = ThisApplication.TransientObjects.CreateNameValueMap DWFOptions = ThisApplication.TransientObjects.CreateNameValueMap PDFOptions = ThisApplication.TransientObjects.CreateDataMedium DWFOptions = ThisApplication.TransientObjects.CreateDataMedium 'Define the save path Dim fileName As String filename = ThisDoc.FileName(True) Prefix = Left(fileName, 3) 'First 3 characters i.e. "803" FirstSpace = InStr(1, fileName, " ") 'Gets location in strine of the first space If Not FirstSpace = 0 PartNumber = Left(fileName, FirstSpace-1) 'Assign everything left of the space to part number Else 'There is no space dot = InStr(1, fileName, ".") 'Gets the location in string of the first "." PartNumber = Left(fileName, dot-1) 'Assign everything left of the "." to part number End If 'PartNumber = Left(fileName, 9) 'Full Part number i.e. "803-00044" 'last5 = Right(DNumber, 5) 'Last 5 characters i.e. "00044" 'Image Library folder names have additional descriptors... If Left(Prefix, 3) = "801" folderDescription = "Table Lamps" Else If Left(Prefix, 3) = "802" folderDescription = "Floor Lamps" Else If Left(Prefix, 3) = "803" folderDescription = "Wall Lamps" Else If Left(Prefix, 3) = "804" folderDescription = "Ceiling Lamps" Else If Left(Prefix, 3) = "809" folderDescription = "Shades" End If PDF_Path = "I:\" & Prefix & " - " & folderDescription & "\PDF" DWF_Path = "I:\" & Prefix & " - " & folderDescription & "\DWF" 'Create PDF file PDF_File_Name = PartNumber & "-" & iProperties.Value("Project", "Project") & ".pdf" 'Debug line to show PDF save path 'MessageBox.Show(PDF_PATH, "PDF Save Path") If PDFAddIn.HasSaveCopyAsOptions(PDFDataMedium, oContext, PDFOptions) Then PDFOptions.Value("All_Color_AS_Black") = 0 PDFOptions.Value("Remove_Line_Weights") = 0 PDFOptions.Value("Vector_Resolution") = 4800 PDFOptions.Value("Gradient_Resolution") = 4800 PDFOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets 'PDFOptions.Value("Custom_Begin_Sheet") = 2 'PDFOptions.Value("Custom_End_Sheet") = 4 End If 'Set the PDF target file name 'Shop Drawing If ActiveSheet.TitleBlock = "MEADOW SHOP TITLE BLOCK" Cloc = InStr(1, fileName, "C") 'Gets location in string of the "C" If Cloc = 0 MFG_Folder = PartNumber & "-C" 'Add "-C" to PartNumber PDF_File_Name = ThisDoc.FileName(False) & ".pdf" Else MFG_Folder = Left(PartNumber, Cloc) 'Crop the end of Shop Drawing Part Number to the "C" PDF_File_Name = PartNumber & ".pdf" End If Shop_PDF_Path = PDF_Path & "\MFG DRAWINGS\" & MFG_Folder PDFDataMedium.FileName = Shop_PDF_Path & "\" & PDF_File_Name 'MessageBox.Show(Shop_PDF_PATH, "PDF Save Path") Else 'MessageBox.Show(PDF_PATH, "PDF Save Path") PDFDataMedium.FileName = PDF_Path & "\" & PDF_File_Name End If 'Publish PDF document Try PDFAddIn.SaveCopyAs(oDocument, oContext, PDFOptions, PDFDataMedium) Catch MessageBox.Show("Error - PDF may be open in another viewer, or destination folder does not exist.", "Problem saving PDF to P", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try If ActiveSheet.TitleBlock = "MEADOW SALES TITLE BLOCK" 'Sales Drawing 'Publish copy with Rev info in Project Directory copy_Path = ThisDoc.Path & "/" & PartNumber & " DOCS" copy_FileName = PartNumber & "-" & iProperties.Value("Project", "Project") & " Rev " & iProperties.Value("Project", "Revision Number") & ".pdf" PDFDataMedium.FileName = copy_Path & "\" & copy_FileName PDFAddIn.SaveCopyAs(oDocument, oContext, PDFOptions, PDFDataMedium) 'Create DWF file 'Store and delete Project, Side Mark, & Quantity storeProject = iProperties.Value("Project", "Project") storeSideMark = iProperties.Value("Custom", "SIDEMARK") storeQuantity = iProperties.Value("Custom", "QUANTITY") iProperties.Value("Project", "Project") = "" iProperties.Value("Custom", "SIDEMARK") = "" iProperties.Value("Custom", "QUANTITY") = "" InventorVb.DocumentUpdate() DWF_File_Name = PartNumber & ".dwf" 'Debug line to show DWF save path 'MessageBox.Show(DWF_PATH, "DWF Save Path" 'askview = MessageBox.Show("Launch the DWF Viewer now?", "Launch DWF Viewer",MessageBoxButtons.YesNo) 'If askview = vbYes Then : launchviewer = 1 : Else : launchviewer = 0 : End IF launchviewer = 0 If DWFAddIn.HasSaveACopyAsOptions(DWFDataMedium, oContext, DWFOptions)Then DWFOptions.Value("Launch_Viewer") = launchviewer DWFOptions.Value("Publish_All_Component_Props") = 1 DWFOptions.Value("Publish_All_Physical_Props") = 1 DWFOptions.Value("Password") = 0 DWFOptions.Value("Publish_3D_Models") = Publish_3D_Models If TypeOf oDocument Is DrawingDocument Then Dim oSheets As NameValueMap oSheets = ThisApplication.TransientObjects.CreateNameValueMap DWFOptions.Value("Publish_Mode") = DWFPublishModeEnum.kCompleteDWFPublish DWFOptions.Value("Publish_All_Sheets") = 1 'Publish the first sheet AND its 3D model Dim oSheet1Options As NameValueMap oSheet1Options = ThisApplication.TransientObjects.CreateNameValueMap oSheet1Options.Add("Name", "Sheet:1") oSheet1Options.Add("3DModel", True) oSheets.Value("Sheet1") = oSheetOptions End If End If DWFDataMedium.FileName = DWF_PATH & "\" & DWF_File_Name Try Call DWFAddin.SaveCopyAs(oDocument, oContext, DWFOptions, DWFDataMedium) Catch MessageBox.Show("Error - DWF may be open in another viewer, or destination folder does not exist.", "Problem saving to DWF to P", MessageBoxButtons.OK, MessageBoxIcon.Error) 'Restore Project, Side Mark, Quantity fields iProperties.Value("Project", "Project") = storeProject iProperties.Value("Custom", "SIDE MARK") = storeSideMark iProperties.Value("Custom", "QUANTITY") = storeQuantity InventorVb.DocumentUpdate() End Try If launchviewer = 1 Then ThisDoc.Launch(DWF_PATH & "\" & DWF_File_Name) 'Export copy to project directory copy_Path = ThisDoc.Path & "\" & PartNumber & " DOCS" copy_FileName = PartNumber & " REV " & iProperties.Value("Project", "Revision Number") & ".dwf" DWFOptions.Value("Launch_Viewer") = 0 'Dont launch viewer for the copy DWFDataMedium.FileName = copy_Path & "\" & copy_FileName Call DWFAddIn.SaveCopyAs(oDocument, oContext, DWFOptions, DWFDataMedium) 'Restore Project, Side Mark, Quantity Fields iProperties.Value("Project", "Project") = storeProject iProperties.Value("Custom", "SIDE MARK") = storeSideMark iProperties.Value("Custom", "QUANTITY") = storeQuantity InventorVb.DocumentUpdate() ErrorHandle: If OldDrawing = 1 MessageBox.Show("Unable to remove Project information - old Title Block format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) End If End If End Sub
0 Likes

ASchlaack
Collaborator
Collaborator

If you look at the above comment marked as a solution, that is a completely functional rule for exporting PDFs in 2018.

 

I don't know if your rule does something different but this rule works very well and has a few things that are very easy to modify such as your DPI. So I would recommend at least trying it.

Thanks,
Aaron Schlaack
---------------------------------------------------------------------------------
Autodesk Inventor 2018
Dell Windows 8.1 64 bit Intel(R) Xeon(R) @ 3.50GHz 32GB Ram

Anonymous
Not applicable

Thank you for replying. I will give it a try. Thanks again!

0 Likes

ASchlaack
Collaborator
Collaborator

I don't know how well you understand this code but this is the section with the editable settings. Let me know if you've got anymore questions.

 

easypfd.PNG

Thanks,
Aaron Schlaack
---------------------------------------------------------------------------------
Autodesk Inventor 2018
Dell Windows 8.1 64 bit Intel(R) Xeon(R) @ 3.50GHz 32GB Ram

Anonymous
Not applicable

Thank you very much, I will try this out.  Thanks for your time.

0 Likes

MattH_Work
Collaborator
Collaborator

I appreciate I'm answering an old post, but this may be helpful to others...

 

It seems that in 2017 the following line is correct
???= ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

 

But by the time we get to 2020 (changed around 2018 I believe) it should be
???= ThisApplication.ApplicationAddIns.ItemById("{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}")

 

Notice the chgange from 6 to 5 in the first block of numbers


MattH
Product Design Collection 2025
Vault Pro 2025
0 Likes

Anonymous
Not applicable

Hi, 

 

I am using this code but i need to add stock number to pdf file name. But my Stock number is in the part or assembly document, not in drawing.

 

Kindly ask for your help.

0 Likes

Anonymous
Not applicable

Got it.

 

Sub Main()
    Dim oDoc As Document
    Dim oPDFAddIn As TranslatorAddIn
	Dim oContext As TranslationContext
	Dim oOptions As NameValueMap
	Dim oDataMedium As DataMedium
	
	Try
		Dim ThisApp = ThisApplication
		Dim TransObj As TransientObjects = ThisApp.TransientObjects
		oDoc = ThisDoc.Document
		docFile = ThisDoc.ModelDocument
		Dim FNamePos As Long
FNamePos = InStrRev(docFile.FullFileName, "\", -1)                        
Dim docFName As String 
docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos) 
dim SN as String
SN = iProperties.Value(docFName, "Project", "Stock Number")

		If oDoc.DocumentType <> 12292 Then '12292 = kDrawingDocument object
			MessageBox.Show("This Rule must be run from within a Drawing Document.", "Incorrect Document Type")
			Exit Sub
		End If
		
		oPDFAddIn = ThisApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
		
		oContext = TransObj.CreateTranslationContext
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		oOptions = TransObj.CreateNameValueMap
		oDataMedium = TransObj.CreateDataMedium
		
		If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
			oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
			oOptions.Value("Remove_Line_Weights") = 1 ' 0 = False, 1 = True
			oOptions.Value("Vector_Resolution") = 400 ' DPI
			oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
			'oOptions.Value("Custom_Begin_Sheet") = 2
			'oOptions.Value("Custom_End_Sheet") = 4
		End If
		
		'Set the PDF target file name
		oDataMedium.FileName = ThisDoc.Path & "\" & ThisDoc.FileName(False) &"_" & SN & ".pdf"
	Catch
		MessageBox.Show("Error Getting Document Information" & vbCr & "Is the current Document Saved?", "Document Error")
	End Try
'Publish document

	Try
		oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
	Catch
		MessageBox.Show("Unable to Save PDF" & vbCr & "Is the file open or read only?", "Error Saving Document")
	End Try
End Sub 
0 Likes

Anonymous
Not applicable

But if we want to save each Sheet (in a sigle .idw) as a pdf with its Sheet name. How we do it?

0 Likes