UG NX二次开发(C#)-分析-测量曲面的面积
Posted GimiGimmy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UG NX二次开发(C#)-分析-测量曲面的面积相关的知识,希望对你有一定的参考价值。
1、 简要说明
在UG NX中随着版本的更新,测量曲面功能的界面也有所变化,在NX10中,有个单独的“测量面”功能,能测量曲面的面积和长度,如下图所示。
通过搜索查看面测量功能
NX10中的面测量界面
通过面测量能得到单张曲面或者多张曲面的面积。
但是到了NX改版后,其功能界面归属于测量,如NX1984版本中,其不再有单独的面测量功能,而是与其他测量进行了合并,如下图所示。
NX改版后的测量界面
在这个测量功能里面,选择对象,能自动判断测量对象的数据,如选择曲面,则会测量曲面的面积和测量,并能在一个弹出窗口中显示测量信息,如下图所示。
选择一个球体,则能测量出面积、轴侧、半径、质心和最小曲率半径。
2、NX二次开发
如果采用二次开发,测量曲面的面积时,可以通过录制功能来获得测量的代码和函数,如测量一个长方体的一个表面,则通过录制得到以下的代码。
// NX 1980
// Journal created by Administrator on Tue May 3 00:42:33 2022 中国标准时间
//
using System;
using NXOpen;
public class NXJournal
public static void Main(string[] args)
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
// ----------------------------------------------
// 菜单:分析(L) ->测量(S)...
// ----------------------------------------------
NXOpen.Session.UndoMarkId markId1;
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "起点");
theSession.SetUndoMarkName(markId1, "测量 对话框");
workPart.MeasureManager.SetPartTransientModification();
NXOpen.ScCollector scCollector1;
scCollector1 = workPart.ScCollectors.CreateCollector();
scCollector1.SetMultiComponent();
workPart.MeasureManager.SetPartTransientModification();
NXOpen.SelectionIntentRuleOptions selectionIntentRuleOptions1;
selectionIntentRuleOptions1 = workPart.ScRuleFactory.CreateRuleOptions();
selectionIntentRuleOptions1.SetSelectedFromInactive(false);
NXOpen.Face[] faces1 = new NXOpen.Face[1];
NXOpen.Features.Block block1 = ((NXOpen.Features.Block)workPart.Features.FindObject("BLOCK(7)"));
NXOpen.Face face1 = ((NXOpen.Face)block1.FindObject("FACE 1 (-157.804011120567,14.3935481774355,100) BLOCK(7)"));
faces1[0] = face1;
NXOpen.FaceDumbRule faceDumbRule1;
faceDumbRule1 = workPart.ScRuleFactory.CreateRuleFaceDumb(faces1, selectionIntentRuleOptions1);
selectionIntentRuleOptions1.Dispose();
NXOpen.SelectionIntentRule[] rules1 = new NXOpen.SelectionIntentRule[1];
rules1[0] = faceDumbRule1;
scCollector1.ReplaceRules(rules1, false);
workPart.MeasureManager.SetPartTransientModification();
NXOpen.ScCollector scCollector2;
scCollector2 = workPart.ScCollectors.CreateCollector();
scCollector2.SetMultiComponent();
NXOpen.Session.UndoMarkId markId2;
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "测量");
theSession.DeleteUndoMark(markId2, null);
NXOpen.Session.UndoMarkId markId3;
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "测量");
NXOpen.Session.UndoMarkId markId4;
markId4 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Measurement Apply");
workPart.MeasureManager.ClearPartTransientModification();
NXOpen.Session.UndoMarkId markId5;
markId5 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Measurement Update");
workPart.MeasureManager.SetPartTransientModification();
theSession.DeleteUndoMark(markId4, "Measurement Apply");
bool datadeleted1;
datadeleted1 = theSession.DeleteTransientDynamicSectionCutData();
theSession.DeleteUndoMark(markId3, null);
theSession.SetUndoMarkName(markId1, "测量");
scCollector1.Destroy();
scCollector2.Destroy();
workPart.MeasureManager.ClearPartTransientModification();
theSession.DeleteUndoMark(markId5, null);
// ----------------------------------------------
// 菜单:工具(T)->操作记录(J)->停止录制(S)
// ----------------------------------------------
public static int GetUnloadOption(string dummy) return (int)NXOpen.Session.LibraryUnloadOption.Immediately;
从这个代码中间,也看不到面积信息,那怎么才能通过二次开发获得曲面的面积呢?我们还是要查看测量的帮助文档,测量面的帮助文档如下图所示。测量面的方法是属于MeasureManager类中NewFaceProperties。
public MeasureFaces NewFaceProperties(
Unit areaUnit,
Unit lengthUnit,
double accuracy,
ScCollector objects
)
从这个方法中可以看出,其需要4个参数,面积单位、长度单位、精度和需要测量的对象集合。
从这个帮助文档出发,我们编写测量面的方法如下:
/// <summary>
/// 测量面的体积,可以测量单个,也可以测量多个
/// </summary>
/// <param name="faces">测量面对象数组</param>
/// <param name="area">输出面积的值,单位是mm^2</param>
/// <param name="perimeter"> 输出面的周长,单位是mm</param >
public static void MeasureFacesArea(Face[]faces,out double area,out double perimeter)
area = 0.0;
perimeter = 0.0;
NXOpen.NXObject nullNXOpen_NXObject = null;
NXOpen.MeasureFaceBuilder measureFaceBuilder;
NXOpen.Part workPart = theSession.Parts.Work;
measureFaceBuilder = workPart.MeasureManager.CreateMeasureFaceBuilder(nullNXOpen_NXObject);
NXOpen.FaceDumbRule faceDumbRule;
faceDumbRule = workPart.ScRuleFactory.CreateRuleFaceDumb(faces);
NXOpen.SelectionIntentRule[] selectionIntentRules = new SelectionIntentRule[1];
selectionIntentRules[0] = faceDumbRule;
measureFaceBuilder.FaceCollector.ReplaceRules(selectionIntentRules, false);
NXOpen.Unit unit = (NXOpen.Unit)workPart.UnitCollection.FindObject(UFConstants.UF_UNIT_AREA_mm2);
NXOpen.Unit unit1 = (NXOpen.Unit)workPart.UnitCollection.FindObject(UFConstants.UF_UNIT_LENGTH_mm);
NXOpen.MeasureFaces measureFaces;
measureFaces = workPart.MeasureManager.NewFaceProperties(unit, unit1, 0.99, measureFaceBuilder.FaceCollector);
area = measureFaces.Area;
perimeter = measureFaces.Perimeter;
measureFaces.Dispose();
measureFaceBuilder.Destroy();
这个方法就是输出单张曲面或者多张曲面的面积。
3、 测量实例
选择一张长方体的表面,长方体的长宽高都是100mm,则其一张曲面的表面积就是10000mm2,采用二次开发的测试实例如下图所示,说明测量方法的正确性。
测量单个曲面的表面积
如果是两张曲面,则其表面积输出结果如下:
说明测量方法的正确性。
以上是关于UG NX二次开发(C#)-分析-测量曲面的面积的主要内容,如果未能解决你的问题,请参考以下文章