实例开发-NX二次开发批量设置体到图层工具

本实例非博主原创,是从大神的视频课程中学习的。

  • 版本:

NX9+VS2012

  • 演示:

  • 源代码:
//==============================================================================
//  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:  D:CaesarToolkitapplicationSetBodyLayer.hpp
//
//        This file was generated by the NX Block Styler
//        Created by: Administrator
//              Version: NX 9
//              Date: 02-18-2021  (Format: mm-dd-yyyy)
//              Time: 20:38
//
//==============================================================================

#ifndef SETBODYLAYER_H_INCLUDED
#define SETBODYLAYER_H_INCLUDED

//------------------------------------------------------------------------------
//These includes are needed for the following template code
//------------------------------------------------------------------------------
#include <uf_defs.h>
#include <uf_ui_types.h>
#include <iostream>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
#include <NXOpen/BlockStyler_Group.hxx>
#include <NXOpen/BlockStyler_BodyCollector.hxx>
#include <NXOpen/BlockStyler_IntegerBlock.hxx>

//头文件
#include <uf.h>
#include <uf_layer.h>
#include <uf_obj.h>

//------------------------------------------------------------------------------
//Bit Option for Property: EntityType
//------------------------------------------------------------------------------
#define                         EntityType_AllowBodies (1 << 6);
//------------------------------------------------------------------------------
//Bit Option for Property: BodyRules
//------------------------------------------------------------------------------
#define                           BodyRules_SingleBody (1 << 0);
#define                        BodyRules_FeatureBodies (1 << 1);
#define                        BodyRules_BodiesinGroup (1 << 2);
//------------------------------------------------------------------------------
// Namespaces needed for following template
//------------------------------------------------------------------------------
using namespace std;
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

class DllExport SetBodyLayer
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;
    SetBodyLayer();
    ~SetBodyLayer();
    int Show();
    
    //----------------------- 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 SetBodyLayer.cpp file. 
    // You are REQUIRED to write the implementation for these functions.
    //------------------------------------------------------------------------------
    void initialize_cb();
    void dialogShown_cb();
    int ok_cb();
    int update_cb(NXOpen::BlockStyler::UIBlock* block);
    PropertyList* GetBlockProperties(const char *blockID);
    
private:
    const char* theDlxFileName;
    NXOpen::BlockStyler::BlockDialog* theDialog;
    NXOpen::BlockStyler::Group* group1;// Block type: Group
    NXOpen::BlockStyler::BodyCollector* bodySelect0;// Block type: Body Collector
    NXOpen::BlockStyler::Group* group2;// Block type: Group
    NXOpen::BlockStyler::IntegerBlock* integer0;// Block type: Integer
    
};
#endif //SETBODYLAYER_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:  D:CaesarToolkitapplicationSetBodyLayer.cpp
//
//        This file was generated by the NX Block UI Styler
//        Created by: Administrator
//              Version: NX 9
//              Date: 02-18-2021  (Format: mm-dd-yyyy)
//              Time: 20:38 (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 "SetBodyLayer.hpp"
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(SetBodyLayer::theSession) = NULL;
UI *(SetBodyLayer::theUI) = NULL;
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
SetBodyLayer::SetBodyLayer()
{
    try
    {
        // Initialize the NX Open C++ API environment
        SetBodyLayer::theSession = NXOpen::Session::GetSession();
        SetBodyLayer::theUI = UI::GetUI();
        theDlxFileName = "SetBodyLayer.dlx";
        theDialog = SetBodyLayer::theUI->CreateDialog(theDlxFileName);
        // Registration of callback functions
        theDialog->AddOkHandler(make_callback(this, &SetBodyLayer::ok_cb));
        theDialog->AddUpdateHandler(make_callback(this, &SetBodyLayer::update_cb));
        theDialog->AddInitializeHandler(make_callback(this, &SetBodyLayer::initialize_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &SetBodyLayer::dialogShown_cb));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        throw;
    }
}

//------------------------------------------------------------------------------
// Destructor for NX Styler class
//------------------------------------------------------------------------------
SetBodyLayer::~SetBodyLayer()
{
    if (theDialog != NULL)
    {
        delete theDialog;
        theDialog = NULL;
    }
}
//------------------------------- 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_ROOT_DIRmenuscustom_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.
//
//------------------------------------------------------------------------------
extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{
    UF_initialize();//初始化

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

    UF_terminate();//终止
}

//------------------------------------------------------------------------------
// 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 -----
        SetBodyLayer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

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

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

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
void SetBodyLayer::initialize_cb()
{
    try
    {
        group1 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group1"));
        bodySelect0 = dynamic_cast<NXOpen::BlockStyler::BodyCollector*>(theDialog->TopBlock()->FindBlock("bodySelect0"));
        group2 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group2"));
        integer0 = dynamic_cast<NXOpen::BlockStyler::IntegerBlock*>(theDialog->TopBlock()->FindBlock("integer0"));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        SetBodyLayer::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 SetBodyLayer::dialogShown_cb()
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        SetBodyLayer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

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

//------------------------------------------------------------------------------
//Callback Name: ok_cb
//------------------------------------------------------------------------------
int SetBodyLayer::ok_cb()
{
    int errorCode = 0;
    try
    {
        //获得当前选择的所有实体
        vector<NXOpen::TaggedObject*> bodys = bodySelect0->GetSelectedObjects();
        
        //获得当前输入的起始图层
        int layer_start = integer0->Value();
        
        for (int i = 0; i < bodys.size(); i++)
        {
            //得到单个实体
            tag_t body_tag = bodys[i]->Tag();

            //设置图层
            UF_OBJ_set_layer(body_tag, layer_start);
            layer_start++;

        }


    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        SetBodyLayer::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}

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

Caesar卢尚宇

2021年2月18日

作者: 阿飞

出处: https://www.cnblogs.com/nxopen2018/>

关于作者:......

如有问题, 可在底部(留言)咨询.

原文地址:https://www.cnblogs.com/nxopen2018/p/14413579.html