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:
Então criamos um novo projeto com este template. Após este passo seu explorer deve estar assim:
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)
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.
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:
Entendendo o código:
Adicionando os namespaces necessários:
-
using System;
-
using System.Runtime.InteropServices;
-
using Inventor;
-
using Microsoft.Win32|>;
-
using System.Diagnostics;
Funções de registro. São chamadas pelo utilitário de registro do .NET.
-
#region COM Registration functions
-
-
///
-
/// Registers this class as an Add-In for Autodesk Inventor.
-
/// This function is called when the assembly is registered for COM.
-
///
-
[ComRegisterFunctionAttribute()]
-
public static void Register(Type t)
-
{
-
RegistryKey clssRoot = Registry.ClassesRoot;
-
RegistryKey clsid = null;
-
RegistryKey subKey = null;
-
-
try
-
{
-
clsid = clssRoot.CreateSubKey("CLSID\\" + AddInGuid(t));
-
clsid.SetValue(null, "InventorAddIn1");
-
subKey = clsid.CreateSubKey("Implemented Categories\\{39AD2B5C-7A29-11D6-8E0A-0010B541CAA8}");
-
subKey.Close();
-
-
subKey = clsid.CreateSubKey("Settings");
-
subKey.SetValue("AddInType", "Standard");
-
subKey.SetValue("LoadOnStartUp", "1");
-
-
//subKey.SetValue("SupportedSoftwareVersionLessThan", "");
-
subKey.SetValue("SupportedSoftwareVersionGreaterThan", "12..");
-
//subKey.SetValue("SupportedSoftwareVersionEqualTo", "");
-
//subKey.SetValue("SupportedSoftwareVersionNotEqualTo", "");
-
//subKey.SetValue("Hidden", "0");
-
//subKey.SetValue("UserUnloadable", "1");
-
subKey.SetValue("Version", 0);
-
subKey.Close();
-
-
subKey = clsid.CreateSubKey("Description");
-
subKey.SetValue(null, "InventorAddIn1");
-
}
-
catch
-
{
-
System.Diagnostics.Trace.Assert(false);
-
}
-
finally
-
{
-
if (subKey != null) subKey.Close();
-
if (clsid != null) clsid.Close();
-
if (clssRoot != null) clssRoot.Close();
-
}
-
-
}
-
-
///
-
/// Unregisters this class as an Add-In for Autodesk Inventor.
-
/// This function is called when the assembly is unregistered.
-
///
-
[ComUnregisterFunctionAttribute()]
-
public static void Unregister(Type t)
-
{
-
RegistryKey clssRoot = Registry.ClassesRoot;
-
RegistryKey clsid = null;
-
-
try
-
{
-
clssRoot = Microsoft.Win32|>.Registry.ClassesRoot;
-
clsid = clssRoot.OpenSubKey("CLSID\\" + AddInGuid(t), true);
-
clsid.SetValue(null, "");
-
clsid.DeleteSubKeyTree("Implemented Categories\\{39AD2B5C-7A29-11D6-8E0A-0010B541CAA8}");
-
clsid.DeleteSubKeyTree("Settings");
-
clsid.DeleteSubKeyTree("Description");
-
}
-
catch { }
-
finally
-
{
-
if (clsid != null) clsid.Close();
-
if (clssRoot != null) clssRoot.Close();
-
}
-
}
-
-
// This function uses reflection to get the value for the GuidAttribute attached to the class.
-
private static String AddInGuid(Type t)
-
{
-
string guid = "";
-
-
try
-
{
-
Object[] customAttributes = t.GetCustomAttributes(typeof(GuidAttribute), false);
-
GuidAttribute guidAttribute = (GuidAttribute)customAttributes[0];
-
guid = "{" + guidAttribute.Value.ToString() + "}";
-
}
-
catch
-
{
-
}
-
-
return guid;
-
-
}
-
-
#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).
-
-
public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
-
{
-
// This method is called by Inventor when it loads the addin.
-
// The AddInSiteObject provides access to the Inventor Application object.
-
// The FirstTime flag indicates if the addin is loaded for the first time.
-
-
// Initialize AddIn members.
-
m_inventorApplication = addInSiteObject.Application;
-
-
// TODO: Add ApplicationAddInServer.Activate implementation.
-
// e.g. event initialization, command creation etc.
-
}
-
-
public void Deactivate()
-
{
-
// This method is called by Inventor when the AddIn is unloaded.
-
// The AddIn will be unloaded either manually by the user or
-
// when the Inventor session is terminated
-
-
// TODO: Add ApplicationAddInServer.Deactivate implementation
-
-
// Release objects.
-
Marshal.ReleaseComObject(m_inventorApplication);
-
m_inventorApplication = null;
-
-
GC.WaitForPendingFinalizers();
-
GC.Collect();
-
}
-
-
public void ExecuteCommand(int commandID)
-
{
-
// Note:this method is now obsolete, you should use the
-
// ControlDefinition functionality for implementing commands.
-
}
-
-
public object Automation
-
{
-
// This property is provided to allow the AddIn to expose an API
-
// of its own to other programs. Typically, this would be done by
-
// implementing the AddIn's API interface in a class and returning
-
// that class object through this property.
-
-
get
-
{
-
// TODO: Add ApplicationAddInServer.Automation getter implementation
-
return null;
-
}
-
}
-
-
#endregion
E é isso pessoal!
Obrigado!












