- 文章讨论的主题
前两天有朋友问我,怎么将控件选择的曲线-面-体等tag 进行类型转换,给入到手动录制的 修改颜色代码中去。
首先对NXOpen::DisplayModification这个类做个简单的介绍:表示对 对象的显示特征的更改 , 比如可以 改对象的颜色 。
- 中间的步骤过程
手动录制设置曲线颜色代码
录制出来的代码如下
// ---------------------------------------------- // Menu: 编辑(E)->对象显示(J)... // ---------------------------------------------- // ---------------------------------------------- // Dialog Begin 颜色 // ---------------------------------------------- NXOpen::Session::UndoMarkId markId1; markId1 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityVisible, "Edit Object Display"); NXOpen::DisplayModification *displayModification1; displayModification1 = theSession->DisplayManager()->NewDisplayModification(); displayModification1->SetApplyToAllFaces(true); displayModification1->SetApplyToOwningParts(false); displayModification1->SetNewColor(186); displayModification1->SetNewWidth(NXOpen::DisplayableObject::ObjectWidthTwo); std::vector<NXOpen::DisplayableObject *> objects1(1); NXOpen::Features::AssociativeLine *associativeLine1(dynamic_cast<NXOpen::Features::AssociativeLine *>(workPart->Features()->FindObject("LINE(1)"))); NXOpen::Line *line1(dynamic_cast<NXOpen::Line *>(associativeLine1->FindObject("CURVE 1"))); objects1[0] = line1; displayModification1->Apply(objects1); int nErrs1; nErrs1 = theSession->UpdateManager()->DoUpdate(markId1); delete displayModification1; // ---------------------------------------------- // Menu: 工具(T)->操作记录(J)->停止录制(S) // ----------------------------------------------
- 粗略的代码解析
我们看到,对哪些对象设置颜色是这一句 displayModification1->Apply, 这里面给入了一个vector数组
displayModification1->Apply(objects1);
而这个数组里面,添加进去了一个 直线类型的对象,Line
NXOpen::Line *line1(dynamic_cast<NXOpen::Line *>(associativeLine1->FindObject("CURVE 1"))); objects1[0] = line1;
- 遇到的问题
那么问题来了,如果 我想设置面或者体颜色怎么办?应该怎么加?
接着去录制,研究录制出来的代码。
面
NXOpen::Face *face1(dynamic_cast<NXOpen::Face *>(block1->FindObject("FACE 2 {(50,0,50) BLOCK(2)}"))); objects1[0] = face1;
体
NXOpen::Body *body1(dynamic_cast<NXOpen::Body *>(workPart->Bodies()->FindObject("BLOCK(2)"))); objects1[0] = body1;
我们会发现录制 出来给我们的代码,类型分的很细。直线就是Line,样条就是spline,面就是Face,体就是Body 等等。
现在的问题是,我选择控件 随便选择了一个 对象,可是我也不知道 它是 曲线,还是 面 还是 体。
可能有人会想到,用函数UF_OBJ_ask_type_and_subtype去获取下tag是什么类型,然后写一堆if else
判断。来使用 往数组里添加对象,但是这样的缺点就是要写一堆代码,而且每种类型都要写进去,太麻烦。不太可取
if 直线 NXOpen::Line *line1(dynamic_cast<NXOpen::Line *>(associativeLine1->FindObject("CURVE 1"))); if 面 NXOpen::Face *face1(dynamic_cast<NXOpen::Face *>(block1->FindObject("FACE 2 {(50,0,50) BLOCK(2)}"))); if 体 NXOpen::Body *body1(dynamic_cast<NXOpen::Body *>(workPart->Bodies()->FindObject("BLOCK(2)")));
- 解决的办法
我们期望的是一行代码就可以转换解决。
不知道怎么弄,那就去查帮助
找到-类 里的这个-方法
我们点击 < NXOpen::DisplayableObject * > 点击去查看,它支持哪些类型。我们看到 它下面的子类 有Body,Face 等等 有很多。
然后我们在去Line, Face,Body这样类里,在去单独查看一下它们的父类有些。
我们发现 它们的父类里都有 NXOpen::DisplayableObject ,那就是说 我们在强制转换的时候,不用每一个Line, Face, Body这样很详细的去转换了,直接去使用DisplayableObject这个父类去转换就可以了,通杀下面的所有不同子类型。
强制转换方式如下,一句话搞定,tObject 为 曲线 面 体 类型的 tag
objects1[0] = (dynamic_cast<NXOpen::DisplayableObject *>(NXOpen::NXObjectManager::Get(tObject)));
- 例子代码
//TestDisplayableObjectDemo // Mandatory UF Includes #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 <uf.h> #include <uf_ui.h> #include <NXOpen/DisplayManager.hxx> #include <NXOpen/NXObjectManager.hxx> // Std C++ Includes #include <iostream> #include <sstream> using namespace NXOpen; 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*); private: Part *workPart, *displayPart; NXMessageBox *mb; ListingWindow *lw; LogFile *lf; }; //------------------------------------------------------------------------------ // Initialize static variables //------------------------------------------------------------------------------ Session *(MyClass::theSession) = NULL; UI *(MyClass::theUI) = 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(); } //------------------------------------------------------------------------------ // 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); } static int select_filter_proc_fn(tag_t object, int type[3], void* user_data, UF_UI_selection_p_t select) { if (object == NULL) { return UF_UI_SEL_REJECT; } else { return UF_UI_SEL_ACCEPT; } } static int init_proc(UF_UI_selection_p_t select, void* user_data) { int num_triples = 3;//可选类型的数量 UF_UI_mask_t mask_triples[] = { UF_solid_type, UF_solid_face_subtype, UF_UI_SEL_FEATURE_CYLINDRICAL_FACE, UF_line_type, UF_line_normal_subtype, UF_UI_SEL_NOT_A_FEATURE, UF_solid_type, UF_solid_body_subtype, UF_UI_SEL_FEATURE_BODY };//可选对象类型 UF_UI_set_sel_mask(select, UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, num_triples, mask_triples); if ((UF_UI_set_sel_procs(select, select_filter_proc_fn, NULL, user_data)) == 0) { return UF_UI_SEL_SUCCESS; } else { return UF_UI_SEL_FAILURE; } } //------------------------------------------------------------------------------ // Do something //------------------------------------------------------------------------------ void MyClass::do_it() { // TODO: add your code here UF_initialize(); //单对象选择对话框 char sCue[] = "单对象选择对话框"; char sTitle[] = "单对象选择对话框"; int iScope = UF_UI_SEL_SCOPE_NO_CHANGE; int iResponse; tag_t tObject; tag_t tView; double adCursor[3]; UF_UI_select_with_single_dialog(sCue, sTitle, iScope, init_proc, NULL, &iResponse, &tObject, adCursor, &tView); NXOpen::DisplayModification *displayModification1; displayModification1 = theSession->DisplayManager()->NewDisplayModification(); displayModification1->SetApplyToAllFaces(true); displayModification1->SetApplyToOwningParts(false); displayModification1->SetNewColor(186); std::vector<NXOpen::DisplayableObject *> objects1(1); objects1[0] = (dynamic_cast<NXOpen::DisplayableObject *>(NXOpen::NXObjectManager::Get(tObject))); displayModification1->Apply(objects1); delete displayModification1; UF_terminate(); } //------------------------------------------------------------------------------ // Entry point(s) for unmanaged internal NXOpen C/C++ programs //------------------------------------------------------------------------------ // Explicit Execution extern "C" DllExport void ufusr( char *parm, 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::LibraryUnloadOptionImmediately; } 阿飞 2022年4月26日
- 演示
阿飞
2022年4月16日