点击查看详细介绍

分享C# NXOpen源代码 组件视图管理器

hiboboloo1988 2年前 15228 4

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();
    }

}



0

最新回复 (4)
  • shuyangchao 1月前
    1

    学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习学习

  • 序曲 24天前
    2

    学习学习学习学习

  • yy306683560 20天前
    3

    感谢楼主分享,多谢楼主!

  • dhdhdhasdf 19天前
    4

    感谢分享谢谢。。。。。。。

请登录后发表新帖