UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression
列文章目錄
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.1 BlockStyler/ColoredBlock |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.2 BlockStyler/EditExpression |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.3 BlockStyler/ExtrudewithPreview |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.4 BlockStyler/HoleCoordinates |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.5 BlockStyler/MatrixOperations |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.6 BlockStyler/SelectionExample |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.7 BlockStyler/TreeListDemo |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.8 BlockStyler/UDB_CreateCylinder |
| UG/NX二次開發(fā)Siemens官方NXOPEN實例解析—1.9 BlockStyler/UpdateExample |
文章目錄
目錄
列文章目錄
文章目錄
前言
一、UI樣式文件分析
1. 樣式文件目錄
2. 樣式文件導入預覽?
?3. 樣式文件解析
二、源碼文件解析
1. 主程序分析
2. 處理模塊分析
3. 運行結(jié)果截圖
總結(jié)
前言
????????隨著工業(yè)智能化的不斷發(fā)展,UG二次開發(fā)的需求越來越多,也吸引了大批的二開從業(yè)人員,本人作為一名資深I(lǐng)T從業(yè)者(10年+)也毅然加入二次開發(fā)大軍。
? ? ? ? 然而,和流行IT行業(yè)(互聯(lián)網(wǎng)、金融、醫(yī)療等)相比,工業(yè)智能化的門檻顯得更高一點,專業(yè)的工業(yè)軟件,相對封閉的開發(fā)理念和更小的開發(fā)圈子,讓剛進入二開的從業(yè)者有點舉步維艱。邊學邊整理,希望通過這系列文章的整理能給二開的生態(tài)增添一葉綠。
一、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;} }構(gòu)造函數(shù)里,我們需要關(guān)注dlx文件加載,回調(diào)方法里面主要關(guān)注:提交方法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; }根據(jù)編輯后的表達式,更新對象
expValue << setprecision(16) << editedvalue;
workPart->Expressions()->EditWithUnits(expToEdit, unit1, expValue.str());
int nErrs1 = EditExpression::theSession->UpdateManager()->DoUpdate(markId2);
3. 運行結(jié)果截圖
總結(jié)
官方實例 EditExpression
1、前端BlockUI,用到了集列表、列表框、選擇特征、雙精度、字符串、按鈕。主要涉及集列表的初始化、賦值和更新,列表框的賦值和取值,特征選擇器的使用。
2、后臺根據(jù)特征選擇的對象,在表達式編輯后,提交更新到對象。
總結(jié)
以上是生活随笔為你收集整理的UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新疆计算机信息管理专升本可以报哪些专业,
- 下一篇: 面兜兜1688上货助手 V1.0.8