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日

原文地址:https://www.cnblogs.com/nxopen2018/p/11923139.html