- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @TGibson7A8HD. Sorry of the delay without any responses. Sometimes when we see a lot of long code scrolling down a page, we hesitate to jump into trying to solve possible coding issues, because we assume it may take more time to diagnose than we may want to invest at the moment, plus sometimes we've just got a lot of other stuff going on.
I looked over your batch PDF export code, copied it over to a local iLogic rule editor screen for proofreading and editing. I did make some changes to it that I believe will improve its functionality, but I have not tested the result yet. Here is what I have right now that you can check out. I did not really have to move the folder selection bit out into its own Function, but that just cleans up the main part of the code a bit. I believe most folks have a similar custom function that they use in multiple scenarios, and making it modular this way makes it easier to use in many other scenarios. I often have my PDF export process out in a Sub routine of its own too, but I did not go that route with your code right now.
Sub Main
'PDF all IDW in Directory.iLogicVb:
Dim oPath As String = GetFolder
If oPath = "" Then Exit Sub
If MsgBox("This will write PDF files from all idw files in this location: " & vbCr & vbCr & _
oPath & vbCr & vbCr & " *** Are you sure you want to do this? ***", vbOKCancel + vbExclamation, _
"Batch Write PDF Files") = vbCancel Then Exit Sub
'get PDF target folder path
Dim oFolder As String = oPath & "\" & "PDF"
'Check for the PDF folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If
Dim MyFiles As String()
' Sets up the variable "MyFile" to be each file in the directory
' This example looks for all the files that have an .idw extension.
' This can be changed to whatever extension is needed. Also, this
' macro searches the current directory and all sub-directories.
MyFiles = System.IO.Directory.GetFiles(oPath, "*.idw", System.IO.SearchOption.AllDirectories)
'get the PDF Translator AddIn, create variables, and set most options before loop
Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 1
'oOptions.Value("Custom_End_Sheet") = 4
' Starts the Loop, which will Continue Until there are no more files found.
ThisApplication.SilentOperation = True
For Each MyFile As String In MyFiles
' Opens the file and saves as PDF. This can be
' changed to any procedure that would be needed to run on every
' file in the directory such as opening each file.
Dim drawDoc As DrawingDocument = ThisApplication.Documents.Open(MyFile, False)
If drawDoc.DocumentType <> kDrawingDocumentObject Then Continue For
Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(drawDoc.FullFileName)
oFileName = oFolder & "\" & oFileName & ".pdf"
oDataMedium.FileName = oFileName
Try
oPDFAddIn.SaveCopyAs(drawDoc, oContext, oOptions, oDataMedium)
Catch
Logger.Error("Failed to export following drawing as PDF:" & vbCrLf & drawDoc.FullFileName)
End Try
drawDoc.ReleaseReference
drawDoc = Nothing
Next
ThisApplication.SilentOperation = False
ThisApplication.Documents.CloseAll(True) 'close all unreferenced documents
Shell("explorer.exe " & oFolder, vbNormalFocus)
End Sub
Function GetFolder() As String
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.InitialDirectory = "C:\$Workspace\Engineering Data"
oFileDlg.CancelError = True
oFileDlg.MultiSelectEnabled = False
oFileDlg.Filter = "All Files (*.*)|*.*"
oFileDlg.DialogTitle = "Choose a Directory with IDW's and/or Sub-Directories with IDW's"
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
Return Nothing
ElseIf oFileDlg.FileName <> "" Then
oPath = System.IO.Path.GetDirectoryName(oFileDlg.FileName)
Return oPath
End If
End Function
Wesley Crihfield
(Not an Autodesk Employee)