NX二次开发-自定义添加右键菜单RegisterConfigureContextMenuCallback

时间:2021-11-24 17:08:12

首先声明这个知识我以前不知道,是夏天的时候看到别人在唐工的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命名空间里,用的下面几个类里的方法

NX二次开发-自定义添加右键菜单RegisterConfigureContextMenuCallback

 //NX11_NXOpenCPP_Wizard1

 // Mandatory UF Includes
#include <uf.h>
#include <uf_object_types.h> // Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx> // Internal+External Includes
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Session.hxx> //头文件
#include <NXOpen/Session.hxx>
#include <NXOpen/MenuBar_ContextMenu.hxx>
#include <NXOpen/MenuBar_ContextMenuEntry.hxx>
#include <NXOpen/MenuBar_ContextMenuProperties.hxx>
#include <NXOpen/MenuBar_MenuBarManager.hxx>
#include <NXOpen/MenuBar_MenuButton.hxx>
#include <NXOpen/MenuBar_MenuButtonEvent.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx> //命名空间
using namespace NXOpen;
using namespace MenuBar; // Std C++ Includes
#include <iostream>
#include <sstream> using std::string;
using std::exception;
using std::stringstream;
using std::endl;
using std::cout;
using std::cerr; //------------------------------------------------------------------------------
// NXOpen c++ test class
//------------------------------------------------------------------------------
class MyClass
{
// class members
public:
static Session *theSession;
static UI *theUI; MyClass();
~MyClass(); void do_it();
void print(const NXString &);
void print(const string &);
void print(const char*); //声明函数
int CustomizeMenu(MenuBar::ContextMenu* menu, MenuBar::ContextMenuProperties* props); private:
Part *workPart, *displayPart;
NXMessageBox *mb;
ListingWindow *lw;
LogFile *lf;
}; //------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(MyClass::theSession) = NULL;
UI *(MyClass::theUI) = NULL; static MyClass* theConfigureContextMenu = NULL;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
MyClass::MyClass()
{ // Initialize the NX Open C++ API environment
MyClass::theSession = NXOpen::Session::GetSession();
MyClass::theUI = UI::GetUI();
mb = theUI->NXMessageBox();
lw = theSession->ListingWindow();
lf = theSession->LogFile(); workPart = theSession->Parts()->Work();
displayPart = theSession->Parts()->Display(); //注册一个回调,每当要显示可自定义的右键菜单时调用该回调
MyClass::theUI->MenuBarManager()->RegisterConfigureContextMenuCallback("ConfigureContextMenu.cpp",
"An example of context menu customization demonstrating various functions.",
make_callback(this, &MyClass::CustomizeMenu));
} //------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
MyClass::~MyClass()
{
} //------------------------------------------------------------------------------
// Print string to listing window or stdout
//------------------------------------------------------------------------------
void MyClass::print(const NXString &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const string &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const char * msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
} int MyClass::CustomizeMenu(MenuBar::ContextMenu* menu, MenuBar::ContextMenuProperties* props)
{
try
{
//查找与给定名称关联的MenuButton,此名称必须与.men文件中使用的按钮名称匹配
MenuBar::MenuButton* newButton1 = theUI->MenuBarManager()->GetButtonFromName("NuoPuWrite_Dim_NameTools"); //查找与给定名称关联的MenuButton,此名称必须与.men文件中使用的按钮名称匹配
MenuBar::MenuButton* newButton2 = theUI->MenuBarManager()->GetButtonFromName("OpenTxtWriteExp"); //查找与给定名称关联的MenuButton,此名称必须与.men文件中使用的按钮名称匹配
MenuBar::MenuButton* newButton3 = theUI->MenuBarManager()->GetButtonFromName("RenameComponent"); //将菜单栏按钮添加到右键菜单
menu->AddMenuButton(newButton1, );//创建新按钮的位置,0是第一个,使用-1将按钮添加到菜单的末尾 //在右键菜单中添加一个分隔符
menu->AddSeparator(); //在右键菜单中添加一个子菜单
MenuBar::ContextMenu* subMenu = menu->AddSubmenu("用户自定义子菜单", );
subMenu->AddMenuButton(newButton2, );
subMenu->AddMenuButton(newButton3, ); //将标签添加到右键菜单
menu->AddMenuLabel("这是标签", ); //指示右键菜单是否包含具有给定的名称
if (menu->HasEntryWithName("UG_EDIT_DELETE"))
{
MenuBar::ContextMenuEntry* deleteMenuEntry = menu->GetEntryWithName("UG_EDIT_DELETE"); //防止所指示的菜单项显示在右键菜单上
menu->HideEntry(deleteMenuEntry);
} //查找菜单上最后一个可见的按钮项
MenuBar::ContextMenuEntry* entry = NULL;
//返回右键菜单中按钮的数量
int numMenuEntries = menu->NumberOfEntries();
for (int i = ; i < numMenuEntries; i++)
{
//返回右键菜单中指定索引处
MenuBar::ContextMenuEntry* entry2 = menu->GetEntry(i); //EntryType返回此菜单项的类型
//IsHidden如果此项在右键菜单上隐藏,则返回true
//IsSensitive如果可以运行与此条目对应的命令,则返回true
if (entry2->EntryType() == MenuBar::ContextMenuEntry::TypePushButton &&
!entry2->IsHidden() && entry2->IsSensitive())
{
entry = entry2;
}
} //对已有的右键菜单重新排序
//将标识项设置为默认项并移动到菜单的顶部
if (entry != NULL)
{
//使指定的菜单项成为右键菜单的默认项
menu->SetDefaultEntry(entry); //重新排序菜单,以将菜单项移动到列表中的新位置
menu->MoveEntry(entry, );
} }
catch (const NXOpen::NXException& ex)
{
std::cerr << "Caught exception" << ex.Message() << std::endl;
} return ; } //------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{ // TODO: add your code here } //------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
// NX Startup
//ufsta在NX启动时调用,向NX注册回调
extern "C" DllExport void ufsta( char *param, int *returnCode, int rlen )
{
try
{
// Create NXOpen C++ class instance
MyClass *theMyClass;
theMyClass = new MyClass();
theMyClass->do_it();
delete theMyClass;
}
catch (const NXException& e1)
{
UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
}
catch (const exception& e2)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
}
catch (...)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
}
} //------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
return (int)NXOpen::Session::LibraryUnloadOptionAtTermination;//卸载方式一定要用这个
} Caesar卢尚宇
2019年11月24日

NX二次开发-自定义添加右键菜单RegisterConfigureContextMenuCallback