You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过.rtb/.tbr/.men在NX中创建菜单调用WinForms程序无法读取Session对象

问题描述

通过.rtb、.tbr和.men文件在Siemens NX中创建自定义工具栏与菜单项,配置按钮动作调用基于NX Open开发的Windows Forms应用,但已打开工程图图纸时,应用仍无法读取Session对象。

应用背景

开发的WinForms应用包含一个按钮,点击时检查当前NX会话中是否存在工程图图纸,并弹窗显示相应提示。该应用通过手动添加用户命令调用时功能完全正常,但通过配置UGII_USER_DIR自动加载菜单调用时,无法获取Session对象,且应用加载耗时较长。

操作步骤

  • 将包含startup和application子文件夹的NXMenu文件夹置于C盘根目录(C:\NXMenu);
  • startup文件夹存放my_programs.rtb、custom_toolbar.tbr和custom_menu.men配置文件;
  • application文件夹存放WinForms应用程序文件;
  • 创建系统变量UGII_USER_DIR,值设为C:\NXMenu;
  • 启动NX后,自动生成名为“Test Custom Macro”的新标签页及“Test User Command”用户命令项,点击可启动应用但功能异常。

注意事项

该应用为签名.NET程序,通过「File→Execute→NX Open」选择.exe文件,或手动添加用户命令配置调用时均功能正常。

配置文件及代码

my_programs.rtb文件

!
!
!Custom Toolbar File
!
TITLE Test Custom Macro
VERSION 170
DOCK TOP


BUTTON IMPORT_BUTTON
LABEL Import
BITMAP $UGII_USER_DIR\application\icons\iso-filled.bmp

GROUP custom_toolbar.tbr

BUTTON NEW_BUTTON

custom_toolbar.tbr文件

TITLE Drawings
VERSION 170

BUTTON CUSTOM_BUTTON_1
LABEL Test User Command

custom_menu.men文件

! Customer menu file
!
! Contains button definitions to be used in
! toolbars and Ribbon files
!
VERSION 120

EDIT UG_GATEWAY_MAIN_MENUBAR


TOP_MENU
    CASCADE_BUTTON CUSTOMER_MENU
    LABEL Test Customer Menu
END_OF_TOP_MENU

MENU CUSTOMER_MENU
    BUTTON CUSTOM_BUTTON_1
    LABEL Test User Command   
    ACTIONS "C:\NXMenu\application\WindowsFormsApp1.exe"

END_OF_MENU

应用代码

Program.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NXOpen;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {           
                NXOpen.Session theSession = NXOpen.Session.GetSession();
                NXOpen.Part workPart = theSession.Parts.Work;
                NXOpen.Part displayPart = theSession.Parts.Display;

                var drawingSheet = displayPart?.DrawingSheets;
                if (drawingSheet != null)
                {
                    MessageBox.Show("Drawing Sheet Available!");
                }
                else
                {
                    MessageBox.Show("Drawing sheet is not available!");
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        public static int GetUnloadOption(string dummy)
        {
            return (int)NXOpen.Session.LibraryUnloadOption.Immediately;
        }
    }
}

内容的提问来源于stack exchange,提问作者Pavan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 14:40:30