UG NX 二次开发 创建包含单位的表达式 C#

裸代码:

下面的代码简短而切中要害,但是它不能进行错误检查。

using System;
using NXOpen;

class NXJ_exp_unit_1
{
    public static void Main(string[] args)
    {

        Session theSession = Session.GetSession();
        Part workPart = theSession.Parts.Work;

        Unit unit1 = (Unit)(workPart.UnitCollection.FindObject("MilliMeter"));

        Expression expression1;
        expression1 = workPart.Expressions.CreateWithUnits("test=25", unit1);
    }
}

改进的代码:

在运行下面的代码之前,请启动一个基本单位为“英寸”的新文件。该代码将创建一个名为“test”的新表达式,其值与以前一样为 25 mm,但当部件单位和表达式单位不匹配时,我们将探讨其中重要的区别。

using System;
using NXOpen;

class NXJ_exp_unit_1
{
    public static void Main(string[] args)
    {
        Session theSession = Session.GetSession();
        ListingWindow lw = theSession.ListingWindow;
        Part workPart = theSession.Parts.Work;
        lw.Open();
        Session.UndoMarkId markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ expression with units");
        Unit unit1 = (Unit)(workPart.UnitCollection.FindObject("MilliMeter"));

        Expression expression1 = null;
        string myExpName = "test";
        double myExpValue = 25;
        lw.WriteLine("myExpName = " + myExpName);
        lw.WriteLine("myExpValue = " + myExpValue.ToString());
        lw.WriteLine("");

        try
        {
            //创建表达式,类型:数字,度量:长度,单位:毫米
            expression1 = workPart.Expressions.CreateWithUnits(myExpName + "=" + myExpValue.ToString(), unit1);
            lw.WriteLine("expression created: '" + expression1.Name + " = " + expression1.RightHandSide + "'");
        }
        catch (NXException ex)
        {
            if (ex.ErrorCode == 1050017)
            {
                //指定的表达式变量已存在。
                expression1 = workPart.Expressions.FindObject(myExpName);

                //选项1
                //下一行将以表达式为单位设置表达式的值
                expression1.RightHandSide = myExpValue.ToString();

                //选项2
                //下一行将以基本(零件)单位设置表达式的值
                //可能不是你想要的,但在某些情况下有用
                //  expression1.Value = myExpValue;       

                lw.WriteLine("existing expression updated: '" + expression1.Name + " = " + expression1.RightHandSide + "'");
            }
            else
            {
                lw.WriteLine("NX exception: " + ex.ErrorCode + ", " + ex.Message);
            }

        }

        lw.Close();
    }

    public static int GetUnloadOption(string dummy)
    {
        //Unloads the image immediately after execution within NX
        return System.Convert.ToInt32(NXOpen.Session.LibraryUnloadOption.Immediately);
    }


}


转换单位:

以下程序将创建一个名为“test2”的表达式,其值为 12 英寸。在接下来的运行中,程序会将表达式单位从英寸转换为厘米。

using System;
using NXOpen;

class NXJ_exp_unit_2
{
    public static void Main(string[] args)
    {
        Session theSession = Session.GetSession();
        ListingWindow lw = theSession.ListingWindow;
        Part workPart = theSession.Parts.Work;
        lw.Open();
        Session.UndoMarkId markId1;
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ convert expression units");

        Unit unit1 = (Unit)(workPart.UnitCollection.FindObject("Inch"));
        Unit unit2 = (Unit)(workPart.UnitCollection.FindObject("CentiMeter"));

        Expression expression1 = null;
        string myExpName = "test2";
        double myExpValue = 12;

        lw.WriteLine("myExpName = " + myExpName);
        lw.WriteLine("myExpValue = " + myExpValue.ToString());
        lw.WriteLine("");

        try
        {
            //创建表达式,类型:数字,度量:长度,单位:毫米
            expression1 = workPart.Expressions.CreateWithUnits(myExpName + "=" + myExpValue.ToString(), unit1);
            lw.WriteLine("expression created: '" + expression1.Name + " = " + expression1.RightHandSide + " " + expression1.Units.Abbreviation + "'");

        }
        catch (NXException ex)
        {
            if (ex.ErrorCode == 1050017)
            {
                //指定的表达式变量已存在。
                expression1 = workPart.Expressions.FindObject(myExpName);

                lw.WriteLine("expression1.Units: " + expression1.Units.Abbreviation);
                lw.WriteLine("target units: " + unit2.Abbreviation);
                lw.WriteLine("");

                if (expression1.Units.Equals(unit2))
                {
                    lw.WriteLine("无需转换");
                    return;
                }

                lw.WriteLine("convert expression units from " + expression1.Units.Abbreviation + " to " + unit2.Abbreviation);
                lw.WriteLine("before conversion: " + expression1.Name + " = " + expression1.RightHandSide + " " + expression1.Units.Abbreviation);

                double theValue = 0;
                if (double.TryParse(System.Convert.ToString(expression1.RightHandSide), out theValue))
                {
                    expression1.RightHandSide = workPart.UnitCollection.Convert(expression1.Units, unit2, theValue).ToString();
                    expression1.Units = unit2;

                    lw.WriteLine("after conversion: " + expression1.Name + " = " + expression1.RightHandSide + " " + expression1.Units.Abbreviation);
                }
            }
            else
            {
                //other error
                lw.WriteLine("NX exception: " + ex.ErrorCode + ", " + ex.Message);
            }
        }

        lw.Close();
    }
    public static int GetUnloadOption(string dummy)
    {
        //Unloads the image immediately after execution within NX
        return System.Convert.ToInt32(NXOpen.Session.LibraryUnloadOption.Immediately);
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UG NXOpen学徒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值