Summary: How to Add extra buttons to already existing Addin Command Bar.
This is in continuation of my earlier posts on Developing Inventor Addins using C# Part1, Part2, Part3 and Part4. In our last screencast, I had explained how to replicate assembly tree view inside our addin command dialog. We go a step further and interact with the opened Assembly document / file.
In this ScreenCast, we mainly deal with How to add another button to our existing addin command bar. By clicking on that button, a form would show up. We use SelectSet method of Inventor to capture user Selections. In screencasts to follow, we would try to explore how InteractionEvents can be used in place of SelectSet.
Upon selecting an ocurrence (Part/ Sub-assembly), the label in our Form is changed to its Display Name. Its not a big deal though, but atleast we are heading in the right direction.
Would love to hear any feedback in the form of comments.
This is in continuation of my earlier posts on Developing Addins for Autodesk Inventor using C# Part1, Part2 and Part3. In this screencast, I have introduced a TreeView component in our Custom Form, which gets displayed upon clicking on our Addin Button. I have also introduced concept of ImageList briefly and how it was coupled together with our TreeView.
We also added a new set of methods/ functions in AssemblyTreeCmdDlg.cs file. The major part of the code has been taken from AssemblyTree Apprentice Server example, that gets shipped with Inventor SDK.
Upon clicking on Submit button (on our Form), Inventor quickly produces a TreeView with all the Parts, Sub-Assemblies in the active assembly file, and also shows the Constraints that each of these parts / sub-assemblies have.
This post is in continuation of my earlier posts on Developing Addins for Inventor using C# part1 and part2. In this screencast / video tutorial, we go a step forward and show a Form / Command Dialog when our Custom Button is clicked in Inventor. The form created is very much basic. Ideally the form should minimize along with Inventor if Inventor is minimized. That part is not taken care of in this screencast. It would be done in coming tutorials.
When our Custom Button is clicked (OnExecute), a form is displayed with a label and a Submit Button. Upon clicking Submit Button, the file name of the active document (opened file) is retrieved from Inventor Application object and updated in the Form.
This video tutorial is in continuation of my earlier post on Developing Autodesk Inventor Addin using C# - part 1. In this session, we go a step further and create Command Button(s), that is added into a Custom Command Bar, which in turn gets added into Panel Bar of Assembly Files.
We start with a project that gets formed when we use Inventor Addins Template for C# language. We then add a couple of references and use most of the code from SimpleAddin that gets shipped along with Inventor in its SDK kit. We use the Button class from SimpleAddin as it is, and then append code to StandardAddInServer.cs file as explained in the video.
Some of the main points discussed in the video are as follows:
How to deal with User Interface Event
How to deal with Event Handler for Command Bars and Environments on Reset
How to use ‘try & catch’ loop to handle errors effectively
How to extend Button base class. Each ButtonCommand (AssemblyTreeButton) will have a Class which implements Button class
How to load Image icons (.ico) for User Interface items
How to create Buttons (CommandButtons)
How to create Command Category
How to add Button to Command Category
How to create a custom command bar (toolbar), only the first time the addin loads up
How to add a custom command bar to an available Environment (again only first time)
How to Restore CommandBars and Environments and clean up the custom CommandBars and Environment changes done by activate method
How to run a command when a button is clicked
How to Build a Solution from Visual Studio 2005
How to register and unregister an Addin using RegAsm.exe (easier way)
How to check whether you addin has been loaded in the Inventor when it is started
I hope a lot of basics of a Command Button are covered in this video tutorial. In the coming ones, I would try to work on “Debugging an Addin from Visual Studio” and more complex addins which actually do something other than just showing some messages.
Please comment below if there is anything that is not clear. To view high resolution Videos of the above screencasts, check them out at AR-CAD at part2a, part2b, part2c.
Update: Looks like the install.bat and uninstall.bat files content are not very clear in the video, here are they in text
I hope you have gone through my earlier video tutorial and post, as they provide a good (if not great) foundation for basic concepts of Autodesk Inventor API and how it Inventor can be customized using Visual C#. In the previous posts, we had connected to Inventor as an external exe file, which runs out 0f process with respect to Inventor and hence can be considered slow. To make the addins work faster and to give the end user a better work-flow and experience, we have to make an addin which is internal to Inventor and which runs in-process with respect to Inventor.
In the above videos ( they have been broken into part 1a and 1b as youtube couldn’t upload the whole video, which is 12 mins long), basic introduction is given to “How to develop an addin to Inventor using C#”. Please go through it and I hope the explanation in it is good enough to get you started. In Next versions of this tutorial, I shall try to increase the complexities. If there are any doubts/ clarifications / suggestions, I would love to hear from you as comments to this post. Watch High Resolution video of both above videos on AR-CAD
Start a new C# Windows Application project in Visual Studio 2005, as done in previous tutorial. Design the form as shown in the image to the left. Double clicking on Hide / Show will take you to Form1.cs in the Code Viewer window and add/append the following code into it. Please note that I have changed the Names/Identifiers of form elements to txtSearchName , cmdShow, cmdHide as explained in the above tutorial document.
In this example, when you hit ‘F5′ or Debug, a windows form appears as shown. Type a part name in the Search Field and hit “Hide”. If any part(s) exist in the opened assembly with that name, it/they would be made invisible. If its hidden and you hit on “Show”, the part would be made visible. In our case, we have made Piston part invisible in the engine assembly that ships with Inventor as an example assembly. Save the application by File> Save All in Visual Studio.
You can also use this Windows Application by executing the .exe file that is produced, when you debug/build your application. In my case, its at My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\WindowsApplication1.exe . Double clicking on the exe file would also popup the Form that was developed.
Form1.cs code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
//Instantiate a variable with Inventor.Application object.
//It should have a scope throughout this Class
private Inventor.Application oApp=(Inventor.Application)System.Runtime.
InteropServices.Marshal.GetActiveObject("Inventor.Application");
//Declare oAsmDoc to have a scope within this Class
private Inventor.AssemblyDocument oAsmDoc;
public Form1()
{
InitializeComponent();
//Check that a document is open
if (oApp.Documents.Count == 0)
{
MessageBox.Show("An Assembly must be active");
}
//Check that an assembly document is active
if (oApp.ActiveDocument.DocumentType !=
Inventor.DocumentTypeEnum.kAssemblyDocumentObject)
{
MessageBox.Show("As Assembly document must be active");
}
//First Type Cast ActiveDocument into AssemblyDocument
//Set a reference to the active document
oAsmDoc = (Inventor.AssemblyDocument) oApp.ActiveDocument;
}
private void cmdHide_Click(object sender, EventArgs e)
{
//Call the function that traverses the assembly
// and sets the visibility.
SetVisibility(oAsmDoc.ComponentDefinition.Occurrences,
txtSearchName.Text, false);
//Update the View.
oApp.ActiveView.Update();
}
private void cmdShow_Click(object sender, EventArgs e)
{
//Call the function that traverses the assembly
// and sets the visibility.
SetVisibility(oAsmDoc.ComponentDefinition.Occurrences,
txtSearchName.Text, true);
//Update the View.
oApp.ActiveView.Update();
}
private static void SetVisibility
(Inventor.ComponentOccurrences Occurences, string SearchName,
bool VisibilityOn)
{
//Iterate through each of the occurences
//in the collection provided.
foreach (Inventor.ComponentOccurrence oOccurence in Occurences)
{//Check to see if the occurence name matches the search name
//The strings are converted to lower case
// to remove context sensitivity.
if(oOccurence.Name.ToLower().Contains(SearchName.ToLower()))
{//Check to see if the visibility is different than the specified
if (oOccurence.Visible != VisibilityOn)
{//Set the Visibility of the occurence.
oOccurence.Visible = VisibilityOn;
}
}
}
}
}
}
I hope you have gone through my previous posts on Inventor API a) Introduction to Autodesk Inventor API and Customization and b) Customize Autodesk Inventor using C#. It is also assumed that you have gone through DevTV: Introduction to Inventor Programming Video, which is on Inventor Customization page. In the above video, we connect to Inventor API from Visual C# (Visual Studio 2005). I have just replaced the VB.NET code that was used in DevTV tutorial with the corresponding C# code. You can see the comparison below. Please note how GetObject in VB.NET is replaced by a much more lengthier code. If the above youtube video is not very clear, watch it on AR-CAD.
VB.NET Code
Dim oApp As Inventor.Application
oApp = GetObject( , "Inventor.Application")
MsgBox("Number of open docs = " & oApp.Documents.Count)
Visual C# Code
Inventor.Application oApp;
//The below initialization is on a single line
oApp =
(Inventor.Application)System.Runtime.InteropServices.Marshall.
GetActiveObject("Inventor.Application");
int number_int = oApp.Documents.Count;
string number_string = Convert.ToString(number_int);
MessageBox.Show ("Number of open docs =" + number_string);
I am a Mechanical Engineer turned CAD Enthusiast. My interests include CAD, Web 2.0 and would love to integrate both. I enjoy Seaside, CodeIgniter. Of late, I have started developing CAD Add-ins for various CAD software including Autodesk Inventor and SpaceClaim