Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: TGibson7A8HD

Hi @TGibson7A8HD.  Since the options for exporting an (.idw) drawing document to PDF do not include anything about scaling or paper size, I assume that it must look at the current 'Print Setup' and/or printing settings for things like page size, orientation, and scaling.  That is likely why your other code works better, because it is specifying printer settings, then printing to a PDF printer directly.  Another thing that might help would be if your drawing sheet's are designed to be the same size as the paper size you will be printing them to.  When that is done, you can pretty much print at 100% or 'Actual' scale and what you have on your drawing sheet on screen will fit the paper perfectly when you print.

 

I assume that in this case, we just need to incorporate some DrawingPrintManager related code into the loop, just before we export the PDF, so it will know the intended paper size, orientation, and scale better before exporting.  I noticed some inconsistencies between the settings of your single PDF export code and your batch PDF export codes, so I was not sure how you really wanted it set-up.  In the single export, you have AllColorsAsBlack set to True, but in the PDF options, you have 'All_Color_As_Black' set to 0 (zero), which represents False.  Similarly, in the single export code you are not specifying a setting for 'RemoveLineWeights', but within the batch PDF export code, you have the 'Remove_Line_Weights' setting set to 1 (which represents True).  I think that is normally False, but no default is documented.

 

You can give this a try if you want.  I just injected some of the DrawingPringManager settings into the loop as mentioned.  You may want to adjust those print settings if they do not look correct to your needs.

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
		
		'set print settings
		Dim oDPrintMgr As DrawingPrintManager = drawDoc.PrintManager
		oDPrintMgr.AllColorsAsBlack = True
		oDPrintMgr.NumberOfCopies = 1
		oDPrintMgr.Orientation = PrintOrientationEnum.kPortraitOrientation
		oDPrintMgr.PaperSize = PaperSizeEnum.kPaperSizeLedger '11 x 17 inches
		oDPrintMgr.PrintRange = PrintRangeEnum.kPrintAllSheets
		oDPrintMgr.RemoveLineWeights = False
		oDPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
		
		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

EESignature

(Not an Autodesk Employee)