Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ilogic to save PDF in 2 locations

16 REPLIES 16
Reply
Message 1 of 17
bengee54
2022 Views, 16 Replies

ilogic to save PDF in 2 locations

Can anyone help me; I'm trying find some code that will allow me to save a PDF in 2 different locations. 

 

I want to save a copy on my C: drive - this part has been done, but I'd like the code expanded to save a copy on the F: drive (under a similar path to that on the C: drive.

 

For example, for drawing XXX-XXX.dwg (C:\Vault Workspace\CAD\Sales\Enquiries\2013\13-XXX\Drawings), I would like a PDF created in C:\Vault Workspace\CAD\Sales\Enquiries\2013\13-XXX\Drawings\PDF  (this part has been done) and i would also like a copy saved in F:\Sales\Enquiries\2013\13-XXX\Drawings\PDF.

 

I do not know which will be easier; to create 2 PDFs, or create 1 and copy it to F:

 

Can anyone help me?

16 REPLIES 16
Message 2 of 17
karthur1
in reply to: bengee54

Search for "batch Print pdf" or something like that.  You might be able to look at the code and figure out the part you need.

Message 3 of 17
pcrawley
in reply to: bengee54

Surely if you have already written the code for publishing a PDF to somewhere on C: then it wouldn't he a huge task to copy and paste the line of code and change the drive letter to F: ?

 

Often with iLogic queries it helps to post your code.  If you are not into iLogic yourself, someone here might spot the appropriate line and fix it for you.

Peter
Message 4 of 17
bengee5454
in reply to: pcrawley

Sorry its taken so long to reply, but the issue raised its head again so I searched the IV discussion pages and found a posting on the topic by ME!

 

the code which was given to me is below. I havent used this in a while, but believe that it creates a folder "PDF" on C: then saves the file in there.

 

to recap, I want a PDF saved in C: (done), then a PDF saved in the 'same' location in F: - a yes/no pop up saying something like 'Save in F:? would be helpful.

 

I am not experienced in writing code, hence my cry for help!

 

SyntaxEditor Code Snippet

Question1 = MessageBox.Show("Do you want to save a PDF to the working folder\PDF?" _,"iLogic",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1)
If Question1 = vbNo Then
Return
End If

'------start of iLogic-------
oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("Project", "Revision Number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, 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
End If

'get PDF target folder path
oFolder = oPath & "\PDF"
'oFolder = "F:\Sales\Enquiries\2013\" & 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

'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & _
" Rev" & oRevNum & ".pdf"

oMyFileName = oFolder & "\" & oFileName & " Rev" & oRevNum & ".pdf"

'check to see that the xls file exists
If Dir(oMyFileName) <> "" Then
Overwrite = MessageBox.Show("A PDF for this file already exists, overwrite it?", "iLogic",MessageBoxButtons.YesNo, _
MessageBoxIcon.Question,MessageBoxDefaultButton.Button1)
If Overwrite = vbNo Then
    Return
    End If
End If

Try
'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Catch
MessageBox.Show("PDF could not be saved, this may be due to another user having the file open", "iLogic" & ThisApplication.GeneralOptions.UserName & "!")
End Try

'Show message box
MessageBox.Show("Drawing saved and PDF Exported to " & oFolder & vbLf & vbLf & _
"Filename is: " & oFilename & " [" & oRevNum & "].pdf", "iLogic",MessageBoxButtons.Ok,MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)

 

 

Message 5 of 17
bengee5454
in reply to: bengee5454

Please please please can someone help on this?

 

In response to one of the replies - 'simply change the location where it's saved....' - I have tried to amend the code (in red) but sadly, no luck. When i tried this (apostrophe removed), it gave an error. Foolishly I thought it would be that simple.

 

I've just been audited and was pulled up on a couple of missing files Smiley Sadhaving something like this would (hopefully) eliminate that.

 

SyntaxEditor Code Snippet

'------start of iLogic-------
oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("Project", "Revision Number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, 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
End If

'get PDF target folder path
oFolder = oPath & "\PDF"
'oFolder = "F:\" & 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

'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & _
" Rev" & oRevNum & ".pdf"

oMyFileName = oFolder & "\" & oFileName & " Rev" & oRevNum & ".pdf"

'check to see that the xls file exists
If Dir(oMyFileName) <> "" Then
Overwrite = MessageBox.Show("A PDF for this file already exists, overwrite it?", "iLogic",MessageBoxButtons.YesNo, _
MessageBoxIcon.Question,MessageBoxDefaultButton.Button1)
If Overwrite = vbNo Then
    Return
    End If
End If

Try
'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Catch
MessageBox.Show("PDF could not be saved, this may be due to another user having the file open", "iLogic" & ThisApplication.GeneralOptions.UserName & "!")
End Try

'Show message box
MessageBox.Show("Drawing saved and PDF Exported to " & oFolder & vbLf & vbLf & _
"Filename is: " & oFilename & " [" & oRevNum & "].pdf", "iLogic",MessageBoxButtons.Ok,MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)

.

 

 

Message 6 of 17
pcrawley
in reply to: bengee54

In your red text, remove the \ after F:
The "\" is added later in the code.
Peter
Message 7 of 17
bengee5454
in reply to: pcrawley

thanks for the reply.

 

I've done as you said but get the error

 

The given path's format is not supported

 

 

Message 8 of 17
pcrawley
in reply to: bengee54

Sorry, that's not right... Teach me to try and do this on my phone...

It's creating the path for the PDF by joining "F:\" and then the current path - but that could be C:\... The joined up text would read F:\C:\... and that would cause your error.

Where are you trying to save the PDF?
Peter
Message 9 of 17
bengee5454
in reply to: pcrawley

i'm working in C:\Vault Workspace\CAD\Sales\Enquiries\2015\15-809 Hindley Additions\5. Drawings\CPV Drawings (for example)

 

the location where i want it saved (so everyone can see it) is:

 

F:\Sales\Enquiries\2015\2015\15-809 Hindley Additions\5. Drawings\CPV Drawings

Message 10 of 17
mdavis22569
in reply to: bengee5454

try this:

 

SyntaxEditor Code Snippet

strFolder = "E:\PDF\" & ThisDoc.FileName(False) 'without extension

ThisDoc.Document.SaveAs(strFolder & (".pdf") , True)

strFolder = "C:\PDF\" & ThisDoc.FileName(False) 'without extension

ThisDoc.Document.SaveAs(strFolder & (".pdf") , True)

MessageBox.Show("All file formats saved", "File Save")

 

 

Just Change your file locations for each one to where you want it ..the E and the c....   You do need to create the PDF folder first though  


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------
Mike Davis

EESignature

Message 11 of 17
mdavis22569
in reply to: mdavis22569

So it would be 

 

SyntaxEditor Code Snippet

strFolder = "C:\Vault Workspace\CAD\Sales\Enquiries\2015\15-809 Hindley Additions\5. Drawings\CPV Drawings\" & ThisDoc.FileName(False) 'without extension

ThisDoc.Document.SaveAs(strFolder & (".pdf") , True)

strFolder = "F:\Sales\Enquiries\2015\2015\15-809 Hindley Additions\5. Drawings\CPV Drawings\" & ThisDoc.FileName(False) 'without extension

ThisDoc.Document.SaveAs(strFolder & (".pdf") , True)

MessageBox.Show("All file formats saved", "File Save")

 


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------
Mike Davis

EESignature

Message 12 of 17
pcrawley
in reply to: bengee54

But tal will only ever save PDF files in the Hindley Additions folder. What about the next job?

This replacement for the red text ought to work for all jobs regardless of the folder name:

oFolder = "F:\" & Right(oPath, len(oPath) - 17)) & "\PDF"
Peter
Message 13 of 17
mdavis22569
in reply to: pcrawley

You have to update the path for where you want it....

and the folder MUST be made..

 

 

or add 1 part of my code for the folder that will always be teh same and add keep the part of your code where it saves it to the workspace .... 

 

 

 

 

 

 


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------
Mike Davis

EESignature

Message 14 of 17
bengee5454
in reply to: pcrawley

hi Peter,

 

I do as you suggest, but get:

 

Error on Line 29 : End of statement expected.

Message 15 of 17
bengee5454
in reply to: bengee5454

the PDF actually doesn't need to be in a new folder named "PDF", i'm more than happy about it being saved in the CPV Drawings folder. that might make it a ittle easier?

 

 

Message 16 of 17
pcrawley
in reply to: bengee5454

This should do it:

 

oFolder = "F:\" & Right(oPath, (Len(oPath) - Len("C:\Vault Workspace\"))) & "\PDF"

 

It might look complicated, but it takes "F:\" then adds the current-drawing path, then adds the "\PDF" sub folder (and creates it if it doesn't exist)

 

The bit that makes it look complicated is all the "Len" stuff, and I'll try and explain it:

 

Your current drawing lives at C:\Vault Workspace\CAD\Sales\...bla bla bla

The bit in bold needs to be replaced by F:\

 

Len counts the number of characters in a string of text: Len("test") would return 4.

Right reads a specific number of characters from the text in brackets starting from the right.
So Right("read this", 4) would return "this".

 

What I did was use Len("C:\Vault Workspace\") to return 19, and Len(oPath) to return the length of your current path (93 in this instance).  So the line of code (in English!) reads:

 

Right(text to read starting from the right, number of characters to read)

or

Right(current path, 93 - 19)

or

Right("C:\Vault Workspace\CAD\Sales\Enquiries\2015\15-809 Hindley Additions\5. Drawings\CPV Drawings", 74)
which would extract the last 74 characters of the path;
CAD\Sales\Enquiries\2015\15-809 Hindley Additions\5. Drawings\CPV Drawings

 

Making the final oFolder = F:\ and CAD\Sales\Enquiries\2015\15-809 Hindley Additions\5. Drawings\CPV Drawings and \PDF

 

So if you don't want the extra \PDF folder, just delete & "\PDF"

 

You could argue "why not just work out 93-19 and put that in as a number?", but as you work on different folders in Vault, the path length changes, so the resulting folder for your PDF's needs to change too.

 

It will fail at one point: If you create a drawing in the root foler (C:\Vault Workspace) because Len needs to return a non-zero result, but by the looks of your folder naming and the fact that you are actually audited, I doubt that'll ever happen!

 

Sorry, this turned into an essay - but I know how frustrating it is having something that almost works and your stuck on the last little bit!

Peter
Message 17 of 17
bengee5454
in reply to: pcrawley

Thank you very much Peter,

 

Thats exactly what I was looking for.

 

...and a nice explanation of the code.

 

Much appreciated.

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report