购买与咨询 NX 二次开发视频教程,请联系微信号:13890821008 QQ号:85585969
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;
static AssemblyViewer() {
colorTable = new Hashtable();
colorTable["Blue"] = UFDisp.ColorName.BlueName;
colorTable["Green"] = UFDisp.ColorName.GreenName;
colorTable["Cyan"] = UFDisp.ColorName.CyanName;
colorTable["Red"] = UFDisp.ColorName.RedName;
colorTable["Magenta"] = UFDisp.ColorName.MagentaName;
colorTable["Yellow"] = UFDisp.ColorName.YellowName;
colorTable["White"] = UFDisp.ColorName.WhiteName;
colorTable["Black"] = UFDisp.ColorName.BlackName;
}
// WinForm components
private System.Windows.Forms.TreeView assemblyTree;
private System.Windows.Forms.ComboBox colorBox;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button setColorButton;
private System.Windows.Forms.RadioButton allComponent *** utton;
private System.Windows.Forms.RadioButton singleComponentButton;
private System.Windows.Forms.Button closeButton;

●●●请先
登陆 或
注册 后查看●●●
// 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();
}
}