NX二次开发-基于Winform界面对话框与NXOPEN C#交互的开发(对话框嵌套)

一.用类库方式创建

1.新建类库

进来后编译代码,成功

 添加NXOPEN的库到项目中

 

 

 不同NX版本,可能dll所在位置不一样,NX11以上版本在NXBIN这里,NX11以下版本大概在UGII。

 添加头文件

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;

 2.添加winform窗体

 

 

 3.添加main入口函数和卸载方式

 1 public static int Main()
 2 {
 3     Form1 aa = new Form1();
 4     aa.Show();//1.使用.show()为非模态对话框2.使用.ShowDialog()为模态对话框
 5 
 6     return 0;
 7 }
 8 
 9 public static int GetUnloadOption(string dummy)
10 {
11     return UFConstants.UF_UNLOAD_UG_TERMINATE;//卸载方式显示卸载
12 }
13 
14 Caesar卢尚宇
15 2020年3月1日

4.去winform对话框设计控件

在工具箱中拖过去botton控件

 设置控件属性

 更改控件显示的名字

更改控件ID

 

添加回调函数,按回车创建。

 

 

 依次在添加如下几个控件,就不详细截图了。不会弄,去看C# winform使用相关知识

添加头文件

 添加Session代码

5.去回调函数里添加代码Form1.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 using NXOpen;
12 using NXOpen.Utilities;
13 using NXOpen.UF;
14 
15 namespace test
16 {
17     public partial class Form1 : Form
18     {
19         public Form1()
20         {
21             InitializeComponent();
22         }
23 
24         public static Session theSession = Session.GetSession();
25         public static UFSession theUfSession = UFSession.GetUFSession();
26         public static UFUi theUFUi = theUfSession.Ui;
27 
28         double[] base_pt;
29         private void setOrigin(object sender, EventArgs e)
30         {
31             theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM);//对话框加锁
32 
33             //使用点构造器创建点  
34             string cue = "请选择一点";
35             UFUi.PointBaseMethod basemethod = UFUi.PointBaseMethod.PointCursorPos;
36             Tag tPt;
37             base_pt = new double[3];
38             int resp;
39             theUFUi.PointConstruct(cue, ref basemethod, out tPt, base_pt, out resp);
40 
41             theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM);//对话框解锁
42         }
43 
44         private void createBlock(object sender, EventArgs e)
45         {
46             FeatureSigns sign = FeatureSigns.Nullsign;//定义布尔
47             string[] edge_len = { textL.Text, textW.Text, textH.Text };//定义长宽高
48             Tag blk_obj_id = new Tag();
49             theUfSession.Modl.CreateBlock1(sign, base_pt, edge_len, out blk_obj_id);//创建块
50 
51             this.Close();//关闭对话框
52         }
53     }
54 }
55 Caesar卢尚宇
56 2020年3月1日

 Caesar卢尚宇

2020年3月1日

二.NXOPEN C#向导方式创建(对话框嵌套在NX主窗口)

2020年7月5日新增

1.新建项目

 

更改卸载方式,要不然Winform对话框显示不出来

 

 2.添加对话框

 

 

 3.调用对话框

1.添加对话框命名空间

using System;
using NXOpen;
using NXOpen.UF;
using NXOpenUI;
using NXOpen.Utilities;
using NX9_Open_CS_Wizard3;

 2.在main函数里调用对话框

//调用对话框
MyDialog dlg = new MyDialog();

//使显示的窗口成为主NX窗口的子窗口
NXOpenUI.FormUtilities.SetApplicationIcon(dlg);
NXOpenUI.FormUtilities.ReparentForm(dlg);
dlg.Show();

 3.设置对话框属性

图片出处  https://study.163.com/course/introduction/1209726808.htm

根据需要设置对应的属性(我这里只设置两处)

 4.演示

参考资料

https://www.ugapi.com/thread-10016.html

https://www.icax.org/thread-191354-1-1.html

Caesar卢尚宇

2020年7月5日

原文地址:https://www.cnblogs.com/nxopen2018/p/12387596.html