iLogic, save all .idw sheets to PDF

iLogic, save all .idw sheets to PDF

vance
Contributor Contributor
3,608 Views
11 Replies
Message 1 of 12

iLogic, save all .idw sheets to PDF

vance
Contributor
Contributor

I am having trouble updating an ilogic rule that saved IDW files to PDF it worked fine in 2016 but is throwing error messages in 2018.

I have followed the forum answers for fixing the 1 first error but now get a new error.

 

" unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)"

 

The code creates a PDF but it is unreadable by adobe PDF reader??

 

I have pasted my code below:

 

SyntaxEditor Code Snippet

 '------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


oSetFileName = InputRadioBox("Set the file names as created??", "Yes", "No", True, Title := "SET FILE NAMES")

oRevNo = InputBox("REV-NO."& vbCrLf & _ 
"IF NONE REQUIRED DELETE ? ", "Set REV NO", "?")

oFolder = oPath & "\PDF"
MessageBox.Show("FILES WILL BE SAVED IN. " & vbCrLf & vbCrLf & oFolder, "Files saved in.")


'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

oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
oOptions.Value("Custom_Begin_Sheet") = iPageNumber
oOptions.Value("Custom_End_Sheet") = iPageNumber 


'get PDF target folder path


oFolder = oPath & "\PDF"
'MessageBox.Show(oFolder, "Title")


        '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



        If oSetFileName = True Then
            
            oFilename = InputBox("Is this the file name you want??", "Title", sSheetName  & "-" &  oRevNo )


        ElseIf oSetFileName = False Then

            oFilename = sSheetName  & iSheetNumber & "-" &  oRevNo


    End If

'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFilename & ".pdf"

'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

FilePath = oFolder
'MessageBox.Show(oFilename, "Title")

Next
'------end of iLogic-------

 

 

Any help would be greatly appreciated....

0 Likes
Accepted solutions (1)
3,609 Views
11 Replies
Replies (11)
Message 2 of 12

rhasell
Advisor
Advisor

Hi

 

I have not had time to debug your code, but in testing I found that it is linked to the "Sheet Range" The original code was generating zero byte files, that why you could not read them.

 

Anyway attached is my testing, perhaps you can take it from there?

 

export to pdf.PNG

Reg
2026.1
0 Likes
Message 3 of 12

vance
Contributor
Contributor

Thank you for your response.

 

Unfortunately I am still unable to make the code work, any further assistance would be appreciated.

0 Likes
Message 4 of 12

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @vance,

 

Try the following iLogic code which exports all sheets to single pdf from a .Idw file.

 

Sub Main()
    ' 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("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

    'Set the destination file name
    oDataMedium.FileName = "c:\temp\test.pdf"

    'Publish document.
    Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
End Sub

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 5 of 12

vance
Contributor
Contributor

Thank you yes that worked. 

0 Likes
Message 6 of 12

FProcp
Collaborator
Collaborator

Thanks very much for this ilogic code Chandra.

I am trying to get it to save to a folder named "pdf" in one directory up from where I am working.

I have placed in some code to establish the "pdf" target folder and it uses the variable oFolder

But I cannot seem to get it to work?

 

SyntaxEditor Code Snippet

Sub Main()
    ' 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("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
    oPath = ThisDoc.Path
    oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
    oFileName = ThisDoc.FileName(False) 'without extension
    oRevNum = iProperties.Value("Project", "Revision Number")
    
    'Set the destination file name
    oDataMedium.FileName = "x:\design\pdf\" & oFileName & " Rev" & oRevNum & ".pdf"
    'oDataMedium.FileName = oFolder & "\" & oFileName & " Rev" & oRevNum & ".pdf
    
    'Publish document.
    Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
    '
End Sub

 

Franco
GMT +08:00
0 Likes
Message 7 of 12

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @FProcp,

 

I don't see any problem in the code. Try the following code which is cleaned.

 

Sub Main()
    ' 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("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    
    
    oFileName = ThisDoc.FileName(False) 'without extension
    oRevNum = iProperties.Value("Project", "Revision Number")
    
    'Set the destination file name
    oDataMedium.FileName = "x:\design\pdf\" & oFileName & " Rev" & oRevNum & ".pdf"
        
    'Publish document.
    Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
    '
End Sub

Please feel free to contact if there is any queries,

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 8 of 12

FProcp
Collaborator
Collaborator

Thank you so much Chandra but I do not want it to save to "x:\design\pdf\"

Please remove that line and un-comment the next line where it will save to "oFolder"

 

"oFolder" will be a folder determined as one up from where my .idw is.

 

    oDataMedium.FileName = "x:\design\pdf\" & oFileName & " Rev" & oRevNum & ".pdf"
   'oDataMedium.FileName = oFolder & "\" & oFileName & " Rev" & oRevNum & ".pdf

 

Franco
GMT +08:00
0 Likes
Message 9 of 12

FProcp
Collaborator
Collaborator

This seems to work?

 

SyntaxEditor Code Snippet

Sub Main()
    ' 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("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
    oPath = ThisDoc.Path
    oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
    oFileName = ThisDoc.FileName(False) 'without extension
    oRevNum = iProperties.Value("Project", "Revision Number")
    
    'Set the destination file name
    'oDataMedium.FileName = "x:\design\pdf\" & oFileName & " Rev" & oRevNum & ".pdf"
oDataMedium.FileName = oFolder & "\" & oFileName & _
" Rev" & oRevNum & ".pdf"

    'Publish document.
    Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
    '
End Sub
Franco
GMT +08:00
0 Likes
Message 10 of 12

FProcp
Collaborator
Collaborator

The above will not work if the "PDF" folder does not already exist.

The following code has been modified to create the "PDF" folder if it does not exist.

 

This version is better.

 

SyntaxEditor Code Snippet

Sub Main()
    ' 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("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
    oPath = ThisDoc.Path
    oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"
    oFileName = ThisDoc.FileName(False) 'without extension
    oRevNum = iProperties.Value("Project", "Revision Number")
    
'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 = "x:\design\pdf\" & oFileName & " Rev" & oRevNum & ".pdf"
oDataMedium.FileName = oFolder & "\" & oFileName & _
" Rev" & oRevNum & ".pdf"

    'Publish document.
    Call PDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
    '
End Sub
Franco
GMT +08:00
0 Likes
Message 11 of 12

Anonymous
Not applicable

Hi All

A lot of these codes are quite long to just save a PDF.

This is mine:

'------start of iLogic-------

'Obtain info
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 

'Options
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

'File Path
oPath=ThisDoc.PathAndFileName(False)
oRev = iProperties.Value("Project", "Revision Number")
oDataMedium.FileName = oPath & " Rev" & oRev & ".pdf" 

'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

'------end of iLogic-------
Message 12 of 12

Anonymous
Not applicable

Good day to all!

Is it possible for this code to run one time for all of my open idw files?