- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Exporting Inventor drawing to specified location
I am trying to figure out if there is a way to export a pdf from a drawing to a selected location. (Have a prompt or window appear to allow you to select that location when you run the logic code).
I have found a ton of codes that send it to a specific location that if in the file path but nothing to a selected folder.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @brandon.lee97. Below is an example of a custom Function that you can include in your rule...after the Sub Main...your other code here...End Sub routine, then you can call this function to run from within your Sub Main area of code. But this example also includes an example Sub Main routine, just to show you how to use it, and what it returns. Most paths you get will not contain the final "\" character at the end, so keep that in mind when assembling a full file name using that returned path.
Sub Main
Dim sFolder As String = SelectFolder
MsgBox(sFolder,,"iLogic")
End Sub
Function SelectFolder(Optional oPrompt As String = vbNullString) As String
If oPrompt = "" Then oPrompt = "Select Folder"
Dim oFDialog As New System.Windows.Forms.FolderBrowserDialog
oFDialog.Description = oPrompt
oFDialog.ShowNewFolderButton = True
oFDialog.RootFolder = System.Environment.SpecialFolder.MyComputer
Dim oResult As System.Windows.Forms.DialogResult = oFDialog.ShowDialog()
If oResult = System.Windows.Forms.DialogResult.OK Then
Return oFDialog.SelectedPath
Else 'if dialog was canceled or something else
Return vbNullString
End If
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
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
HI Wesley,
Thanks for this, i am having a little trouble integrating this into the other code.
I am also wondering if there is a way to have the pop up you had written start at a specific location.
For example, have it pop up already in
G:\Dropbox\PROJECTS WIP
I have written the code below that i have been using below
Sub Main()
Dim myDate As String = Now().ToString("yyyy-MM-dd HHmmss")
myDate = myDate.Replace(":","") ' & " - " & TypeString
userChoice = InputRadioBox("Defined the scope", "This Document", "All Open Documents", True, Title := "Defined the scope")
UserSelectedActionList = New String(){"DXF & PDF", "PDF Only", "DXF Only"}
UserSelectedAction = InputListBox("What action must be performed with selected views?", _
UserSelectedActionList, UserSelectedActionList(0), Title := "Action to Perform", ListName := "Options")
Select UserSelectedAction
Case "DXF & PDF": UserSelectedAction = 3
Case "PDF Only": UserSelectedAction = 1
Case "DXF Only": UserSelectedAction = 2
End Select
If userChoice Then
Call MakePDFFromDoc(ThisApplication.ActiveDocument, myDate, UserSelectedAction)
Else
For Each oDoc In ThisApplication.Documents
If oDoc.DocumentType = kDrawingDocumentObject
Try
If Len(oDoc.File.FullFileName)>0 Then
Call MakePDFFromDoc(oDoc, myDate, UserSelectedAction)
End If
Catch
End Try
End If
Next
End If
End Sub
Sub MakePDFFromDoc(ByRef oDocument As Document, DateString As String, UserSelectedAction As Integer)
' oPath = oDocument.Path
' oFileName = oDocument.FileName(False) 'without extension
'oDocument = ThisApplication.ActiveDocument
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
oFullFileName = oDocument.File.FullFileName
oPath = Left(oFullFileName, InStrRev(oFullFileName, "\")-1)
oFileName = Right(oFullFileName, Len(oFullFileName)-InStrRev(oFullFileName, "\"))
oFilePart = Left(oFileName, InStrRev(oFileName, ".")-1)
'oRevNum = oDocument.iProperties.Value("Project", "Revision Number")
'oDocument = ThisApplication.ActiveDocument
' 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 = Left(oPath, InStrRev(oPath, "\")) & "PDF"
oFolder = oPath & "\iLogic PDF's (" & DateString & ")"
'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 & "\" & oFilePart & ".pdf"
'Publish document
If (UserSelectedAction = 1) Or (UserSelectedAction = 3) Then
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)'For PDF's
End If
If (UserSelectedAction = 2) Or (UserSelectedAction = 3) Then
oDocument.SaveAs(oFolder & "\" & oFilePart & ".dxf", True) 'For DXF's
End If
'oDocument.SaveAs(oFolder & "\" & ThisDoc.ChangeExtension(".dxf"), True) 'For DXF's
'------end of iLogic-------
End Sub
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Looking at documentation for FolderBrowserDialog you can set the property RootFolder or SelectedPath prior to ShowDialog() to limit and affect what the dialog will allow you to choose.
I'd recommend adding the SelectFolder function to your script, and modify the function signature for MakePDFFromDoc so you can pass a path or filename into it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Sorry for the delay, but I have a lot going on today at work. In the following example, I basically just added the SelectFolder Function as an additional routine, similar to the previous example, but that function is now being called to run from within your existing MakePDFFromDoc Sub routine. I usually pass the full file name of the file that I want to export to the routine that will be doing the exporting, making it more robust for use in multiple scenarios. You are currently passing just the 'DateString' to your custom routine, but you could have transferred most, if not all of the code for assembling the full file name of the PDF from that custom routine, into your 'Main' routine, then just pass the full file name of the PDF to that custom routine. You could also call the SelectFolder routine to run from the 'Main' routine somewhere before you call your other routine to run, then pass the folder / path data to that routine. Since I am not sure what all your design intent is for this, I am not sure if you want to manually select a folder using the SelectFolder routine, then still add the "\iLogic PDF's ( DateString )" text after that point, or if you want to handle that part while you have the dialog open, then skip that step in the code. If doing it all in the dialog, you should no longer need to check if the directory exists.
I did customize the SelectFolder routine a bit more for you, to make it easier to specify a starting folder. When setting the RootFolder, you limit how far up you can browse within the dialog, so I left that line of code out in this version. Instead I added an extra optional input variable (sStartFrom) to the routine, allowing you an easier way to specify that starting path. Then within the routine, it uses that to set the SelectedPath property, which will cause that path expanded and selected when the dialog opens. If a simple folder browser is too basic, and you want to step it up a notch, we could instead use an Inventor.FileDialog, then set its settings similarly, then use its ShowSave method, which will show the same dialog you see when using SaveAs. But it will not actually save the file, just return the final full path and file name that you specify to the code that called it to run. Then you can use that later in the code. That online help page contains a link to a VBA (not iLogic) sample that uses one.
Sub Main()
Dim myDate As String = Now().ToString("yyyy-MM-dd HHmmss")
myDate = myDate.Replace(":","") ' & " - " & TypeString
userChoice = InputRadioBox("Defined the scope", "This Document", "All Open Documents", True, Title := "Defined the scope")
UserSelectedActionList = New String(){"DXF & PDF", "PDF Only", "DXF Only"}
UserSelectedAction = InputListBox("What action must be performed with selected views?", _
UserSelectedActionList, UserSelectedActionList(0), Title := "Action to Perform", ListName := "Options")
Select UserSelectedAction
Case "DXF & PDF" : UserSelectedAction = 3
Case "PDF Only" : UserSelectedAction = 1
Case "DXF Only" : UserSelectedAction = 2
End Select
If userChoice Then
MakePDFFromDoc(ThisApplication.ActiveDocument, myDate, UserSelectedAction)
Else
Dim oDocs As Documents = ThisApplication.Documents
For Each oDoc As Document In oDocs
If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject
Try
If Len(oDoc.File.FullFileName) > 0 Then
MakePDFFromDoc(oDoc, myDate, UserSelectedAction)
End If
Catch
End Try
End If
Next
End If
End Sub
Sub MakePDFFromDoc(ByRef oDocument As Document, DateString As String, UserSelectedAction As Integer)
Dim oPDFAddIn As TranslatorAddIn = 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
'<<< calling the other custom Function to run in next line >>>
Dim oPath As String = SelectFolder("Select Folder", "G:\Dropbox\PROJECTS WIP")
'checking to make sure it returned something, if not, exit this routine
If String.IsNullOrEmpty(oPath) Then Return 'if no path was returned, exit routine
Dim oFilePart As String = System.IO.Path.GetFileNameWithoutExtension(oDocument.FullFileName)
'Dim oRevNum As String = oDocument.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
'get PDF target folder path
Dim oFolder As String = oPath & "\iLogic PDF's (" & DateString & ")"
'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 & "\" & oFilePart & ".pdf"
If oPDFAddIn.HasSaveCopyAsOptions(oDocument, 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
'Publish document
If (UserSelectedAction = 1) Or (UserSelectedAction = 3) Then
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)'For PDF's
End If
If (UserSelectedAction = 2) Or (UserSelectedAction = 3) Then
oDocument.SaveAs(oFolder & "\" & oFilePart & ".dxf", True) 'For DXF's
End If
End If
End Sub
Function SelectFolder(Optional sPrompt As String = vbNullString, Optional sStartFrom As String = vbNullString) As String
If String.IsNullOrEmpty(oPrompt) Then sPrompt = "Select Folder"
If String.IsNullOrEmpty(sStartFrom) Then sStartFrom = "C:\"
Dim oFDialog As New System.Windows.Forms.FolderBrowserDialog
oFDialog.Description = sPrompt
oFDialog.ShowNewFolderButton = True
oFDialog.SelectedPath = sStartFrom
Dim oResult As System.Windows.Forms.DialogResult = oFDialog.ShowDialog()
If oResult = System.Windows.Forms.DialogResult.OK Then
Return oFDialog.SelectedPath
Else 'if dialog was canceled or something else
Return vbNullString
End If
End Function
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
No Worries, i appreciate the help. This works but i realized my intent was to select the folder and have the document save in that folder. And not create a folder with the Ilogic (Datestring). I will have to try and edit it to remove that and just save the document to the selected folder.