UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression

列文章目录

UG/NX二次开发Siemens官方NXOPEN实例解析—1.1 BlockStyler/ColoredBlock
UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression
UG/NX二次开发Siemens官方NXOPEN实例解析—1.3 BlockStyler/ExtrudewithPreview
UG/NX二次开发Siemens官方NXOPEN实例解析—1.4 BlockStyler/HoleCoordinates
UG/NX二次开发Siemens官方NXOPEN实例解析—1.5 BlockStyler/MatrixOperations
UG/NX二次开发Siemens官方NXOPEN实例解析—1.6 BlockStyler/SelectionExample
UG/NX二次开发Siemens官方NXOPEN实例解析—1.7 BlockStyler/TreeListDemo
UG/NX二次开发Siemens官方NXOPEN实例解析—1.8 BlockStyler/UDB_CreateCylinder
UG/NX二次开发Siemens官方NXOPEN实例解析—1.9 BlockStyler/UpdateExample

文章目录

目录

列文章目录

文章目录

前言

一、UI样式文件分析

1. 样式文件目录

2. 样式文件导入预览 

 3. 样式文件解析

二、源码文件解析

1. 主程序分析

2. 处理模块分析

3. 运行结果截图

总结


前言

        随着工业智能化的不断发展,UG二次开发的需求越来越多,也吸引了大批的二开从业人员,本人作为一名资深IT从业者(10年+)也毅然加入二次开发大军。

        然而,和流行IT行业(互联网、金融、医疗等)相比,工业智能化的门槛显得更高一点,专业的工业软件,相对封闭的开发理念和更小的开发圈子,让刚进入二开的从业者有点举步维艰。边学边整理,希望通过这系列文章的整理能给二开的生态增添一叶绿。


一、UI样式文件分析

1. 样式文件目录

目录位置:$UGII_BASE_DIR\UGOPEN\SampleNXOpenApplications\C++\BlockStyler\EditExpression

2. 样式文件导入预览 

UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression_第1张图片

UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression_第2张图片

3. 样式文件解析

本实例主要用到的控件有:集列表、列表框、选择特征、双精度、字符串、按钮。

二、源码文件解析

1. 主程序分析

extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{
    try
    {
        theEditExpression = new EditExpression();
        // The following method shows the dialog immediately
        theEditExpression->Show();
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        EditExpression::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    delete theEditExpression;
}
EditExpression::EditExpression()
{
    try
    {
        // Initialize the NX Open C++ API environment
        EditExpression::theSession = NXOpen::Session::GetSession();
        EditExpression::theUI = UI::GetUI();
        theDialogName = "EditExpression.dlx";
        theDialog = EditExpression::theUI->CreateDialog(theDialogName.c_str());
        // Registration of callback functions
        theDialog->AddApplyHandler(make_callback(this, &EditExpression::apply_cb));
        theDialog->AddUpdateHandler(make_callback(this, &EditExpression::update_cb));
        theDialog->AddFilterHandler(make_callback(this, &EditExpression::filter_cb));
        theDialog->AddInitializeHandler(make_callback(this, &EditExpression::initialize_cb));
        theDialog->AddDialogShownHandler(make_callback(this, &EditExpression::dialogShown_cb));
    }
    catch(exception& ex)
    {
        throw;
    }
}

构造函数里,我们需要关注dlx文件加载,回调方法里面主要关注:提交方法apply_cb、更新方法update_cb、初始化方法initialize_cb。

2. 处理模块分析

2.1 初始化方法

void EditExpression::initialize_cb()
{
    try
    {
        GroupFeatureSelection = theDialog->TopBlock()->FindBlock("GroupFeatureSelection");
        FeatureList = dynamic_cast(theDialog->TopBlock()->FindBlock("FeatureList"));
        GroupExpressionList = theDialog->TopBlock()->FindBlock("GroupExpressionList");
        ButtonGetExpressions = theDialog->TopBlock()->FindBlock("ButtonGetExpressions");
        ExpressionList = dynamic_cast(theDialog->TopBlock()->FindBlock("ExpressionList"));
        GroupExpression = theDialog->TopBlock()->FindBlock("GroupExpression");
        ExpressionName = theDialog->TopBlock()->FindBlock("ExpressionName");
        ExpressionValue = theDialog->TopBlock()->FindBlock("ExpressionValue");
        // User defined code
        newFeatCol.clear();
        newBlock = FeatureList->AddNewSet(true);
        FeatureList->SetAddHandler(make_callback(this, &EditExpression::add_handler));
        FeatureList->SetDeleteHandler(make_callback(this, &EditExpression::delete_handler));
        FeatureList->SetReorderObserver(make_callback(this, &EditExpression::reorder_handler));
        expToEdit = NULL;
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        EditExpression::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

初始化方法initialize_cb()

1、获取控件方法:

        GroupFeatureSelection = theDialog->TopBlock()->FindBlock("GroupFeatureSelection");
        FeatureList = dynamic_cast(theDialog->TopBlock()->FindBlock("FeatureList"));
        GroupExpressionList = theDialog->TopBlock()->FindBlock("GroupExpressionList");
        ButtonGetExpressions = theDialog->TopBlock()->FindBlock("ButtonGetExpressions");
        ExpressionList = dynamic_cast(theDialog->TopBlock()->FindBlock("ExpressionList"));
        GroupExpression = theDialog->TopBlock()->FindBlock("GroupExpression");
        ExpressionName = theDialog->TopBlock()->FindBlock("ExpressionName");
        ExpressionValue = theDialog->TopBlock()->FindBlock("ExpressionValue");

2、初始化集列表

        newFeatCol.clear();
        newBlock = FeatureList->AddNewSet(true);
        FeatureList->SetAddHandler(make_callback(this, &EditExpression::add_handler));
        FeatureList->SetDeleteHandler(make_callback(this, &EditExpression::delete_handler));
        FeatureList->SetReorderObserver(make_callback(this, &EditExpression::reorder_handler));
        expToEdit = NULL;

2.2 控件更新/点击/激活方法 

int EditExpression::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
    try
    {
        NXOpen::Part* workPart = EditExpression::theSession->Parts()->Work();
        if(block == FeatureList)
        {
            NXOpen::BlockStyler::CompositeBlock* updatedBlock = 
                dynamic_cast(FeatureList->FindUpdated());
            if (updatedBlock != NULL) 
            {
                NXOpen::BlockStyler::UIBlock* subBlock = updatedBlock->FindBlock("select_feature0");
                if( subBlock != NULL) 
                {
                    PropertyList* subBlockProperties = subBlock->GetProperties();
                    std::vector featCol = subBlockProperties->GetTaggedObjectVector("SelectedObjects");
                    if (featCol.size() > 1)
                    {
                        newFeatCol.clear();
                        newFeatCol.push_back(featCol[featCol.size() - 1]);
                        subBlockProperties->SetTaggedObjectVector("SelectedObjects", newFeatCol);
                    }
                    else if (featCol.size() == 1) 
                    {
                        newFeatCol.clear();
                        newFeatCol.push_back(featCol[0]);
                    }
                    NXOpen::Features::Feature* feature1 = dynamic_cast(newFeatCol[0]);
                    std::vector str;
                    str.push_back(feature1->JournalIdentifier());
                    FeatureList->SetItemText(updatedBlock, str);
                    delete subBlockProperties;
                }
            }
        }
        else if(block == ButtonGetExpressions)
        {
            std::vector selectedBlocks = FeatureList->GetSelected();
            UIBlock* updatedBlock = NULL;

            if (selectedBlocks.size() > 0)
            {
                updatedBlock = selectedBlocks[0];
            }

            if (updatedBlock != NULL)
            {
                
                FeatureList->GetItemText(updatedBlock);
                std::vector strings = FeatureList->GetItemText(updatedBlock);
                if ( strings[0].GetText() != "" )
                {
                    NXOpen::Features::Feature* feat = workPart->Features()->FindObject(strings[0]);
                    std::vector allExp = feat->GetExpressions();
                    std::vector allExpStr;
                    for (unsigned int ii = 0; ii < allExp.size(); ++ii)
                    {
                        Expression* exp = allExp[ii];
                        stringstream tmpStream;
                        tmpStream << exp->Name().GetUTF8Text() << " = " << exp->Value() << " - " << exp->GetDescriptor().GetUTF8Text();
                        allExpStr.push_back(tmpStream.str());
                    }

                    PropertyList* ExpressionListProperties = ExpressionList->GetProperties();
                    ExpressionListProperties->SetStrings("ListItems", allExpStr);
                    delete ExpressionListProperties;

                    FeatureList->SetItemText(updatedBlock, strings);
                }
            }

        }
        else if(block == ExpressionList)
        {
            PropertyList* ExpressionListProperties = ExpressionList->GetProperties();
            std::vector listStrings = ExpressionListProperties->GetStrings("ListItems");
            std::vector index = ExpressionListProperties->GetIntegerVector("SelectedItems");
            delete ExpressionListProperties;

            std::string splitStr (listStrings[index[0]].GetUTF8Text());
            size_t ind = splitStr.find(" ");
            if (ind != splitStr.npos)
            {
                splitStr = splitStr.substr(0, ind);
            }

            PropertyList* ExpressionNameProperties = ExpressionName->GetProperties();
            ExpressionNameProperties->SetString("Value", splitStr);
            delete ExpressionNameProperties;

            expToEdit = workPart->Expressions()->FindObject(splitStr);

            PropertyList* ExpressionValueProperties = ExpressionValue->GetProperties();
            ExpressionValueProperties->SetDouble("Value", expToEdit->Value());
            delete ExpressionValueProperties;
        }
        else if(block == ExpressionName)
        {
        //---------Enter your code here-----------
        }
        else if(block == ExpressionValue)
        {
        //---------Enter your code here-----------
        }
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        EditExpression::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return 0;
}

 这里涉及到FeatureList、ButtonGetExpressions、ExpressionList三个控件的Update方法

1、FeatureList 主要用来获取特征,并把特征插入到集列表

      获取选择的特征方式如下:

      NXOpen::BlockStyler::UIBlock* subBlock = updatedBlock->FindBlock("select_feature0");

      featCol = subBlockProperties->GetTaggedObjectVector("SelectedObjects");

      把特征插入到集列表方法如下: newFeatCol.push_back(featCol[0]);

2、ButtonGetExpressions用来获取指定对象的表达式,并在ExpressionsList显示出来

      获取表达式方法:

      NXOpen::Features::Feature* feat = workPart->Features()->FindObject(strings[0]);
      std::vector allExp = feat->GetExpressions();

      显示表达式方法:

      PropertyList* ExpressionListProperties = ExpressionList->GetProperties();
      ExpressionListProperties->SetStrings("ListItems", allExpStr);

3、ExpressionList的更新方法,用来把选择的表达式显示在下面的输入框,方便后面更新具体的表达式

      std::vector index = ExpressionListProperties->GetIntegerVector("SelectedItems");

      std::string splitStr (listStrings[index[0]].GetUTF8Text());

      ExpressionNameProperties->SetString("Value", splitStr);

      ExpressionValueProperties->SetDouble("Value", expToEdit->Value());

2.3 更新表达式方法 

int EditExpression::apply_cb()
{
    int errorCode = 0;
    try
    {
        if (expToEdit != NULL)
        {
            NXOpen::Part* workPart = EditExpression::theSession->Parts()->Work();
            PropertyList* ExpressionValueProperties = ExpressionValue->GetProperties();
            double editedvalue = ExpressionValueProperties->GetDouble("Value");
            delete ExpressionValueProperties;
            ExpressionValueProperties = NULL;

            Unit* unit1 = dynamic_cast(workPart->UnitCollection()->FindObject("MilliMeter"));

            NXOpen::Session::UndoMarkId markId2 = EditExpression::theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, "Update Expression Data");


            // Convert angle in double to std::string
            std::stringstream expValue;
            expValue << setprecision(16) << editedvalue;
            workPart->Expressions()->EditWithUnits(expToEdit, unit1, expValue.str());

            int nErrs1 = EditExpression::theSession->UpdateManager()->DoUpdate(markId2);

            if (nErrs1 > 0)
            {
                errorCode = 1;
                EditExpression::theUI->NXMessageBox()->Show("Update Errors", NXOpen::NXMessageBox::DialogTypeError, "Update Failed with errors");
            }

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

根据编辑后的表达式,更新对象

expValue << setprecision(16) << editedvalue;
workPart->Expressions()->EditWithUnits(expToEdit, unit1, expValue.str());

int nErrs1 = EditExpression::theSession->UpdateManager()->DoUpdate(markId2);

3. 运行结果截图

UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression_第3张图片

总结

官方实例 EditExpression

1、前端BlockUI,用到了集列表、列表框、选择特征、双精度、字符串、按钮。主要涉及集列表的初始化、赋值和更新,列表框的赋值和取值,特征选择器的使用。

2、后台根据特征选择的对象,在表达式编辑后,提交更新到对象。

你可能感兴趣的:(UG二开自学,学习,c#,ui)