Inventor API Training – Lesson 11

Here is section eleven of the Inventor API training. In this section we cover how to use the API to print and translate files. There are  21 sections to this training and we decided that they did not need to be posted in order. (Section one is here). I am going to alternate posts between sections 1-10 and 10-21.

Printing and Translating Files

image

Here is the agenda for this section. Be sure to see the Lab instructions at the end of this section. (completed sample is available)

PrintManager Object
DrawingPrintManager
Printing Example
File Translation
Accessing Translators Options
DataIO Object

PrintManager Object

image

The PrintManager object can be accessed from the different types of documents such as an part or assembly. Also Drawings and Apprentice support special print managers that provide additional printing capabilities.These PrintManager objects provide that same settings that are available in the Print command dialogs in the user interface.

PrintManager – Main Properties
Here are the main properties and methods of the PrintManager object.

NumberOfCopies: Default value to 1
PaperHeight / PaperWidth: Sets the paper dimensions. This property is only used when the PaperSize property is set to kPaperSizeCustom
Printer:  gets and sets the name of the printing device

PrintToFile(String FileName): Prints to the specified file using the current property settings
SubmitPrint: Prints the file using the current property settings

DrawingPrintManager Object
The DrawingPrintManager object derives from PrintManager and has some additional properties and methods specific to drawing documents.

AllColorsAsBlack: No colors in the print
PrintRange: Can be set to kPrintCurrentSheet, kPrintAllSheets, kPrintSheetRange.
ScaleMode / Scale:  Gets and sets how the scale of the print is defined.
RemoveLineWeights: All lines will have the same default width

GetSheetRange / SetSheetRange:  Gets/sets the sheet range to print.

Print Sample VB.NET

Public Sub PrintSample()
 
    Dim oDrawDoc As DrawingDocument
    oDrawDoc = mApp.ActiveDocument
 
    Dim oPrintMgr As DrawingPrintManager
    oPrintMgr = oDrawDoc.PrintManager
 
    With oPrintMgr
        .Printer = "\ADSOREPS1oreprn001"
        .NumberOfCopies = 1
        .ScaleMode = _
      PrintScaleModeEnum.kPrintFullScale
        .PaperSize = _
          PaperSizeEnum.kPaperSize11x17
        .SubmitPrint()
    End With
 
End Sub

 

File Translation

Simple Translation using Open and SaveAs
You can use the Document.SaveAs and Documents.Open methods to translate documents in and out of Inventor. The extension of the filename supplied will be used to determine which translator to use. With this approach default settings are used. The default settings are the settings last used when interactively translating a file. In this code snippet an stp file is opened and a document is saved as and AutoCAD dwg file.

Documents.Open( “C:TempTest.stp” )
Document.SaveAs( “C:TempNew.dwg”, True )

File Translation using the translator Add-Ins
If you want to control the settings when translating files you use the Translator Add-Ins. The main properties and methods of translator add-ins are these:

HasOpenOptions: Returns whether or not the translator has options available for opening files. The three arguments (DataMedium, TranslationContext, NameValueMap ) contain settings you can use.

Open: Opens (translates) a file. This method takes four objects as arguments that control how the open will work.

DataMedium: Use the FileName property to set the file name to open.

TranslationContext: For file, set Type to kFileBrowseIOMechanism you can place the translated file into an existing document with the OpenIntoExisting property

NameValueMap: Use this to set the options for the open

Object: The object that will be created – usually a document

HasSaveCopyAsOptions: Returns whether or not the translator has options available for saving files. The three arguments (Object, TranslationContext, NameValueMap ) will contain settings you can use.

SaveCopyAs: Save the document to the specified data-source. This method takes the following arguments:

Object: The document to be saved
TranslationContext: For file set Type to  kFileBrowseIOMechanism 

NameValueMap: Use this to set the options for the translation
DataMedium: Use the FileName property to set the file name to save

 

Each of the translator Add-Ins have a GUID. You can use this identifier (String) with the ItemById method of the ApplicationAddIns property of the Inventor Application object to instantiate the translator. The following example gets the STEP translator and uses the HasOpenOptions() method to print the options (that could be set before using the Open method) to the immediate window.

Accessing Translator’s Open Options – VB.NET

Public Sub GetTranslatorOpenOptions()
 
    'Using the GUID of the STEP Translator
    Dim clsId As String
    clsId = "{90AF7F40-0C01-11D5-8E83-0010B541CD80}"
 
    Dim oTranslator As TranslatorAddIn
    oTranslator = _
        mApp.ApplicationAddIns.ItemById(clsId)
 
    Dim medium As DataMedium
    medium = _
        mApp.TransientObjects.CreateDataMedium
    ' medium.FileName = "C:MyInventorDoc.xxx"
    medium.MediumType = _
            MediumTypeEnum.kFileNameMedium
 
    Dim context As TranslationContext
    context = mApp.TransientObjects.
                     CreateTranslationContext
 
    Dim options As NameValueMap
    options = mApp.TransientObjects.
                          CreateNameValueMap
 
    Dim index As Integer
    If oTranslator.HasOpenOptions _
            (medium, context, options) Then
        For index = 1 To options.Count
            Debug.Print(options.Name(index) _
& " = " & options.Value(options.Name(index)))
        Next
    End If
 
End Sub

Accessing Translator’s Save Options – VB.NET

Public Sub GetTranslatorSaveAsOptions()
    ' Using GUID of the DWF Translator
    Dim clsId As String
    clsId = "{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}"
 
    Dim oTranslator As TranslatorAddIn
    oTranslator =
        mApp.ApplicationAddIns.ItemById(clsId)
 
    Dim context As TranslationContext
    context =
   mApp.TransientObjects.CreateTranslationContext
    context.Type =
        IOMechanismEnum.kUnspecifiedIOMechanism
 
    Dim options As NameValueMap
    options =
        mApp.TransientObjects.CreateNameValueMap
 
    Dim SourceObject As Object
    SourceObject = mApp.ActiveDocument
 
    Dim index As Integer
    If oTranslator.HasSaveCopyAsOptions _
        (SourceObject, context, options) Then
        For index = 1 To options.Count
            Debug.Print(options.Name(index) &
      " = " & options.Value(options.Name(index)))
        Next
    End If
 
End Sub

Inventor 2013: Built-in Translator Add-Ins GUIDs

DWF:                {0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}
PDF:                 {0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}
DWFx:              {0AC6FD97-2F4D-42CE-8BE0-8AEA580399E4}
JT:                    {16625A0E-F58C-4488-A969-E7EC4F99CACD}
i-drop:               {21DB88B0-BFBF-11D4-8DE6-0010B541CAA8}
CATIA Part:       {2FEE4AE5-36D3-4392-89C7-58A9CD14D305}
SolidWorks:       {402BE503-725D-41CB-B746-D557AB83BAF1}
Pro/E:               {46D96B7A-CF8A-49C9-8703-2F40CFBDF547}
STL:                  {533E9A98-FC3B-11D4-8E7E-0010B541CD80}
DWF:                {55EBD0FA-EF60-4028-A350-502CA148B499}
Pro/E Granite:    {66CB2667-73AD-401C-A531-64EC701825A1}
IDF:                  {6C5BBC04-5D6F-4353-94B1-060CD6554444}
SAT:                 {89162634-02B6-11D5-8E80-0010B541CD80}
CATIA Product: {8A88FC01-0C32-4B3E-BE12-DDC8DF6FFF18}
Pro/E Neutral     {8CEC09E3-D638-4E8F-A6E1-0D1E1A5FC8E3}
CATIA Import:    {8D1717FA-EB24-473C-8B0F-0F810C4FC5A8}Parasolid Text:   {8F9D3571-3CB8-42F7-8AFF-2DB2779C8465}
STEP:               {90AF7F40-0C01-11D5-8E83-0010B541CD80}
IGES:                {90AF7F44-0C01-11D5-8E83-0010B541CD80}
UGS NX:            {93D506C4-8355-4E28-9C4E-C2B5F1EDC6AE}
Content Center   {A547F528-D239-475F-8FC6-8F97C4DB6746}
Parasolid Binary:{A8F8F8E5-BBAB-4F74-8B1B-AC011251F8AC}
Drag & Drop      {B95D705C-E915-4A5B-A498-E73AC98923A2}
DWG:               {C24E3AC2-122E-11D5-8E91-0010B541CD80}
DXF:                 {C24E3AC4-122E-11D5-8E91-0010B541CD80}
Alias:                {DC5CD10A-F6D1-4CA3-A6E3-42A6D646B03E}

DataIO Object
Some Inventor objects support a DataIO object. You can use this object to get input and output of formatted data. Use the GetInputFormats / GetOutputFormats methods to get the list of formats supported by a DataIO objects (ex DXF, DWG for sketches, FlatPattern, XML for iProperties, SAT for worksurfaces,…)

DataIO VB.NET examples

 

Public Sub ExportWorkSurface()
 
        Dim oWorkSurfaces As WorkSurfaces
        oWorkSurfaces = mApp.ActiveDocument.
              ComponentDefinition.WorkSurfaces
 
        Dim oDataIO As DataIO
        oDataIO = oWorkSurfaces(1).
                     SurfaceBodies(1).DataIO
        oDataIO.WriteDataToFile _
           ("ACIS SAT", "C:Tempresult.sat")
 
    End Sub
 
    Public Sub ExportSketchDXF()
 
        Dim oSketch As PlanarSketch
        oSketch = mApp.ActiveDocument.
            ComponentDefinition.Sketches(1)
 
        Dim oDataIO As DataIO
        oDataIO = oSketch.DataIO
        Call oDataIO.WriteDataToFile _
               ("DXF", "C:Tempdxfout.dxf")
 
    End Sub

Lab: Access the Translator Add-In options
Create a windows forms application. On the form have a ListView, a ComboBox and a Button. In the ComboBox allow the user to select the name of a Translator. When the button is clicked populate the ListView with the open options and save options for the selected Translator Add-In. Here are examples of the completed Lab. (VB.NET and C#) 

  Download Inventor_Training_Module_11_Samples

-Wayne


Comments

One response to “Inventor API Training – Lesson 11”

  1. Hi Wayne,
    Thanks for the great post. This is going to help us quite a lot with what we are currently doing with customizing Inventor. I am looking forward to your next post and topic!

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading