iCAx开思工具箱

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1374|回复: 0
打印 上一主题 下一主题

[技术交流] 分享C# NXOpen源代码 组件视图管理器

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-20 11:16:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册登录,享用更多网站功能!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

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 allComponentsButton;
    private System.Windows.Forms.RadioButton singleComponentButton;
    private System.Windows.Forms.Button closeButton;


    // 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;
    }


    // Removes the registered callbacks
    private void UnregisterCallbacks()
    {
        theUFSession.UF.RemoveCallbackFunction(onChangeWorkPartID);
        theUFSession.UF.RemoveCallbackFunction(onClosePartID);
    }

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

}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 支持支持
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手板模型制作,在线3D打印服务

QQ|小黑屋|手机版|开思工具箱 CAD工具箱_CAM工具箱  

GMT+8, 2024-4-26 08:34 , Processed in 0.012020 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.3

© 2002-2024 www.iCAx.org

快速回复 返回顶部 返回列表