UG/NX二次开发Siemens官方NXOPEN实例解析 您所在的位置:网站首页 cloudreve二次开发 UG/NX二次开发Siemens官方NXOPEN实例解析

UG/NX二次开发Siemens官方NXOPEN实例解析

2023-01-05 15:52| 来源: 网络整理| 查看: 265

列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析—1.1 BlockStyler/ColoredBlockUG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpressionUG/NX二次开发Siemens官方NXOPEN实例解析—1.3 BlockStyler/ExtrudewithPreviewUG/NX二次开发Siemens官方NXOPEN实例解析—1.4 BlockStyler/HoleCoordinatesUG/NX二次开发Siemens官方NXOPEN实例解析—1.5 BlockStyler/MatrixOperationsUG/NX二次开发Siemens官方NXOPEN实例解析—1.6 BlockStyler/SelectionExampleUG/NX二次开发Siemens官方NXOPEN实例解析—1.7 BlockStyler/TreeListDemoUG/NX二次开发Siemens官方NXOPEN实例解析—1.8 BlockStyler/UDB_CreateCylinderUG/NX二次开发Siemens官方NXOPEN实例解析—1.9 BlockStyler/UpdateExample 文章目录

目录

列文章目录

文章目录

前言

一、UI样式文件分析

1. 样式文件目录

2. 样式文件导入预览 

 3. 样式文件解析

二、源码文件解析

1. 主程序分析

2. 处理模块分析

3. 运行结果截图

总结

前言

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

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

一、UI样式文件分析 1. 样式文件目录

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

2. 样式文件导入预览 

3. 样式文件解析

本实例主要用到的控件如下:曲线收集器、切换开关、选择特征、双精度

二、源码文件解析 1. 主程序分析 extern "C" DllExport void ufusr(char *param, int *retcod, int param_len) { try { theSelectionExample = new SelectionExample(); // The following method shows the dialog immediately theSelectionExample->Show(); } catch(exception& ex) { //---- Enter your exception handling code here ----- SelectionExample::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } delete theSelectionExample; } SelectionExample::SelectionExample() { try { // Initialize the NX Open C++ API environment SelectionExample::theSession = NXOpen::Session::GetSession(); SelectionExample::theUI = UI::GetUI(); theDialogName = "SelectionExample.dlx"; theDialog = SelectionExample::theUI->CreateDialog(theDialogName.c_str()); // Registration of callback functions theDialog->AddApplyHandler(make_callback(this, &SelectionExample::apply_cb)); theDialog->AddOkHandler(make_callback(this, &SelectionExample::ok_cb)); theDialog->AddUpdateHandler(make_callback(this, &SelectionExample::update_cb)); theDialog->AddFilterHandler(make_callback(this, &SelectionExample::filter_cb)); theDialog->AddInitializeHandler(make_callback(this, &SelectionExample::initialize_cb)); theDialog->AddDialogShownHandler(make_callback(this, &SelectionExample::dialogShown_cb)); } catch(exception& ex) { std::string exceptionText = ex.what(); throw; } }

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

2. 处理模块分析

2.1 初始化方法

void SelectionExample::initialize_cb() { try { group0 = theDialog->TopBlock()->FindBlock("group0"); edgeSelect = theDialog->TopBlock()->FindBlock("edge_select0"); faceToggle = theDialog->TopBlock()->FindBlock("toggle0"); angleDouble = theDialog->TopBlock()->FindBlock("double0"); group01 = theDialog->TopBlock()->FindBlock("group01"); chamferSelect = theDialog->TopBlock()->FindBlock("select_feature0"); angleToggle = theDialog->TopBlock()->FindBlock("toggle01"); angleLimitDouble = theDialog->TopBlock()->FindBlock("double01"); //Initialize limitingFace = NULL; } catch(exception& ex) { //---- Enter your exception handling code here ----- SelectionExample::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } }

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

int SelectionExample::update_cb(NXOpen::BlockStyler::UIBlock* block) { try { PropertyList* faceToggleProps = faceToggle->GetProperties(); PropertyList* edgeSelectProps = edgeSelect->GetProperties(); PropertyList* chamferSelectProps = chamferSelect->GetProperties(); PropertyList* angleLimitDoubleProps = angleLimitDouble->GetProperties(); PropertyList* angleToggleProps = angleToggle->GetProperties(); if(block == edgeSelect) { // When face filtering is on, establish a limiting face after two edges are selected if(faceToggleProps->GetLogical("Value")) { std::vector edges = edgeSelectProps->GetTaggedObjectVector("SelectedObjects"); if (edges.size() == 2) { limitingFace = sharedFace(dynamic_cast(edges[0]), dynamic_cast(edges[1])); } } } else if(block == faceToggle) { // When the face filter is turned on... // Clear the current edge selection list if (faceToggleProps->GetLogical("Value")) { std::vector edges(0); edgeSelectProps->SetTaggedObjectVector("SelectedObjects", edges); limitingFace = NULL; } } else if(block == angleDouble) { //---------Enter your code here----------- } else if(block == chamferSelect) { //---------Enter your code here----------- } else if(block == angleToggle) { // When the angle filter is turned on... // 1. Clear the current chamfer selection list // 2. Also, enable/disable the angle limit entry field if (angleToggleProps->GetLogical("Value")) { std::vector chamfers(0); chamferSelectProps->SetTaggedObjectVector("SelectedObjects", chamfers); angleLimitDoubleProps->SetLogical("Enable", true); } else { angleLimitDoubleProps->SetLogical("Enable", false); } } else if(block == angleLimitDouble) { // When the angle limit value changes... // Clear the current chamfer selection list std::vector chamfers(0); chamferSelectProps->SetTaggedObjectVector("SelectedObjects", chamfers); } delete faceToggleProps; faceToggleProps = NULL; delete edgeSelectProps; faceToggleProps = NULL; delete chamferSelectProps; chamferSelectProps = NULL; delete angleLimitDoubleProps; angleLimitDoubleProps = NULL; delete angleToggleProps; angleToggleProps = NULL; } catch(exception& ex) { //---- Enter your exception handling code here ----- SelectionExample::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return 0; }

这里主要是控件edgeSelect、faceToggle、angleToggle、angleLimitDouble的update方法

1、edgeSelect 用来选择曲线并返回,如果选择共面曲线,返回公共面

        std::vector edges = edgeSelectProps->GetTaggedObjectVector("SelectedObjects");

        limitingFace = sharedFace(dynamic_cast(edges[0]), dynamic_cast(edges[1]));

2、faceToggle、angleToggle 共面和共角度开关update事件,触发的时候,清空选择曲线和选择的特征

3、angleLimitDouble 角度输入框update事件,更新的时候清空选择特征控件

2.3 曲线收集器、选择特征控件过滤器

int SelectionExample::filter_cb(NXOpen::BlockStyler::UIBlock* block, NXOpen::TaggedObject* selectedObject) { int accept = UF_UI_SEL_ACCEPT; try { if (block == edgeSelect) { // Edge Select Filter PropertyList* faceToggleProps = faceToggle->GetProperties(); if (faceToggleProps->GetLogical("Value")) { accept = faceFilter(dynamic_cast(selectedObject)); } delete faceToggleProps; faceToggleProps = NULL; } else if (block == chamferSelect) { // Feature Selection Filter - limit selection to chamfer features NXOpen::Features::Feature* featureObject = dynamic_cast(selectedObject); if (strcmp(featureObject->FeatureType().GetText(),"CHAMFER") == 0) { accept = angleFilter(dynamic_cast(featureObject)); } else { accept = UF_UI_SEL_REJECT; } } } catch(exception& ex) { //---- Enter your exception handling code here ----- accept = UF_UI_SEL_REJECT; SelectionExample::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return accept; }

曲线收集器和选择特征控件通过faceFilter和angleFilter方法对控件进行过滤,控制共面和共角度的特征

accept = faceFilter(dynamic_cast(selectedObject));

accept = angleFilter(dynamic_cast(featureObject));

这里分析思路如下:

1)共面分析:获取当前选择的曲线,与之前已选择的曲线进行对比

      1.1  如果没有已选择的曲线,当前曲线直接选择

      1.2  如果已选择的曲线有1条,判断已选择曲线和当前选择曲线是否共面

      1.3  如果已选择的曲线有多条,找出已选择曲线的共面,判断当前现在曲线是否在共面上

2)同角度,直接判断选择的特征角度是否和设置角度匹配即可

      if (chamferBuilder->AngleExp()->Value() != angleLimitDoubleProps->GetDouble("Value")) 

2.4 提交方法

int SelectionExample::apply_cb() { int errorCode = 0; try { //***************************************************************************************** //Add Chamfers to Selected Edges //Get the chamfer angle and edge selection list from the dialog PropertyList* angleDoubleProps = angleDouble->GetProperties(); double creationAngle = angleDoubleProps->GetDouble("Value"); delete angleDoubleProps; angleDoubleProps = NULL; PropertyList* edgeSelectProps = edgeSelect->GetProperties(); std::vector edges = edgeSelectProps->GetTaggedObjectVector("SelectedObjects"); delete edgeSelectProps; edgeSelectProps = NULL; //Add a chamfer to each selected edge for (unsigned int ii = 0; ii < edges.size(); ++ii) { NXOpen::Edge* selectedEdge = dynamic_cast(edges[ii]); addChamfer(selectedEdge, creationAngle); } //***************************************************************************************** //Remove Selected Chamfers //Get the selected chamfers from the dialog PropertyList* chamferSelectProps = chamferSelect->GetProperties(); std::vector chamfers = chamferSelectProps->GetTaggedObjectVector("SelectedObjects"); delete chamferSelectProps; chamferSelectProps = NULL; //Set an undo mark for update Session::UndoMarkId undoMark = theSession->SetUndoMark(Session::MarkVisibilityVisible, "Remove Chamfers"); //Add the selected chamfers to the delete list for (unsigned int ii = 0; ii < chamfers.size(); ++ii) { NXOpen::Features::Chamfer* chamferObject = dynamic_cast(chamfers[ii]); errorCode = deleteObject(chamferObject); } //Update the model to delete the chamfers int nErrs = theSession->UpdateManager()->DoUpdate(undoMark); //Report any errors - normally the error list should be scanned and each error processed if (nErrs > 0) { errorCode = 1; std::stringstream tmpString; tmpString Show("Update Errors", NXOpen::NXMessageBox::DialogTypeError,tmpString.str()); } } catch(exception& ex) { //---- Enter your exception handling code here ----- errorCode = 1; SelectionExample::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what()); } return errorCode; }

 提交方法分2部分,创建倒斜角和删除倒斜角,跟别对应方法:

addChamfer(selectedEdge, creationAngle);

errorCode = deleteObject(chamferObject);

3. 运行结果截图

总结

官方实例MatrixOperations

1、前端BlockUI使用控件:曲线收集器、切换开关、选择特征、双精度,主要知识点如下:

     1)曲线收集器的初始化和获取返回曲线,共面过滤的实现

     2)选择特征的初始化和获取返回特征,共特性过滤的实现

2、后台通过前端获取的信息,实现了曲线收集器和选择特征按照一定的条件进行过滤处理,同时通过ChamferBuilder实现了倒斜角的功能



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有