NX二次开发-BlockUI不用选择控件,用鼠标点选去实时选择

NX二次开发-BlockUI不用选择控件,用鼠标点选去实时选择_第1张图片

用计时器去实现

//==============================================================================
//  WARNING!!  This file is overwritten by the Block Styler while generating
//  the automation code. Any modifications to this file will be lost after
//  generating the code again.
//
//       Filename:  C:\Program Files\Siemens\NX 11.0\CaesarToolkits\Application\testCAM.hpp
//
//        This file was generated by the NX Block Styler
//        Created by: Administrator
//              Version: NX 11
//              Date: 05-29-2020  (Format: mm-dd-yyyy)
//              Time: 11:08
//
//==============================================================================

#ifndef TESTCAM_H_INCLUDED
#define TESTCAM_H_INCLUDED

//------------------------------------------------------------------------------
//These includes are needed for the following template code
//------------------------------------------------------------------------------
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

/*头文件,命名空间,宏定义*/
#include 
#include 
#include 
using std::ostrstream;
using std::endl;
using std::ends;
using std::cerr;
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include   
#pragma comment(lib, "User32.lib")
#pragma warning(disable:4267)

//------------------------------------------------------------------------------
// Namespaces needed for following template
//------------------------------------------------------------------------------
using namespace std;
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

#undef CreateDialog

class DllExport testCAM
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;
    testCAM();
    ~testCAM();
    int Show();
    
    //void CALLBACK MyTimer(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime);
    //----------------------- BlockStyler Callback Prototypes ---------------------
    // The following member function prototypes define the callbacks 
    // specified in your BlockStyler dialog.  The empty implementation
    // of these prototypes is provided in the testCAM.cpp file. 
    // You are REQUIRED to write the implementation for these functions.
    //------------------------------------------------------------------------------
    void initialize_cb();
    void dialogShown_cb();
    int update_cb(NXOpen::BlockStyler::UIBlock* block);
    PropertyList* GetBlockProperties(const char *blockID);
    
private:
    const char* theDlxFileName;
    NXOpen::BlockStyler::BlockDialog* theDialog; 
};

//将字符串控件定义到类的外面
NXOpen::BlockStyler::StringBlock* string0;// Block type: String

#endif //TESTCAM_H_INCLUDED
//==============================================================================
//  WARNING!!  This file is overwritten by the Block UI Styler while generating
//  the automation code. Any modifications to this file will be lost after
//  generating the code again.
//
//       Filename:  C:\Program Files\Siemens\NX 11.0\CaesarToolkits\Application\testCAM.cpp
//
//        This file was generated by the NX Block UI Styler
//        Created by: Administrator
//              Version: NX 11
//              Date: 05-29-2020  (Format: mm-dd-yyyy)
//              Time: 11:08 (Format: hh-mm)
//
//==============================================================================

//==============================================================================
//  Purpose:  This TEMPLATE file contains C++ source to guide you in the
//  construction of your Block application dialog. The generation of your
//  dialog file (.dlx extension) is the first step towards dialog construction
//  within NX.  You must now create a NX Open application that
//  utilizes this file (.dlx).
//
//  The information in this file provides you with the following:
//
//  1.  Help on how to load and display your Block UI Styler dialog in NX
//      using APIs provided in NXOpen.BlockStyler namespace
//  2.  The empty callback methods (stubs) associated with your dialog items
//      have also been placed in this file. These empty methods have been
//      created simply to start you along with your coding requirements.
//      The method name, argument list and possible return values have already
//      been provided for you.
//==============================================================================

//------------------------------------------------------------------------------
//These includes are needed for the following template code
//------------------------------------------------------------------------------
#include "testCAM.hpp"
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

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

//定义句柄变量
HWND UGHwnd;
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
testCAM::testCAM()
{
    try
    {
        // Initialize the NX Open C++ API environment
        testCAM::theSession = NXOpen::Session::GetSession();
        testCAM::theUI = UI::GetUI();
        theDlxFileName = "testCAM.dlx";
        theDialog = testCAM::theUI->CreateDialog(theDlxFileName);
        // Registration of callback functions
        theDialog->AddUpdateHandler(make_callback(this, &testCAM::update_cb));
        theDialog->AddInitializeHandler(make_callback(this, &testCAM::initialize_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &testCAM::dialogShown_cb));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        throw;
    }
}

//------------------------------------------------------------------------------
// Destructor for NX Styler class
//------------------------------------------------------------------------------
testCAM::~testCAM()
{
    if (theDialog != NULL)
    {
        delete theDialog;
        theDialog = NULL;
    }
    //取消定时器
    KillTimer(UGHwnd, 1);
}
//------------------------------- DIALOG LAUNCHING ---------------------------------
//
//    Before invoking this application one needs to open any part/empty part in NX
//    because of the behavior of the blocks.
//
//    Make sure the dlx file is in one of the following locations:
//        1.) From where NX session is launched
//        2.) $UGII_USER_DIR/application
//        3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
//            recommended. This variable is set to a full directory path to a file 
//            containing a list of root directories for all custom applications.
//            e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_BASE_DIR\ugii\menus\custom_dirs.dat
//
//    You can create the dialog using one of the following way:
//
//    1. USER EXIT
//
//        1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
//        2) Invoke the Shared Library through File->Execute->NX Open menu.
//
//------------------------------------------------------------------------------


//定义变量
string data1 = "";
//创建回调函数
void CALLBACK MyTimer(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
    //获取当前加工导航器选中的对象数量和TAG
    int count = 0;
    tag_t* objects = NULL_TAG;
    UF_UI_ONT_ask_selected_nodes(&count, &objects);

    //UF_UI_open_listing_window();
    if (objects[0] != NULL_TAG)
    {
        //由tag获得名字
        char name[256];
        UF_OBJ_ask_name(objects[0], name);
        //转换
        string data2 = name;

        if (data1 != data2)//判断名字是否重复
        {
            //UF_UI_write_listing_window(name);
            //UF_UI_write_listing_window("\n");
            string0->SetValue(name);//把名字设置到字符串控件上
            data1 = name;//赋值
        }
    }
}


extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{
    testCAM *thetestCAM = NULL;
    try
    {
        UF_initialize();

        thetestCAM = new testCAM();
        // The following method shows the dialog immediately
        thetestCAM->Show();
        
        UF_terminate();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        testCAM::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    if(thetestCAM != NULL)
    {
        delete thetestCAM;
        thetestCAM = NULL;
    }
}

//------------------------------------------------------------------------------
// This method specifies how a shared image is unloaded from memory
// within NX. This method gives you the capability to unload an
// internal NX Open application or user  exit from NX. Specify any
// one of the three constants as a return value to determine the type
// of unload to perform:
//
//
//    Immediately : unload the library as soon as the automation program has completed
//    Explicitly  : unload the library from the "Unload Shared Image" dialog
//    AtTermination : unload the library when the NX session terminates
//
//
// NOTE:  A program which associates NX Open applications with the menubar
// MUST NOT use this option since it will UNLOAD your NX Open application image
// from the menubar.
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
    //return (int)Session::LibraryUnloadOptionExplicitly;
    return (int)Session::LibraryUnloadOptionImmediately;
    //return (int)Session::LibraryUnloadOptionAtTermination;
}

//------------------------------------------------------------------------------
// Following method cleanup any housekeeping chores that may be needed.
// This method is automatically called by NX.
//------------------------------------------------------------------------------
extern "C" DllExport void ufusr_cleanup(void)
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        testCAM::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

int testCAM::Show()
{
    try
    {
        theDialog->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        testCAM::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

//------------------------------------------------------------------------------
//---------------------Block UI Styler Callback Functions--------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
void testCAM::initialize_cb()
{
    try
    {
        string0 = dynamic_cast(theDialog->TopBlock()->FindBlock("string0"));

        //获得NX窗口句柄
        UGHwnd = (HWND)UF_UI_get_default_parent();

        //每隔500ms运行一次回调函数
        SetTimer(UGHwnd, 1, 500, (TIMERPROC)MyTimer);
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        testCAM::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: dialogShown_cb
//This callback is executed just before the dialog launch. Thus any value set 
//here will take precedence and dialog will be launched showing that value. 
//------------------------------------------------------------------------------
void testCAM::dialogShown_cb()
{
    try
    {
        //---- Enter your callback code here -----    
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        testCAM::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int testCAM::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
    try
    {
        if(block == string0)
        {
        //---------Enter your code here-----------
        }
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        testCAM::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

//------------------------------------------------------------------------------
//Function Name: GetBlockProperties
//Description: Returns the propertylist of the specified BlockID
//------------------------------------------------------------------------------
PropertyList* testCAM::GetBlockProperties(const char *blockID)
{
    return theDialog->GetBlockProperties(blockID);
}

Caesar卢尚宇

2020年5月30日

你可能感兴趣的:(NX二次开发-BlockUI不用选择控件,用鼠标点选去实时选择)