news 2026/8/22 18:20:20

NX二次开发C#-获取曲线最小曲率半径

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
NX二次开发C#-获取曲线最小曲率半径

摘要:本文提供了一个完整的NX Open Block Styler C#应用程序,用于在Siemens NX中计算选定曲线的最大曲率。程序通过UF_MODL_ask_max_curvature函数获取曲率数据,能区分恒定曲率曲线(如圆/圆弧)和变曲率曲线,输出曲率参数、坐标点和曲率半径信息。代码包含对话框初始化、事件回调处理等完整功能模块,通过Block UI Styler生成的DLX文件实现用户交互界面。程序还处理了曲线类型判断和结果格式化显示,为NX二次开发提供了实用的曲率分析工具。

//============================================================================== // WARNING!! This file is overwritten by the Block UI Styler while generating // the automation code. Any modifications to this file will be lost after // generating the code again. // // Filename: E:\NXopen\Test\Application\SelectCurve.cs // // This file was generated by the NX Block UI Styler // Created by: 地球驾驶员 // Version: Designcenter 2512 // Date: 08-20-2026 (Format: mm-dd-yyyy) // Time: 22:56 (Format: hh-mm) // //============================================================================== //============================================================================== // Purpose: This TEMPLATE file contains C# source to guide you in the // construction of your Block application dialog. The generation of your // dialog file (.dlx extension) is the first step towards dialog construction // within NX. You must now create a NX Open application that // utilizes this file (.dlx). // // The information in this file provides you with the following: // // 1. Help on how to load and display your Block UI Styler dialog in NX // using APIs provided in NXOpen.BlockStyler namespace // 2. The empty callback methods (stubs) associated with your dialog items // have also been placed in this file. These empty methods have been // created simply to start you along with your coding requirements. // The method name, argument list and possible return values have already // been provided for you. //============================================================================== //------------------------------------------------------------------------------ //These imports are needed for the following template code //------------------------------------------------------------------------------ using NXOpen; using NXOpen.BlockStyler; using NXOpen.UF; using NXOpen.Utilities; using System; using System.Runtime.InteropServices; //------------------------------------------------------------------------------ //Represents Block Styler application class //------------------------------------------------------------------------------ public class SelectCurve { //class members private static Session theSession = null; private static UI theUI = null; private string theDlxFileName; private NXOpen.BlockStyler.BlockDialog theDialog; private NXOpen.BlockStyler.Group group0; private NXOpen.BlockStyler.CurveCollector edge_select0;// Block type: Curve Collector //------------------------------------------------------------------------------ //Bit Option for Property: EntityType //------------------------------------------------------------------------------ public static readonly int EntityType_AllowEdges = (1 << 0); public static readonly int EntityType_AllowCurves = (1 << 2); public static readonly int EntityType_AllowPoint = (1 << 3); public static readonly int EntityType_AllowBodies = (1 << 6); //------------------------------------------------------------------------------ //Bit Option for Property: CurveRules //------------------------------------------------------------------------------ public static readonly int CurveRules_SingleCurve = (1 << 0); public static readonly int CurveRules_ConnectedCurves = (1 << 1); public static readonly int CurveRules_TangentCurves = (1 << 2); public static readonly int CurveRules_FaceEdges = (1 << 3); public static readonly int CurveRules_BodyEdges = (1 << 4); public static readonly int CurveRules_SheetEdges = (1 << 5); public static readonly int CurveRules_FeatureCurves = (1 << 6); public static readonly int CurveRules_VertexEdges = (1 << 8); public static readonly int CurveRules_VertexTangentEdges = (1 << 9); public static readonly int CurveRules_RegionBoundaryCurves = (1 <<11); public static readonly int CurveRules_OuterEdgesofFaces = (1 <<13); public static readonly int CurveRules_RibTopFaceEdges = (1 <<14); public static readonly int CurveRules_FeatureIntersectionEdges = (1 <<15); public static readonly int CurveRules_组合面边界边 = (1 <<16); //------------------------------------------------------------------------------ //Constructor for NX Styler class //------------------------------------------------------------------------------ public SelectCurve() { try { theSession = Session.GetSession(); theUI = UI.GetUI(); string path = AppDomain.CurrentDomain.BaseDirectory; theDlxFileName = path + "SelectCurve.dlx"; theDialog = theUI.CreateDialog(theDlxFileName); theDialog.AddApplyHandler(new NXOpen.BlockStyler.BlockDialog.Apply(apply_cb)); theDialog.AddOkHandler(new NXOpen.BlockStyler.BlockDialog.Ok(ok_cb)); theDialog.AddUpdateHandler(new NXOpen.BlockStyler.BlockDialog.Update(update_cb)); theDialog.AddFilterHandler(new NXOpen.BlockStyler.BlockDialog.Filter(filter_cb)); theDialog.AddInitializeHandler(new NXOpen.BlockStyler.BlockDialog.Initialize(initialize_cb)); theDialog.AddDialogShownHandler(new NXOpen.BlockStyler.BlockDialog.DialogShown(dialogShown_cb)); } catch (Exception ex) { //---- Enter your exception handling code here ----- throw ex; } } //------------------------------- DIALOG LAUNCHING --------------------------------- // // Before invoking this application one needs to open any part/empty part in NX // because of the behavior of the blocks. // // Make sure the dlx file is in one of the following locations: // 1.) From where NX session is launched // 2.) $UGII_USER_DIR/application // 3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly // recommended. This variable is set to a full directory path to a file // containing a list of root directories for all custom applications. // e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_BASE_DIR\ugii\menus\custom_dirs.dat // // You can create the dialog using one of the following way: // // 1. Journal Replay // // 1) Replay this file through Tool->Journal->Play Menu. // // 2. USER EXIT // // 1) Create the Shared Library -- Refer "Block UI Styler programmer's guide" // 2) Invoke the Shared Library through File->Execute->NX Open menu. // //------------------------------------------------------------------------------ public static void Main() { SelectCurve theSelectCurve = null; try { theSelectCurve = new SelectCurve(); // The following method shows the dialog immediately theSelectCurve.Launch(); } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } finally { if(theSelectCurve != null) theSelectCurve.Dispose(); theSelectCurve = null; } } //------------------------------------------------------------------------------ // This method specifies how a shared image is unloaded from memory // within NX. This method gives you the capability to unload an // internal NX Open application or user exit from NX. Specify any // one of the three constants as a return value to determine the type // of unload to perform: // // // Immediately : unload the library as soon as the automation program has completed // Explicitly : unload the library from the "Unload Shared Image" dialog // AtTermination : unload the library when the NX session terminates // // // NOTE: A program which associates NX Open applications with the menubar // MUST NOT use this option since it will UNLOAD your NX Open application image // from the menubar. //------------------------------------------------------------------------------ public static int GetUnloadOption(string arg) { //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly); return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately); // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination); } //------------------------------------------------------------------------------ // Following method cleanup any housekeeping chores that may be needed. // This method is automatically called by NX. //------------------------------------------------------------------------------ public static void UnloadLibrary(string arg) { try { //---- Enter your code here ----- } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } } //------------------------------------------------------------------------------ //This method launches the dialog to screen //------------------------------------------------------------------------------ public NXOpen.BlockStyler.BlockDialog.DialogResponse Launch() { NXOpen.BlockStyler.BlockDialog.DialogResponse dialogResponse = NXOpen.BlockStyler.BlockDialog.DialogResponse.Invalid; try { dialogResponse = theDialog.Launch(); } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return dialogResponse; } //------------------------------------------------------------------------------ //Method Name: Dispose //------------------------------------------------------------------------------ public void Dispose() { if(theDialog != null) { theDialog.Dispose(); theDialog = null; } } //------------------------------------------------------------------------------ //---------------------Block UI Styler Callback Functions-------------------------- //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //Callback Name: initialize_cb //------------------------------------------------------------------------------ public void initialize_cb() { try { group0 = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group0"); edge_select0 = (NXOpen.BlockStyler.CurveCollector)theDialog.TopBlock.FindBlock("edge_select0"); } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } } //------------------------------------------------------------------------------ //Callback Name: dialogShown_cb //This callback is executed just before the dialog launch. Thus any value set //here will take precedence and dialog will be launched showing that value. //------------------------------------------------------------------------------ public void dialogShown_cb() { try { //---- Enter your callback code here ----- } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } } //------------------------------------------------------------------------------ //Callback Name: apply_cb //------------------------------------------------------------------------------ public int apply_cb() { int errorCode = 0; try { //---- Enter your callback code here ----- } catch (Exception ex) { //---- Enter your exception handling code here ----- errorCode = 1; theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return errorCode; } //------------------------------------------------------------------------------ //Callback Name: update_cb //------------------------------------------------------------------------------ public int update_cb( NXOpen.BlockStyler.UIBlock block) { try { if(block == edge_select0) { TaggedObject[] curveTag = edge_select0.GetSelectedObjects(); CurvatureResult max = GetMaxCurvature(curveTag[0].Tag); string msg = $"====曲率信息====\n"; if (max.IsConstantCurvature) { msg += $"恒定曲率曲线\n曲率k={max.Curvature:F6}\n半径R={max.Radius:F3}\n"; } else { msg += $"参数t:{max.ParamT:F6}\n点坐标:X={max.Point.X:F3},Y={max.Point.Y:F3},Z={max.Point.Z:F3}\n曲率k={max.Curvature:F6}, R={max.Radius:F3}\n"; double[] p=new double[3]; } theUI.NXMessageBox.Show("曲率计算结果", NXMessageBox.DialogType.Information, msg); } } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return 0; } //------------------------------------------------------------------------------ //Callback Name: ok_cb //------------------------------------------------------------------------------ public int ok_cb() { int errorCode = 0; try { errorCode = apply_cb(); //---- Enter your callback code here ----- } catch (Exception ex) { //---- Enter your exception handling code here ----- errorCode = 1; theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return errorCode; } //------------------------------------------------------------------------------ //Callback Name: filter_cb //------------------------------------------------------------------------------ public int filter_cb(NXOpen.BlockStyler.UIBlock block, NXOpen.TaggedObject selectedObject) { return(NXOpen.UF.UFConstants.UF_UI_SEL_ACCEPT); } //------------------------------------------------------------------------------ //Function Name: GetBlockProperties //Returns the propertylist of the specified BlockID //------------------------------------------------------------------------------ public PropertyList GetBlockProperties(string blockID) { PropertyList plist =null; try { plist = theDialog.GetBlockProperties(blockID); } catch (Exception ex) { //---- Enter your exception handling code here ----- theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString()); } return plist; } /////============================================================================= #region 导入UFUN API /// <summary> /// 获取曲线最大曲率 UF_MODL_ask_max_curvature /// </summary> [DllImport("libufun.dll", EntryPoint = "UF_MODL_ask_max_curvature")] private static extern int UF_MODL_ask_max_curvature( Tag eid, double[] range, int curva_type, double[] max_curva, ref int status); #endregion /// <summary> /// 曲率结果结构体 /// </summary> public struct CurvatureResult { public bool IsConstantCurvature; // 是否恒定曲率(圆/圆弧) public double ParamT; // 参数 public Point3d Point; // 坐标 public double Curvature; // 曲率k public double Radius; // 曲率半径 R=1/k } /// <summary> /// 获取曲线【最大曲率】 public static CurvatureResult GetMaxCurvature(Tag curveTag) { CurvatureResult res = new CurvatureResult(); //Tag curveTag = curve.Tag; double[] range = { 0.0, 100.0, 0.0, 0.0 }; // 全曲线,百分比0‑100 int curvaType = 0; double[] maxCurva = new double[5]; int status = 0; int err = UF_MODL_ask_max_curvature(curveTag, range, curvaType, maxCurva, ref status); if (err != 0) { throw new Exception($"UF_MODL_ask_max_curvature失败,错误码:{err}"); } res.IsConstantCurvature = status == 1; res.Curvature = maxCurva[4]; if (res.IsConstantCurvature) { //恒定曲率曲线,坐标和参数无效 res.ParamT = 0; res.Point = new Point3d(0, 0, 0); } else { res.ParamT = maxCurva[0]; res.Point = new Point3d(maxCurva[1], maxCurva[2], maxCurva[3]); } if (Math.Abs(res.Curvature) < 1e-9) res.Radius = double.PositiveInfinity; else res.Radius = 1.0 / res.Curvature; return res; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/22 18:16:51

国产操作系统如何选型?主流产品定位与应用场景解析

国产化转型持续深化背景下&#xff0c;越来越多政企单位启动操作系统替换工作。市面上可选的国产操作系统品类持续增多&#xff0c;但不同产品技术路线、适配场景、服务能力存在明显差异&#xff0c;不少采购人员容易混淆各类商业发行版与开源社区版本的定位&#xff0c;难以精…

作者头像 李华
网站建设 2026/8/22 18:13:57

PyMacroRecord:免费键鼠宏录制工具,重复操作一键托管

PyMacroRecord&#xff1a;免费键鼠宏录制工具&#xff0c;重复操作一键托管 【免费下载链接】PyMacroRecord Free and Open Source Macro Recorder with a modern GUI using Python 项目地址: https://gitcode.com/gh_mirrors/py/PyMacroRecord 同样 20 个地方点一遍、…

作者头像 李华
网站建设 2026/8/22 18:13:06

数学建模竞赛实战:从问题抽象到模型求解的全流程解析

1. 项目概述&#xff1a;从“解题”到“建模”的思维跃迁又到了一年一度的华为杯数学建模竞赛季&#xff0c;看到“2023年华为杯数学建模研赛A题思路解析代码论文”这个标题&#xff0c;相信很多参赛同学&#xff0c;尤其是初次接触这类竞赛的研究生&#xff0c;第一反应是寻找…

作者头像 李华
网站建设 2026/8/22 18:11:45

操作系统调度算法:从FCFS到Linux CFS,一图掌握核心原理与实战

调度算法&#xff0c;这个在操作系统课程里让无数学生头疼、在考研408试卷上频繁出现的概念&#xff0c;到底该怎么学&#xff1f;是死记硬背那些FCFS、SJF、RR的英文缩写和公式&#xff0c;还是真正理解它们背后的设计哲学和适用场景&#xff1f;很多同学在复习时陷入一个误区…

作者头像 李华
网站建设 2026/8/22 18:11:42

AI如何自动识别AI评审废标风险?智能评审项目实践

这里写自定义目录标题欢迎使用Ma rkdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个…

作者头像 李华
网站建设 2026/8/22 18:09:48

A100 云 GPU 怎么选?租之前先看显存、CPU、内存和磁盘

如果你要临时跑大模型推理、QLoRA 微调、科研项目复现&#xff0c;或者本地显存只有 24GB&#xff0c;A100 往往比单纯比较小时价格更值得关注。 但选云 GPU 不能只看“A100”三个字。真正需要确认的是&#xff1a;显存是否满足任务、CPU 和内存是否拖后腿、模型文件放在哪里&…

作者头像 李华