PDF Addin from VB.Net fails

PDF Addin from VB.Net fails

J_Dumont
Advocate Advocate
1,457 Views
14 Replies
Message 1 of 15

PDF Addin from VB.Net fails

J_Dumont
Advocate
Advocate

I am having issues trying to get pdf files from drawings.

I iterate through my assembly I need to create for drawings if they exist.

I had this working in iLogic but its not working in VB.Net.

 

Public Sub PublishPDF(ByRef DrawingFName As String)
Dim PDFAddIn As Inventor.TranslatorAddIn
PDFAddIn = invApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

 

Dim oContext As Inventor.TranslationContext
oContext = invApp.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

 

Dim oOptions As Inventor.NameValueMap
oOptions = invApp.TransientObjects.CreateNameValueMap
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
oOptions.Value("Custom_Begin_Sheet") = 1
oOptions.Value("Custom_End_Sheet") = 1

 

Dim oDataMedium As Inventor.DataMedium
oDataMedium = invApp.TransientObjects.CreateDataMedium


Dim oDrawName As String ' Use to store drawing filename. Remove path, extension and replace with PDFFoldername and pdf extension
oDrawName = PDFFolderName + "\" + System.IO.Path.GetFileNameWithoutExtension(DrawingFName) + ".pdf"

 

oDataMedium.FileName = oDrawName


Call PDFAddIn.SaveCopyAs(DrawingFName, oContext, oOptions, oDataMedium)

0 Likes
Accepted solutions (1)
1,458 Views
14 Replies
Replies (14)
Message 2 of 15

WCrihfield
Mentor
Mentor

This is what I've been using for years.

It doesn't place the PDF in a special sub-folder, as your does, but it may give you some ideas how to fix yours.

A couple differences I noticed right up front:

  • I don't see your "End Sub" at the end of your posted code
  • I noticed you're not using an If...Then statement around where you set the 'oOptions
  • I don't see where you've defined that PDF folder location (unless that was intentional)
  • I don't see where you check to see if that PDF folder exists, and to create it if it doesn't (This may never be an issue for you. I don't know.)
  • I don't see where you check to see if the PDF already exists, and how to handle that situation, rather than to silently write over it.

 

 

Public Sub Export_to_PDF()

If ThisApplication.ActiveDocumentType <> kDrawingDocumentObject Then
    Call MsgBox("The 'ActiveDocument' must be a Drawing Document, for this to work.", 0, "WRONG DOCUMENT TYPE")
    Exit Sub
End If

Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument

If oDoc Is Nothing Then
    Call MsgBox("YOU MUST ACTIVATE A DOCUMENT FIRST", 0, "NO ACTIVE DOCUMENT")
    Exit Sub
End If

Dim oAddIns As ApplicationAddIns
Dim oAddIn As ApplicationAddIn
Set oAddIns = ThisApplication.ApplicationAddIns
Dim oPDFAddIn As TranslatorAddIn

For Each oAddIn In oAddIns
    If oAddIn.DisplayName = "Translator: PDF" Then
        Set oPDFAddIn = oAddIn
    End If
Next

On Error Resume Next

Dim oContext As TranslationContext
Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism

Dim oOptions As NameValueMap
Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap

Dim oDataMedium As DataMedium
Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
'Set the PDF target file name
Dim oNewFileName As String
oNewFileName = Left(oDoc.FullFileName, Len(oDoc.FullFileName) - 4) & ".pdf"
oDataMedium.FileName = oNewFileName

If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 720
oOptions.Value("Sheet_Range") = kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("Custom_End_Sheet") = 4
oOptions.Value("Launch_Viewer") = 1 ' 0 = False, 1 = True
End If

Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(oNewFileName) = True Then
   oOverwritePDF = MsgBox("A PDF with that name already exists.  Do you want to overwrite it?", vbYesNo + vbExclamation, "PDF ALREADY EXISTS")
End If

If oOverwritePDF = vbNo Then
    Exit Sub
ElseIf oOverwritePDF = vbTYes Then
End If

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

End Sub

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE" 👍.

Also, when you have time, please review & vote for these 'Ideas' I'd like to get implemented.

  • Add more capabilities to the 'Customize' dialog box (exe. Add Tab & Add Panel) Click Here
  • Constrain & Dimension Images In Assembly Sketches & Drawing Sketches (TitleBlocks & SketchedSymbols) Click Here
  • Save Section View Status In DesignViewRepresentation (So It Can Be Used In The Drawing) Click Here
  • Add SolidBodies Folder In iLogic Rule Editor Model Tab Click Here
  • Convert All Views To Raster Before Autosave Stores To 'OldVersions' Folder Click Here
  • SetDesignViewRepresentation - Fix limitations for DrawingView of a Part Click Here
  • Create DocumentSubTypeEnum Click Here
  • Add kRevisionTag or kDrawingRevisionTag to ObjectTypeEnum Click Here

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 15

yan.gauthier
Advocate
Advocate

Hi, 

 

I am not sure you should specify Begin and End_Sheet when you set kPrintAllSheets.

 

But other than that, in C#, "\" is an escape statement. you need to use "\\" instead or you can also use System.IO.Path.DirectorySeparatorChar

0 Likes
Message 4 of 15

WCrihfield
Mentor
Mentor

The "Custom_Begin_Sheet" & "Custom_End_Sheet" are a good catch, as they should be either commented out (as I did in my example) or deleted, when using kPrintAllSheets.

Since his code is in VBA and not C#, the "\" symbol is normal within a file path.  That is, assuming the "\" isn't already included at the end of the "PDFFolderName" variable.  If it is already at the end of that variable, adding another "\" symbol will cause an error.

As I stated before, I'm not seeing where "PDFFolderName" is being defined, unless it is not defined on purpose to avoid publishing personal / propriety information on this forum, which would be totally understandable.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 15

yan.gauthier
Advocate
Advocate

He mentionned his code is VB.NET. not VBA. Since C# is a .NET framework language just like VB.NET.

 

In my case, my code looks like this 

 

//Setup Pdf translator
                Inventor.TranslatorAddIn PDFAddin = AddInServer.invApp.ApplicationAddIns.ItemById["{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}"] as Inventor.TranslatorAddIn;

                //Translation context
                Inventor.TranslationContext translationContext = AddInServer.invApp.TransientObjects.CreateTranslationContext();
                translationContext.Type = IOMechanismEnum.kFileBrowseIOMechanism;

                //DataMedium for document simulation
                DataMedium dataMedium = AddInServer.invApp.TransientObjects.CreateDataMedium();
                
                //NameValueMap for options
                NameValueMap options = AddInServer.invApp.TransientObjects.CreateNameValueMap();

if (PDFAddin.HasSaveCopyAsOptions[drawingDocument, translationContext, options])
                            {
                                options.Value["All_Color_AS_Black"] = 1;
                                options.Value["Remove_Line_Weigths"] = 1;
                                options.Value["Sheet_Range"] = PrintRangeEnum.kPrintAllSheets;
                                options.Value["Vector_Resolution"] = 200;
                            }

 string pdfFileName = "C:\\users\\yan.gauthier\\desktop\\pdf2\\" + System.IO.Path.GetFileNameWithoutExtension(drawingFileName) + ".pdf";
                            
                            //drawingDocument.SaveAs(pdfFileName,true);
                            dataMedium.FileName = pdfFileName;
                            PDFAddin.SaveCopyAs(drawingDocument, translationContext, options, dataMedium);
0 Likes
Message 6 of 15

WCrihfield
Mentor
Mentor

True. I guess I missed that.  It seemed odd, translating iLogic to vb.net, since they both already use the same programming language.  But I forgot that iLogic is an addin itself, and has a lot of pre-defined Classes, Subs, Functions, etc., that won't exist in a parallel addin, without referencing most of the other addin's resources, somehow.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 15

WCrihfield
Mentor
Mentor

Within his code and yours, I see "invApp", but I don't see where it is being defined or where it's value is set by getting the 'Inventor.Application'.  Is this something defined outside of the Sub, or some sort of environment variable within your Visual Studio?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 15

yan.gauthier
Advocate
Advocate

When you create a .Net Addin, You use a AddinServer standard Class. Inventor, when launching your addin will call the .Activate of this class and pass its addInSiteObject as parameter. which contain the Inventor.Application reference. I have it stored as public so I can access it from outside.

Message 9 of 15

J_Dumont
Advocate
Advocate

Thank you for your input.

 

I am using Public variables as shown below.

 

Imports Inventor
Public Module Module1
Public _InvApplication As Inventor.Application
Public invApp As Inventor.Application
Public oForm As New FormXYZ
Public oDocCount As String 'Get total occurance for use in Progress Bar
Public designTrackPropSet As Inventor.PropertySet
Public descAssyProject As Inventor.Property
Public oDocName As String
Public PDFFolderName As String

Sub Main()

' Get the Inventor Application object.
'Dim invApp As Inventor.Application
invApp = GetObject(, "Inventor.Application")

 

Then I pass the Inventor Drawing Name to the PDF subroutine.

 

I do want to print all sheets and forgot to comment out the sheet range. I have since deleted these lines. Thank you for that.

 

Here is my latest code for the publish.

 

Public Sub PublishPDF(ByRef DrawingFName As String)
Dim PDFAddIn As Inventor.TranslatorAddIn
PDFAddIn = invApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
Dim oContext As Inventor.TranslationContext
oContext = invApp.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As Inventor.NameValueMap
oOptions = invApp.TransientObjects.CreateNameValueMap
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
Dim oDataMedium As Inventor.DataMedium
oDataMedium = invApp.TransientObjects.CreateDataMedium


Dim oDrawName As String ' Use to store drawing filename. Remove path, extension and replace with PDFFoldername and pdf extension
oDrawName = PDFFolderName + "\" + System.IO.Path.GetFileNameWithoutExtension(DrawingFName) + ".pdf"
oDataMedium.FileName = oDrawName


Call PDFAddIn.SaveCopyAs(DrawingFName, oContext, oOptions, oDataMedium)

End Sub

 

Here is an image o the error.

J_Dumont_0-1595355985000.png

 

 

 

 

0 Likes
Message 10 of 15

yan.gauthier
Advocate
Advocate

Try checking PDFAddin.HasSaveCopyAsOption. doing so copy the default NameValueMap to your Option NameValueMap (translating from C# to Vb.NET should not be too difficult):

 

if (PDFAddin.HasSaveCopyAsOptions[drawingDocument, translationContext, options])
{
options.Value["All_Color_AS_Black"] = 1;
options.Value["Remove_Line_Weigths"] = 1;
options.Value["Sheet_Range"] = PrintRangeEnum.kPrintAllSheets;
options.Value["Vector_Resolution"] = 200;
}

 

Also, Check your output string in debug. Normaly, a folder path contain a trailing backslash "\"

 

Cheers,

0 Likes
Message 11 of 15

J_Dumont
Advocate
Advocate

 I added If statement as suggested and that is where the code fails now.

 

Public Sub PublishPDF(ByRef DrawingFName As String)
Dim PDFAddIn As Inventor.TranslatorAddIn
PDFAddIn = invApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
Dim oContext As Inventor.TranslationContext
oContext = invApp.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As Inventor.NameValueMap
oOptions = invApp.TransientObjects.CreateNameValueMap
Dim oDataMedium As Inventor.DataMedium
oDataMedium = invApp.TransientObjects.CreateDataMedium

 

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
End If


Dim oDrawName As String ' Use to store drawing filename. Remove path, extension and replace with PDFFoldername and pdf extension
oDrawName = PDFFolderName + "\" + System.IO.Path.GetFileNameWithoutExtension(DrawingFName) + ".pdf"

oDataMedium.FileName = oDrawName


Call PDFAddIn.SaveCopyAs(DrawingFName, oContext, oOptions, oDataMedium)

End Sub

J_Dumont_0-1595359735390.png

 

 

0 Likes
Message 12 of 15

yan.gauthier
Advocate
Advocate

Don't use oDataMedium, pass your drawingDocument instead

0 Likes
Message 13 of 15

J_Dumont
Advocate
Advocate

The drawing name didn't work and I get an IntelliSense error when trying to use the drawing name is in the line that calls the addin.

 

If PDFAddIn.HasSaveCopyAsOptions(DrawingFName, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
End If


Dim oDrawName As String ' Use to store drawing filename. Remove path, extension and replace with PDFFoldername and pdf extension
oDrawName = PDFFolderName + "\" + System.IO.Path.GetFileNameWithoutExtension(DrawingFName) + ".pdf"

oDataMedium.FileName = oDrawName

'MsgBox(PDFFolderName)
Call PDFAddIn.SaveCopyAs(DrawingFName, oContext, oOptions, oDataMedium)

 

Do I need to replace the "\" in the path with "/"?

 

0 Likes
Message 14 of 15

yan.gauthier
Advocate
Advocate
Accepted solution

You need to get the DrawingDocument object.

if you don't have the document already open, open it using this method :

 

drawingDocument = AddInServer.invApp.Documents.Open(drawingFileName, false) as DrawingDocument;

 

passing false means you open the document without displaying it. So remember to close it after you are done.

 

Also, this make me realize that you have the very same issue in your PDFAddin.SaveCopyAs. First parameter must be your DrawingDocument, not the file name!

 

This should fix everything !

0 Likes
Message 15 of 15

J_Dumont
Advocate
Advocate

Thank you all for your assistance.

With some minor tweaking, the PDF publishing now works!!!!!!!!!!!!!!

 

I can't thank you all enough!!!

 

For anyone interested, below is the function. The red indicates what was needed to complete the function.

 

Public Sub PublishPDF(ByRef DrawingFName As String)

 

Dim DrawingDocument As Document = invApp.Documents.Open(DrawingFName, False)

Dim PDFAddIn As Inventor.TranslatorAddIn
PDFAddIn = invApp.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
Dim oContext As Inventor.TranslationContext
oContext = invApp.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As Inventor.NameValueMap
oOptions = invApp.TransientObjects.CreateNameValueMap
Dim oDataMedium As Inventor.DataMedium
oDataMedium = invApp.TransientObjects.CreateDataMedium

 

If PDFAddIn.HasSaveCopyAsOptions(DrawingDocument, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
End If


Dim oDrawName As String ' Use to store drawing filename. Remove path, extension and replace with PDFFoldername and pdf extension
oDrawName = PDFFolderName + "\" + System.IO.Path.GetFileNameWithoutExtension(DrawingFName) + ".pdf"

oDataMedium.FileName = oDrawName

 

Call PDFAddIn.SaveCopyAs(DrawingDocument, oContext, oOptions, oDataMedium)
DrawingDocument.Close()

 

End Sub

0 Likes