Automated PDF export question

Automated PDF export question

machiel.veldkamp
Collaborator Collaborator
1,390 Views
13 Replies
Message 1 of 14

Automated PDF export question

machiel.veldkamp
Collaborator
Collaborator

Hi guys, 

I'm trying to improve our workflow a bit and I had a couple of Ideas

 

1 - I want to let the user pick the project they are working on and let iLogic place the PDF's in subfolders in that project. 

2 - based on the filename (X-YYPZZZZ-001.dwg) I want iLogic to find the correct folder (we have folders with numbers in them)
3 - no user interaction; just press the button and iLogic will find out - based on the filenumber - where the drawing should be saved to. 

 

 

So! I'm currently working on no. 1

 

I'll paste the whole code below but what I'm struggeling with at the moment is the point after I chose where to save the file. 

 

'Ask which project the User uses
	oQuestion2 = InputListBox("Choose where", ProjectChoice, ProjectChoice, Title := "KIES PROJECT", ListName := "ProjectChooser")

the relevant piece of code is this: 

 

'Choose Preselected Folder
	Dim oChoice
	Dim ProjectChoice As New ArrayList
	ProjectChoice.Add("Project 1")
	ProjectChoice.Add("Project 2")
	
	oFolder = "G:\folder\"& ProjectChoice(oChoice) &"\folder\"

	Dim Proc As String = "Explorer.exe"
	Dim Args As String = ControlChars.Quote & IO.Path.Combine("G:\", oFolder) & ControlChars.Quote

no matter what I choose, it will always save on the (0) value in the arraylist. 

 

Any advise? 

 

Here is the complete code I've written.

 

 

Public Sub Main()
	Dim oPath = ThisDoc.Path
	oFileName = ThisDoc.FileName(False) 'without extension
	oDuctName = iProperties.Value("Project", "Description")
	oRevNum = iProperties.Value("Project", "Revision Number")
	oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	Dim oDWGAddIn As TranslatorAddIn
	oDWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
	oDrafstman = ThisApplication.UserName
	oDocument = ThisApplication.ActiveDocument
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMediumPDF = ThisApplication.TransientObjects.CreateDataMedium
	oDataMediumDWG = ThisApplication.TransientObjects.CreateDataMedium

	'Choose Preselected Folder
	Dim oChoice
	Dim ProjectChoice As New ArrayList
	ProjectChoice.Add("Project 1")
	ProjectChoice.Add("Project 2")
	
	oFolder = "G:\COMPANYNAME\orders\2016\Projecten\"& ProjectChoice(oChoice) &"\Tekeningen Engineering\2D Tekeningen\"

	Dim Proc As String = "Explorer.exe"
	Dim Args As String = ControlChars.Quote & IO.Path.Combine("G:\", oFolder) & ControlChars.Quote

	'PDF Setup
	If oPDFAddIn.HasSaveCopyAsOptions(oDataMediumPDF, oContext, oOptions) Then
		oOptions.Value("All_Color_AS_Black") = 0
		oOptions.Value("Remove_Line_Weights") = 1
		oOptions.Value("Vector_Resolution") = 500
		oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	End If
	
	' DWG SETUP - Check whether the translator has 'SaveCopyAs' options
	If oDWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
		Dim strIniFile As String
		strIniFile = "G:\folder\INVENTOR\iLogic\Save as DWG aparte sheets.ini"
		' Create the name-value that specifies the ini file to use.
		oOptions.Value("Export_Acad_IniFile") = strIniFile
	End If
	
	'get  target folder path
	If Not System.IO.Directory.Exists(oFolder) Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If
	
		'Set the destination file name
	Dim fileName As String
	fileName = ThisDoc.FileName(False) & " - " & oDuctName & " - REV " & iProperties.Value("Project", "Revision Number")
	oDataMediumDWG.FileName = oFolder & fileName  & ".dwg"
	oDataMediumPDF.FileName = oFolder & fileName  & ".pdf"
	
	'Ask which project the User uses
	oQuestion2 = InputListBox("KIES HET PROJECT WAAR DE PDF/DWG MOET WORDEN OPGESLAGEN", ProjectChoice, ProjectChoice, Title := "KIES PROJECT", ListName := "ProjectChooser")
	
	'Ask the User if he wants PDF or PDF+DWG
	oQuestion =  MessageBox.Show("For PDF select YES         for PDF+DWG select NO", "PDF or PDF/DWG", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
	
	'Publish document.
	If oQuestion = vbYes Then
		oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMediumPDF)
		Else If oQuestion = vbNo Then
			oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMediumPDF)
			oDWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMediumDWG)
			Else
			'MessageBox.Show("Canceled", "We're doing nothing now...")
	End If
		
	If oQuestion = vbYes Or oQuestion = vbNo Then
		OpenFolder=  MessageBox.Show("Would you open the folder?", "PDF EXPORTED", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
		'open the folder where the new files are saved
		If OpenFolder = vbYes Then
			Process.Start(Proc, Args)
			Else
			'Return
		End If
		Else
	End If 

End Sub

	
	

 

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

___________________________
0 Likes
Accepted solutions (1)
1,391 Views
13 Replies
Replies (13)
Message 2 of 14

Ktomberlin
Advocate
Advocate
Accepted solution

I believe its not working because you are setting oFolder before you ask the question and not resetting it based on your answer to Question2.  If you move the question2 up, it works in my testing.  Hope this helps.

 

Now for the second part I need a little more information on the file names to fully understand what your looking for, but you'll most likely need to use something like this to get the folder location calculated.

 

Dim PDF_Folder As String
Dim F_Finder As String
If iProperties.Value("Project", "Project")= NullString
    Goto CalculateIt
    Else
        F_Finder = (Mid(oFileName,2,3))
        PDF_Folder = (Left(iProperties.Value("Project", "Project"),4))
'Choose Preselected Folder
    Dim oChoice
    Dim ProjectChoice As New ArrayList
    ProjectChoice.Add("Project 1")
    ProjectChoice.Add("Project 2")
    
    oQuestion2 = InputListBox("KIES HET PROJECT WAAR DE PDF/DWG MOET WORDEN OPGESLAGEN", ProjectChoice, ProjectChoice, Title := "KIES PROJECT", ListName := "ProjectChooser")
    oFolder = "C:\Temp\"& oQuestion2 &"\Tekeningen Engineering\2D Tekeningen\"
    
    'oFolder = "C:\Temp\"& ProjectChoice(oChoice) &"\Tekeningen Engineering\2D Tekeningen\"

 

 

0 Likes
Message 3 of 14

machiel.veldkamp
Collaborator
Collaborator
This is great! Thank you for the help. I didn't think about the order before so thanks for that.

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

___________________________
0 Likes
Message 4 of 14

machiel.veldkamp
Collaborator
Collaborator

Hi @Ktomberlin

 

 

 

Just to finnish the topic: 

 

SyntaxEditor Code Snippet

    '''''''''''''''''''''''
    'Beginning of the Code'
    '''''''''''''''''''''''
    Dim FolderNameLoc As String
    
    'Declare projects in the 2016 Projects Folder
    Dim oChoice
    Dim ProjectChoice As New ArrayList
    ProjectChoice.Add("HOME") 'Drawings will be saved on a seperate location
    ProjectChoice.Add("ProjectExample1")
    ProjectChoice.Add("ProjectExample2")
    
    oQuestion2 = InputListBox("Project specification - in doubt select HOME", ProjectChoice, ProjectChoice, Title := "CHOOSE PROJECT", ListName := "ProjectChooser")
    
    FolderNameLoc = Mid(oPath, InStrRev(oPath , "\"))
    
    If oQuestion2 = "HOME"
        oFolder = "G:\CompanyName\INVENTOR\Save As PDF DWG location\" & oDrafstman & "\"
        Else
            oFolder = "G:\CompanyName\orders\2016\Projecten\"& oQuestion2 &"\Tekeningen Engineering\2D Tekeningen"& FolderNameLoc & "\"
    End If
            
    ''''''''''

 This is the relevant piece of code. 

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

___________________________
0 Likes
Message 5 of 14

Anonymous
Not applicable

Can this be used for printing all sheets as individual PDFs?

0 Likes
Message 6 of 14

Anonymous
Not applicable

This is what I have:

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 38787 StartFragment: 314 EndFragment: 38755 StartSelection: 314 EndSelection: 314SyntaxEditor Code Snippet

Sub Main
    
    Dim IPJ as String
    Dim IPJ_Name As String
    Dim IPJ_Path As String
    Dim FNamePos As Long
    'set a reference to the FileLocations object. 
    IPJ = ThisApplication.FileLocations.FileLocationsFile
    'get the location of the last backslash seperator 
    FNamePos = InStrRev(IPJ, "\", -1)     
    'get the project file name with the file extension
    IPJ_Name = Right(IPJ, Len(IPJ) - FNamePos) 
    'get the project name (without extension)
    IPJ_ShortName = Left(IPJ_Name, Len(IPJ_Name) - 4)
    'get the path of the folder containing the project file
    IPJ_Folder_Location = Left(IPJ, Len(IPJ) - Len(IPJ_Name))


    MessageBox.Show("Would you like to print to the current Project Folder?" & vbLf & "Project Name:" & IPJ_Name _
    & vbLf & "Project Path:" & IPJ_Folder_Location _ 
     ,"iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
     
    Dim i As Integer
    iLogicVb.RunRule("CustOn")
    oFileName = ThisDoc.FileName(False) 'without extension
    oRevNum = iProperties.Value("Project", "Revision Number")
    oFolder = Left(ThisDoc.Path, 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
    
    Dim fname As String
    Dim PrintedSheets as String
        For i=1 To ThisDrawing.Document.sheets.count
            ActiveSheet = ThisDrawing.Sheet(ThisDrawing.Document.Sheets(i).Name)
            fname = oFileName & "-sht"& i  & "-Rev" & oRevNum & " .pdf"
            printedSheets = fname & vbCrLf & printedSheets
            printsheet (oFolder,fname)        
        Next i
        'iProperties.Value("Custom", "2Shop ShtTotal")= Chr(i + 64)
    MessageBox.Show("Print Created in default Projects -  " &  oFolder & " \" &  vbCrLf & vbCrLf & printedSheets , "Inventor")
        
End Sub




Function PrintSheet(sheetPath As String, sheetName As String  )


    ' Get the PDF translator Add-In.
    Dim PDFAddIn As TranslatorAddIn
    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
    oDocument = ThisApplication.ActiveDocument
     Dim oContext As TranslationContext
    oContext =  ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = 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 PDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
        
        ' Options for drawings...
        'oOptions.Value("Launch_Viewer") = launchviewer
        oOptions.Value("All_Color_AS_Black") = 1
        'oOptions.Value("Sheet_Range") = ThisApplication.PrintRangeEnum.kPrintAllSheets
        oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet
        
        'oOptions.Value("Remove_Line_Weights") = 0
        'oOptions.Value("Vector_Resolution") = 400
        
        'oOptions.Value("Custom_Begin_Sheet") = 2
        'oOptions.Value("Custom_End_Sheet") = 4
    End If
    'get PDF target folder path
    
     'Set the PDF target file name
    
    oDataMedium.FileName = sheetPath & "\" & sheetName 
    'Publish document.
    If  System.IO.Directory.Exists( sheetPath & "\" & sheetName ) Then
        System.IO.File.Delete( sheetPath & "\" & sheetName )
        MessageBox.Show("Earlier PDF deleted! ", "Inventor")
    End If
    PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)


End Function

 

 

I want the message box to control where the PDFs go. So if yes it goes to the project file, and if no you can manually map it to any folder like a "Save As" command 

0 Likes
Message 7 of 14

machiel.veldkamp
Collaborator
Collaborator
Yeah I have that bit of code at work. The code shown at this thread and att the other I just posted lack THAT tiny bit of code.

I'm not behind a powerfull PC with a working version of Inventor so best I can do is send you that first thing monday. (I'm in europe)

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

___________________________
0 Likes
Message 8 of 14

machiel.veldkamp
Collaborator
Collaborator

SyntaxEditor Code Snippet

Public Sub Main()
    Dim oPath = ThisDoc.Path
    Dim oFilePath As String
    Dim dialog As New FolderBrowserDialog()
    oFileName = ThisDoc.FileName(False) 'without extension
    oDuctName = iProperties.Value("Project", "Description")
    oRevNum = iProperties.Value("Project", "Revision Number")
    oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
    Dim oDWGAddIn As TranslatorAddIn
    oDWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
    oDrafstman = ThisApplication.UserName
    oDocument = ThisApplication.ActiveDocument
    oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
    oOptions = ThisApplication.TransientObjects.CreateNameValueMap
    oDataMediumPDF = ThisApplication.TransientObjects.CreateDataMedium
    oDataMediumDWG = ThisApplication.TransientObjects.CreateDataMedium
    Dim FolderNameLoc As String
    Dim ProjYear As String
    
    
    Try 'Get FolderName for current file
        FolderNameLoc = Mid(oPath, InStrRev(oPath , "\"))
        Catch
            MessageBox.Show("Save the Document First","Error" ,MessageBoxButtons.OK, MessageBoxIcon.STOP)
            Exit Sub
    End Try
    
        'Declare projects in the 2016 Projects Folder
    Dim ProjectChoice As New ArrayList
        ProjectChoice.Add("HOME") 'Drawings will be saved on a seperate location
        ProjectChoice.Add("CUSTOM") 'Choose where to save files
            'Custom Folders from here on.
        ProjectChoice.Add("Custom Folder 1")
        ProjectChoice.Add("Custom Folder 2")
        ProjectChoice.Add("Custom Folder 3")
        ProjectChoice.Add("Custom Folder 4")
        
        
        
    ProjectChooser = InputListBox("Project specification - in doubt select HOME", ProjectChoice, ProjectChoice, Title := "CHOOSE PROJECT", ListName := "ProjectChooser")
        
    If ProjectChooser = "HOME" Then
        oFolder = "G:\TEMP\" & oDrafstman & "\"
        ElseIf ProjectChooser = "CUSTOM" Then
            If dialog.ShowDialog() = dialogresult.ok
                oFolder = dialog.SelectedPath & "\"        
                Else
                    Exit Sub
            End If 
        
        Else 'Choosing a Project
            Try 
                ProjYear = Left(ProjectChooser,2) ' Read the ProjectYear (valid until the year 2100) 
            Catch 
                MessageBox.Show("Wrong Project. Ask ADMIN to fix the Code", "ERROR #ProjYear")

            End Try    
            oFolder = "C:\TEMP\20" & ProjYear & "\Projecten\"& ProjectChooser &"\Tekeningen Engineering\2D Tekeningen"& FolderNameLoc & "\"
    End If
        
    Dim Proc As String = "Explorer.exe"
    Dim Args As String = ControlChars.Quote & oFolder & ControlChars.Quote 
    
        'PDF Setup
    If oPDFAddIn.HasSaveCopyAsOptions(oDataMediumPDF, oContext, oOptions) Then
        oOptions.Value("All_Color_AS_Black") = 0 '0 = color // 1 = black
        oOptions.Value("Remove_Line_Weights") = 1 'boolean
        oOptions.Value("Vector_Resolution") = 500 
        oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
    End If
    
        ' DWG SETUP - Check whether the translator has 'SaveCopyAs' options
    If oDWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
        Dim strIniFile As String
        strIniFile = "G:\TEMP\Save as DWG aparte sheets.ini" 
        ' Create the name-value that specifies the ini file to use.
        oOptions.Value("Export_Acad_IniFile") = strIniFile
    End If
    
        'get  target folder path
    If Not System.IO.Directory.Exists(oFolder) Then
        System.IO.Directory.CreateDirectory(oFolder)
    End If
    
        'Set the destination file name
    Dim fileName As String
    fileName = ThisDoc.FileName(False) & " - " & oDuctName & " - REV " & oRevNum
    oDataMediumDWG.FileName = oFolder & fileName  & ".dwg"
    oDataMediumPDF.FileName = oFolder & fileName  & ".pdf"

        'Ask the User if he wants PDF or PDF+DWG
    oQuestion =  MessageBox.Show("For PDF select YES         for PDF+DWG select NO", "PDF or PDF/DWG", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
    
        'Publish document.
    If oQuestion = vbYes Then
        oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMediumPDF)
        Else If oQuestion = vbNo Then
            oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMediumPDF)
            oDWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMediumDWG)
            Else
    End If
        
        'open the folder where the new files are saved
    If oQuestion = vbYes Or oQuestion = vbNo Then
        OpenFolder =  MessageBox.Show("Would you open the folder? Files saved in:       " & oFolder, "PDF EXPORTED", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If OpenFolder = vbYes Then
            Process.Start(Proc, Args)
            Else
        End If
        Else
    End If 

End Sub


    
    
    
    

alright. so as promised here is the finished code as we are using it right now. I edited some project specific locations to C:\TEMP

 

 

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

___________________________
0 Likes
Message 9 of 14

Anonymous
Not applicable

So using this can I have it mapped per project file?

0 Likes
Message 10 of 14

machiel.veldkamp
Collaborator
Collaborator
Not per project FILE. Per project yes.



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

___________________________
0 Likes
Message 11 of 14

Anonymous
Not applicable

Ok, so this will basically map itself to where the project is?

 

Because what I need to do is for the code to print all the sheets as individual PDF's in a PDF folder mapped to where the project is. And if you are not wanting the project you can map it.

0 Likes
Message 12 of 14

machiel.veldkamp
Collaborator
Collaborator
Play around with it. Learn

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

___________________________
0 Likes
Message 13 of 14

Anonymous
Not applicable

So I was able to figure out everything except individual sheets. Do you have any experience with that?

Message 14 of 14

machiel.veldkamp
Collaborator
Collaborator
Not persé, however I thought of something...

you could do a for each loop for the

oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

part. Haven't done that. Maybe there is a better way. Easiest thing is to look it up in the API.

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

___________________________
0 Likes