UG NX 二次开发 显示位图的完整列表 C#

 1、简介

在做NX二次开发的过程中,如果引用NX软件的图标,不仅可以节省设计图标的时间,也能开发出NX风格的插件。但是查找NX图标操作繁琐复杂。为了方便查找图标,下面分享一个快速查找位图的一个小工具。界面如下图所示:

UG NX 二次开发 显示位图的完整列表 C#_第1张图片

2、代码

源代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using NXOpen;
using NXOpen.BlockStyler;
using Group = NXOpen.BlockStyler.Group;
using System.Windows;
public class SimpleDialog
{
    private static Session session;
    private static UI ui;
    private BlockDialog dialog;
    private Group grpSelection;
    private StringBlock strWildcard;
    private Label lblInfo;
    private Tree tree;
    private Label lblHits;
    private Group grpSelection1;
    private Label lblBitmap;
    private Label lblBitmapName;
    private Button btnCopy;
    public string bitmapListPath;
    private string dlxPath;
    public List iconNames;

    public SimpleDialog()
    {
        bitmapListPath = @"C:\Users\ylp\Desktop\AllBitmaps_new\allBitmaps.txt";
        dlxPath = @"C:\Users\ylp\Desktop\AllBitmaps_new\SimpleDialog.dlx";

        try
        {
            session = Session.GetSession();
            ui = UI.GetUI();
            dialog = ui.CreateDialog(dlxPath);
            dialog.AddUpdateHandler(new BlockDialog.Update(update_cb));
            dialog.AddInitializeHandler(new BlockDialog.Initialize(initialize_cb));
            dialog.AddDialogShownHandler(new BlockDialog.DialogShown(dialogShown_cb));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public int update_cb(UIBlock block)
    {
        try
        {
            if (block == strWildcard)
            {
                UpdateTreeList();
            }
            else if (block == btnCopy)
            {
                if (String.IsNullOrWhiteSpace(lblBitmapName.Label))
                {
                    System.Windows.Forms.Clipboard.Clear();
                }
                else
                {
                    System.Windows.Forms.Clipboard.SetText(lblBitmapName.Label);
                }
            }
        }
        catch (Exception ex)
        {
            ui.NXMessageBox.Show("Block Styler", 0, ex.ToString());
        }
        return 0;
    }

    public void initialize_cb()
    {
        try
        {
            grpSelection = (Group)dialog.TopBlock.FindBlock("grpSelection");
            strWildcard = (StringBlock)dialog.TopBlock.FindBlock("strWildcard");
            lblInfo = (Label)dialog.TopBlock.FindBlock("lblInfo");
            tree = (Tree)dialog.TopBlock.FindBlock("tree");
            lblHits = (Label)dialog.TopBlock.FindBlock("lblHits");
            grpSelection1 = (Group)dialog.TopBlock.FindBlock("grpSelection1");
            lblBitmap = (Label)dialog.TopBlock.FindBlock("lblBitmap");
            lblBitmapName = (Label)dialog.TopBlock.FindBlock("lblBitmapName");
            btnCopy = (Button)dialog.TopBlock.FindBlock("btnCopy");
            tree.SetOnSelectHandler(OnSelectCallback);
        }
        catch (Exception ex)
        {
            Exception exception = ex;
        }
    }

    public void dialogShown_cb()
    {
        try
        {
            PropertyList properties = this.strWildcard.GetProperties();
            properties.SetString("Value", "");
            properties.Dispose();
            tree.InsertColumn(0, "Name", 200);
            tree.SetColumnSortOption(0, 0);
            tree.InsertColumn(1, "Bitmap", 50);
            tree.SetColumnSortOption(1, 0);
            tree.SetColumnDisplayType(1, Tree.ColumnDisplay.Icon);
            iconNames = new List(File.ReadAllLines(bitmapListPath));
            UpdateTreeList();
        }
        catch (Exception ex)
        {
            Exception exception = ex;
        }
    }

    public static void Main()
    {
        SimpleDialog simpleDialog = null;
        try
        {
            simpleDialog = new SimpleDialog();
            simpleDialog.Show();
        }
        catch (Exception ex)
        {
            ui.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
        }
        finally
        {
            if (simpleDialog != null)
                simpleDialog.Dispose();
            simpleDialog = null;
        }
    }

    public void Dispose()
    {
        if (dialog != null)
        {
            dialog.Dispose();
            dialog = null;
        }
    }

    public static int GetUnloadOption(string arg)
    {
        return 1;
    }

    public List GetAllNodes()
    {
        List nodes = new List();
        Node rootNode = tree.RootNode;
        nodes.Add(rootNode);
        for (Node i = rootNode.NextNode; i != null; i = i.NextNode)
        {
            nodes.Add(i);
        }
        return nodes;
    }

    public void UpdateTreeList()
    {
        List.Enumerator enumerator = new List.Enumerator();
        try
        {
            if (tree.RootNode != null)
            {
                foreach (var node in GetAllNodes())
                    tree.DeleteNode(node);
            }

            var filter = strWildcard.Value;
            if (String.IsNullOrWhiteSpace(filter))
                filter = "*";

            filter = filter.Replace("*", ".*");
            filter = filter.Replace("?", ".");
            var regex = new Regex(filter);

            foreach (var name in iconNames)
            {
                if (regex.IsMatch(name))
                {
                    Node node = tree.CreateNode(name);
                    tree.InsertNode(node, null, null, Tree.NodeInsertOption.Last);
                    node.SetColumnDisplayText(1, name);
                }
            }
        }
        finally
        {
            ((IDisposable)enumerator).Dispose();
        }
    }

    public void OnSelectCallback(Tree tree, Node node, int columnID, bool selected)
    {
        lblBitmap.Bitmap = node.DisplayText;
        lblBitmapName.Label = node.DisplayText;
    }

    public void Show()
    {
        try
        {
            dialog.Show();
        }
        catch (Exception ex)
        {
            ui.NXMessageBox.Show("Block Styler", 0, ex.ToString());
        }
    }

    public static void UnloadLibrary(string arg)
    {
        try
        {

        }
        catch (Exception ex)
        {
            ui.NXMessageBox.Show("Block Styler", 0, ex.ToString());
        }
    }

}

allBitmaps.txt 文件下载地址:【免费】所有UGNX默认图标(位图)列表allBitmaps资源-CSDN文库

你可能感兴趣的:(UGOpen,C#,c#,开发语言)