In continuation to my previous post on Customizing Inventor using C#, I go a step further and interact with an opened Assembly through API. I have ported the code from VBA to C# for the example explained in Introduction to Inventor API and VBA (Visual Basic for Applications). The attached document is from Autodesk University of 2003.
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 DocumentsVisual Studio 2005ProjectsWindowsApplication1WindowsApplication1binDebugWindowsApplication1.exe . Double clicking on the exe file would also popup the Form that was developed.
Form1.cs code
[sourcecode language=’c#’]
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;
}
}
}
}
}
}
[/sourcecode]