using "Save as PDF" iLogic code

using "Save as PDF" iLogic code

mmckee
Participant Participant
607 Views
7 Replies
Message 1 of 8

using "Save as PDF" iLogic code

mmckee
Participant
Participant

Hello,

 

I am trying to make the attached Ilogic code save a PDF file to the root directory instead of the current file directory.

 

for example: root folder is XXXXXXXX, file is in _Cad File Only folder in this root folder.

I want to save the PDF from _Cad File Only folder to XXXXXXXX root folder.

 

Here is the code:

'Get the Full Path of the file
FullPath = ThisDoc.Path

'Gets the Workspace Path
WorkspacePath= ThisDoc.WorkspacePath()

'Gets the Length of the WorkspacePath String
WorkspacePathLength = Len(WorkspacePath)

'Gets just the Path of the file
PathOnly = ThisDoc.Path

'Removes the Workspace Path from FullPath
'DirectoryPath = Strings.Right(PathOnly, PathOnly. Length= WorkspacePathLength)

'Sets the Dirctory that the PDF should be saved in
PDFPath = FullPath

'Checks to see if that directory exists, if not, it is created
If(Not System.IO.Directory.Exists(PDFPath)) Then
	System.IO.Directory.CreateDirectory(PDFPath)
End If

'Saves the PDF in the desired location
ThisDoc.Document.SaveAs(PDFPath & "\" & ThisDoc.FileName(False) & (".pdf") , True)

Thanks!

 

0 Likes
Accepted solutions (1)
608 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @mmckee.  Maybe this similar example will work better.  It is using an already available method for getting the 'root' directory of the file path, then using another available method for combining the path and file name.

Dim oDoc As Inventor.Document = ThisDoc.Document
If oDoc.FileSaveCounter = 0 Then
	MsgBox("This Document Must Be Saved First!", vbCritical, "iLogic")
	Return
End If
'get this document file's path
Dim sPath As String = ThisDoc.Path
'get the root of this path
sPath = System.IO.Directory.GetDirectoryRoot(sPath)
'get thid document file's name, without extension, then add PDF extension
Dim sName As String = ThisDoc.FileName(False) & ".pdf"
Dim sNewFullFileName As String = System.IO.Path.Combine(sPath, sName)
Try
	oDoc.SaveAs(sNewFullFileName, True)
Catch ex As Exception
	MsgBox("SaveAs Method failed:" & vbCrLf & _
	ex.Message & vbCrLf & ex.StackTrace, vbExclamation, "iLogic")
End Try

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

mmckee
Participant
Participant

that didn't work as planned. it took it and put it at the root of the entire drive.

see screenshot below. this is what I want it to do.

 

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor
Accepted solution

OK, I guess the 'root' term threw me off.  So you just wanted to put the PDF file in the 'next higher' folder than the one the current file is already in.  Here is an updated example that should do that.  In this example, I am using the System.IO.Path.GetDirectoryName method.  If the String provided to that method is a full file name, including the file name, it will get the folder path that the file is directly in.  When the String provided is just a path to a folder, it will get the next higher folder's path.

Dim oDoc As Inventor.Document = ThisDoc.Document
If oDoc.FileSaveCounter = 0 Then
	MsgBox("This Document Must Be Saved First!", vbCritical, "iLogic")
	Return
End If
'get the path to the folder this file is directly within
Dim sPath As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
'get next higher folder than that previous folder
sPath = System.IO.Path.GetDirectoryName(sPath)
'get this document file's name, without extension, then add PDF extension
Dim sName As String = ThisDoc.FileName(False) & ".pdf"
Dim sNewFullFileName As String = System.IO.Path.Combine(sPath, sName)
Try
	oDoc.SaveAs(sNewFullFileName, True)
Catch ex As Exception
	MsgBox("SaveAs Method failed:" & vbCrLf & _
	ex.Message & vbCrLf & ex.StackTrace, vbExclamation, "iLogic")
End Try

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

mmckee
Participant
Participant

Perfect, Thanks!

0 Likes
Message 6 of 8

89198826955
Collaborator
Collaborator

This rule works if there's only one sheet.

How do I do this if there are multiple sheets?

Снимок экрана 2026-03-04 172401.png

 

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Hi @89198826955.  Exporting drawings to PDF format has been a very popular topic on this forum for a long time, so a lot of working code examples can be found for that process.  The example in this discussion is very basic, so it does not offer any 'options'.  If you need to be able to specify options for the export process, then the code will need to become longer and more complex, because it will need to involve accessing and using the TranslatorAddIn that is specifically for exporting PDF's.  When doing things that way, more code is required, because the methods involved are asking for more 'inputs', which gives us the chance to customize some options for the export.  In the example below, it is also attempting to save the PDF file in the 'next higher' folder than the one the original drawing file is currently within, because that functionality is part of what this discussion is about.  It sill also use the same file name as the original file, same as before.  But in this example, we are specifying that we want all sheets to be included, the 'all color as black' option is turned off, the 'remove line weights' option is turned off, the 'vector resolution' is set to 720, and the 'launch viewer' option is set to True.  So, if it is exporting a multi-sheet drawing, the resulting PDF should have multiple sheets also.  Since this code is customized to this forum topic, I have not tested it yet.

Dim oDoc As Inventor.Document = ThisDoc.Document
If oDoc.FileSaveCounter = 0 Then
	MsgBox("This Document Must Be Saved First!", vbCritical, "iLogic")
	Return
End If
'get the path to the folder this file is directly within
Dim sPath As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
'get next higher folder than that previous folder
sPath = System.IO.Path.GetDirectoryName(sPath)
'get this document file's name, without extension, then add PDF extension
Dim sName As String = ThisDoc.FileName(False) & ".pdf"
Dim sNewFullFileName As String = System.IO.Path.Combine(sPath, sName)
'now use TranslatorAddIn for PDF's to export this drawing to a PDF file
Dim oPDFAddIn As Inventor.TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
'create variables needed by this tranalstor's methods
Dim oTO As Inventor.TransientObjects = ThisApplication.TransientObjects
Dim oContext As Inventor.TranslationContext = oTO.CreateTranslationContext()
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As Inventor.NameValueMap = oTO.CreateNameValueMap()
Dim oDataMedium As Inventor.DataMedium = oTO.CreateDataMedium()
'set the full file name for the new PDF to be created
oDataMedium.FileName = sNewFullFileName
'use initial Translator method to get/set options for export
If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
	oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
	oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
	oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
	oOptions.Value("Launch_Viewer") = 1 ' 0 = False, 1 = True
	'call the main method for exporting, supplying all necessary data
	Try
		oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
	Catch
		MsgBox("TranslatorAddIn.SaveCopyAs Method Failed To Export PDF!", vbExclamation, "iLogic")
	End Try
End If

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8

89198826955
Collaborator
Collaborator

Great, it works

Thanks!!!

0 Likes