Create a Translator Addin in C#

By Barbara Han

Issue

We need to create a translator addin in C#. Using VC 2005 we can create a project of translator addin using Inventor AddIn wizard, but for C#, those options are not available when using Inventor AddIn wizard.
Could you please guide us how to create translator addin project in C# using Visual Studio 2010?

Solution

The following is the steps for creating translator AddIn in C# from Visual Studio 2010:
1.Install Inventor AddIn wizard first. It locates under Inventor SDK\DeveloperTools\Tools\Wizards folder.
2.Create an empty C# project using Autodesk Inventor AddIn template, give the name of the project to "SampleTranslator", then click "Ok" to generate SampleTranslator project skeleton.
3.Change the deriving class of StandardAddInServer class from Inventor.ApplicationAddInServer to Inventor.TranslatorAddInServer.
4.In the StandardAddInServer.cs, add this line to refer to System.IO library:
using System.IO;
5.Right click mouse on the deriving class ‘TranslatorAddInServer’ in StandardAddInServer.cs, select ‘Implement Interface’ command, the interface methods will be auto-generated. Modify them as below code:

public object GetThumbnail(Inventor.DataMedium SourceData)

{

  return null;

}

public void Open(Inventor.DataMedium SourceData, Inventor.TranslationContext Context, Inventor.NameValueMap Options, ref object TargetObject)

{

}

public void SaveCopyAs(object SourceObject, Inventor.TranslationContext Context, Inventor.NameValueMap Options, Inventor.DataMedium TargetData)

{

  try

  {

    Inventor.Document doc = SourceObject as Inventor.Document;

    if (null == doc)

    {

      return;

    }

    using (TextWriter writer = new StreamWriter(TargetData.FileName))

    {

      writer.WriteLine(doc.FullFileName);

    }

  }

  catch (Exception ex)

  {

  }

}

public void ShowOpenOptions(Inventor.DataMedium SourceData, Inventor.TranslationContext Context, Inventor.NameValueMap ChosenOptions)

{

}

public void ShowSaveCopyAsOptions(object SourceObject, Inventor.TranslationContext Context, Inventor.NameValueMap ChosenOptions)

{

}

public bool get_HasOpenOptions(Inventor.DataMedium SourceData, Inventor.TranslationContext Context, Inventor.NameValueMap DefaultOptions)

{

  return false;

}

public
bool get_HasSaveCopyAsOptions(object SourceObject, Inventor.TranslationContext Context, Inventor.NameValueMap DefaultOptions)

{

  return false;

}

6.Open Autodesk.SampleTranslator.Inventor.addin file, and change the AddIn Type from "Standard" to "Translator", set LoadOnStartUp to zero, like this:

<LoadOnStartUp>0</LoadOnStartUp>

and add these lines within the same file:

<SupportsOpenInto>idw;.dwg;.ipt</SupportsOpenInto>

<SupportsImportInto>idw;.dwg;.ipt</SupportsImportInto>

<SupportsSaveCopyAsFrom>.idw;.dwg;.ipt;.iam</SupportsSaveCopyAsFrom>

<FileExtensions>.adsk</FileExtensions>

<FilterText>Adsk Files (*.adsk)</FilterText>

<SupportsOpen>1</SupportsOpen>

<SupportsImport>1</SupportsImport>

<SupportsSaveCopyAs>1</SupportsSaveCopyAs>

The base file type can be ".ipt", ".iam", ".idw", or ".ipn". You
r customized file format will appear in the Save as type list when the file type is corresponding to those file type.

7.Right click on the project name ‘SampleTranslator’, click ‘Properties’ to open the project’s properties dialog. In the Application tab, click ‘Assembly Information’ button to show Assembly Information, change the Title to, e.g. "Translator: adsk". You may input some data in other input box, and click ‘OK’ button to quit.
8.Build the project.
9.Start Inventor, and open a .ipt file, call Save Copy As command, you will see the My Output Files(*.adsk) format in the Save as type drop-down list, chose this format, Inventor generates the .adsk file for you.

This sample only exports a line of string in the customized file. You can of course modify the Open, SaveCopyAs and so on translator addin interface to implement your own opening and save copy as feature.


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading