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: brandon.lee97

Sorry for the delay, but I have a lot going on today at work.  In the following example, I basically just added the SelectFolder Function as an additional routine, similar to the previous example, but that function is now being called to run from within your existing MakePDFFromDoc Sub routine.  I usually pass the full file name of the file that I want to export to the routine that will be doing the exporting, making it more robust for use in multiple scenarios.  You are currently passing just the 'DateString' to your custom routine, but you could have transferred most, if not all of the code for assembling the full file name of the PDF from that custom routine, into your 'Main' routine, then just pass the full file name of the PDF to that custom routine.  You could also call the SelectFolder routine to run from the 'Main' routine somewhere before you call your other routine to run, then pass the folder / path data to that routine.  Since I am not sure what all your design intent is for this, I am not sure if you want to manually select a folder using the SelectFolder routine, then still add the "\iLogic PDF's ( DateString )" text after that point, or if you want to handle that part while you have the dialog open, then skip that step in the code.  If doing it all in the dialog, you should no longer need to check if the directory exists.

I did customize the SelectFolder routine a bit more for you, to make it easier to specify a starting folder.  When setting the RootFolder, you limit how far up you can browse within the dialog, so I left that line of code out in this version.  Instead I added an extra optional input variable (sStartFrom) to the routine, allowing you an easier way to specify that starting path.  Then within the routine, it uses that to set the SelectedPath property, which will cause that path expanded and selected when the dialog opens.  If a simple folder browser is too basic, and you want to step it up a notch, we could instead use an Inventor.FileDialog, then set its settings similarly, then use its ShowSave method, which will show the same dialog you see when using SaveAs.  But it will not actually save the file, just return the final full path and file name that you specify to the code that called it to run.  Then you can use that later in the code.  That online help page contains a link to a VBA (not iLogic) sample that uses one.

Sub Main()
	Dim myDate As String = Now().ToString("yyyy-MM-dd HHmmss")
	myDate = myDate.Replace(":","")  ' & " - " & TypeString
	userChoice = InputRadioBox("Defined the scope", "This Document", "All Open Documents", True, Title := "Defined the scope")
	UserSelectedActionList = New String(){"DXF & PDF", "PDF Only", "DXF Only"}
	UserSelectedAction = InputListBox("What action must be performed with selected views?", _
	UserSelectedActionList, UserSelectedActionList(0), Title := "Action to Perform", ListName := "Options")
	Select UserSelectedAction
		Case "DXF & PDF" : UserSelectedAction = 3
		Case "PDF Only" : UserSelectedAction = 1
		Case "DXF Only" : UserSelectedAction = 2
	End Select
	If userChoice Then
		MakePDFFromDoc(ThisApplication.ActiveDocument, myDate, UserSelectedAction)
	Else
	Dim oDocs As Documents = ThisApplication.Documents
	For Each oDoc As Document In oDocs
		If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject
			Try
				If Len(oDoc.File.FullFileName) > 0 Then
					MakePDFFromDoc(oDoc, myDate, UserSelectedAction)
				End If
			Catch
			End Try
		End If
	Next
	End If
End Sub

Sub MakePDFFromDoc(ByRef oDocument As Document, DateString As String, UserSelectedAction As Integer)
	Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
	("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oContext As TranslationContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	Dim oOptions As NameValueMap = oTO.CreateNameValueMap
	Dim oDataMedium As DataMedium = oTO.CreateDataMedium
	'<<< calling the other custom Function to run in next line >>>
	Dim oPath As String = SelectFolder("Select Folder", "G:\Dropbox\PROJECTS WIP")
	'checking to make sure it returned something, if not, exit this routine
	If String.IsNullOrEmpty(oPath) Then Return 'if no path was returned, exit routine
	Dim oFilePart As String = System.IO.Path.GetFileNameWithoutExtension(oDocument.FullFileName)
	'Dim oRevNum As String = oDocument.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
	'get PDF target folder path
	Dim oFolder As String = oPath & "\iLogic PDF's (" & DateString & ")"
	'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
	'Set the PDF target file name
	oDataMedium.FileName = oFolder & "\" & oFilePart & ".pdf"
	If oPDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
		oOptions.Value("All_Color_AS_Black") = 0
		oOptions.Value("Remove_Line_Weights") = 0
		oOptions.Value("Vector_Resolution") = 400
		oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
		'oOptions.Value("Custom_Begin_Sheet") = 2
		'oOptions.Value("Custom_End_Sheet") = 4
		
		'Publish document
		If (UserSelectedAction = 1) Or (UserSelectedAction = 3) Then
			oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)'For PDF's
		End If
		If (UserSelectedAction = 2) Or (UserSelectedAction = 3) Then
			oDocument.SaveAs(oFolder & "\" & oFilePart & ".dxf", True) 'For DXF's
		End If
	End If
End Sub

Function SelectFolder(Optional sPrompt As String = vbNullString, Optional sStartFrom As String = vbNullString) As String
	If String.IsNullOrEmpty(oPrompt) Then sPrompt = "Select Folder"
	If String.IsNullOrEmpty(sStartFrom) Then sStartFrom = "C:\"
	Dim oFDialog As New System.Windows.Forms.FolderBrowserDialog
	oFDialog.Description = sPrompt
	oFDialog.ShowNewFolderButton = True
	oFDialog.SelectedPath = sStartFrom
	Dim oResult As System.Windows.Forms.DialogResult = oFDialog.ShowDialog()
	If oResult = System.Windows.Forms.DialogResult.OK Then
		Return oFDialog.SelectedPath
	Else 'if dialog was canceled or something else
		Return vbNullString
	End If
End Function

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)