NX二次开发 NXOpen::BlockStyler::Tree

简单介绍:

在NX二次开发中,经常使用集列表,列表框,树控件,

但随着功能的变更,发现集列表/列表框有时已不再适用,所以优先使用树控件,

下面介绍树控件常用方法,直接上代码!

//基本概念:一个Node节点代表一行
//***部分命令请注意顺序关系***

//插入列,注意插入列仅initialize_cb()/dialogShown_cb()设置一次,update()时会报错
for (int i = 0; i < columnCount; ++i)
{
    tree->InsertColumn(i, "", columnWidth);
    tree->SetColumnResizePolicy(i, BlockStyler::Tree::ColumnResizePolicyConstantWidth);
    tree->SetColumnSortable(i, false);
}

//创建    
Node *node = tree->CreateNode("label");
tree->InsertNode(node, NULL, NULL, Tree::NodeInsertOptionLast);
tree->InsertNode(node, parentNode, NULL, Tree::NodeInsertOptionLast);//父节点

//遍历子节点
void GetAllNode(Node* inputNode, vector<Node*> &nodes)
{
    if (inputNode == NULL)
    {
        return;
    }

    Node* node = inputNode->FirstChildNode();
    while (node != NULL)
    {
        nodes.push_back(node);
        NXCommon::GetAllNode(node, nodes);
        node = node->NextSiblingNode();
    }
}

//得到树控件所有节点
vector<Node*> GetAllNodes(Tree* tree)
{
    vector<Node*> res;
    Node *nd = tree->RootNode();
    while (nd != NULL)
    {
        res.push_back(nd);
        nd = nd->NextSiblingNode();
    }
    return res;
}

/得到树控件所有节点 含子节点
std::vector<Node*> GetAllNodesContainSon(Tree* tree)
{
    Node *nd = tree->RootNode();

    vector<Node*> res;
    while (nd != NULL)
    {
        res.push_back(nd);
        GetAllNode(nd, res);
        nd = nd->NextSiblingNode();
    }

    return res;
}

//清空树节点
void ClearTree(Tree* tree)
{
    vector<BlockStyler::Node*> nodes = GetAllNodes(tree);
    for (int i = 0; i < nodes.size(); i++)
    {
        tree->DeleteNode(nodes[i]);
    }
}

//节点选择状态
tree->SelectNode(node, false, false); //不选中
node->IsSelected();  //节点是否选中

//显示文本
node->SetColumnDisplayText(columnID, "Text");

//节点添加属性
node->GetNodeData()->AddInteger("XX",int);
node->GetNodeData()->SetInteger("XX",int); //先调用AddInteger才有效,否则报错

//展开节点
node->Expand(NXOpen::BlockStyler::Node::ExpandOption::ExpandOptionExpand);

//注意:OnEditOptionSelectedCallback()不支持节点增删改
NXOpen::BlockStyler::Tree::EditControlOption OnEditOptionSelectedCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID, int selectedOptionID, NXString selectedOptionText, NXOpen::BlockStyler::Tree::ControlType type)
{
    if (tree == NULL || node == NULL)
        return Tree::EditControlOptionReject;

    //如果要检查输入值是否为double,必须强制检查
    std::string originText = selectedOptionText.GetLocaleText(); //重要 得到输入的值
    std::string text = NXCommon::DoubleToStr(NXCommon::StrToDouble(originText));
    if (originText != text)
    {
        return Tree::EditControlOptionReject;
    }

    return Tree::EditControlOptionAccept;
}

//编辑Node某列
NXOpen::BlockStyler::Tree::ControlType AskEditControlCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID)
{
    if (tree == NULL || node == NULL)
    {
        return Tree::ControlTypeNone;
    }
    
    //只有枚举样式,设置一个值时,效果等同字符串控件    
    std::vector<NXString> tmpContents;
    tmpContents.push_back("A");
    tmpContents.push_back("B");
    tree->SetEditOptions(tmpContents, 0);
    return Tree::ControlTypeComboBox;
}

//Node 右键菜单
void OnMenuCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID )
{
    TreeListMenu *menu = tree->CreateMenu();
    menu->AddMenuItem(0, "创建");
    menu->AddMenuItem(1, "删除");
    tree->SetMenu(menu);
    delete menu;
    menu = NULL;
}

//Node 拖动
//更新树控件时,Tree最好只更新一次,
//否则代码中未执行完的代码可能报错,如:node[i]->GetColumnDisplayText(0).GetLocaleText();
目前NX树控件不支持直接拖动,可以通过
(1) 重绘树控件
(2) 将拖动节点备份,重新插入目标节点(个人觉得比较麻烦)
(3) 其他....

NXOpen::BlockStyler::Node::DragType IsDragAllowedCallback(NXOpen::BlockStyler::Tree *tree, NXOpen::BlockStyler::Node *node, int columnID)
{
    if (tree == NULL || node == NULL)
        return BlockStyler::Node::DragTypeNone;

    return BlockStyler::Node::DragTypeAll;//允许节点拖动,但没有拖动效果
}

//Node 某列排序  注意:代码放置在数据填充之后生效
tree->SetSortRootNodes(true);
tree->SetColumnSortable(columnID, true);
tree->SetColumnSortOption(columnID, Tree::ColumnSortOption::ColumnSortOptionDescending);
//排序回调函数处理 
int ColumnSortCallback(NXOpen::BlockStyler::Tree *tree, int columnID, NXOpen::BlockStyler::Node *node1, NXOpen::BlockStyler::Node *node2)
{
    //返回值为0、正负值;分别表示两个节点相同,第一个节点大于第二个,第一个节点小于第二个
    int res = 0;

    
    //注意:node1是树控件上后一个节点,node2是前一个节点 !!!
    std::string content1 = node1->GetColumnDisplayText(columnID).GetLocaleText();
    std::string content2 = node2->GetColumnDisplayText(columnID).GetLocaleText();
    if (IsDoubleString(content1))
    {
        if (IsGT(StrToDouble(content1), StrToDouble(content2)))
        {
            res = 1;
        }
        else
        {
            res = -1;
        }
    }
    else
    {
        if (content1 > content2)
        {
            res = 1;
        }
        else
        {
            res = -1;
        }
    }

    return res;
}


//Node 某列显示/隐藏
tree->SetColumnVisible(columnID,false);

//Node 第一列前:打勾+图片
node->SetState(1); //1 = 不打勾,2 = 打勾
node->SetDisplayIcon(bmpPath);
node->SetSelectedIcon(bmpPath);

//Node 设置显示文本颜色
node->SetForegroundColor(186);

补充:

1. 如果树控件上的菜单按钮调用子对话框,则主对话框关闭,NX内部报错

2. 关于树节点,子节点是获取不到的,如果父节点删除了,再删除子节点报错

  • 4
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
是的,NXOpen::BlockStyler中也有PickPoint函数,用于在用户界面中选择一个点。BlockStyler是NXOpen API的一部分,它提供了一种创建用户界面的方法,可以将自定义功能添加到NX的工具栏和菜单中。 下面是一个使用BlockStyler中PickPoint函数的示例代码: ```cpp #include "NXOpen/BlockStyler_UIBlock.hxx" #include "NXOpen/BlockStyler.hxx" #include "NXOpen/NXMessageBox.hxx" #include "NXOpen/Part.hxx" #include "NXOpen/PartCollection.hxx" #include "NXOpen/Point.hxx" #include "NXOpen/Point3d.hxx" using namespace NXOpen; using namespace NXOpen::BlockStyler; class MyClass { public: static void OnButtonClick(const NXOpen::BlockStyler::UIBlock* block) { Part* workPart = NXOpen::Session::GetSession()->Parts()->Work(); Point* point = workPart->Points()->CreatePoint(0, 0, 0); Point3d pickedPoint = NXOpen::BlockStyler::Utilities::PickPoint(workPart); point->SetCoordinates(pickedPoint.X, pickedPoint.Y, pickedPoint.Z); workPart->ModelingViews()->WorkView()->Regenerate(); } }; extern "C" DllExport int ufusr_ask_unload() { return (int)Session::LibraryUnloadOptionImmediately; } extern "C" DllExport void ufusr(char* param, int* retCode, int paramLen) { NXOpen::Part* part = NXOpen::Session::GetSession()->Parts()->BaseWork(); NXOpen::BlockStyler::BlockDialog* dialog = NXOpen::BlockStyler::BlockDialog::Create(); NXOpen::BlockStyler::UIBlock* button = dialog->TopBlock()->FindBlock("myButton"); button->SetDouble("MinimumWidth", 120.0); button->SetHandler(MyClass::OnButtonClick); dialog->Show(); delete dialog; } ``` 在上面的示例中,我们定义了一个名为MyClass的类,并在其中定义了一个名为OnButtonClick的静态函数。在这个函数中,我们首先获取当前工作部件,然后创建一个点,并使用BlockStyler中PickPoint函数选择一个点。然后,我们将选择的点的坐标设置为点的坐标,并更新显示以查看结果。 在ufusr函数中,我们创建了一个BlockDialog,并获取“myButton”按钮的UIBlock。然后,我们设置按钮的最小宽度,并将MyClass::OnButtonClick函数指定为按钮的处理程序。最后,我们显示对话框,让用户与之交互。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值