2011-01-11

Open APIs and NXOpen Class Library of UG/NX Secondary Development

(I am not native speaker, so sorry for my English. If you find some mistakes, please tell me in the following comments or email to me: begtostudy@gmail.com)
There are two open application interface system, i.e., Open APIs and NXOpen class library. Open, also named UFun, is based on C-Langrage at the start and later supported C++, which is good at geometry functions. After purchased by Siemens (or extended to PLM business), UG/NX have another one, NXOpen class library. It concludes almost the functions of NX software and is entirely object-oriented. However NX didn’t give up the Open APIs, with NXOpen.UF namespace, we can use them by encapsulation technology. Maybe for the short history, NXOpen have less help data than Open. And the same reason, before NX6, there are so many bugs in NXOpen. We give some comparisons in Table 1.
Table 1. Comparisons

Language Open API/UFun NXOpen
C procedure-oriented
good at geometry functions, especially efficiency
Not support
C++ (Open C++)
Partly object-oriented
Partly support NX functions
Almost support NX functions
(native code)
.net/java NXOpen.UF namespace by encapsulation technology Almost support NX functions
(managed code)
In conclusion, if using C/C++, you could use Open C and Open C++ directly, and if using .net, you primary use NXOpen and API by NXOpen.UF, i.e., you could use both systems separately.
Following, I would take some examples to illustrate their difference. For .net, I only picked C#, and others, e.g. VB, are the same. Sorry for java, I know little about it.

I. Open C

1. Find each body in the work part

#include <uf_object_types.h>
#include <uf_part.h>
#include <uf_obj.h>
#include <uf_modl.h>
#include <string>
#include <sstream>
using std::string;
using std::stringstream;
//code segments as follow
UgSession session( true );
try
    {
/* TODO: Add your application code here */
uf_list_p_t lpObj;
UF_MODL_create_list(&lpObj);
tag_t prt = UF_PART_ask_display_part();
tag_t Next_tag=NULL_TAG;
do
         {
UF_OBJ_cycle_objs_in_part(prt,UF_solid_type,&Next_tag);
if(Next_tag==NULL_TAG) break;
int t,subtype;
UF_OBJ_ask_type_and_subtype(Next_tag,&t,&subtype);
if(subtype==UF_solid_body_subtype)
UF_MODL_put_list_item(lpObj,Next_tag);
         } while(1);
logical is_open;
UF_UI_is_listing_window_open(&is_open);
if(!is_open) UF_UI_open_listing_window();
int sum;
UF_MODL_ask_list_count(lpObj,&sum);
for (int i=0;i<sum;i++)
         {
tag_t t;
UF_MODL_ask_list_item(lpObj,i,&t);
stringstream s;
s<<(int)t;
string str;
str = s.str();
UF_UI_write_listing_window(str.c_str());
UF_UI_write_listing_window("\n");
         }
//       UF_UI_exit_listing_window();
UF_MODL_delete_list(&lpObj);
    }
/* Handle errors */
catch ( const UgException &exception )
    {
processException( exception );
}

2. Create a block

#include <uf.h>
#include <uf_ui.h>
#include <uf_exit.h>
#include <uf_modl.h>
//code segments as follow
/* Initialize the API environment */
if( UF_CALL(UF_initialize()) )
    {
/* Failed to initialize */
return;
    }
/* TODO: Add your application code here */
double corner[3] ={0,0,0};
char* edge[3] = {"10","5","20"};
tag_t tag;
UF_MODL_create_block(UF_NULLSIGN,NULL_TAG,corner,edge,&tag);
/* Terminate the API environment */
UF_CALL(UF_terminate());

II. Open C++

1. Find each body in the work part

#include <ug_typed.hxx>
#include <ug_part.hxx>
#include <ug_body.hxx>
#include <ug_string.hxx>
using std::string;
// code segments as follow
UgSession session( true );
try
{
/* TODO: Add your application code here */
UgPart *pWorkPart = UgSession::getWorkPart();
UgTypedObject *pObj;
ostrstream buffer;
for ( pObj = pWorkPart->iterateFirst ( );
pObj;
pObj = pWorkPart->iterateNext ( pObj ) )
{
std::string name = pObj->getName ( );
UgBody *pBody = dynamic_cast<UgBody*>(pObj);
if (pBody)
{
buffer<<(int)pBody->getTag()<<"\n";
}
}
UgInfoWindow::open();
UgInfoWindow::write(string(buffer.str()));
delete buffer.str();
}
/* Handle errors */
catch ( const UgException &exception )
{
processException( exception );
}

2. serching

#include <ug_body.hxx>
#include <ug_iterator.hxx>
#include <ug_string.hxx>
// code segments as follow
UgSession session( true );
try
{
/* TODO: Add your application code here */
ostrstream buffer;
// Construct an iterator for NX face objects
UgIterator < UgBody > pObj;//workpart 可以通过其他方式指定
//UgIterator < UgFace *> curFace;
//// Loop through all faces
//while ( !curFace.isFinished ( ) )
//{
//   // Get the name of the current face
//   std::string faceName = (*(*curFace))->getName ( );
//   curFace.findNext ( );
//}
// Loop through all faces
while ( !pObj.isFinished ( ) )
{
// Get the name of the current face
std::string faceName = (*pObj)->getName ( );
buffer<<(int)(*pObj)->getTag()<<endl;
pObj.findNext ( );
}
UgInfoWindow::open();
UgInfoWindow::write( std::string( buffer.str() ));
delete buffer.str();
}
/* Handle errors */
catch ( const UgException &exception )
{
processException( exception );
}

III. NXOpen C++

1. Create a block

#include <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
using namespace NXOpen;
// code segments as follow
NXOpen::Session *theSession = NXOpen::Session::GetSession();
try
    {
/* TODO: Add your application code here */
Part* thePart = theSession->Parts()->Work();
NXOpen::Features::Feature* block = NULL;
NXOpen::Features::BlockFeatureBuilder* theBuilder = thePart->Features()->CreateBlockFeatureBuilder(block);
NXOpen::Point3d basePoint(100, 100, 100);
theBuilder->SetOriginAndLengths(basePoint, "100", "200", "300");
//              NXOpen.Body theBody = null;
//              theBuilder.SetBooleanOperationAndTarget(NXOpen.Features.Feature.BooleanType.Create, theBody);
theBuilder->Commit();
//theBuilder->CommitFeature();
NXOpen::UI::GetUI()->NXMessageBox()->Show("", NXMessageBox::DialogType::DialogTypeInformation, "OK!");
//   UI->GetUI()->NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");
    }
/* Handle errors */
catch ( const UgException &exception )
    {
processException( exception );
}

2. Find each Feature in the work part

#include <NXOpen/Session.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_Block.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/ListingWindow.hxx>
using namespace NXOpen;
// code segments as follow
NXOpen::Session *theSession = NXOpen::Session::GetSession();
try
     {
/* TODO: Add your application code here */
Part* thePart = theSession->Parts()->Work();
theSession->ListingWindow()->Open();
NXOpen::Features::FeatureCollection::iterator i;
for (i=thePart->Features()->begin();i!=thePart->Features()->end();i++)
         {
theSession->ListingWindow()->WriteLine((*i)->Name()+"--"+(*i)->GetJournalIdentifier());
         }
NXOpen::UI::GetUI()->NXMessageBox()->Show("", NXMessageBox::DialogType::DialogTypeInformation, "OK!");
//   UI->GetUI()->NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");
     }
/* Handle errors */
catch ( const UgException &exception )
    {
processException( exception );
}

IV. NXOpen C#

1. Create a block

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;
using NXOpenUI;
// code segments as follow
Session theSession = Session.GetSession();
try
            {
Part thePart = theSession.Parts.Work;
NXOpen.Features.Feature block = null;
NXOpen.Features.BlockFeatureBuilder theBuilder = thePart.Features.CreateBlockFeatureBuilder(block);
NXOpen.Point3d basePoint = new Point3d(100f, 100f, 100f);
theBuilder.SetOriginAndLengths(basePoint, "100", "200", "300");
theBuilder.Commit();
//theBuilder.CommitFeature();
UI.GetUI().NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");
            }
catch(NXException ex)
            {
UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);
            }

2. Find each Feature in the work part

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;
using NXOpenUI;
// code segments as follow
Session theSession = Session.GetSession();
theSession.ListingWindow.Open();
try
            {
Part thePart = theSession.Parts.Work;
foreach (NXOpen.Features.Feature c in thePart.Features)
                {
//NXOpen.Features.Block t = c as NXOpen.Features.Block;
//if(t!=null)
//{
//    theSession.ListingWindow.WriteLine(t.ToString());
//}
theSession.ListingWindow.WriteLine(c.Name+"--"+c.ToString());
                }
UI.GetUI().NXMessageBox.Show("", NXMessageBox.DialogType.Information, "OK!");
            }
catch (NXException ex)
            {
UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);
            }

V. NXOpen.UF

1. analogy of Case I.1 for Open C

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;
using NXOpenUI;
NXOpen.UF.UFSession theUFSession = NXOpen.UF.UFSession.GetUFSession();
try
            {
double[] corner ={ 0, 0, 0 };
string[] edge = { "10", "5", "20" };
Tag tag;
theUFSession.Modl.CreateBlock1(FeatureSigns.Nullsign, corner, edge, out tag);
            }
catch (NXException ex)
            {
UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);
            }

1. analogy of Case I.1 for Open C

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;
using NXOpenUI;
NXOpen.UF.UFSession theUFSession = NXOpen.UF.UFSession.GetUFSession();
try
            {
Tag[] list=null;
Tag thePart = theUFSession.Part.AskDisplayPart();
theUFSession.Modl.CreateList(out list);
Tag Next_tag=Tag.Null;
do
                {
theUFSession.Obj.CycleObjsInPart(thePart,70/* UF_solid_type*/,ref Next_tag);
if (Next_tag == Tag.Null) break;
int t, subType;
theUFSession.Obj.AskTypeAndSubtype(Next_tag,out t, out subType);
if (subType == 0/*UF_solid_body_subtype*/)
theUFSession.Modl.PutListItem(list, Next_tag);
                } while (true);
bool isOpen;
theUFSession.Ui.IsListingWindowOpen(out isOpen);
if (!isOpen) theUFSession.Ui.OpenListingWindow();
int sum;
theUFSession.Modl.AskListCount(list,out sum);
for (int i = 0; i < sum;i++ )
                {
Tag t;
theUFSession.Modl.AskListItem(list, i, out t);
theUFSession.Ui.WriteListingWindow(t.ToString());
                }
/*Treat all the arguments with the"Output to be freed " annotation as an output parameter.
                 * The system takes care of freeing memory.*/
            }
catch (NXException ex)
            {
UI.GetUI().NXMessageBox.Show("error!", NXMessageBox.DialogType.Error, ex.Message);
            }

No comments:

Post a Comment