裸代码:
下面的代码简短而切中要害,但是它不能进行错误检查。
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);
}
}