Date: 17-SEP-2008
Subject: Sample NX Open C++ program : select notes
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.
#include <uf.h>
#include <uf_object_types.h>
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Annotations_Note.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXMessageBox.hxx> // as of NX5
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Selection.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
using namespace NXOpen;
using namespace std;
/*****************************************************************************
** Activation Methods
*****************************************************************************/
/* Explicit Activation
** This entry point is used to activate the application explicitly, as in
** "File->Execute UG/Open->User Function..." */
extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
/* Initialize the API environment */
Session* theSession = NXOpen::Session::GetSession();
try
{
// ask user to select text
UI *ui = UI::GetUI();
Selection *sm = ui->SelectionManager();
NXMessageBox *mb = ui->NXMessageBox(); // as of NX5
NXString message("Select Notes:");
NXString title("Select Notes");
Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;
Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
bool include_features = 0;
bool keep_highlighted = 0;
// Define the mask triple(s)
std::vector<Selection::MaskTriple> mask(1);
mask[0] = Selection::MaskTriple( UF_drafting_entity_type, UF_draft_note_subtype, 0 );
std::vector<NXObject *> objects;
// Select objects using filter defined by mask triples
Selection::Response res = sm->SelectObjects(
message,
title,
scope,
action,
include_features,
keep_highlighted,
mask,
objects
);
if (objects.size())
{
Annotations::Note *note;
std::vector<NXString> messages;
std::vector<NXString> texts;
for (unsigned int ii = 0; ii < objects.size(); ii++)
{
note = dynamic_cast<Annotations::Note *>(objects[ii]);
texts = note->GetText();
for(unsigned int jj=0; jj<texts.size(); jj++)
{
messages.push_back(texts[jj]);
}
}
// NXMessageBox works as of NX5
mb->Show("Selected Notes", NXMessageBox::DialogTypeInformation, messages);
// otherwise simply write to the listing window
// ListingWindow *lw = theSession->GetListingWindow();
// lw->Open();
// for (unsigned int kk = 0; kk < messages.size(); kk++)
// {
// lw->WriteLine(messages[kk].getText());
// }
}
}
/* Handle errors */
catch ( const NXOpen::NXException& ex )
{
// NXMessageBox works as of NX5
UI::GetUI()->NXMessageBox()->Show("Error", NXMessageBox::DialogTypeInformation, ex.Message());
// Until NX4 simply write to the listing window
// theSession->GetListingWindow()->WriteLine(ex.GetMessage());
}
}
/*****************************************************************************
** Utilities
*****************************************************************************/
/* Unload Handler
** This function specifies when to unload your application from Unigraphics.
** If your application registers a callback (from a MenuScript item or a
** User Defined Object for example), this function MUST return
** "UF_UNLOAD_UG_TERMINATE". */
extern "C" DllExport int ufusr_ask_unload()
{
return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}