admin 发表于 2013-11-6 18:54:58

NX二次开发源码: 获取面的相邻面的信息

通过这个代码,可以获取选择面的相邻面的信息
NX open C++中使用了 选择对象 selectobject 函数,通过面获取边数据,通过边获取面,从而得到信息!

#include <uf.h>
#include <uf_ui.h>
#include <uf_object_types.h>

#include <NXOpen/Edge.hxx>
#include <NXOpen/Face.hxx>

#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/SelectDisplayableObject.hxx>
#include <NXOpen/SelectDisplayableObjectList.hxx>
#include <NXOpen/SelectObject.hxx>
#include <NXOpen/SelectObjectList.hxx>
#include <NXOpen/Selection.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/View.hxx>

#include <iostream>
#include <sstream>
#include <list>

using namespace NXOpen;
using namespace NXOpen::Annotations;
using namespace std;

class Gtac
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;

    Gtac();
    ~Gtac();

    void do_it();

private:
    Part *workPart, *displayPart;
    Selection *sm;
    NXMessageBox *mb;
    ListingWindow *lw;

    Face* select_a_face();

};

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(Gtac::theSession) = NULL;
UI *(Gtac::theUI) = NULL;

//------------------------------------------------------------------------------
// Declaration of global variables
//------------------------------------------------------------------------------
Gtac *theGtac;

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
Gtac::Gtac()
{
    // Initialize the NX Open C++ API environment
    Gtac::theSession = NXOpen::Session::GetSession();
    Gtac::theUI = UI::GetUI();

    sm = theUI->SelectionManager();
    mb = theUI->NXMessageBox();
    lw = theSession->ListingWindow();

    workPart = 0;
    displayPart = 0;
}

//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
Gtac::~Gtac()
{
}

//------------------------------------------------------------------------------
// Selection
//------------------------------------------------------------------------------
Face* Gtac::select_a_face()
{
    // ask user to select a face
    UI *ui = UI::GetUI();
    Selection *sm = ui->SelectionManager();
    NXMessageBox *mb = ui->NXMessageBox(); // as of NX5

    NXString message("Select Face:");
    NXString title("Select Face");
    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 = Selection::MaskTriple( UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE);
    Point3d cursor;
    NXObject *object;

    // Select objects using filter defined by mask triples
    Selection::Response res = sm->SelectObject(
      message, title, scope, action, include_features,
            keep_highlighted, mask, &object, &cursor );

    if( res == Selection::ResponseObjectSelected )
    {
      Face *aFace;
      aFace = (Face *)(object);
      return aFace;
    }

    return 0;
}

//------------------------------------------------------------------------------
// start with our job...
//------------------------------------------------------------------------------
void Gtac::do_it()
{
    workPart = theSession->Parts()->Work();
    Part *displayPart = theSession->Parts()->Display();
    stringstream out;

    Face *theFace = 0;
    std::list<Face*> myfaces;
   
    while( (theFace=select_a_face()) != 0 )
    {
      if(! lw->IsOpen() ) lw->Open();

      out << "Selected Object: " << theFace->Tag() << endl;
      lw->WriteLine(out.str().c_str());

      std::vector<Edge*> edges = theFace->GetEdges();
      out.str(""); out.clear();
      out << " Number of edges: " << edges.size() << endl;
      lw->WriteLine(out.str().c_str());

      for( int ii = 0; ii < edges.size(); ii++)
      {
            out.str(""); out.clear();
            out << " Edge: " << edges->Tag() << endl;
            lw->WriteLine(out.str().c_str());

            std::vector< NXOpen::Face * > adj_faces = edges->GetFaces();
            out.str(""); out.clear();
            out << " Number of adjacent faces: " << adj_faces.size() << endl;
            lw->WriteLine(out.str().c_str());

            for( int jj = 0; jj < adj_faces.size(); jj++)
            {
                out.str(""); out.clear();
                out << " Adjacent face: " << adj_faces->Tag() << endl;
                lw->WriteLine(out.str().c_str());

                myfaces.push_back(adj_faces);

            } // jj

      } // ii

      myfaces.unique();
      for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
      {
            (*it)->Highlight();
      }
      Gtac::theUI->NXMessageBox()->Show("Adjacent Faces", NXOpen::NXMessageBox::DialogTypeInformation, "Confirm");
      for (list<Face*>::iterator it=myfaces.begin(); it!=myfaces.end(); ++it)
      {
            (*it)->Unhighlight();
      }
      myfaces.clear();


    } // while
}

//------------------------------------------------------------------------------
// Entry point for our unmanaged internal C++ program
//------------------------------------------------------------------------------
extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
    try
    {
      theGtac = new Gtac();
      theGtac->do_it();
      delete theGtac;
    }
    catch (const NXOpen::NXException& ex)
    {
      // ---- Enter your exception handling code here -----
      Gtac::theUI->NXMessageBox()->Show("Caught Error", NXOpen::NXMessageBox::DialogTypeError, ex.Message());
    }
}


//------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}

页: [1]
查看完整版本: NX二次开发源码: 获取面的相邻面的信息