Date: 16-JAN-2008
Subject: Sample NX Open .NET C# program : create instantiated udf
Note: GTAC provides programming examples for illustration only, and
assumes that you are familiar with the programming language being
demonstrated and the tools used to create and debug procedures. GTAC
support professionals can help explain the functionality of a particular
procedure, but we will not modify these examples to provide added
functionality or construct procedures to meet your specific needs.
using System;
using System.IO;
using System.Runtime.InteropServices;
using NXOpen;
using NXOpen.UF;
using NXOpenUI;
public class Program
{
// class members
private static Session theSession;
private static UI theUI;
private static UFSession theUfSession;
private static ListingWindow lw;
public static Program theProgram;
public static bool isDisposeCalled;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
public Program()
{
try
{
theSession = Session.GetSession();
theUI = UI.GetUI();
theUfSession = UFSession.GetUFSession();
lw = theSession.ListingWindow;
isDisposeCalled = false;
}
catch (NXOpen.NXException ex)
{
// NX5
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
// for NX4 use this
lw.Open(); lw.WriteLine(ex.Message);
}
}
//------------------------------------------------------------------------------
// Part selection
//------------------------------------------------------------------------------
public static bool Part_selection(string prompt, ref string filename)
{
int resp;
string filter, dir_name,fltr_name;
theUfSession.Ui.AskDialogDirectory(UFUi.DialogDirId.PartDir, out dir_name);
theUfSession.Ui.AskDialogFilter(UFUi.DialogFilterId.PartOpenFltr, out fltr_name);
filter = dir_name + fltr_name;
theUfSession.Ui.CreateFilebox(prompt, prompt, ref filter, "*.prt", out filename, out resp);
if (resp != UFConstants.UF_UI_CANCEL) return true;
else return false;
}
//------------------------------------------------------------------------------
// Report load status on failure
//------------------------------------------------------------------------------
public static void report_load_status(UFPart.LoadStatus ls)
{
string msg;
int ii;
for (ii=0; ii<ls.n_parts; ii++)
{
theUfSession.UF.GetFailMessage(ls.statuses[ii], out msg);
lw.WriteLine("Error loading Part " + ls.file_names[ii]);
lw.WriteLine(msg);
}
}
//------------------------------------------------------------------------------
// Select any object by mask
//------------------------------------------------------------------------------
public static NXObject select_anything_by_mask(string prompt)
{
NXObject selobj;
Point3d cursor;
Selection.MaskTriple[] mask = new Selection.MaskTriple[2];
mask[0].Type = UFConstants.UF_solid_type;
mask[0].Subtype = 0;
mask[0].SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE;
mask[1].Type = UFConstants.UF_solid_type;
mask[1].Subtype = 0;
mask[1].SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE;
Selection.Response resp = theUI.SelectionManager.SelectObject("Select", prompt,
Selection.SelectionScope.WorkPart, Selection.SelectionAction.ClearAndEnableSpecific, false, false, mask, out selobj, out cursor);
if (resp == Selection.Response.ObjectSelected ||
resp == Selection.Response.ObjectSelectedByName)
{
return selobj;
}
else
return null;
}
//------------------------------------------------------------------------------
// Cycle part for specified feature type
//------------------------------------------------------------------------------
public static Tag ask_next_feature_of_type(Tag parttag, string type, ref Tag feat)
{
string this_type = null;
do
{
theUfSession.Obj.CycleObjsInPart(parttag, UFConstants.UF_feature_type, ref feat);
if (feat != Tag.Null && theUfSession.Obj.AskStatus(feat) == UFConstants.UF_OBJ_ALIVE)
{
theUfSession.Modl.AskFeatType(feat, out this_type);
if (this_type.CompareTo(type) == 0) return feat;
}
} while ( feat != Tag.Null );
return Tag.Null;
}
//------------------------------------------------------------------------------
// Explicit Activation
// This entry point is used to activate the application explicitly
//------------------------------------------------------------------------------
public static int Main(string[] args)
{
string udfpartname = null;
try
{
theProgram = new Program();
//TODO: Add your application code here
lw.Open();
// check for existing target work part
Tag work_part = theUfSession.Assem.AskWorkPart();
if( work_part == Tag.Null)
{
lw.WriteLine("No active work part...exit.");
return 1;
}
if (Part_selection("Select an UDF part", ref udfpartname) != false)
lw.WriteLine("Part selected: " + udfpartname);
else
{
lw.WriteLine("No Part selected");
return 0;
}
Tag UdfPartTag = Tag.Null;
UFPart.LoadStatus error_status;
theUfSession.Part.OpenQuiet(udfpartname, out UdfPartTag, out error_status);
if (error_status.failed)
{
report_load_status(error_status);
return 1;
}
Tag UdfFeatTag = Tag.Null;
ask_next_feature_of_type(UdfPartTag, "UDF_DEF", ref UdfFeatTag);
if (UdfFeatTag == Tag.Null)
{
lw.WriteLine("No UDF in Part " + udfpartname + " found.");
return 1;
}
String udf_name;
theUfSession.Obj.AskName(UdfFeatTag, out udf_name);
lw.WriteLine("UDF Feature Tag=" + UdfPartTag.ToString() + " Name=" + udf_name);
lw.WriteLine("\nAskUdfDefinition...");
Tag[] lParents;
String[] lParentsPrompt;
String[] lExpressionPrompt;
int lNumberParents, lNumberExpressions;
Tag[] lExpressions;
theUfSession.Modl.AskUdfDefinition(
UdfFeatTag,
out lParents,
out lParentsPrompt,
out lNumberParents,
out lExpressions,
out lExpressionPrompt,
out lNumberExpressions);
lw.WriteLine("lNumberParents=" + lNumberParents.ToString());
Tag[] newParents = new Tag[lNumberParents];
for (int i = 0; i < lNumberParents; i++)
{
lw.WriteLine(" " + (i + 1).ToString() + ". " + lParents[i].ToString() + " " + lParentsPrompt[i]);
newParents[i] = select_anything_by_mask(lParentsPrompt[i]).Tag;
}
String[] new_exp_rhs = new String[lNumberExpressions];
String exp_str, lhs_str, rhs_str;
Tag exp_tag;
lw.WriteLine("lNumberExpressions=" + lNumberExpressions.ToString());
for (int i = 0; i < lNumberExpressions; i++)
{
theUfSession.Modl.AskExpTagString(lExpressions[i], out exp_str);
theUfSession.Modl.DissectExpString(exp_str, out lhs_str, out rhs_str, out exp_tag);
lw.WriteLine(" " + (i + 1).ToString() + ". " + lExpressions[i].ToString() + " " + lExpressionPrompt[i] + " = " + rhs_str);
new_exp_rhs[i] = NXInputBox.GetInputString(lExpressionPrompt[i], "Enter new Expression:", rhs_str);
}
// works as of 5.0.3, see PR-1635826
//theUfSession.Modl.RegisterRpoRoutine( theUfSession.Modl.UdfRpoMenu);
String cgmName = Path.GetFileNameWithoutExtension(udfpartname);
cgmName = cgmName + ".cgm";
lw.WriteLine("\nCreateInstantiatedUdf...");
Tag new_udf;
theUfSession.Modl.CreateInstantiatedUdf(UdfFeatTag, cgmName, lParents, newParents, lNumberParents, lExpressions, new_exp_rhs, lNumberExpressions, out new_udf);
//UF.Free is not allowed/necessary, see IR-05406789
theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
// NX5
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
// for NX4 use this
lw.Open(); lw.WriteLine(ex.Message);
}
return 0;
}
//------------------------------------------------------------------------------
// Following method disposes all the class members
//------------------------------------------------------------------------------
public void Dispose()
{
try
{
if (isDisposeCalled == false)
{
//TODO: Add your application code here
}
isDisposeCalled = true;
}
catch (NXOpen.NXException ex)
{
// NX5
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
// for NX4 use this
lw.Open(); lw.WriteLine(ex.Message);
}
}
public static int GetUnloadOption(string arg)
{
//Unloads the image immediately after execution within NX
//Will be overridden once a RPO routine was registered
return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
}
}
다음검색