mildcat 发表于 2014-9-27 21:06:44

UG NX二次开发源码分享:创建自定义引用集

UG NX二次开发源码分享:创建自定义引用集



//create_specified_reference_set

// Mandatory UF Includes
// UF_initialize, UF_terminate, uc1600
#include <uf.h>
#include <uf_object_types.h>
#include <uf_ui.h>

// Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx>

// Internal+External Includes
#include <uf_defs.h>
#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/BasePart.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/ReferenceSet.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Update.hxx>


// Std C++ Includes
#include <iostream>
#include <sstream>

using namespace NXOpen;
using namespace std;

//------------------------------------------------------------------------------
// Open C error handling
//------------------------------------------------------------------------------
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
int report_error( char *file, int line, char *call, int code)
{
    if (code)
    {

      stringstream errmsg;
      errmsg << "Error " << code << " in " << file << " at line " << line << endl;
      errmsg << call << endl;
      UI::GetUI()->NXMessageBox()->Show("Open C Error", NXOpen::NXMessageBox::DialogTypeError, errmsg.str().c_str());
      throw NXOpen::NXException::Create(code);
    }
    return(code);
}

//------------------------------------------------------------------------------
// NXOpen c++ test class
//------------------------------------------------------------------------------
class MyClass
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;

    MyClass();
    ~MyClass();

    void do_it();
    void print(const NXString &);

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

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

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
MyClass::MyClass()
{
    // Initialize the Open C API environment */
    UF_CALL( UF_initialize() );

    // Initialize the NX Open C++ API environment
    MyClass::theSession = NXOpen::Session::GetSession();
    MyClass::theUI = UI::GetUI();
    mb = theUI->NXMessageBox();
    lw = theSession->ListingWindow();

    workPart = theSession->Parts()->Work();
    displayPart = theSession->Parts()->Display();
   
}

//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
MyClass::~MyClass()
{
    UF_CALL( UF_terminate() );
}

//------------------------------------------------------------------------------
// Print string to listing window or stdout
//------------------------------------------------------------------------------
void MyClass::print(const NXString &msg)
{
    if(! lw->IsOpen() ) lw->Open();
    lw->WriteLine(msg);
}




//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{
    // Prompt for Reference Set name
    char refsetName = "My Bodies";
    int length = 0, resp = 0;
    resp = uc1600("Reference Set Name", refsetName, &length);

    if( resp == 5 || resp == 3)
    {
      // Create Reference Set
      UI::GetUI()->NXMessageBox()->Show("Reference Set Name", NXOpen::NXMessageBox::DialogTypeInformation, refsetName);

      Session::UndoMarkId markId1;
      markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible, "Start");

      ReferenceSet *referenceSet1;
      referenceSet1 = workPart->CreateReferenceSet();
      referenceSet1->SetName(refsetName);
   
      int nErrs2;
      nErrs2 = theSession->UpdateManager()->DoUpdate(markId1);
    }
}

//------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
//Explicit Execution
extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
    try
    {
      // Create NXOpen C++ class instance
      MyClass *theMyClass;
      theMyClass = new MyClass();
      theMyClass->do_it();
      delete theMyClass;
    }
    catch (const NXException& e1)
    {
      UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
    }
    catch (const exception& e2)
    {
      UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
    }
    catch (...)
    {
      UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
    }
}


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


页: [1]
查看完整版本: UG NX二次开发源码分享:创建自定义引用集