当前位置: 首页 > news >正文

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; } }
http://www.cnnetsun.cn/news/4161706.html

相关文章:

  • 国产操作系统如何选型?主流产品定位与应用场景解析
  • PyMacroRecord:免费键鼠宏录制工具,重复操作一键托管
  • 数学建模竞赛实战:从问题抽象到模型求解的全流程解析
  • 操作系统调度算法:从FCFS到Linux CFS,一图掌握核心原理与实战
  • AI如何自动识别AI评审废标风险?智能评审项目实践
  • A100 云 GPU 怎么选?租之前先看显存、CPU、内存和磁盘
  • 从拍脑袋到建模型:掌握数学建模思维,用数据驱动科学决策
  • LaTeX公式转Word只要一次右键:LaTeX2Word-Equation插件快速上手指南
  • 明日方舟游戏素材:从立绘到数据的完整获取指南
  • vue-circle-progress 教程:如何用 Vue 组件快速做出动画圆形进度条
  • 卫星通信中气象数据传输的优化建模与调度算法设计
  • DM Ticket:大麦网自动抢票 Docker 一键部署完整指南
  • 软件外包市场多了一类活:给Vibe Coding项目做验收
  • 基于强化学习的自适应检索深度优化:提升RAG系统效率与质量
  • 把散落的想法画成一张节点图:Project Graph 快速上手指南
  • 层次分析法(AHP)在数学建模中的应用:从原理到实战
  • 数学建模竞赛:蔬菜定价与补货联合优化模型构建与求解
  • C++模板进阶:从实例化到元编程的深度解析与实践
  • WinBtrfs 快速上手:3 步让 Windows 直接读写 Btrfs 分区
  • AI驱动的停车场照明方案:主流服务商技术特色与落地表现分析
  • 如何用 Apktool 解包并重建 APK:从解码到签名的实用实操教程
  • 130、双摄/多摄外参标定——视差校正与产线标定工位设计在手机/车载平台的量产实践
  • 利用UU远程实现《我的世界》Java版零门槛联机:无需公网IP与端口映射
  • vue-webtopo-svgeditor:用 3 步把网络拓扑图搬进浏览器的 Vue3 SVG 图形编辑器
  • Git 提交备注修改与合并、回退版本
  • Companion:免费把 700+ 设备塞进一块按键面板
  • AI旅游谁在买单?4类B端用户付费率超35%的ROI分析
  • 计算机考研408虚拟存储器真题精讲:从地址转换到实战解析
  • 从TCP三次握手到守护进程:Linux网络编程实战与日志分析
  • Python的weekday()一出手,星期几立马现原形