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

列文章目录

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. 样式文件目录

目录位置:UGOPEN\SampleNXOpenApplications\C++\BlockStyler\TreeListDemo\TreeListDemo.dlx

2. 样式文件导入预览 

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

3. 样式文件解析

本实例主要用到的控件如下 : 树列表

二、源码文件解析

1. 主程序分析

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

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

2. 处理模块分析

2.1 初始化TreeList

void TreeListDemo::dialogShown_cb()
{
    try
    {
        //---- Enter your callback code here -----

        //Insert Columns        
        tree_control0->InsertColumn(ColumnFirst, "First Column", 80);
        tree_control0->InsertColumn(ColumnSecond, "Second Column", 80);
        tree_control0->InsertColumn(ColumnThird, "Third Column", 80);

        //Set resize policy for columns
        tree_control0->SetColumnResizePolicy(ColumnFirst, Tree::ColumnResizePolicyConstantWidth);
        tree_control0->SetColumnResizePolicy(ColumnSecond, Tree::ColumnResizePolicyConstantWidth);
        tree_control0->SetColumnResizePolicy(ColumnThird, Tree::ColumnResizePolicyConstantWidth);

        //Let the second column interpret the text as icon
        tree_control0->SetColumnDisplayType(ColumnSecond, Tree::ColumnDisplayIcon);        
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        TreeListDemo::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

树列表单独拿出来预初始化,设置表头

 2.2 初始化控件

void TreeListDemo::initialize_cb()
{
    try
    {
        group0 = dynamic_cast(theDialog->TopBlock()->FindBlock("group0"));
        tree_control0 = dynamic_cast(theDialog->TopBlock()->FindBlock("tree_control0"));
        addDeleteNodeGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("addDeleteNodeGroup"));
        nodeString = dynamic_cast(theDialog->TopBlock()->FindBlock("nodeString"));
        addNodeButton = dynamic_cast(theDialog->TopBlock()->FindBlock("addNodeButton"));
        deleteNodeButton = dynamic_cast(theDialog->TopBlock()->FindBlock("deleteNodeButton"));
        stateIconGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("stateIconGroup"));
        stateIconOptions = dynamic_cast(theDialog->TopBlock()->FindBlock("stateIconOptions"));
        nodeToolTip = dynamic_cast(theDialog->TopBlock()->FindBlock("nodeToolTip"));
        NodeEditGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("NodeEditGroup"));
        nodeEditOptions = dynamic_cast(theDialog->TopBlock()->FindBlock("nodeEditOptions"));
        menuGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("menuGroup"));
        showMenuToggle = dynamic_cast(theDialog->TopBlock()->FindBlock("showMenuToggle"));
        dragDropGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("dragDropGroup"));
        disallowDragToggle = dynamic_cast(theDialog->TopBlock()->FindBlock("disallowDragToggle"));
        dropOptions = dynamic_cast(theDialog->TopBlock()->FindBlock("dropOptions"));
        defaultActionGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("defaultActionGroup"));
        defaultActionToggle = dynamic_cast(theDialog->TopBlock()->FindBlock("defaultActionToggle"));
        nodeDataGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("nodeDataGroup"));
        instructions = dynamic_cast(theDialog->TopBlock()->FindBlock("instructions"));
        selection0 = dynamic_cast(theDialog->TopBlock()->FindBlock("selection0"));
        addCrossSelectionNodeButton = dynamic_cast(theDialog->TopBlock()->FindBlock("addCrossSelectionNodeButton"));
        redrawGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("redrawGroup"));
        redrawInstruction = dynamic_cast(theDialog->TopBlock()->FindBlock("redrawInstruction"));
        redrawToggle = dynamic_cast(theDialog->TopBlock()->FindBlock("redrawToggle"));
        listingWindowGroup = dynamic_cast(theDialog->TopBlock()->FindBlock("listingWindowGroup"));
        listingWindowToggle = dynamic_cast(theDialog->TopBlock()->FindBlock("listingWindowToggle"));
        //------------------------------------------------------------------------------
        //Registration of Treelist specific callbacks
        //------------------------------------------------------------------------------
        //tree_control0->SetOnExpandHandler(make_callback(this, &TreeListDemo::OnExpandCallback));

        //tree_control0->SetOnInsertColumnHandler(make_callback(this, &TreeListDemo::OnInsertColumnCallback));

        tree_control0->SetOnInsertNodeHandler(make_callback(this, &TreeListDemo::OnInsertNodeCallback));

        tree_control0->SetOnDeleteNodeHandler(make_callback(this, &TreeListDemo::OnDeleteNodeCallback));

        //tree_control0->SetOnPreSelectHandler(make_callback(this, &TreeListDemo::OnPreSelectCallback));

        tree_control0->SetOnSelectHandler(make_callback(this, &TreeListDemo::OnSelectCallback));

        tree_control0->SetOnStateChangeHandler(make_callback(this, &TreeListDemo::OnStateChangeCallback));

        tree_control0->SetToolTipTextHandler(make_callback(this, &TreeListDemo::ToolTipTextCallback));

        //tree_control0->SetColumnSortHandler(make_callback(this, &TreeListDemo::ColumnSortCallback));

        tree_control0->SetStateIconNameHandler(make_callback(this, &TreeListDemo::StateIconNameCallback));

        tree_control0->SetOnBeginLabelEditHandler(make_callback(this, &TreeListDemo::OnBeginLabelEditCallback));

        tree_control0->SetOnEndLabelEditHandler(make_callback(this, &TreeListDemo::OnEndLabelEditCallback));        

        tree_control0->SetOnEditOptionSelectedHandler(make_callback(this, &TreeListDemo::OnEditOptionSelectedCallback));

        tree_control0->SetAskEditControlHandler(make_callback(this, &TreeListDemo::AskEditControlCallback));

        tree_control0->SetOnMenuHandler(make_callback(this, &TreeListDemo::OnMenuCallback));

        tree_control0->SetOnMenuSelectionHandler(make_callback(this, &TreeListDemo::OnMenuSelectionCallback));

        tree_control0->SetIsDropAllowedHandler(make_callback(this, &TreeListDemo::IsDropAllowedCallback));

        tree_control0->SetIsDragAllowedHandler(make_callback(this, &TreeListDemo::IsDragAllowedCallback));

        tree_control0->SetOnDropHandler(make_callback(this, &TreeListDemo::OnDropCallback));

        //tree_control0->SetOnDropMenuHandler(make_callback(this, &TreeListDemo::OnDropMenuCallback));

        tree_control0->SetOnDefaultActionHandler(make_callback(this, &TreeListDemo::OnDefaultAction));

        //------------------------------------------------------------------------------

        BlockStyler::PropertyList *stringPropList = nodeString->GetProperties() ;
        nodeBaseString = stringPropList->GetString("Value");    
        NXOpen::NXString nodeDisplayString = nodeBaseString;
        std::stringstream stringBuffer;
        stringBuffer << nodeSuffixNumber;            
        nodeDisplayString += stringBuffer.str();                
        stringPropList->SetString("Value",nodeDisplayString);            
        delete stringPropList;
    }
    catch(exception& ex)
    {
        //---- Enter your exception handling code here -----
        TreeListDemo::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
}

初始化方法initialize_cb()

获取控件句柄,注册Treelist回调方法,同时设置默认node title自增长

2.3 创建节点、删除节点、创建关联部件对象的节点

BlockStyler::Node* TreeListDemo::CreateAndAddNode(BlockStyler::Node* afterNode)
{
    BlockStyler::Node *parentNode = NULL;

    //Get node display string from the block
    BlockStyler::PropertyList *stringPropList = nodeString->GetProperties() ;
    NXOpen::NXString nodeDisplayString = stringPropList->GetString("Value");
    delete stringPropList;

    BlockStyler::Node *node = tree_control0->CreateNode(nodeDisplayString);

    //Set few properties of node
    node->SetForegroundColor(198);
    node->SetDisplayIcon(randomIconString[randomIcon]);
    node->SetSelectedIcon("draft");

    //Take first selected node as parent node    
    std::vectorselectedNodes=tree_control0->GetSelectedNodes();
    if (selectedNodes.size()>0)
    {
        parentNode = selectedNodes[0];    
    }

    //Insert Node
    tree_control0->InsertNode(node,parentNode,afterNode,tree_control0->NodeInsertOptionSort);
    node->ScrollTo(ColumnFirst,BlockStyler::Node::ScrollCenter);

    //Change the last node color. Making sure that new node color is distinct
    if (previousAddedNode != NULL)
    {
        previousAddedNode->SetForegroundColor(211);
    }
    previousAddedNode = node;    
    
    return node;    
}

void TreeListDemo::DeleteSelectedNodes()
{
    std::vectorselectedNodes=tree_control0->GetSelectedNodes();                
    if(selectedNodes.size()>0)
    {
        while(selectedNodes.size()>0)
        {
            tree_control0->DeleteNode(selectedNodes[0]);
            selectedNodes=tree_control0->GetSelectedNodes();
        }
    }
    else
    {
        theUI->NXMessageBox()->Show("Delete Node", NXOpen::NXMessageBox::DialogTypeInformation, "No Nodes are selected");
    } 
}

3. 运行结果截图

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

总结

官方实例TreeListDemo

本节主要讲解了树列表的使用方法,包括初始化、复制、创建节点、删除节点及各种附加属性的使用

你可能感兴趣的:(UG二开自学,学习,开发语言,c++)