Export current drawing as pdf using drawing iproperty as file name

Export current drawing as pdf using drawing iproperty as file name

alan.wedge
Enthusiast Enthusiast
389 Views
6 Replies
Message 1 of 7

Export current drawing as pdf using drawing iproperty as file name

alan.wedge
Enthusiast
Enthusiast

Hi All,

 

I am trying to get a script that will export the currently selected drawing as a pdf to the same file location as the drawing using a drawing iproperty as the file name.

 

im pretty bad at ilogic, please dumb it down for me

 

Thanks

Accepted solutions (1)
390 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @alan.wedge.  Can you please inform us of which iProperty you want to use the value of for the PDF's file name?  If this drawing has more than one sheet, then do you want all of its sheets to be exported as a single, multi-page PDF document, or do you need each sheet to be a separate PDF document?  If you were to export the drawing as a PDF manually, how would you set each option?  For example:  Just current sheet or All Sheets or Sheets in specific range?  All colors as Black or not?  Remove object line weights or not?  Vector resolution: set values ranging from 150 DPI to 4800 DPI?  Display published file in viewer or not?

WCrihfield_0-1675172186920.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

alan.wedge
Enthusiast
Enthusiast

thanks for the help,

 

I would like to use the Stock Number Iproperty as the file name

 

Here are my usual settings;

alanwedge_0-1675175431564.png

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Hi @alan.wedge.  Here is an iLogic rule you can try out for that task.  I customized that settings within to match the settings in the image you posted.  It also has a built-in checker, which checks if that PDF file already exists, and if it does, it pauses to ask if you want to overwrite it, or leave the existing one alone (therefore not exporting it again).

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This Rule only works on Drawing Documents.  Exiting Rule.", vbExclamation, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	If oDDoc.FileSaveCounter = 0 Then
		MsgBox("You must save the drawing file before exporting it to PDF.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim sPath As String = System.IO.Path.GetDirectoryName(oDDoc.FullFileName)
	Dim sDirSep As Char = System.IO.Path.DirectorySeparatorChar
	Dim sStockNum As String = oDDoc.PropertySets.Item(3).Item("Stock Number").Value
	Dim sNewFullName As String = sPath & sDirSep & sStockNum & ".pdf"
	ExportToPDF(oDDoc, sNewFullName)
End Sub

Sub ExportToPDF(oDrawing As DrawingDocument, sNewFullFileName As String)
	Dim oPDF As TranslatorAddIn
	oPDF = 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
	If System.IO.File.Exists(sNewFullFileName) = True Then
		oAns = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = sNewFullFileName
	If oPDF.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
		oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
		oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
		oOptions.Value("Vector_Resolution") = 2400 ' DPI
		oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
		oOptions.Value("Launch_Viewer") = 0 ' 0 = False, 1 = True
	End If
	Try
		oPDF.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
	Catch e As Exception
		Logger.Error("Error using SaveCopyAs method." & vbCrLf & e.Message & vbCrLf & e.StackTrace)
	End Try
End Sub

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 5 of 7

alan.wedge
Enthusiast
Enthusiast

That is 100% perfect. thank you so much for the help

0 Likes
Message 6 of 7

alan.wedge
Enthusiast
Enthusiast

one small issue worth noting it that if the pdf is open by someone else the export fails but there is no notification that the export failed, it looks like it worked. the easiest solution is to just check if i can delete the pdf but im wondering if there is a way for the code to catch the failure?

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @alan.wedge.  That is a tricky subject, and probably depends on where the file is stored, and where the 'other' person may be in relation to you (as far as network/internet and such), who may have that file open.  There is a way to check the 'ReadOnly' attribute of a file, but that is not really a good indicator that someone else may have a file open, because it is independent.  I saw a fairly simple code once for checking this, but it is not 100% effective for all file types.  For instance, when checked against simple text files, it did not seem to work, but when checked against PDF files, it seemed to work as expected.  'Seemed' being the keyword.  Here is something you can play around with.

Sub Main
	Dim oFile As String = "C:\Temp\ABC.pdf"
	If FileIsOpen(oFile) Then
		MsgBox("File is open.", , "")
	Else
		MsgBox("File not open.", , "")
	End If
End Sub

Function FileIsOpen(sFullFileName As String) As Boolean
	If Not System.IO.File.Exists(sFullFileName) Then
		Logger.Debug("sFullFileName was not found, or does not exist.")
		Return False
	End If
	Dim FS As System.IO.FileStream = Nothing
	Try
		FS = System.IO.File.Open(sFullFileName, System.IO.FileMode.Open, _
		System.IO.FileAccess.Read, System.IO.FileShare.None)
		FS.Close
		FS.Dispose
		FS = Nothing
		Return False
	Catch
		Return True
	End Try
End Function

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