Skip to content

Autodesk Inventor template to use with dotnet cli

License

Notifications You must be signed in to change notification settings

TWiesendanger/inventorTemplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inventor Template in Csharp

InventorTemplate

Introduction

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 here

The following features are provided at the moment:

  • setup functioning Inventor addin in minutes
  • default logging with nLog
  • dark and Lighttheme support
  • default buttons for all environments
  • info dialog (show patch notes)
  • unload and load of addin
  • reset of user interface
  • loading settings file
  • structured code base to extend
  • build script
  • inno setup installer script
  • documentation for using and extending the addin

Planned features

  • multi language support
  • nuget package

Feel free to ask for other features by emailing me [email protected] or by opening a issue.

Demo

QuickStart

Version

The template is setup for Inventor 2023 but can be used for other versions.

Version higher than 2023

If you want to use this template for a version higher than 2023 you dont need to change anything if you dont want to. Consider if you might want to reference a newer interop dll file.

To do this remove the existing one:

image

and reference the newer one that should be used. It can normaly be found here:
C:\Program Files\Autodesk\Inventor 20xx\Bin\Public Assemblies

Make sure to check the properties after. Embeding is set to true normaly and should be set to false. Also copy should not be needed.

image

Version lower than 2023

The addin is setup to load with 2021 and should load with no problems. If you try to use it with an even lower version you need to change the button / icon methods because these are made for the light and dark theme that was added with 2021. Also make sure to use the correct interop dll as shown above.

Help providing for this template

Feel free to fork and then create a pull 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.

Using this template

Clone this project to an empty folder. After that run the following command to install it:

dotnet new --install "C:\temp\inventorTemplate" in any terminal you like. I prefer to use the windows terminal.

Make sure to replace the path with yours pointing to where the .template.config folder is.

You can test if the installation did work by typing: dotnet new --list

image

create new from template

To see what options are available you can always use this: dotnet new invAddin -h

image

As you can see there is currently one option that needs to be provided.

This could look like this: dotnet new invAddin -n sampleAddin -o "C:\temp\sampleAddin\" -instFld "C:\ProgramData\Company\sampleAddin"

Notice that there is also a -n option that defines the name of the addin and an -o option which allows to determine where to place the project. If you dont provide the -o option the current folder will be used.

Use the addin name at the end of the -o path (see sample command)

provide a installer path with the option -instFld (or --installFolder). This is where the addin will be deployed by the build script.

What to change after creation

If you dont want to you dont need to change anything, but it makes sense to modify things like the description of the addin and a few other things.

Look for a folder called Addin and open a file called *.addin. Inside of it at least the description should be changed.

image

If you plan to use the info dialog that is provided by default make sure to edit the file inside of the Ressources folder. Info Dialog

If you want to use the installer you should have a look inside the Installer folder. Installer

Info dialog

There is a default dialog provided.

image

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

image

Use the installer to create a setup

The installer uses inno. Make sure to install Setup.

Edit the sample script to your liking. At least the header needs some modifications.

#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.

Build script

If you open the properties of the project and look for the tab Build Events there is a script that is run each time build is used. There is at least one important step inside of it that needs to be done.

SET PATHTORUNIN=%CD:bin\Debug=%
"C:\Program Files (x86)\Windows Kits\10\bin\10.0.22000.0\x64\mt.exe" -manifest "%PATHTORUNIN%Addin\InventorTemplate.manifest" -outputresource:"%PATHTORUNIN%bin\Debug"

This will embed the manifest inside the dll file. If this fails 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. Try checking the path to mt.exe. This can also be provided from other sources. If there is no mt.exe file see Troubleshooting

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 in the next step that looks like this:

echo - Copy the .addin file folder into the standard Inventor Addins folder.
XCopy "%PATHTORUNIN%Addin\InventorTemplate.Inventor.addin" "C:\ProgramData\Autodesk\Inventor Addins" /y

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.

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 field already created at Line 34 and 35.

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 principe:

_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

Add Settings

For settings you should use applicationSettings. This allows to add typesafe settings and no dependecies / nuget packages are needed. In a earlier version microsoft.binding was used which lead to some problems.

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 different. 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

Loggin with Nlog

Nlog is now correctly creating its own instance, so it no longer leads to any conflicts when using multiple addins. Use it like this:

var logger = LogManagerAddin2.Instance.GetCurrentClassLogger();
logger.Debug("TEST");

Troubleshooting

dotnet new not available in terminal

To use this you need to install the .NET SDK 3.1 or higher. Download it here.

mt.exe missing

If you dont have a mt.exe, then download the Windows Dev Kit. It's sufficient to install SDK for Desktop C++ x86 Apps. After this look here: C:\Program Files (x86)\Windows Kits\10\bin\<version>\<platform>.

There is now an mt.exe inside of the ressource folder that gets referenced by the build script. So this should no longer be a problem.

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.

About

Autodesk Inventor template to use with dotnet cli

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published