I've been asked the same question twice recently that's related to my previous Converting VBA Auto Macros to an Add-In post. In that example I showed how to write out dwf and pdf files using the API. The question asked was how to create a pdf file that contains all of the sheets in the drawing instead of just the active sheet. Similar questions about how to control the translators have been asked in the customization newsgroup too, so I think it's worthwhile to discuss how to read and write various file formats using Inventor's API. Inventor's API supports three different ways of importing and exporting files in and out of Inventor. These are discussed below.
Method 1 (Save As)
The first way to translate files is very easy to use and supports a wide variety of formats but has limited flexibility with respect to controlling the various options when doing the translation. This first method uses the Document.SaveAs method to translate a file from Inventor to another format and the Documents.Open method to translate a foreign file into Inventor. Below is an example of a program that uses the SaveAs method to save a PDF file of the active document.
' Get the active docuement.
Dim oDoc As Document
Set oDoc = ThisApplication.activeDocument
' Save a copy as a PDF file.
Call oDoc.SaveAs("C:\Temp\TestFile.pdf", True)
End Sub
The SaveAs and Open methods are the methods you use to open and save Inventor files. The key to also using them to translate other file formats is the file extension. When you specify the filename to open or save to, Inventor looks at the extension of the filename and will automatically call the appropriate translator. In this example it sees the PDF file extension and automatically calls the PDF translator. The same limitations of the file types that you have in the Inventor Open and Save Copy As commands apply. For example, you can save a part as an stl file but you can't open an stl file. The picture below shows the file types available in the Open dialog. These are also the file types that are supported by the Documents.Open method.
This technique is very simple, as you can see in the sample code above. The primary limitation with this approach is that you don't have control over any of the options for the translator. For example, the picture below shows the options dialog that's available when you're using the Save Copy As command to create a PDF file. When you use the Document.SaveAs method from the API the PDF will be created using default values for these settings. Typically these defaults are the settings last specified by the user when using the Save Copy As command, although this behavior can vary from one translator type to another.
Method 2 (Translator Add-Ins)
This second technique is more complicated to use but provides access to the various options that the translator supports. It takes advantage of the fact that translators in Inventor are implemented as a special type of Add-In. All translator Add-Ins are required to support a common set of methods and properties which allows you to write a program that drives the translator. This is illustrated in the sample program below.
' Get the PDF translator Add-In.
Dim oPDFTrans As TranslatorAddIn
Set oPDFTrans = ThisApplication.ApplicationAddIns.ItemById( _
"{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
If oPDFTrans Is Nothing Then
MsgBox "Could not access PDF translator."
Exit Sub
End If
' Create some objects that are used to pass information to
the translator Add-In.
Dim oContext As TranslationContext
Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
If oPDFTrans.HasSaveCopyAsOptions(ThisApplication.activeDocument, _
oContext, oOptions) Then
' Set to print all sheets. This can also have the value
' kPrintCurrentSheet or kPrintSheetRange. If kPrintSheetRange
' is used then you must also use the CustomBeginSheet and
' Custom_End_Sheet to define the sheet range.
oOptions.Value("Sheet_Range") = kPrintAllSheets
' Other possible options...
'oOptions.Value("Custom_Begin_Sheet") = 1
'oOptions.Value("Custom_End_Sheet") = 5
'oOptions.Value("All_Color_AS_Black") = True
'oOptions.Value("Remove_Line_Weights") = True
'oOptions.Value("Vector_Resolution") = 200
' Define various settings and input to provide the translator.
oContext.Type = kFileBrowseIOMechanism
Dim oData As DataMedium
Set oData = ThisApplication.TransientObjects.CreateDataMedium
oData.FileName = "C:\temp\test.pdf"
' Call the translator.
Call oPDFTrans.SaveCopyAs(ThisApplication.activeDocument, _
oContext, oOptions, oData)
End If
End Sub
This program begins by getting a specific translator Add-In using its ID, the PDF translator in this case. (See the translator reporter paragraph below for one method to determine the Add-In ID.) It then creates some objects that are required for some input arguments. The next section is the most interesting and is where it sets the values for the various options of the translator. In this cases it's setting the PDF translator to print all of the sheets in the drawing. Next it defines the filename of the output file and finally it calls the method on the translator to perform the actual translation.
The hardest part of using this approach to translate files is understanding what the supported options are and how to set them. The best resource for this information is the programming help. This discusses the options for each translator type along with samples for most of the translators. Look for the help topic "TranslatorAddIn Object". You can also get to this topic from the Samples page in the programming help and selecting one of the samples from the Import/Export section. The picture below illustrates how to access the programming help.
Here's a translator reporter macro that I created before the help files existed but that I still find useful when working with translators. To use it copy and paste the code into a VBA project and run the macro. It writes a report that contains information about all of the loaded translators. The most interesting information reported is the list of all of the available translator Add-Ins, their Add-In ID, and the options supported by each Add-In. The online help is still the best resource for the option information since it also includes a description of the options and valid input for the options.
Method 3 (DataIO)
The final method of translating data has the most limitations but supports some special cases that aren't handled by the standard translators. This method uses an API object called DataIO which you can get from some Inventor API objects. Basically, you get the DataIO object from the object of Interest and use the methods on the DataIO object to write out the object of interest in the specified format. The DataIO object can also be used to read data into Inventor. The number of objects that support DataIO and the formats supported are very small. Here are what I think are the most interesting:
- Writing out a sheet metal flat pattern as DWG or DXF.
- Writing out the contents of a sketch as DWG or DXF.
- Reading and writing a part solid as SAT.
These and more information about DataIO are discussed in more detail in the programming help. Since it's not used very often and rather than just repeat that information here I'll refer you to it.