Thursday, 21 August 2014

Save As PDF DWF iLogic

A while back I posted a blog about locating a certain project folder on the network before doing the PDF, DWF export. I used a simple ilogic command calling the saveas function:

ThisDoc.Document.SaveAs(strNameDWF & (".dwf") , True)
ThisDoc.Document.SaveAs(strNamePDF & (".pdf") , True)

Unfortunately as in the save as Inventor dialog it only saves the first sheet. On save as you need to click on options to choose all sheets as well as "print in colors" and dpi resolution.

So just as in the Save As dialog where you need to click options to export all sheets we need to add more complex code:

I wanted to have all sheets exported and the dwf file to contain a full 3d model for better visualization and markup.

Her's how the final code looks like:

' set the filepath of the contracts folder to start with.
strStartFolder = "G:\Commercial & Engineering\Engineering\Contracts\"
' get the filename without extension
oFileName = ThisDoc.FileName(False)  
'get the project type by reading the first 6 
'digits of the filename EX 105280 or 105281
strFolderType = ( Left (oFileName,6)) 
'get the last 3 digits of the project no start 
'after first 7 digits of filename Ex 005 or 109
strFolderNo = (Mid (oFileName,7, 3)) 
'get the first 9 digits to compose project 
'type and number Ex 105281005 or 105281109
strFolderProj = ( Left (oFileName,9)) 
'et the document revision to use in the new filename
oRevNum = iProperties.Value("Project", "Revision Number") 

'Set folder for project based on Contract No
If strFolderNo < "100" Then
strFolder3 = "\001-099\"
ElseIf strFolderNo >= "100" Then
strFolder3 = "\100-199\"
ElseIf strFolderNo >= "200" Then
strFolder3 = "\200-299\"
End If

'Find folder in start path that contains Folder Type string 
Dim dir1() As String = System.IO.Directory.GetDirectories _
(strStartFolder, strFolderType & "*")
'Add Contract No folder to our folder
Dim dir2 As String = String.Concat(dir1) & strFolder3

'Find folder in new path containing Folder Type 
Dim dir3() As String = System.IO.Directory.GetDirectories _
(dir2, strFolderProj & "*")
'Add Drawing folder to our path
dir4 = String.Concat(dir3) & "\Drawing\"
dir5 = String.Concat(dir4) & "\PDF"

' verify if foder exists, create if not
If Not System.IO.Directory.Exists(dir5) Then
End If

'------------------- Complex code added to save all sheets    ----------------
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
oDWFAddIn = ThisApplication.ApplicationAddIns.ItemById _
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
    'this publish all doens't work
    oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
    'try a different publish all command
    'oOptions.Value("Publish_All_Sheets") = 1
    'oOptions.Value("Custom_Begin_Sheet") = 2
    'oOptions.Value("Custom_End_Sheet") = 4
End If

 'Set the PDF target file name
oDataMedium.FileName = dir5 & "\" & oFileName & "_Rev" & oRevNum & ".pdf"
'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
i = MessageBox.Show("View the PDF file?", "Title",MessageBoxButtons.YesNo)
If i = vbYes Then : launchviewer = 1 : Else : launchviewer = 0 : End If 
If launchviewer = 1 Then ThisDoc.Launch(oDataMedium.FileName)

If oDWFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
    oOptions.Value("Launch_Viewer") = launchviewer
    oOptions.Value("Publish_All_Component_Props") = 1
    oOptions.Value("Publish_All_Physical_Props") = 1
    oOptions.Value("Password") = 0
    oOptions.Value("Publish_3D_Models") = Publish_3D_Models
    If TypeOf oDocument Is DrawingDocument Then
        Dim oSheets As NameValueMap
        oSheets = ThisApplication.TransientObjects.CreateNameValueMap
        oOptions.Value("Publish_Mode") = DWFPublishModeEnum.kCustomDWFPublish
        oOptions.Value("Publish_All_Sheets") = 1
        ' Publish the first sheet AND its 3D model
        Dim oSheet1Options As NameValueMap
        oSheet1Options = ThisApplication.TransientObjects.CreateNameValueMap
        oSheet1Options.Add("Name", "Sheet:1")
        oSheet1Options.Add("3DModel", True)
        oSheets.Value("Sheet1") = oSheet1Options
    End If
End If

 'Set the DWF target file name
oDataMedium.FileName = dir4 & oFileName & "_Rev" & oRevNum & ".dwf"
'Publish document
oDWFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
i = MessageBox.Show("View the DWF file?", "Title",MessageBoxButtons.YesNo)
If i = vbYes Then : launchviewer = 1 : Else : launchviewer = 0 : End If 
If launchviewer = 1 Then ThisDoc.Launch(oDataMedium.FileName)
'------------------- End Complex code added In To save all sheets --------------

Again, thanks to Curtis Waguespack for his code that I've used here. Check his blog, he even has a code that does batch export for all components of an assembly.

Best of luck.