NX二次開發-C++經常用的功能遍歷資料夾裡的檔案,白嫖黨專屬,點贊一波666
阿新 • • 發佈:2021-10-11
下面的這兩種遍歷檔案,是個常用的程式碼,在這裡存一份,方便查詢
版本
NX11+VS2013
1.遍歷一個資料夾裡所有符合字尾條件的檔案
//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 <io.h> #include <uf.h> #include <uf_ui.h> // Std C++ Includes #include <iostream> #include<sstream> //名稱空間 using namespace std; 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); } void filesearch(string path, int layer) { char msg[256] = ""; char msgg[256] = ""; struct _finddata_t filefind; string f; string curr = path + "\\*.*"; intptr_t done = 0, i, handle; if ((handle = _findfirst(curr.c_str(), &filefind)) != -1) { while (!(done = _findnext(handle, &filefind))) { if (strcmp(filefind.name, "..") == 0) continue; for (i = 0; i < layer; i++) printf("\t"); if ((_A_SUBDIR == filefind.attrib)) // 是目錄 { curr = path + "\\" + filefind.name; filesearch(curr, layer + 1); // 遞迴遍歷子目錄 } else // 是檔案 { sprintf_s(msg, "%s", filefind.name); if (strlen(msg) > 4) { strncpy_s(msgg, msg + strlen(msg) - 4, 4); if (strcmp(msgg, ".txt") == 0) // 檔案格式 { f = path + "\\" + filefind.name; //列印資料夾裡的所有txt檔案 UF_UI_write_listing_window(f.c_str()); UF_UI_write_listing_window("\n"); } } } } _findclose(handle); } } //------------------------------------------------------------------------------ // Do something //------------------------------------------------------------------------------ void MyClass::do_it() { // TODO: add your code here UF_initialize(); //開啟資訊視窗 UF_UI_open_listing_window(); //遍歷資料夾 filesearch("D:\\1", 0); 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; } 阿飛 2021年10月10日
2.遍歷一個資料夾裡指定的資料夾裡符合條件的檔案
意思就是這上面有一堆資料夾,我現在只想通過傳入D:\\1資料夾路徑,遍歷這一堆裡面bin資料夾裡的exe,
因為C#專案的exe是在bin資料夾裡,MFC的專案exe是沒有bin目錄的
//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 <io.h> #include <uf.h> #include <uf_ui.h> // Std C++ Includes #include <iostream> #include <sstream> //名稱空間 using namespace std; 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); } void filesearch(string path, int layer) { char msg[256] = ""; char msgg[256] = ""; struct _finddata_t filefind; string f; string curr = path + "\\*.*"; intptr_t done = 0, i, handle; if ((handle = _findfirst(curr.c_str(), &filefind)) != -1) { while (!(done = _findnext(handle, &filefind))) { if (strcmp(filefind.name, "..") == 0) continue; for (i = 0; i < layer; i++) printf("\t"); if ((_A_SUBDIR == filefind.attrib)) // 是目錄 { curr = path + "\\" + filefind.name; sprintf_s(msg, "%s\n", curr.c_str()); //UF_UI_write_listing_window(msg); filesearch(curr, layer + 1); // 遞迴遍歷子目錄 } else // 是檔案 { if (strstr(path.c_str(), "\\bin") == NULL) { continue; } sprintf_s(msg, "%s", filefind.name); if (strlen(msg) > 4) { strncpy(msgg, msg + strlen(msg) - 4, 4); if (strcmp(msgg, ".exe") == 0) // 檔案格式 { f = path + "\\" + filefind.name; //列印資料夾裡的所有txt檔案 UF_UI_write_listing_window(f.c_str()); UF_UI_write_listing_window("\n"); } } } } _findclose(handle); } } //------------------------------------------------------------------------------ // Do something //------------------------------------------------------------------------------ void MyClass::do_it() { // TODO: add your code here UF_initialize(); //開啟資訊視窗 UF_UI_open_listing_window(); //遍歷資料夾 filesearch("D:\\1", 0); 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; } 阿飛 2021年10月10日
阿飛
2021年10月10日