【UG\NX二次开发】NX12.0.2.9版本 选择对象控件-异常问题记录

现象描述

选择对象控件,选择组件中的对象(片体、实体、小平面体等等)时,焦点不位于选择对象控件上时,无法通过常用方法清空选择对象控件,即使在代码中设置Focus,也无法正常清空。
备注:1.选择组件或其他非组件内对象时,则可以正常清空。
     2.焦点在另一个选择对象控件上,则可以正常清空。

尝试解决:
1.使用不同版本制作DLX。
2.调用update_cb(selection0),来清空。
3.封装清空函数,来调用清空。
4.使用空的选择对象控件对当前选择对象控件进行置空。
5.临时容器保存后,设置到当前选择对象控件中。
6.确认平台集是2015。

解决方案:暂无根本解决此问题的方案。上述方法都失败了,皆是对选择对象控件SetSelectedObjects设置时不成功(不报错),只能从逻辑和设置上规避该问题出现。

规避问题:
1.如果选择对象控件本身就是只选组件的控件,则设置过滤器为组件类型。
2.如果选择的对象就是组件中具体某个类型,最好将其单独抽出为一个组件,修改代码,将其进行导入,然后控件设置过滤器为组件类型(代价大)。

结论:
主要就是将选择对象过滤器设为组件类型,围绕着修改代码及逻辑进行处理,将焦点设置到选择对象过滤器上(设置后立马清空也不行,要借助过滤回调),借助过滤回调,在焦点处于选择对象上时,将控件容器进行设置。尽量避免选择对象控件选择的是组件内对象(片体、实体、小平面体等等)。	

现象操作步骤

1.选择对象是组件内的体(过滤器为:片体、实体、小平面体等等)。

2.当焦点不位于选择对象控件上时。用代码置空,不成功。(设置FOCUS也不行,只能手动选择控件改变焦点才有效果)

3.当焦点位于当前选择对象控件上,或者另外一个选择对象控件上时,点击清空。才能成功置空(设置FOCUS也不行,只能手动选择控件改变焦点才有效果)

4.NX 12.0.2.9版本出现该问题,VS2022 采用 2015平台集编译。

5.hpp文件

#ifndef MOVE12TEST_H_INCLUDED
#define MOVE12TEST_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_SelectObject.hxx>
#include <NXOpen/BlockStyler_Button.hxx>
#include <NXOpen/BlockStyler_SpecifyPoint.hxx>

#include <uf_ui.h>
#include <uf_modl.h>
#include <NXOpen/Selection.hxx>

#include <atlbase.h>
#undef CreateDialog

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

class DllExport MOVE12TEST
{
    // class members
public:
    static Session *theSession;
    static UI *theUI;
    MOVE12TEST();
    ~MOVE12TEST();
    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 MOVE12TEST.cpp file. 
    // You are REQUIRED to write the implementation for these functions.
    //------------------------------------------------------------------------------
    void initialize_cb();
    void dialogShown_cb();
    int apply_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* group0;// Block type: Group
    NXOpen::BlockStyler::SelectObject* selection0;// Block type: Selection
    NXOpen::BlockStyler::Button* button0;// Block type: Button
    NXOpen::BlockStyler::SpecifyPoint* point0;// Block type: Specify Point
    
};
#endif //MOVE12TEST_H_INCLUDED

6.cpp文件

#include "MOVE12TEST.hpp"
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(MOVE12TEST::theSession) = NULL;
UI *(MOVE12TEST::theUI) = NULL;
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
/*获取dll完整路径*/
string GetProgramPath()
{
    char filePath[MAX_PATH];
    GetModuleFileNameA(_AtlBaseModule.GetModuleInstance(), filePath, MAX_PATH);
    std::string strDllPath(filePath);
    strDllPath = strDllPath.substr(0, strDllPath.find_last_of("\\") + 1);
    return strDllPath;
}
MOVE12TEST::MOVE12TEST()
{
    try
    {
        // Initialize the NX Open C++ API environment
        MOVE12TEST::theSession = NXOpen::Session::GetSession();
        MOVE12TEST::theUI = UI::GetUI();

        string dllPath = GetProgramPath();
        //拼接好dlx的绝对路径
        theDlxFileName = dllPath.append("MOVE12TEST.dlx").c_str();
        theDialog = MOVE12TEST::theUI->CreateDialog(theDlxFileName);

        // Registration of callback functions
        theDialog->AddApplyHandler(make_callback(this, &MOVE12TEST::apply_cb));
        theDialog->AddOkHandler(make_callback(this, &MOVE12TEST::ok_cb));
        theDialog->AddUpdateHandler(make_callback(this, &MOVE12TEST::update_cb));
        theDialog->AddInitializeHandler(make_callback(this, &MOVE12TEST::initialize_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &MOVE12TEST::dialogShown_cb));
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        throw;
    }
}

//------------------------------------------------------------------------------
// Destructor for NX Styler class
//------------------------------------------------------------------------------
MOVE12TEST::~MOVE12TEST()
{
    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_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.
//
//------------------------------------------------------------------------------
extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{
    MOVE12TEST *theMOVE12TEST = NULL;
    try
    {
        theMOVE12TEST = new MOVE12TEST();
        // The following method shows the dialog immediately
        theMOVE12TEST->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        MOVE12TEST::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    if(theMOVE12TEST != NULL)
    {
        delete theMOVE12TEST;
        theMOVE12TEST = 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 -----
        MOVE12TEST::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

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

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

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
void MOVE12TEST::initialize_cb()
{
    try
    {
        group0 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group0"));
        selection0 = dynamic_cast<NXOpen::BlockStyler::SelectObject*>(theDialog->TopBlock()->FindBlock("selection0"));
        button0 = dynamic_cast<NXOpen::BlockStyler::Button*>(theDialog->TopBlock()->FindBlock("button0"));
        point0 = dynamic_cast<NXOpen::BlockStyler::SpecifyPoint*>(theDialog->TopBlock()->FindBlock("point0"));

       /* Selection::SelectionScope scope = Selection::SelectionScopeUseDefault;
        Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
        vector<Selection::MaskTriple> maskArray(1);
        maskArray[0] = Selection::MaskTriple(UF_component_type, 0, 0);
        selection0->GetProperties()->SetSelectionFilter("SelectionFilter", action, maskArray);*/
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        MOVE12TEST::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 MOVE12TEST::dialogShown_cb()
{
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        MOVE12TEST::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
int MOVE12TEST::apply_cb()
{
    int errorCode = 0;
    try
    {
        //---- Enter your callback code here -----
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        MOVE12TEST::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int MOVE12TEST::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
    try
    {
        if(block == selection0)
        {
        //---------Enter your code here-----------
        }
        else if(block == button0)
        {
            selection0->Focus();
            if (0)
            {
                std::vector<NXOpen::TaggedObject*> GetSel111 = this->selection0->GetSelectedObjects();
                GetSel111.clear();
                this->selection0->SetSelectedObjects(GetSel111);
            }

            if (1)
            {
                std::vector<NXOpen::TaggedObject*> GetSel;
                GetSel.clear();
                this->selection0->SetSelectedObjects(GetSel);
            }

            selection0->Focus();
        }
        else if(block == point0)
        {
        //---------Enter your code here-----------
        }
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        MOVE12TEST::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

//------------------------------------------------------------------------------
//Callback Name: ok_cb
//------------------------------------------------------------------------------
int MOVE12TEST::ok_cb()
{
    int errorCode = 0;
    try
    {
        errorCode = apply_cb();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        errorCode = 1;
        MOVE12TEST::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
}

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

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
UG二次开发选择对象控件时,其主要考虑因素包括控件的功能、易用性、灵活性、性能、稳定性和扩展性等。 首先,对象控件应能满足UG二次开发的功能需求。它应具备与UG主程序无缝集成的能力,能够扩展和优化UG的功能,实现自定义的工具和操作流程。具体而言,对象控件应支持UG的对象模型,能够获取和修改模型数据,执行各种操作,如创建、修改、删除等。 其次,对象控件应易用性强。对于UG二次开发人员而言,使用对象控件应简单直观,提供友好的API和文档,以减少开发难度和时间。控件应提供清晰的接口,使开发人员能够轻松地访问和操作UG的对象模型,开发出高效且符合要求的功能和工具。 此外,对象控件还应具备灵活性。它应能够适应不同的开发需求,具有扩展性,可通过自定义功能以满足特定需求。控件应允许开发人员自定义插件或扩展,以便根据需要添加新的功能或修改现有功能。 在性能方面,对象控件应具备较高的执行效率,能够快速响应用户的操作,不影响UG主程序的运行速度。尤其是在处理大型模型和复杂操作时,控件应具备良好的性能优化能力。 最后,对象控件应具备较高的稳定性和可靠性。它应经过充分测试和验证,没有明显的BUG,并且能够在长时间运行中保持良好的稳定性和可靠性,不会造成UG主程序的崩溃或异常。 综上所述,UG二次开发选择对象控件时,需要综合考虑控件的功能、易用性、灵活性、性能、稳定性和扩展性等因素,并根据具体需求选择最适合的对象控件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

社恐猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值