Import IGES file through TranslatorAddIn

By Barbara Han

Inventor has many TranslatorAddIns which can be used to import/export file through programming. For example, to import IGES file, you can use IGES TranslatorAddin’s API and it allows you to set options for importing.

The HasOpenOptions method of the IGES TranslatorAddIn tells the importing options. This is same for other file format such as DWF/DWG/DXF/IGES/STEP/SAT and so on. After you set the Value of the option (NameValueMap object), the value will be applied when you open the file through TranslatorAddIn API. How please note that although you can use API to change those options, but all those options shown in the UI are saved at the last time you changed it from UI, instead of API.

One of our developers even asked us a question about how to check/uncheck ‘Auto Stitch And Promote’ option. In API world, there is an hidden property ‘AutoStitchAndPromote’. This means this property is unsupported, and Not for public use – you are on your own risk if you are keen to use it. Its function is to attempt to auto stitch the entire contents of the explicit environment and promote the solid result. You need to set CreateSurfIndex option to 3 or 4 to enable this function. From UI IGES import options dialog, you can see what this means when you switch the Create Surface As type and look at the ‘Auto Stitch And Promote’ option.

The following is a sample that gets all available options, and changes a couple of options, then open the IGES file. Before running the sample, please change the dm.FileName to your .igs file path.

VBA code:

Public Sub getImportOptions()

  Dim app As Application

  app = ThisApplication

  Dim addins As ApplicationAddIns

  Dim addIn As TranslatorAddIn

  addins = app.ApplicationAddIns

 

  Dim i As Integer

 

  ‘get the addin

 

  On Error Resume Next

  For i = 1 To addins.Count

    If addins(i).AddInType = kTranslationApplicationAddIn Then

      If addins(i).ClassIdString = "{90AF7F44-0C01-11D5-8E83-0010B541CD80}" Then

        addIn = addins.Item(i)

      End If

    End If

  Next i

 

  ‘Activate AddIns

  addIn.Activate()

 

  Dim dm As DataMedium

  dm = app.TransientObjects.CreateDataMedium

  dm.filename = "C:\Users\hanb\Documents\Inventor\InvPro1\Assembly2.igs"

 

  Dim context As TranslationContext

  context = app.TransientObjects.CreateTranslationContext

  context.Type = kFileBrowseIOMechanism

 

  Dim nvm As NameValueMap

  nvm = app.TransientObjects.CreateNameValueMap

 

 

  ‘you can get the options now…

  If addIn.HasOpenOptions(dm, context, nvm) Then

    For i = 1 To nvm.Count

      Debug.Print("[Option Name] " & nvm.Name(i) & " = " & nvm.Value(nvm.Name(i)))

    Ne
xt

  End If

  ‘Demo: change some options

 

  nvm.Value("CreateSurfIndex") = 3

  nvm.Value("AutoStitchAndPromote") = False

 

  ‘Open the file with options

 

  Dim oNewDoc As Document

  Call addIn.Open(dm, context, nvm, oNewDoc)

  oNewDoc.Views.Add()

 

  ‘Call AutoStitchAndPromote method.

  ‘Just a test, no specific meaning.

 

  If oNewDoc.DocumentType = kPartDocumentObject Then

    Dim ptDoc As PartDocument

    ptDoc = oNewDoc

    ptDoc.ComponentDefinition.[_AutoStitchAndPromote]()

  End If

End Sub

The equivalent C# code:

public void ImportIges()

{

    try

    {

        Inventor.Application app = (Inventor.Application)System.Runtime.

InteropServices.Marshal.GetActiveObject("Inventor.Application");

 

        ApplicationAddIns addins = null;

        TranslatorAddIn addIn = null;

 

        addins = app.ApplicationAddIns;

 

        int nCount = addins.Count;

        for (int i = 1; i <= nCount; i++)

        {

            try

            {

                if (addins[i].AddInType == ApplicationAddInTypeEnum.kTranslationApplicationAddIn)

                {

                    if (addins[i].ClassIdString == "{90AF7F44-0C01-11D5-8E83-0010B541CD80}")

                    {

                        addIn = (TranslatorAddIn)addins[i];

                        break;

                    }

                }

            }

            catch

            {

            }

        }

 

        if (addIn == null)

            return;

 

        //Activate AddIns

 

        addIn.Activate();

 

        DataMedium dm = app.TransientObjects.CreateDataMedium();

        dm.FileName = "C:\\Part1.igs";

 

        TranslationContext context = app.TransientObjects.CreateTranslationContext();

        context.Type = IOMechanismEnum.kFileBrowseIOMechanism;

 

        NameValueMap nvm = app.TransientObjects.CreateNameValueMap();

 

        //you can get the options now…

 

        if (addIn.get_HasOpenOptions(dm, context, nvm))

        {

            for (int i = 1; i <= nvm.Count; i++)

            {

                string strText = "[Option Name] " + nvm.get_Name(i) + " = " + nvm.get_Value(nvm.get_Name(i));

                Console.WriteLine(strText);

            }

        }

 

        //Demo: change some options

 

        nvm.set_Value("CreateSurfIndex", 3);

        nvm.set_Value("AutoStitchAndPromote", true);

 

        object oNewDoc = null;

        addIn.Open(dm, context, nvm, out oNewDoc);

 

        Document doc = (Document)oNewDoc;

        doc.Views.Add();

    }

    catch

    {

    }

}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading