Sample NX Open .NET C# program : report selected object type and subtype
작성자k2ice작성시간08.09.14조회수988 목록 댓글 0Date: 12-AUG-2008
Subject: Sample NX Open .NET C# program : report selected object type and subtype
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.Runtime.InteropServices;
using NXOpen;
using NXOpen.UF;
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)
{
// ---- Enter your exception handling code here -----
// NX5
UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
// for NX4 use this
// lw.Open(); lw.WriteLine(ex.Message);
}
}
//------------------------------------------------------------------------------
// Select any object
//------------------------------------------------------------------------------
public static NXObject select_anything()
{
NXObject selobj;
Point3d cursor;
Selection.SelectionType[] typeArray =
{ Selection.SelectionType.All, Selection.SelectionType.Faces, Selection.SelectionType.Edges };
Selection.Response resp = theUI.SelectionManager.SelectObject("Select anything", "Select anything",
Selection.SelectionScope.WorkPart, false, typeArray, out selobj, out cursor);
if (resp == Selection.Response.ObjectSelected ||
resp == Selection.Response.ObjectSelectedByName)
{
return selobj;
}
else
return null;
}
//------------------------------------------------------------------------------
// Explicit Activation
// This entry point is used to activate the application explicitly
//------------------------------------------------------------------------------
public static int Main(string[] args)
{
int retValue = 0;
NXObject selobj;
try
{
theProgram = new Program();
//TODO: Add your application code here
while ( (selobj=select_anything()) != null )
{
int type = 0;
int subtype = 0;
theUfSession.Obj.AskTypeAndSubtype(selobj.Tag, out type, out subtype);
lw.Open();
lw.WriteLine("Object selected: " + selobj.ToString());
lw.WriteLine(" Tag: " + selobj.Tag.ToString());
lw.WriteLine(" Type: " + type.ToString());
lw.WriteLine(" Subtype: " + subtype.ToString());
}
theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
// NX5
UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
// for NX4 use this
// lw.Open(); lw.WriteLine(ex.Message);
}
return retValue;
}
//------------------------------------------------------------------------------
// 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)
{
// ---- Enter your exception handling code here -----
// 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 explicitly, via an unload dialog
//return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
//Unloads the image immediately after execution within NX
return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
//Unloads the image when the NX session terminates
// return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
}
}