NX二次開發-自定義添加右鍵菜單RegisterConfigureContextMenuCallback


首先聲明這個知識我以前不知道,是夏天的時候看到別人在唐工的QQ群里問的,唐工說西門子官方有這個例子。那個時候我因為在忙其他事情,也就沒去研究那個右鍵菜單到底是怎么做的。關於自定義添加右鍵菜單RegisterConfigureContextMenuCallback相關內容,可以去UGOPEN里研究ConfigureContextMenu這個例子。

結合NXOPEN幫助對照函數一步步去理解,是怎么使用的。

在線幫助地址https://docs.plm.automation.siemens.com/data_services/resources/nx/11/nx_api/custom/en_US/open_c++_ref/a12985.html

都在MenuBar命名空間里,用的下面幾個類里的方法

  1 //NX11_NXOpenCPP_Wizard1
  2 
  3 // Mandatory UF Includes
  4 #include <uf.h>
  5 #include <uf_object_types.h>
  6 
  7 // Internal Includes
  8 #include <NXOpen/ListingWindow.hxx>
  9 #include <NXOpen/NXMessageBox.hxx>
 10 #include <NXOpen/UI.hxx>
 11 
 12 // Internal+External Includes
 13 #include <NXOpen/Annotations.hxx>
 14 #include <NXOpen/Assemblies_Component.hxx>
 15 #include <NXOpen/Assemblies_ComponentAssembly.hxx>
 16 #include <NXOpen/Body.hxx>
 17 #include <NXOpen/BodyCollection.hxx>
 18 #include <NXOpen/Face.hxx>
 19 #include <NXOpen/Line.hxx>
 20 #include <NXOpen/NXException.hxx>
 21 #include <NXOpen/NXObject.hxx>
 22 #include <NXOpen/Part.hxx>
 23 #include <NXOpen/PartCollection.hxx>
 24 #include <NXOpen/Session.hxx>
 25 
 26 //頭文件
 27 #include <NXOpen/Session.hxx>
 28 #include <NXOpen/MenuBar_ContextMenu.hxx>
 29 #include <NXOpen/MenuBar_ContextMenuEntry.hxx>
 30 #include <NXOpen/MenuBar_ContextMenuProperties.hxx>
 31 #include <NXOpen/MenuBar_MenuBarManager.hxx>
 32 #include <NXOpen/MenuBar_MenuButton.hxx>
 33 #include <NXOpen/MenuBar_MenuButtonEvent.hxx>
 34 #include <NXOpen/UI.hxx>
 35 #include <NXOpen/Callback.hxx>
 36 #include <NXOpen/NXException.hxx>
 37 
 38 //命名空間
 39 using namespace NXOpen;
 40 using namespace MenuBar;
 41 
 42 // Std C++ Includes
 43 #include <iostream>
 44 #include <sstream>
 45 
 46 using std::string;
 47 using std::exception;
 48 using std::stringstream;
 49 using std::endl;
 50 using std::cout;
 51 using std::cerr;
 52 
 53 
 54 //------------------------------------------------------------------------------
 55 // NXOpen c++ test class 
 56 //------------------------------------------------------------------------------
 57 class MyClass
 58 {
 59     // class members
 60 public:
 61     static Session *theSession;
 62     static UI *theUI;
 63 
 64     MyClass();
 65     ~MyClass();
 66 
 67     void do_it();
 68     void print(const NXString &);
 69     void print(const string &);
 70     void print(const char*);
 71 
 72     //聲明函數
 73     int CustomizeMenu(MenuBar::ContextMenu* menu, MenuBar::ContextMenuProperties* props);
 74 
 75 private:
 76     Part *workPart, *displayPart;
 77     NXMessageBox *mb;
 78     ListingWindow *lw;
 79     LogFile *lf;
 80 };
 81 
 82 //------------------------------------------------------------------------------
 83 // Initialize static variables
 84 //------------------------------------------------------------------------------
 85 Session *(MyClass::theSession) = NULL;
 86 UI *(MyClass::theUI) = NULL;
 87 
 88 static MyClass* theConfigureContextMenu = NULL;
 89 //------------------------------------------------------------------------------
 90 // Constructor 
 91 //------------------------------------------------------------------------------
 92 MyClass::MyClass()
 93 {
 94 
 95     // Initialize the NX Open C++ API environment
 96     MyClass::theSession = NXOpen::Session::GetSession();
 97     MyClass::theUI = UI::GetUI();
 98     mb = theUI->NXMessageBox();
 99     lw = theSession->ListingWindow();
100     lf = theSession->LogFile();
101 
102     workPart = theSession->Parts()->Work();
103     displayPart = theSession->Parts()->Display();
104     
105     //注冊一個回調,每當要顯示可自定義的右鍵菜單時調用該回調
106     MyClass::theUI->MenuBarManager()->RegisterConfigureContextMenuCallback("ConfigureContextMenu.cpp",
107         "An example of context menu customization demonstrating various functions.",
108         make_callback(this, &MyClass::CustomizeMenu));
109 }
110 
111 //------------------------------------------------------------------------------
112 // Destructor
113 //------------------------------------------------------------------------------
114 MyClass::~MyClass()
115 {
116 }
117 
118 //------------------------------------------------------------------------------
119 // Print string to listing window or stdout
120 //------------------------------------------------------------------------------
121 void MyClass::print(const NXString &msg)
122 {
123     if(! lw->IsOpen() ) lw->Open();
124     lw->WriteLine(msg);
125 }
126 void MyClass::print(const string &msg)
127 {
128     if(! lw->IsOpen() ) lw->Open();
129     lw->WriteLine(msg);
130 }
131 void MyClass::print(const char * msg)
132 {
133     if(! lw->IsOpen() ) lw->Open();
134     lw->WriteLine(msg);
135 }
136 
137 
138 int MyClass::CustomizeMenu(MenuBar::ContextMenu* menu, MenuBar::ContextMenuProperties* props)
139 {
140     try
141     {
142         //查找與給定名稱關聯的MenuButton,此名稱必須與.men文件中使用的按鈕名稱匹配
143         MenuBar::MenuButton* newButton1 = theUI->MenuBarManager()->GetButtonFromName("NuoPuWrite_Dim_NameTools");
144 
145         //查找與給定名稱關聯的MenuButton,此名稱必須與.men文件中使用的按鈕名稱匹配
146         MenuBar::MenuButton* newButton2 = theUI->MenuBarManager()->GetButtonFromName("OpenTxtWriteExp");
147 
148         //查找與給定名稱關聯的MenuButton,此名稱必須與.men文件中使用的按鈕名稱匹配
149         MenuBar::MenuButton* newButton3 = theUI->MenuBarManager()->GetButtonFromName("RenameComponent");
150 
151         //將菜單欄按鈕添加到右鍵菜單
152         menu->AddMenuButton(newButton1, 0);//創建新按鈕的位置,0是第一個,使用-1將按鈕添加到菜單的末尾
153 
154         //在右鍵菜單中添加一個分隔符
155         menu->AddSeparator(1);
156 
157         //在右鍵菜單中添加一個子菜單
158         MenuBar::ContextMenu* subMenu = menu->AddSubmenu("用戶自定義子菜單", 2);
159         subMenu->AddMenuButton(newButton2, 0);
160         subMenu->AddMenuButton(newButton3, 1);
161 
162         //將標簽添加到右鍵菜單
163         menu->AddMenuLabel("這是標簽", 3);
164 
165         //指示右鍵菜單是否包含具有給定的名稱
166         if (menu->HasEntryWithName("UG_EDIT_DELETE"))
167         {
168             MenuBar::ContextMenuEntry* deleteMenuEntry = menu->GetEntryWithName("UG_EDIT_DELETE");
169 
170             //防止所指示的菜單項顯示在右鍵菜單上
171             menu->HideEntry(deleteMenuEntry);
172         }
173 
174         //查找菜單上最后一個可見的按鈕項
175         MenuBar::ContextMenuEntry* entry = NULL;
176         //返回右鍵菜單中按鈕的數量
177         int numMenuEntries = menu->NumberOfEntries();
178         for (int i = 0; i < numMenuEntries; i++)
179         {
180             //返回右鍵菜單中指定索引處
181             MenuBar::ContextMenuEntry* entry2 = menu->GetEntry(i);
182 
183             //EntryType返回此菜單項的類型
184             //IsHidden如果此項在右鍵菜單上隱藏,則返回true
185             //IsSensitive如果可以運行與此條目對應的命令,則返回true
186             if (entry2->EntryType() == MenuBar::ContextMenuEntry::TypePushButton &&
187                 !entry2->IsHidden() && entry2->IsSensitive())
188             {
189                 entry = entry2;
190             }
191         }
192 
193         //對已有的右鍵菜單重新排序
194         //將標識項設置為默認項並移動到菜單的頂部
195         if (entry != NULL)
196         {
197             //使指定的菜單項成為右鍵菜單的默認項
198             menu->SetDefaultEntry(entry);
199 
200             //重新排序菜單,以將菜單項移動到列表中的新位置
201             menu->MoveEntry(entry, 4);
202         }
203 
204 
205     }
206     catch (const NXOpen::NXException& ex)
207     {
208         std::cerr << "Caught exception" << ex.Message() << std::endl;
209     }
210 
211     return 0;
212 
213 }
214 
215 
216 //------------------------------------------------------------------------------
217 // Do something
218 //------------------------------------------------------------------------------
219 void MyClass::do_it()
220 {
221 
222     // TODO: add your code here
223     
224 }
225 
226 //------------------------------------------------------------------------------
227 // Entry point(s) for unmanaged internal NXOpen C/C++ programs
228 //------------------------------------------------------------------------------
229 //  NX Startup
230 //ufsta在NX啟動時調用,向NX注冊回調
231 extern "C" DllExport void ufsta( char *param, int *returnCode, int rlen )
232 {
233     try
234     {
235         // Create NXOpen C++ class instance
236         MyClass *theMyClass;
237         theMyClass = new MyClass();
238         theMyClass->do_it();
239         delete theMyClass;
240     }
241     catch (const NXException& e1)
242     {
243         UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
244     }
245     catch (const exception& e2)
246     {
247         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
248     }
249     catch (...)
250     {
251         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
252     }
253 }
254 
255 
256 //------------------------------------------------------------------------------
257 // Unload Handler
258 //------------------------------------------------------------------------------
259 extern "C" DllExport int ufusr_ask_unload()
260 {
261     return (int)NXOpen::Session::LibraryUnloadOptionAtTermination;//卸載方式一定要用這個
262 }
263 
264 
265 Caesar盧尚宇
266 2019年11月24日

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM