ilogic code to open PDF export Dialogue box

ilogic code to open PDF export Dialogue box

Tim.PelvanRijnsoever
Contributor Contributor
739 Views
9 Replies
Message 1 of 10

ilogic code to open PDF export Dialogue box

Tim.PelvanRijnsoever
Contributor
Contributor

hi all,

I am looking to write some ilogic code to open up the PDF Export dialogue box, File - Export - PDF.

 

I want to use this in conjunction with some code that updates my Plot Date on my dwg.

I am planning to set up a Form button that first updates the Plot date then opens the PDF Export dialogue box.

i have found some code that does the first part (below) but am struggling to find a way to open the DF Export dialogue box after.

if anyone could help that would be great!

 

'define the drawing
Dim odrawdoc As DrawingDocument
odrawdoc = ThisApplication.ActiveDocument
'define the property set
customPropertySet = odrawdoc.PropertySets.Item("Inventor User Defined Properties")

'define the date string
Dim PlotDate As Date
PlotDate = Now
'define the time string
Dim PlotTime As String
PlotTime = Now.ToShortTimeString
'define the user name string
Dim PlotName As String
PlotName= ThisApplication.UserName

'---------------- find or create custom properties -------------
'look for the custom propety and add it if not found 
Try
      prop = customPropertySet.Item("Plotdatestamp")
Catch
      customPropertySet.Add("", "Plotdatestamp")
End Try

'look for the custom propety and add it if not found 
Try
      prop = customPropertySet.Item("Plottimestamp")
Catch
      customPropertySet.Add("", "Plottimestamp")
End Try

'look for the custom propety and add it if not found 
Try
	prop = customPropertySet.Item("PlotAuthor")
Catch
	customPropertySet.Add("", "PlotAuthor")
End Try

'---------------- set the property values -----------------------
'set the date property
iProperties.Value("Custom", "Plotdatestamp") = PlotDate
'set the timeproperty
iProperties.Value("Custom", "Plottimestamp") = PlotTime
'set the name property	
iProperties.Value("Custom", "PlotAuthor") = PlotName
	
'update the file 
InventorVb.DocumentUpdate()

'define the property set
customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

'look for the custom propety and add it if not found 
Try
	prop = customPropertySet.Item("Year")
Catch
	customPropertySet.Add("", "Year")
End Try
'set custom ipropety to copyright year
iProperties.Value("Custom", "Year") = Now.ToString("yyyy")

 

thanks in advance  

0 Likes
740 Views
9 Replies
Replies (9)
Message 2 of 10

A.Acheson
Mentor
Mentor

Hi @Tim.PelvanRijnsoever 

Out of curiosity what would you like to do when the dialogue box is open? Normally you would automate the functionality your looking to achieve to avoid user interaction. 

 

If you really need to open the dialogue then this might be the command name you need

"AppFileExportPDFCmd"

 

You can pick up a list of all commands from this code here

 

And here is how to execute

    Dim def As ButtonDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("AppFileExportPDFCmd")
def.Execute

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 10

Tim.PelvanRijnsoever
Contributor
Contributor

hi @A.Acheson, thanks for the quick reply.

 

I agree, automating it fully would be ideal but for my workflow i need more flexibility on where to save it.

so am happy to have the pdf export dialogue box open and go from there.

 

 maybe im doing something wrong but the code you gave me doesnt execute? nothing happens?

 

any thoughts?

 

Tim   

0 Likes
Message 4 of 10

A.Acheson
Mentor
Mentor

It might not be the right one  i thought it looked right but never tested it. I assume your launching it in a drawing correct? You can create you own dialogue box for saving if you wish. You don't necessarily need the built in one. 

 

Also in reference to your iproperty code your using both API Property Sets and iLogic API iproperties. There both doing the same thing so pick either one. In fact the iLogic API is just the function containing the inventor API so in essence you have written it twice and it is running twice. 

 

Inventor API

'Create and set the custom iproperty to copyright year
Try
	prop = customPropertySet.Item("Year")
Catch
    prop = customPropertySet.Add("", "Year")
End Try

Try
    prop.Value = Now.ToString("yyyy")
Catch
End Try

 

iLogic API

'Create and set the custom iproperty to copyright year
iProperties.Value("Custom", "Year") = Now.ToString("yyyy")

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 10

Tim.PelvanRijnsoever
Contributor
Contributor

Correct, its to be used in an Inventor Drawing (.DWG).

 

thanks for the tip Alan.

I was lucky enough to find this code by a user on here and then slightly adapt it for my use. it seems to work fine tho.

the built-in dialogue box is fine, its all i need.

hopefully i can work out how to finish this off and get the pdf export dialogue box to open after.

 

Tim

 

 

 

 

0 Likes
Message 6 of 10

A.Acheson
Mentor
Mentor

I tested the control definition  but it doesn't launch the dialogue. It does not work either for image, save as etc. Seems to be a bug. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 10

Tim.PelvanRijnsoever
Contributor
Contributor

Hmm, thats annoying.

hopefully someone else can chime in? there must be a workaround for this? it seems too simple.

i know "AppFileSaveCopyAsCmd" does pretty much the same thing but that doesnt work either.

maybe creating my own dialogue box for saving/exporting might bypass this issue.. ill have to keep researching/trying.

 

 

 

0 Likes
Message 8 of 10

A.Acheson
Mentor
Mentor

The export method sample is here 

and the file save dialog is here.

It will only be a matter of putting it all together. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 9 of 10

Tim.PelvanRijnsoever
Contributor
Contributor

Thanks for your help @A.Acheson ,

 

with some assistance from Chat GPT and the code in your link i managed to put together something that works ok ish.

i havent been able to get the "Options" Button in the dialogue box for the "Print Range" (shown in the original export pdf option attached). instead i just made 2 copies of the code and changed the hard coded print options and created 2 buttons in a Form. 

 

full code below

 

Public Sub Main()
	
	    'define the drawing
Dim odrawdoc As DrawingDocument
odrawdoc = ThisApplication.ActiveDocument
'define the property set
customPropertySet = odrawdoc.PropertySets.Item("Inventor User Defined Properties")

'define the date string
Dim PlotDate As Date
PlotDate = Now
'define the time string
Dim PlotTime As String
PlotTime = Now.ToShortTimeString
'define the user name string
Dim PlotName As String
PlotName= ThisApplication.UserName

'---------------- find or create custom properties -------------
'look for the custom propety and add it if not found 
Try
      prop = customPropertySet.Item("Plotdatestamp")
Catch
      customPropertySet.Add("", "Plotdatestamp")
End Try

'look for the custom propety and add it if not found 
Try
      prop = customPropertySet.Item("Plottimestamp")
Catch
      customPropertySet.Add("", "Plottimestamp")
End Try

'look for the custom propety and add it if not found 
Try
	prop = customPropertySet.Item("PlotAuthor")
Catch
	customPropertySet.Add("", "PlotAuthor")
End Try

'---------------- set the property values -----------------------
'set the date property
iProperties.Value("Custom", "Plotdatestamp") = PlotDate
'set the timeproperty
iProperties.Value("Custom", "Plottimestamp") = PlotTime
'set the name property	
iProperties.Value("Custom", "PlotAuthor") = PlotName
	
'update the file 
InventorVb.DocumentUpdate()

'define the property set
customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

'look for the custom propety and add it if not found 
Try
	prop = customPropertySet.Item("Year")
Catch
	customPropertySet.Add("", "Year")
End Try
'set custom ipropety to copyright year
iProperties.Value("Custom", "Year") = Now.ToString("yyyy")

    ' 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

    ' Create Save File Dialog
    Dim saveFileDialog As New System.Windows.Forms.SaveFileDialog()
    saveFileDialog.Title = "Save PDF As"
    saveFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
    saveFileDialog.FilterIndex = 1
    saveFileDialog.RestoreDirectory = True

    ' Show Save File Dialog
    If saveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        ' Set the destination file name
        Dim fileName As String = saveFileDialog.FileName

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

        ' Create a NameValueMap object
        Dim oOptions As NameValueMap
        oOptions = ThisApplication.TransientObjects.CreateNameValueMap

        ' 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") = kPrintCurrentSheet
            'oOptions.Value("Custom_Begin_Sheet") = 2
            'oOptions.Value("Custom_End_Sheet") = 4
        End If

        ' Create a DataMedium object
        Dim oDataMedium As DataMedium
        oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

        ' Set the destination file name
        oDataMedium.FileName = fileName

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

   

0 Likes
Message 10 of 10

WCrihfield
Mentor
Mentor

Hi guys.  After quickly reading through the first post here, asking for a way to show the Export as PDF dialog box, then not seeing what immediately came to mind in any of the following responses, I thought I would drop a hint here about the way I was thinking of.  The TranslatorAddIn object (a sub type of the ApplicationAddIn object that is specifically for add-ins that are translators) has a method available just for that functionality (TranslatorAddIn.ShowSaveCopyAsOptions).  It is rarely used though, because most folks just want to have the whole process 100% automated without any user interactions needed.  I have tested it before in the past though, and you can actually show this as a way of setting the 'Options' (NameValueMap) the way you want them using the actual dialog, then finish the rest of the code in the rule.  In the last code you posted, the 'PDFAddIn' variable is already declared as a TranslatorAddIn, and you are already using the similar TranslatorAddIn.HasSaveCopyAsOptions property, and this method could be used immediately after that check, but would essentially replace the following section of code for hardcoding in what Options you want.  Just pointing that little detail out, just in case it sounds like something you might like.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes