Blog » API Autodesk Inventor »

Primeiro AddIn do Inventor

Softwares e versões utilizadas: Visual Studio 2008 e Autodesk Inventor 2010
Linguagem: C#

Neste post vou ensinar como fazer um simples AddIn do Inventor no melhor estilo “Hello World!”. Apartir dai a imaginação é sua!

Vamos lá!

Primeiro você colocar o template do projeto de um AddIn do Inventor na pasta de Templates do VS2008. Normalmente o template encontra-se na pasta de instalação do Inventor.  Clique para baixar o template.

Agora seu VS2008 deve estar parecido com isto em File>New>Project:

newproject

Então criamos um novo projeto com este template. Após este passo seu explorer deve estar assim:

solutionexplorer

Configurando o projeto

Agora vamos configurar nosso projeto para que ele abra o Inventor quando iniciamos o debug. Para isso Project > Properties (Alt + F7) > Debug. Configure o parâmetro “Star external program” com o caminho do executável do Inventor(Normalmente em C:\Program Files\Autodesk\Inventor 2010\bin\Inventor.exe)

externalprogram

Agora em Build, ainda em propriedades do projeto, devemos marcar a opção Register form COM interop. Isso fará com que as funções de registro sejam chamadas quando iniciarmos o debug do projeto.

cominterop

Com isso seu projeto já esta configurado e deve estar funcionando. Clique em Start Debugging e pronto. Seu primeiro AddIn do Inventor esta funcionando.

Você pode verificar isso no Inventor em Tools > AddIns:

addins

Entendendo o código:

Adicionando os namespaces necessários:

StandardAddInServer.cs
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Inventor;
  4. using Microsoft.Win32|>;
  5. using System.Diagnostics;

Funções de registro. São chamadas pelo utilitário de registro do .NET.

StandardAddInServer.cs
  1. #region COM Registration functions
  2.  
  3.         ///
  4.         /// Registers this class as an Add-In for Autodesk Inventor.
  5.         /// This function is called when the assembly is registered for COM.
  6.         ///
  7.         [ComRegisterFunctionAttribute()]
  8.         public static void Register(Type t)
  9.         {
  10.             RegistryKey clssRoot = Registry.ClassesRoot;
  11.             RegistryKey clsid = null;
  12.             RegistryKey subKey = null;
  13.  
  14.             try
  15.             {
  16.                 clsid = clssRoot.CreateSubKey("CLSID\\" + AddInGuid(t));
  17.                 clsid.SetValue(null, "InventorAddIn1");
  18.                 subKey = clsid.CreateSubKey("Implemented Categories\\{39AD2B5C-7A29-11D6-8E0A-0010B541CAA8}");
  19.                 subKey.Close();
  20.  
  21.                 subKey = clsid.CreateSubKey("Settings");
  22.                 subKey.SetValue("AddInType", "Standard");
  23.                 subKey.SetValue("LoadOnStartUp", "1");
  24.  
  25.                 //subKey.SetValue("SupportedSoftwareVersionLessThan", "");
  26.                 subKey.SetValue("SupportedSoftwareVersionGreaterThan", "12..");
  27.                 //subKey.SetValue("SupportedSoftwareVersionEqualTo", "");
  28.                 //subKey.SetValue("SupportedSoftwareVersionNotEqualTo", "");
  29.                 //subKey.SetValue("Hidden", "0");
  30.                 //subKey.SetValue("UserUnloadable", "1");
  31.                 subKey.SetValue("Version", 0);
  32.                 subKey.Close();
  33.  
  34.                 subKey = clsid.CreateSubKey("Description");
  35.                 subKey.SetValue(null, "InventorAddIn1");
  36.             }
  37.             catch
  38.             {
  39.                 System.Diagnostics.Trace.Assert(false);
  40.             }
  41.             finally
  42.             {
  43.                 if (subKey != null) subKey.Close();
  44.                 if (clsid != null) clsid.Close();
  45.                 if (clssRoot != null) clssRoot.Close();
  46.             }
  47.  
  48.         }
  49.  
  50.         ///
  51.         /// Unregisters this class as an Add-In for Autodesk Inventor.
  52.         /// This function is called when the assembly is unregistered.
  53.         ///
  54.         [ComUnregisterFunctionAttribute()]
  55.         public static void Unregister(Type t)
  56.         {
  57.             RegistryKey clssRoot = Registry.ClassesRoot;
  58.             RegistryKey clsid = null;
  59.  
  60.             try
  61.             {
  62.                 clssRoot = Microsoft.Win32|>.Registry.ClassesRoot;
  63.                 clsid = clssRoot.OpenSubKey("CLSID\\" + AddInGuid(t), true);
  64.                 clsid.SetValue(null, "");
  65.                 clsid.DeleteSubKeyTree("Implemented Categories\\{39AD2B5C-7A29-11D6-8E0A-0010B541CAA8}");
  66.                 clsid.DeleteSubKeyTree("Settings");
  67.                 clsid.DeleteSubKeyTree("Description");
  68.             }
  69.             catch { }
  70.             finally
  71.             {
  72.                 if (clsid != null) clsid.Close();
  73.                 if (clssRoot != null) clssRoot.Close();
  74.             }
  75.         }
  76.  
  77.         // This function uses reflection to get the value for the GuidAttribute attached to the class.
  78.         private static String AddInGuid(Type t)
  79.         {
  80.             string guid = "";
  81.  
  82.             try
  83.             {
  84.                 Object[] customAttributes = t.GetCustomAttributes(typeof(GuidAttribute), false);
  85.                 GuidAttribute guidAttribute = (GuidAttribute)customAttributes[0];
  86.                 guid = "{" + guidAttribute.Value.ToString() + "}";
  87.             }
  88.             catch
  89.             {
  90.             }
  91.  
  92.             return guid;
  93.  
  94.         }
  95.  
  96.         #endregion

Funções que devem ser implementadas em nosso AddIn. São herdades de Inventor.ApplicationAddInServer(Falerei mais detalhes sobre elas em outro post para não fugir deste escopo).

#region ApplicationAddInServer Members
  1.  
  2.         public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
  3.         {
  4.             // This method is called by Inventor when it loads the addin.
  5.             // The AddInSiteObject provides access to the Inventor Application object.
  6.             // The FirstTime flag indicates if the addin is loaded for the first time.
  7.  
  8.             // Initialize AddIn members.
  9.             m_inventorApplication = addInSiteObject.Application;
  10.  
  11.             // TODO: Add ApplicationAddInServer.Activate implementation.
  12.             // e.g. event initialization, command creation etc.
  13.         }
  14.  
  15.         public void Deactivate()
  16.         {
  17.             // This method is called by Inventor when the AddIn is unloaded.
  18.             // The AddIn will be unloaded either manually by the user or
  19.             // when the Inventor session is terminated
  20.  
  21.             // TODO: Add ApplicationAddInServer.Deactivate implementation
  22.  
  23.             // Release objects.
  24.             Marshal.ReleaseComObject(m_inventorApplication);
  25.             m_inventorApplication = null;
  26.  
  27.             GC.WaitForPendingFinalizers();
  28.             GC.Collect();
  29.         }
  30.  
  31.         public void ExecuteCommand(int commandID)
  32.         {
  33.             // Note:this method is now obsolete, you should use the
  34.             // ControlDefinition functionality for implementing commands.
  35.         }
  36.  
  37.         public object Automation
  38.         {
  39.             // This property is provided to allow the AddIn to expose an API
  40.             // of its own to other programs. Typically, this  would be done by
  41.             // implementing the AddIn's API interface in a class and returning
  42.             // that class object through this property.
  43.  
  44.             get
  45.             {
  46.                 // TODO: Add ApplicationAddInServer.Automation getter implementation
  47.                 return null;
  48.             }
  49.         }
  50.  
  51.         #endregion

E é isso pessoal! :) Obrigado!

Compartilhar



Deixe uma réplica

Twitts

  • autodesk authorized http://www.w3.org/1999/xhtml

GRAPHO Software - Todos os direitos reservados - 2012