Hello!
My question: We want to use iLogic to save a PDF-file of the DWG-drawing every time we hit "save". We now use the following iLogic code:
Question 1: Is it possible to save the PDF in a new folder that lies "one step up". For example: My DWG-file is placed in c:/drawings/thing4/DWG, I want the PDF to end up in c:/drawings/thing4/PDF. If I am in folder "c:/drawings/thing2/DWG" I want the PDF to end up in "c:/drawings/thing2/PDF" etc. Possible?
Question 2: My DWG files is named "thing4.dwg" and it is the 2nd revision. I want the PDF-file to be named
"thing4 Ed2.pdf". Possible?
Any help would be very appreciated!
/Gustav
Solved! Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
This is the code:
'------- start of iLogic code ------------------- ' Get the DWG translator Add-In. Dim DWGAddIn As TranslatorAddIn DWGAddIn = ThisApplication.ApplicationAddIns.ItemById _ ("{C24E3AC4-122E-11D5-8E91-0010B541CD80}") 'Set a reference to the active document Dim oDocument As Document oDocument = ThisApplication.ActiveDocument Dim oContext As TranslationContext oContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism ' Create a NameValueMap object Dim oOptions As NameValueMap oOptions = ThisApplication.TransientObjects.CreateNameValueMap ' Create a DataMedium object Dim oDataMedium As DataMedium oDataMedium = ThisApplication.TransientObjects.CreateDataMedium ' Check whether the translator has 'SaveCopyAs' options If DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then ' DWG version. ' 23 = ACAD 2000 ' 25 = ACAD 2004 ' 27 = ACAD 2007 ' 29 = ACAD 2010 oOptions.Value("DwgVersion") = 25 Dim strIniFile As String strIniFile = "C:\temp\DWGout.ini" ' Create the name-value that specifies the ini file to use. oOptions.Value("Export_Acad_IniFile") = strIniFile End If 'get path and file name path_and_name = ThisDoc.PathAndFileName(False) ' without extension 'Set the DWG target file name oDataMedium.FileName = path_and_name & ".DWG" DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) '------- end of iLogic code -------------------
Not sure. Your code works here. I copied and paste your code into a new rule in an existing IDW and it generated the exported DWG just fine. My suggestion to you then is try debugging this code by placing in messageboxes at different locations in the code. Then see which messagebox does not display. This way you can narrow down the search to the line of code that's causing this error.
I am putting the code to a inventor.dwg drawing not .idw, can that be the problem?
Off topic, i dont really get it why i should use idw when i can use dwg.
Now that you say that, it actually is and is not the problem. 🙂 The code is written to use the file name of the current file as the exported DWG. Well since the current file is already a DWG, it's is going to try and overwrite itself with the exported DWG. that's why there's that error. To correct this, use a different name for the exported dwg. Maybe use this line of code instead when defining the filename of the exported dwg.
oDataMedium.FileName = path_and_name & "-Exported.DWG"
Hope that helps.
Dan
Curtis, it is great how you use ilogic and i used your code to save each sheet as a single PDF into the same folder that the DWG/IDW resides in.
But i want to create a folder calded "PDF" in the same folder where the DWG resides in and put in de pfd's.
I hope you can help me out.
Thanks, Jos
I used this code.
'------start of iLogic------- oPath = ThisDoc.Path oFileName = ThisDoc.FileName(False) 'without extension 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 'Define the drawing Dim oDrawing As DrawingDocument oDrawing = ThisDoc.Document Dim oSheet As Sheet Dim lPos As Long Dim rPos As Long Dim sLen As Long Dim sSheetName As String Dim iSheetNumber As Integer 'step through each drawing sheet For Each oSheet In oDrawing.Sheets 'find the seperator in the sheet name:number lPos = InStr(oSheet.Name, ":") 'find the number of characters in the sheet name sLen = Len(oSheet.Name) 'find the sheet name sSheetName = Left(oSheet.Name, lPos -1) 'find the sheet number sSheetNumber = Right(oSheet.Name, sLen -lPos) 'set PDF Options 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.kPrintSheetRange oOptions.Value("Custom_Begin_Sheet") = sSheetNumber oOptions.Value("Custom_End_Sheet") = sSheetNumber End If 'Set the PDF target file name oDataMedium.FileName = oPath & "\" & oFileName & "_" & sSheetName & sSheetNumber & ".pdf" 'Publish document oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) Next '------end of iLogic-------
I also wanted to use this:
'get PDF target folder path
oFolder = Left(oPath, InStrRev(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
Then the folder 'PDF' is made but the pdf files don't get in that folder.
I have now the following code.
But i have a problem.
It now makes the new folder and it puts in the pdf with the good name.
But every pdf is saved with the first sheet.
So the second pdf has the good name but the wrong sheet saved.
'------start of iLogic-------
'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 & "\PDF"
oFileName = ThisDoc.FileName(False) 'without extension
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
End If
'Define the drawing
Dim oDrawing As DrawingDocument
oDrawing = ThisDoc.Document
Dim oSheet As Sheet
Dim lPos As Long
Dim rPos As Long
Dim sLen As Long
Dim sSheetName As String
Dim iSheetNumber As Integer
'step through each drawing sheet
For Each oSheet In oDrawing.Sheets
'find the seperator in the sheet name:number
lPos = InStr(oSheet.Name, ":")
'find the number of characters in the sheet name
sLen = Len(oSheet.Name)
'find the sheet name
sSheetName = Left(oSheet.Name, lPos -1)
'find the sheet number
sSheetNumber = Right(oSheet.Name, sLen -lPos)
'set PDF Options
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.kPrintSheetRange
oOptions.Value("Custom_Begin_Sheet") = sSheetNumber
oOptions.Value("Custom_End_Sheet") = sSheetNumber
End If
'get PDF target folder path
oFolder = Left(oPath, InStrRev(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 = oPath & "\" & oFileName & "_" & sSheetName & ".pdf"
'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Next
'------end of iLogic-------
I also have this code where i want to save all sheets in one pdf and placed in the PDF folder in the same folder where the DWG is.
I get the sheets saved in one pdf and i get the PDF folder but the pdf want get into the PDF folder.
It gets in the folder where the DWG is and where the PDF folder is made.
'------start of iLogic-------
'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 & "\PDF"
oFileName = ThisDoc.FileName(False) 'without extension
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
End If
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 PDF target folder path
oFolder = Left(oPath, InStrRev(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 destination file name
oDataMedium.FileName = oFilename & ".pdf"
'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions,oDataMedium)
'------end of iLogic-------
Hi jostroopers,
If you look at the example at this link:
http://inventortrenches.blogspot.com/2011/07/ilogic-to-save-pdf-files-to-new.html
You will see that the oDataMedium line include the oFolder variable:
In your examples the oFolder variable is not included:
oDataMedium.FileName = oFilename & ".pdf"
oDataMedium.FileName = oPath & "\" & oFileName & "_" & sSheetName & ".pdf"
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
I changed the code but it stil dusn't work.
It now saves the idw into a pdf in the PDF folder and the names of the pdf are good.
But each pdf is saved with the first sheet of the idw.
I used the Example Drawing where in made 3 sheets.
I now have this code:
'------start of iLogic-------
'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 & "\PDF"
oFileName = ThisDoc.FileName(False) 'without extension
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
End If
'Define the drawing
Dim oDrawing As DrawingDocument
oDrawing = ThisDoc.Document
Dim oSheet As Sheet
Dim lPos As Long
Dim rPos As Long
Dim sLen As Long
Dim sSheetName As String
Dim iSheetNumber As Integer
'step through each drawing sheet
For Each oSheet In oDrawing.Sheets
'find the seperator in the sheet name:number
lPos = InStr(oSheet.Name, ":")
'find the number of characters in the sheet name
sLen = Len(oSheet.Name)
'find the sheet name
sSheetName = Left(oSheet.Name, lPos -1)
'find the sheet number
sSheetNumber = Right(oSheet.Name, sLen -lPos)
'set PDF Options
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.kPrintSheetRange
oOptions.Value("Custom_Begin_Sheet") = sSheetNumber
oOptions.Value("Custom_End_Sheet") = sSheetNumber
End If
'get PDF target folder path
oFolder = Left(oPath, InStrRev(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 & " " & sSheetName & " " & sSheetNumber & ".pdf"
'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Next
'------end of iLogic-------
Hi jostroopers,
Okay, I found the mistake in this code, it had to do with the use of
sSheetNumber rather than iSheetNumber
The code below should work.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
'------start of iLogic------- '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 & "\PDF" oFileName = ThisDoc.FileName(False) 'without extension 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 End If 'Define the drawing Dim oDrawing As DrawingDocument oDrawing = ThisDoc.Document Dim oSheet As Sheet Dim lPos As Long Dim rPos As Long Dim sLen As Long Dim sSheetName As String Dim iSheetNumber As Integer 'step through each drawing sheet For Each oSheet In oDrawing.Sheets 'find the seperator in the sheet name:number lPos = InStr(oSheet.Name, ":") 'find the number of characters in the sheet name sLen = Len(oSheet.Name) 'find the sheet name sSheetName = Left(oSheet.Name, lPos -1) 'find the sheet number iSheetNumber = Right(oSheet.Name, sLen -lPos) 'set PDF Options 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.kPrintSheetRange oOptions.Value("Custom_Begin_Sheet") = iSheetNumber oOptions.Value("Custom_End_Sheet") = iSheetNumber End If 'get PDF target folder path oFolder = Left(oPath, InStrRev(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 & " " & sSheetName & " " & iSheetNumber & ".pdf" 'Publish document oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) Next '------end of iLogic-------
Curtis, thanks very much.
The code is working just great.
Btw did you change your code on your blog because i had the same problem but i tried your code again and now that code is working?
But anyway, Thanks.
Hi, I'm Max from Italy!
Can anyone reply to my problem?
I've tried to paste the following code
SyntaxEditor Code Snippet
oPath = ThisDoc.Path oFileName = ThisDoc.FileName(False) 'without extension 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 'Define the drawing Dim oDrawing As DrawingDocument oDrawing = ThisDoc.Document Dim oSheet As Sheet Dim lPos As Long Dim rPos As Long Dim sLen As Long Dim sSheetName As String Dim iSheetNumber As Integer 'step through each drawing sheet For Each oSheet In oDrawing.Sheets 'find the seperator in the sheet name:number lPos = InStr(oSheet.Name, ":") 'find the number of characters in the sheet name sLen = Len(oSheet.Name) 'find the sheet name sSheetName = Left(oSheet.Name, lPos -1) 'find the sheet number sSheetNumber = Right(oSheet.Name, sLen -lPos) 'set PDF Options 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.kPrintSheetRange oOptions.Value("Custom_Begin_Sheet") = sSheetNumber oOptions.Value("Custom_End_Sheet") = sSheetNumber End If 'Set the PDF target file name oDataMedium.FileName = oPath & "\" & oFileName & "_" & sSheetName & sSheetNumber & ".pdf" 'Publish document oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) Next
but when I start the rule there are 2 differrent sheet with the pdf printed of the first sheet; insted I'd like to obtain 2 pdf with two different sheet!
Kind regard
This is great, I love having pdf's saved automatically so other people in the office can view drawings!
I would like to add code to turn off a layer before creating the pdf and then turn it back on again.
There's a layer I do not want on the pdf.
That information must not be on the pdf's so if it get's emailed to outside companies, they won't see who our client is.
Can't find what you're looking for? Ask the community or share your knowledge.