using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using NXOpen;
using NXOpen.Assemblies;
using NXOpen.UF;
// This class demonstrates a simple viewer that displays the tree structure of an
// assembly. The color of the selected component can be changed by pressing the "Set
// Color" button.
public class AssemblyViewer : System.Windows.Forms.Form
{
// Cached NXOpen session
private static NXOpen.Session theSession;
private static NXOpen.UF.UFSession theUFSession;
// The active AssemblyViewer
private static AssemblyViewer theViewer;
// Hashtable containing basic color names and their corresponding color IDs.
// For simplicity, we are just supporting the basic primaries. See UFDisp
// for a full set of color handling methods.
private static Hashtable colorTable;
// Lookup table which maps the selected tree node onto a component
Hashtable componentTable = new Hashtable();
// Currently selected component
Component selectedComponent = null;
// Controls whether to draw just the selected component, or the selected
// component and all similar components.
private static bool drawAll = false;
// Identifier for the part callbacks
private IntPtr onChangeWorkPartID;
private IntPtr onClosePartID;
// Delegates for the part callbacks. Note that these need to be declared
// as members rather than local variables to ensure that they are not
// garbage collected
UF.CallbackFnT WorkPartCallbackFunction;
UF.CallbackFnT ClosePartCallbackFunction;
public AssemblyViewer()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
FillColorBox();
// Fill in the assembly tree
UpdateTree();
// Make the displayed window a child of the main NX window
NXOpenUI.FormUtilities.SetApplicationIcon(this);
NXOpenUI.FormUtilities.ReparentForm(this);
// Register methods to be invoked when the work part changes, or a part is closed.
WorkPartCallbackFunction = new UF.CallbackFnT( OnChangeWorkPart );
theUFSession.UF.AddCallbackFunction(CallbackReason.ChangeWorkPartReason,
WorkPartCallbackFunction ,
IntPtr.Zero, out onChangeWorkPartID);
ClosePartCallbackFunction = new UF.CallbackFnT( OnClosePart );
theUFSession.UF.AddCallbackFunction(CallbackReason.ClosePartReason,
ClosePartCallbackFunction ,
IntPtr.Zero, out onClosePartID);
}
// Fills in the entries in the color combo box
private void FillColorBox()
{
ICollection colorNames = colorTable.Keys;
foreach (string key in colorTable.Keys)
this.colorBox.Items.Add(key);
this.colorBox.SelectedIndex = 0;
}
// Registered method which is invoked when the work part changes.
void OnChangeWorkPart( CallbackReason reason,
IntPtr application_data,
IntPtr user_data)
{
// We should catch any exceptions in our callback, so that we don't
// return an exception to NX.
try
{
HighlightComponent(selectedComponent, false);
UpdateTree();
}
catch (Exception ex)
{
theSession.LogFile.WriteLine("Got exception in callback");
theSession.LogFile.WriteLine(ex.ToString());
}
}
// Registered method which is invoked when a part closes
void OnClosePart( CallbackReason reason,
IntPtr application_data,
IntPtr user_data)
{
// We should catch any exceptions in our callback, so that we don't
// return an exception to NX.
try
{
// If the display part has been closed, call UpdateTree(),
// which will clear all elements from the tree.
if (theSession.Parts.Display == null)
UpdateTree();
}
catch (Exception ex)
{
theSession.LogFile.WriteLine("Got exception in callback");
theSession.LogFile.WriteLine(ex.ToString());
}
}
// Updates the assemblyTree that contains the assembly structrue. Called at startup,
// and whenever the work part changes.
private void UpdateTree()
{
assemblyTree.Nodes.Clear();
componentTable.Clear();
selectedComponent = null;
if (theSession.Parts.Display != null)
{
// For a piece part, the RootComponent will be null, so we don't display
// an assembly tree.
Component rootComponent = theSession.Parts.Display.ComponentAssembly.RootComponent;
if (rootComponent != null)
{
TreeNode rootNode = new TreeNode("ROOT");
assemblyTree.Nodes.Add(rootNode);
componentTable[rootNode] = rootComponent;
AddChildren(rootNode, rootComponent);
rootNode.Expand();
}
}
}
// Recursive method that adds the children of "component" to the tree node "node"
private void AddChildren(TreeNode node, Component component)
{
Component[] childComponents = component.GetChildren();
for (int i = 0; i < childComponents.Length; i++)
{
TreeNode childNode = new TreeNode(childComponents[i].Name);
componentTable[childNode] = childComponents[i];
node.Nodes.Add(childNode);
AddChildren(childNode, childComponents[i]);
}
}
// Invoked when the viewer is closed. Unhighlight any components
// that we have selected, and unregister the work part callback.
private void OnClose(object sender, System.ComponentModel.CancelEventArgs e)
{
HighlightComponent(selectedComponent, false);
selectedComponent = null;
UnregisterCallbacks();
}