Skip to content

TWiesendanger/ExtrabbitCode.Inventor.Core.Template

Repository files navigation

Introduction

alt text

If you ever copied multiple things from other addins over and over again, then you will like this template. The idea is to provide a template that already includes alot of standard features that every addin needs. If you want to there is nothing to change at all. You could use a generated addin from this template right out of the box. Things like guids are already generated. There is also a build script to automatically deploy it to a predefined folder.

This template is based on the dotnet templating. See Templating

The following features are provided at the moment:

  • setup functioning Inventor addin in minutes
  • default logging with log4net
  • dark and Lighttheme support
  • default buttons for all environments
  • info dialog (show patch notes) (wpf or winforms)
  • unload and load of addin
  • reset of user interface
  • loading settings file
  • structured code base to extend
  • nullability
  • enforced coding style with editorconfig
  • build script
  • inno setup installer script or none
  • documentation for using and extending the addin
  • nuget package for inventor reference See Autodesk.Inventor.Sdk
  • Dependecy isolation

Feel free to ask for other features by emailing me tobias.wiesendanger@gmail.com or by opening a issue.

Version

The template is setup for Inventor 2025 and higher. It uses the .net 8.0 or higher framework. By default the highest currently supported Inventor and .net version is used. You can change this in creation dialog.

Version 1.2.0

  • Added the option to choose version 2027
  • Changed analyzer to no longer treat warnings as errors by default. It can be still activated.
  • Bugfix: New files where marked as content by mistake. They should be C# Compiler normally or whatever the default is.

Demo

The following options are currently possibel:

  • Framework: .net 8.0 / .net 9.0
  • Inventor Version: 2027 / 2026 / 2025
  • Addin Description: your description here
  • InstallFolder: C:\ProgramData\YourCompany\YourAddinName (as an example)
  • UI Framework: WPFUI / Winforms
  • Installer Type: Inno Setup / none

Installation

The template is available as a nuget package. You can install it by running the following command in the terminal:

dotnet new install ExtrabbitCode.Inventor.Core.Template@1.0.2

After this it should be available in the command line or as a selection when creating a new procect in visual studio.

The nuget package can be found here:

Link

Use with User Interface

ui demo image

Choose between these options:

Option Selected value What it means
Framework .NET 8.0 (windows) (Long Term Support) Targets the Windows-specific .NET 8 runtime (LTS).
Inventor Version 2027 The add-in/template is aligned to Autodesk Inventor 2027 (e.g., references, compatibility expectations, packaging target).
Addin Description ExtrabbitCode.Description The description string used for the Inventor add-in metadata.
InstallFolder C:\ProgramData\Inventor.Core.Template Default installation/output folder used by the template/installer logic. ProgramData implies a machine-wide location (not user-profile specific).
UI Framework wpfui UI layer is based on wpfui (WPF + wpfui library), influencing how dialogs/windows are built and styled.

Use on the commandline

dotnet new invAddinCore `
  --name "ExtrabbitCode.MyInventorAddin" `
  --framework "net8.0-windows" `
  --inventorVersion "2027" `
  --addinDescription "My custom Inventor Addin for part automation" `
  --installFolder "C:\ProgramData\MyInventorAddin" `
  --ui "wpfui" `
  --installerType "inno"

This will create a new project in the current folder with the name ExtrabbitCode.MyInventorAddin.

Help providing for this template

Feel free to fork and then create a merge request. The template itself cannot be run directly. This is mainly because there are some variables that get only replaced when using dotnet new.

It's best to create an addin based on the template and develop inside of that. If it's working copy changes to the template.

Use Installer Script

There is a script that allows to easily update the template locally for testing. You can find it in the scripts folder. Just run UpdateTemplate.cmd and it will copy the template files into the local template folder. After that you can use dotnet new invAddinCore to create a new addin based on the template.

After installing check with dotnet new --list if the template is listed. It should be listed like this:

installed template list

It should be listed now when creating a new project like this:

installed template list

UI

There are two options provided. One is using wpf and the other is using winforms. Depending on what you prefer you can use either one. Read the corresponding section to get started. I personally recommend wpf, mainly because it is more modern and scales better on high dpi screens.

WPF

The wpf template uses wpfui for theming. You can find more information about it here: WPF UI There is a sample info dialog that shows how to use it. Make sure to also set the theme when showing a dialog.

This can be done like this:

InfoDialog infoDialog = new();
SetDialogTheme(infoDialog);
infoDialog.ShowDialog();

Look for the 'Button.cs' file to see how it is used.

Info dialog

There is a default dialog provided. Depending on if you choose wpf or winforms for the addin, the dialog will be created accordingly.

image

If you plan to use it, you should edit the file inside of the Resources folder. Whatever text is inside of the versionhistory.txt will be shown in this dialog.

image

Logger

Currently you need to be cautious when using a third party library. If you use a library that is also used by autodesk, you might run into problems. Autodesk uses three different logging libraries currently. NLog, Serilog and Microsoft.Extensions.Logging, so there is not much left to choose from. With log4net you should be safe.

Most of the time you can just create a field like this:

    private static readonly ILog Logger = LogManagerAddin.GetLogger(typeof(UiButton));

and then use it like this:

    Logger.Info("This is an info message");
    Logger.Warn("This is a warning message");
    Logger.Debug("This is a debug message");
    Logger.Error("This is an error message");

For further customization you can look into the log4net.config file. By default the log file will be created in the default windows temp folder.

So normally a folder will be created here: C:\Users\YourUser\AppData\Local\Temp\YourAddinName\YourAddinName.log and the log file will be created there. You can change this in the config file if you want to. Just make sure to not write it into a folder where you dont have write access, otherwise the logging will not work.

Add a new button / command

There are some default buttons that show how a buttons has to be setup, but I would like to show a bit more insight here, because that is a step that probably everyone needs to do. Also every addin out there does that a bit different.

Start in StandardAddinServer.cs by adding a new field. There should be two fields.

image

Remove the existing ones if you dont need them and add new ones here. If you want to follow the microsoft best practise use a "_" before the name.

Next, look inside of the Activate method. There is a sample line that looks like this:

_info = UiDefinitionHelper.CreateButton("Info", "InventorTemplateInfo", @"UI\ButtonResources\Info", theme);

Follow the exact same principel:

_yourFieldName = UiDefinitionHelper.CreateButton("DisplayName", "InternalName", @"UI\ButtonResources\YourCommandName", theme);

For InternalName it might be a goode idea to also add your addin name as a prefix to not risk any conflicting names. This has to be a unique name and also does not collide with already existing ones from autodesk inventor. Make sure to also add it to _buttonDefinitions. This makes sure that the button can be deleted when unloading the addin.

Create a Icon folder

To provide icons for your own button, make sure to create a new Folder inside of UI\ButtonRessources Name it the as the name you used for creating the button. Inside of it you need to provide 4 different icons. There are two sizes that need to be provided. (16x16 adn 32x32) Both of them have to be named exactly like those in the default folders.

Make sure that you provide icons for the dark and the lightTheme. If you dont want to support the darkTheme, feel free to just copy lightTheme icons and rename them. They have to be provided as png.

React on click event

To react on the user clicking your button, you need to add a case in the UI\Button.cs

case "InventorTemplateDefaultButton":
    MessageBox.Show(@"Default message.", @"Default title");
    return;
case "InventorTemplateInfo":
    var infoDlg = new FrmInfo();
    infoDlg.ShowDialog(new WindowWrapper((IntPtr)Globals.InvApp.MainFrameHWND));
    return;
default:
    return;

Just add another case exactly the same way as those that are already provided. The case string has to be the internal name you defined while creating the button. After that call your method from here, to do whatever the button should do.

Add to interface

To make sure that your button is shown in the interface you need to edit the AddToUserInterface. Depending on how and where you want to show it, the steps will be different.

IF you want to create a new tab then do the following:

var yourTab = UiDefinitionHelper.SetupTab("DisplayName", "InternalName", onWhatRibbonToPlace); // use the alreay provided variables for onWhatRibbonToPlace

This is a tab

image

Make sure to also add the tab to _ribbonTabs. As with the buttondefinitions, this allows to remove them easily.

Add a panel to the tab:

var yourPanel = UiDefinitionHelper.SetupPanel("DisplayName", "InternalName", yourTab);

This is a panel

image

Make sure to also add the tab to _ribbonPanels. As with the buttondefinitions, this allows to remove them easily.

As a last step, check if your button is not null (could be if Buttodefinition.Add did not work), and then add it to your panel.

if (_yourButton_ != null)
{
    var yourButtonRibbon = yourPanel.CommandControls.AddButton(_yourButton_, true);
}

Globals

There is a file called Globals.cs, which can be used to get a reference to the Application. Use it like this:

Globals.InvApp

More can be added here but should be used with caution. The more you add here, the more you risk to create a mess.

Add Settings

For settings you should use applicationSettings. This allows to add typesafe settings and no dependencies / nuget packages are needed.

Just add a new setting here:

image

This allows to access them like this: var logPath = Properties.Settings.Default.logPath;

Keep in mind that depending on if the setting is a user setting or an application setting, they behave differently. Application settings get their own section in app.config and cannot be changed while running. User settings can but are now overwritten by a special path depending on the user.

Read more here: Application settings

Use the installer to create a setup

There are two options provided. One is using inno setup and the other is using none. Depending on what you prefer you can use either one. Read the corresponding section to get started.

Inno Setup

The installer uses inno. Make sure to install Setup.

Edit the sample script to your liking. At least the header needs some modifications. You can find the script inside of the Installer folder. The relevant part to change is this one:

#define MyAppName "InventorTemplate"
#define MyAppVersion "0.0.0.1"
#define MyAppPublisher "Company"
#define MyAppURL "http://www.company.com"

Also make sure to change the license.txt file if you dont plan to release your addin with a GNU license.

Before running make sure to create a release build of your addin. The installer script looks for the files inside of the release folder.

Build script

There is a script that is run each time build is used. Look for Buildscript.cmd in the root folder. It is used copy the addin into the standard inventor addin folder. Also some resources are copied if needed.

The manifest is no longer handled by this (look for the property ApplicationManifest in the csproj). The embedding is still needed. Without the addin will not load correctly. You can check if this was successfull by opening the dll file inside of visual studio.

image

If this is not present, something went wrong.

After this step, if the addin is already existing, the addin file will be deleted. After this the file has to be copied from your debug folder to this folder. This will be done automatically by the script.

The last and most important stop is to copy everything belonging to the addin into the predefined installfolder. Here the path that you provided at the begining is used.

image

Other

Change Inventor Version

To change the inventor version, you need to edit the project file. Look for <InventorVersion>$(InventorVersion)</InventorVersion> and change it to the desired version. Even better you look for the Directory.Build.props file in the root folder and change it there. This way all projects will use the same version.

Use iLogic

To use iLogic dll's, you need to edit the project file. You can add <UseILogic>true</UseILogic>.

Analyzer

The analyzer can be pretty strict if TreatWarningAsErrors is used. It forces you to use best practises and avoid common mistakes. If it is to strict for you, feel free to edit the ruleset file called .editorconfig in the root folder. By default using var is not allowed. If you want to change this, just edit the file and change the corresponding rule.

If you want to change this, you can do this by changing this line from the project to true:

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

Troubleshooting

There was an error opening the file

If your addin for some reason doesn't load, check what the Add-Ins Dialog shows.

image

If there was an error opening the file is shown. Make sure that it points to the right dll file. It's not enough to just give the path. So this will not work:

image

but this is fine:

image

All my references are missing

Try dotnet restore inside of the developer console to download any referenced nuget packages. For all the other references, there are different things that can lead to this:

  1. Try to unload the project and reload with dependencies.
  2. Remove project and readd it to the solution.
  3. Remove this from the project file (make sure to get a backup from the file first)
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

This should allow to fix everything other than nuget packages. To restore those use this: dotnet restore inside of the developer shell.

Dependecy isolation

Inventor Addins are loaded into the same process as Inventor. This can lead to some dependency conflicts. To avoid this, the template uses a technique called isolation. This means that the addin is loaded into a separate AppDomain. This way, the addin can have its own dependencies without affecting Inventor or other addins.

Before you might have used another logger version than autodesk is using. This could even happen after a dot update.

About

This is a Template for writting dotnet core based Inventor Addins. Version 2025 and higher.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors