列文章目录

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. 样式文件导入预览

3. 样式文件解析

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

二、源码文件解析

1. 主程序分析

extern "C" DllExport void  ufusr(char *param, int *retcod, int param_len)
{try{theEditExpression = new EditExpression();// The following method shows the dialog immediatelytheEditExpression->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 environmentEditExpression::theSession = NXOpen::Session::GetSession();EditExpression::theUI = UI::GetUI();theDialogName = "EditExpression.dlx";theDialog = EditExpression::theUI->CreateDialog(theDialogName.c_str());// Registration of callback functionstheDialog->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<NXOpen::BlockStyler::SetList* >(theDialog->TopBlock()->FindBlock("FeatureList"));GroupExpressionList = theDialog->TopBlock()->FindBlock("GroupExpressionList");ButtonGetExpressions = theDialog->TopBlock()->FindBlock("ButtonGetExpressions");ExpressionList = dynamic_cast<NXOpen::BlockStyler::ListBox* >(theDialog->TopBlock()->FindBlock("ExpressionList"));GroupExpression = theDialog->TopBlock()->FindBlock("GroupExpression");ExpressionName = theDialog->TopBlock()->FindBlock("ExpressionName");ExpressionValue = theDialog->TopBlock()->FindBlock("ExpressionValue");// User defined codenewFeatCol.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<NXOpen::BlockStyler::SetList* >(theDialog->TopBlock()->FindBlock("FeatureList"));
        GroupExpressionList = theDialog->TopBlock()->FindBlock("GroupExpressionList");
        ButtonGetExpressions = theDialog->TopBlock()->FindBlock("ButtonGetExpressions");
        ExpressionList = dynamic_cast<NXOpen::BlockStyler::ListBox* >(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<NXOpen::BlockStyler::CompositeBlock* >(FeatureList->FindUpdated());if (updatedBlock != NULL) {NXOpen::BlockStyler::UIBlock* subBlock = updatedBlock->FindBlock("select_feature0");if( subBlock != NULL) {PropertyList* subBlockProperties = subBlock->GetProperties();std::vector<NXOpen::TaggedObject* > 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<NXOpen::Features::Feature*>(newFeatCol[0]);std::vector<NXOpen::NXString> str;str.push_back(feature1->JournalIdentifier());FeatureList->SetItemText(updatedBlock, str);delete subBlockProperties;}}}else if(block == ButtonGetExpressions){std::vector<NXOpen::BlockStyler::UIBlock* > selectedBlocks = FeatureList->GetSelected();UIBlock* updatedBlock = NULL;if (selectedBlocks.size() > 0){updatedBlock = selectedBlocks[0];}if (updatedBlock != NULL){FeatureList->GetItemText(updatedBlock);std::vector<NXOpen::NXString> strings = FeatureList->GetItemText(updatedBlock);if ( strings[0].GetText() != "" ){NXOpen::Features::Feature* feat = workPart->Features()->FindObject(strings[0]);std::vector<NXOpen::Expression* > allExp = feat->GetExpressions();std::vector<NXOpen::NXString> 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<NXOpen::NXString> listStrings = ExpressionListProperties->GetStrings("ListItems");std::vector<int> 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<NXOpen::Expression* > allExp = feat->GetExpressions();

显示表达式方法:

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

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

std::vector<int> 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<NXOpen::Unit*>(workPart->UnitCollection()->FindObject("MilliMeter"));NXOpen::Session::UndoMarkId markId2 = EditExpression::theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, "Update Expression Data");// Convert angle in double to std::stringstd::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. 运行结果截图

总结

官方实例 EditExpression

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

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

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

  1. UG/NX二次开发Siemens官方NXOPEN实例解析—1.6 BlockStyler/SelectionExample

    列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析-1.1 BlockStyler/ColoredBlock UG/NX二次开发Siemens官方NXOPEN实例解析-1.2 Blo ...

  2. UG/NX二次开发Siemens官方NXOPEN实例解析—1.8 BlockStyler/UDB_CreateCylinder

    列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析-1.1 BlockStyler/ColoredBlock UG/NX二次开发Siemens官方NXOPEN实例解析-1.2 Blo ...

  3. UG/NX二次开发Siemens官方NXOPEN实例解析—1.3 BlockStyler/ExtrudewithPreview

    列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析-1.1 BlockStyler/ColoredBlock UG/NX二次开发Siemens官方NXOPEN实例解析-1.2 Blo ...

  4. UG/NX二次开发Siemens官方NXOPEN实例解析—2.8 DrawingCycle(图纸打印)

    列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析-2.1 AssemblyViewer(树列表) UG/NX二次开发Siemens官方NXOPEN实例解析-2.2 Selectio ...

  5. UG/NX二次开发Siemens官方NXOPEN实例解析—2.7 DiameterSymbol(标注符号)

    列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析-2.1 AssemblyViewer(树列表) UG/NX二次开发Siemens官方NXOPEN实例解析-2.2 Selectio ...

  6. UG/NX二次开发Siemens官方NXOPEN实例解析—2.6 CreateNote(注释)

    列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析-2.1 AssemblyViewer(树列表) UG/NX二次开发Siemens官方NXOPEN实例解析-2.2 Selectio ...

  7. UG/NX二次开发Siemens官方NXOPEN实例解析—2.5 QuickExtrude(拉伸)

    列文章目录 UG/NX二次开发Siemens官方NXOPEN实例解析-2.1 AssemblyViewer(树列表) UG/NX二次开发Siemens官方NXOPEN实例解析-2.2 Selectio ...

  8. UG/NX 二次开发(C#)自动出2D零件图(标准件配置Bata版)

    一.前言 昨天分享了自动出2D零件图的思路(UG/NX 二次开发(C#)自动出2D零件图思路),今天整理了Q群里各位大佬的意见,结合当前实际项目情况,做了一个可配置的半自动出图版本,暂且称之为标准件配 ...

  9. UG NX二次开发(C#)-装配-添加组件AddComponent

    在UG NX的使用过程中,装配是很重要的一环,本文是为了实现组件的批量装配,采用UG NX二次开发,采用的主要函数为AddComponent. AddComponent()的帮助说明如下: 其是属于N ...

最新文章

  1. django(models)视图与html 简单的操作
  2. dsp广告和信息流广告区别_一文搞懂DSP-蘑菇街DSP广告实践
  3. 洛谷 P1417 烹调方案
  4. 如何制定有价值的目标
  5. 李书福退出吉利汽车集团公司董事
  6. 草根学Python(十四) 一步一步了解正则表达式
  7. docker 之镜像制作dockerfile
  8. 计算机软件的英文简称,计算机常见英文缩写.docx
  9. 黑盒测试和白盒测试详解
  10. 面部捕捉技术_FT45面部表情捕捉系统--说明书(中文版)
  11. 基于 RSSHub 搭建 RSS 生成器(群晖 Docker)
  12. samba将远程服务器映射到本地磁盘
  13. 时代当歌,懂行为剑,英雄正少年
  14. 法国优秀的五位数学家
  15. 互联网公司的主要角色以及其职责
  16. 数字信号处理随堂笔记(2)ᝰ离散时间信号与系统的频域分析
  17. H.264(H264)视频文件的制作
  18. 2017年企业调薪幅度公布 你和高薪差多远?
  19. 制造企业如何通过APS智能排产进行生产计划规划?
  20. 中国科学院计算机网络信息中心科学数据中心,中科院计算机网络信息中心简介...

热门文章

  1. 记录一台DELL笔记本电脑重装系统过程
  2. 第一章SayHello程序
  3. ubuntu 16.04 设置输入法切换方法
  4. 使用U盘PE修复电脑常规问题
  5. 养老院人员定位手环真的如此智能?老人定位手环当然很智能-新导智能
  6. java父类强转成子类
  7. 钗头凤——为IT诗人代腾飞而作 (转)
  8. Unity - 手动创建 dithering tex 3d
  9. 微信搜一搜正在用的新一代海量数据搜索引擎 TurboSearch 是什么来头?
  10. FPGA 20个例程篇:20.USB2.0/RS232/LAN控制并行DAC输出任意频率正弦波、梯形波、三角波、方波(二)