I am working on a macro for automatically creating a PDF during a drawing save. For now, since I don't really know that much about programming, I mostly find existing code examples and modify / combine them into what I need. This one was created mostly using some of the examples from the API help, along with some tweaking based on code I found online.
The macro is intended to reside in the document, since the file path for the automatic PDF will need to change for each project, and in some cases by individual drawing. The actual path will replace the "Temp" path that I have left in the code from the sample for testing purposes. This will require some tweaking of the macro on individual drawings, but I have no problem with doing that to set things up if the PDF creation after that becomes automatic. The point is to automatically have a copy of the latest revision of the drawing as PDF in a "Draft" folder, for others to view. We currently are supposed to do this manually, and Iit is often forgotten, so PDF's are missing when needed.
My current issue is this:
I would like the PDF to be automatically named to our standard convention, which is "Drawing Number (Rev ??).pdf". The drawing number is usually the same as the filename, so I am using the document display name to get that part, which I found in an example somewhere on these forums. I'm having some trouble figuring out how to get the revision to fill in.
What would I need to add to my code to get the revision into the PDF file name? My class module code is below.
Option Explicit Private WithEvents oApplicationEvents As ApplicationEvents Private Sub Class_Initialize() Set oApplicationEvents = ThisApplication.ApplicationEvents End Sub Private Sub Class_Terminate() Set oApplicationEvents = Nothing End Sub Private Sub oApplicationEvents_OnSaveDocument(ByVal DocumentObject As Document, ByVal BeforeOrAfter As EventTimingEnum, ByVal Context As NameValueMap, HandlingCode As HandlingCodeEnum) If (BeforeOrAfter = kBefore) Then MsgBox ("about to save " & DocumentObject.FullFileName) 'Get the PDF translator Add-In. Dim PDFAddIn As TranslatorAddIn Set PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}") 'Set a reference to the active document (the document to be published). Dim oDocument As Document Set oDocument = ThisApplication.ActiveDocument Dim oContext As TranslationContext Set oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = kFileBrowseIOMechanism ' Create a NameValueMap object Dim oOptions As NameValueMap Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap ' Create a DataMedium object Dim oDataMedium As DataMedium Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium ' Check whether the translator has 'SaveCopyAs' options If PDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then ' Options for drawings... oOptions.Value("All_Color_AS_Black") = 0 'oOptions.Value("Remove_Line_Weights") = 0 'oOptions.Value("Vector_Resolution") = 400 'oOptions.Value("Sheet_Range") = kPrintAllSheets 'oOptions.Value("Custom_Begin_Sheet") = 2 'oOptions.Value("Custom_End_Sheet") = 4 End If 'Set the destination file name oDataMedium.FileName = "c:\temp\" & Left(DocumentObject.DisplayName, Len(DocumentObject.DisplayName) - 4) & " (REV " & ".pdf" 'Publish document. Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) Else MsgBox (DocumentObject.FullFileName & " has been saved") End If End Sub
Any help would be appreciated.
Solved! Go to Solution.
Solved by Mario-Villada. Go to Solution.
This small code reads the revision letter from the iProperties of the drawing document. I guess you could add the string "rev" to the name of your pdf.
Private Sub ReadRevision() Dim oDrgDoc As DrawingDocument Set oDrgDoc = ThisApplication.ActiveDocument Dim SIPropSet As PropertySet Set SIPropSet = oDrgDoc.PropertySets.Item("Summary Information") Dim rev As String rev = SIPropSet.ItemByPropId(kRevisionSummaryInformation).Value MsgBox ("the current revision is : " & rev) End Sub
Hope this helps.
Mario.
Mario,
Thanks! This worked perfectly. My failed code was similar, but apparantly had something in it that wasn't right. Unfortunately I deleted it before comparing it to your example to see exactly what I did wrong, but I think it was that I had the "rev" variable defined as a Property instead of String.
In case this could benefit someone else, below is the final, working version of the macro. Due to some crazy issues with the original "event listening" based version, it has been converted to a simpler AutoSave macro. The macro automatically names the PDF file using the file name and revision of the drawing.
The file path is revised for each drawing to be the appropriate "Draft PDF" location for that project. Generally I fix it in one drawing, then copy it to all the others in the project. This is saving me a lot of time versus manually creating PDF's every time I make a significant change.
Public Sub AutoSavePublishPDF() ' Get the PDF translator Add-In. Dim PDFAddIn As TranslatorAddIn Set PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}") 'Set a reference to the active document (the document to be published). Dim oDocument As Document Set oDocument = ThisApplication.ActiveDocument Dim oContext As TranslationContext Set oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = kFileBrowseIOMechanism ' Create a NameValueMap object Dim oOptions As NameValueMap Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap ' Create a DataMedium object Dim oDataMedium As DataMedium Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium ' Check whether the translator has 'SaveCopyAs' options If PDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then ' Options for drawings... oOptions.Value("All_Color_AS_Black") = 0 'oOptions.Value("Remove_Line_Weights") = 0 'oOptions.Value("Vector_Resolution") = 400 'oOptions.Value("Sheet_Range") = kPrintAllSheets 'oOptions.Value("Custom_Begin_Sheet") = 2 'oOptions.Value("Custom_End_Sheet") = 4 End If 'Set the destination file name Dim SIPropSet As PropertySet Set SIPropSet = oDocument.PropertySets.Item("Summary Information") Dim rev As String rev = SIPropSet.ItemByPropId(kRevisionSummaryInformation).Value oDataMedium.FileName = "c:\temp\" & Left(oDocument.DisplayName, Len(oDocument.DisplayName) - 4) & " (REV " & rev & ").pdf" 'Publish document. Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) End Sub
The code works perfectly for me. Not being a programmer, I have a question about editing the code. I have a main directory in a specific location, then a whold bunch of sub directories under it where the pdf's need to be distributed to depending on the first three characters of the file name. For example, if the part number is "620-4582", the pdf needs to be saved in the "620-" sub directory. I have already changed the code to make the PDF's save in the main directory I want. I just need to know how to make the code distribute them to their proper sub directories. Is it possible?
Thanks for any help, Wes.
wdemaster,
In actual practice, the way I use this is to individually tweak the file path in the macro for each file. For each project, most of the drawings are pointing the PDF's to the same place, but each project has the folder defined to a different path.
I'm not sure how to make it automatically do what you want (I'm not much of a programmer either). That doesn't mean it's not possible. The easiest way I see to do it based on my level of programming knowledge is to add the "620-" subfolder to the path in the 620- drawings. If you get one set correctly for each series of drawings, you can copy that macro to the others in the same series.
If you want to have one master, smart macro that knows where to put the files based on their names, you'll need someone more knowledgeable about the programming than I am.
Hi wdemaster,
Here is a sample iLogic rule that will take a file that resides in a main directory, and create a folder based on the file name and create a folder next to it in the main directory using the files first three charachters. This example also reads the drawing file's revision iProperty and writes it to the PDF name, but you can remove that part is not needed.
If you need help setting the iLogic up in your part, this might help:
http://inventortrenches.blogspot.com/2012/01/creating-basic-ilogic-rule-with-event.html
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Before running the ilogic:
After running the ilogic:
Inside the 620 folder:
'------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") = 1 oOptions.Value("Remove_Line_Weights") = 1 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 target folder path from the first 3 characters of the file's name and the file's path oFolder = oPath & "\" & Left(oFileName, 3) '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" 'Publish document oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) '------end of iLogic-------
I like where that I logic is going. However, it gives me an error for the oContext, and the oOptions lines. It says "End of statement expected"
Hi wdemaster,
What version of Inventor are you using? It might just be a copy paste issue caused by a line return or something. I'll post a sample file with the rule already setup up if you indicate the version. I've also attached the code in a .txt file here, in case that helps.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Hello,
I'm new here on this forum. I have copied the program to make automatic PDF files after closing my drawing.
It does work but I tried to change the directory part. I would like to save all the files in a directory called PDF that has to be created in the workspace. I have tried some things but a directory is being created but not in the right place
oNaam = "PDF"
= oPath & oNaam 'Check for the PDF folder and create it if it does not existIf Not System.IO.Directory.Exists(oFolder) Then System.IO.Directory.CreateDirectory(oFolder) End If
I hope somebody can give me some tips. One more thing. Is it possible to build in a choice: do you want to save PDF yes/no?
Thanks.
I solved my first problem with the wrong directory name. But still looking for a way to build in a yes / no option for the PDF creation.
Hi helge,
Here's a quick example.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
'query user question = MessageBox.Show("Do you want to create a PDF?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question) 'set condition based on answer If question = vbno then 'exit the rule Return Else if question = vbyes then 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") = 1 oOptions.Value("Remove_Line_Weights") = 1 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 target folder path from the first 3 characters of the file's name and the file's path oFolder = oPath & "\" & Left(oFileName, 3) '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" 'Publish document oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) End if
Hei Curtis,
Thanks for the file. Is workin great but I noticed one thing. When I'm saving a PDF file and there is already a PDF file with the same name, it doesn't overwrite but the rules stops and displays an error.
Any thought about that?
Hi guys great thanks for all the info! I also put something together along these lines. Mine had to be an Inventor add-in and its been quite a challenge. I have created several standalone Inventor apps but never an add-in before. Its certainly got its advantages being able to auto load when Inventor starts etc
Anyway I like the idea of creating PDF file names with every new Inventor revision. I think its something I will add to my add-in as well. Anyway I have a video and blog about it at http://mgfx.co.za/nutsandbolts/?p=31 if you are interested.
Cheers
Pieter
HI Helge,
I have found this VBA code that checks if a file already exists. it uses a string parameter that is the name of the file you want to check if it exists and then it returns true or false accordingly. maybe you can implement it by checking first if the pdf file name alrady exists and if it is true, then you could either delete it, use another name or promt for a new name.
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean 'Purpose: Return True if the file exists, even if it is hidden. 'Arguments: strFile: File name to look for. Current directory searched if no path included. ' bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True. 'Note: Does not look inside subdirectories for the file. 'Author: Allen Browne. http://allenbrowne.com June, 2006. Dim lngAttributes As Long 'Include read-only files, hidden files, system files. lngAttributes = (vbReadOnly Or vbHidden Or vbSystem) If bFindFolders Then lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well. Else 'Strip any trailing slash, so Dir does not look inside the folder. Do While Right$(strFile, 1) = "\" strFile = Left$(strFile, Len(strFile) - 1) Loop End If 'If Dir() returns something, the file exists. On Error Resume Next FileExists = (Len(Dir(strFile, lngAttributes)) > 0) End Function
Mario.
i am having problem with this macro. i added in my ilogic browser and it giving me error
"all other sub's or funstion's must be after submain()"
Post what you have in your rule browser.
Edit: I mean the contents of the rule, not the "iLogic Browser".
Which line of code dictates where it saves? i have a separate pdf folder from my drawings i would like to save it in.
oPath = ThisDoc.Path
Change the ThisDoc.Path to whatever you want. Make sure you put any hard coded paths in "".
Example:
oPath = "C:\PDF\"