I was wondering if there is a sort of wiki page where we can post helpful code.
For V8i, I was going through the provided examples about levels and fixed some mistakes/made some changes for myself. Anyways, I was wondering if there was a means for sharing this code (except for posting on the forums). Thank you!
The following code iterates through all the levels in the active model reference and writes their name and id to a text file (V8i). I thought that the code snippets could be useful to others (such as myself).
#include <iostream> #include <fstream> #include <sstream> using namespace std; void IterateLevels(); template <typename T> string NumberToString(T Number) { ostringstream ss; ss << Number; return ss.str(); } void appendToFile(int number) { ofstream myfile; myfile.open("List Levels.txt", ios::app); myfile << NumberToString(number); myfile.close(); } void appendToFile(MSWChar s[512]) { wofstream myfile; myfile.open("List Levels.txt", ios::app); myfile << s; myfile.close(); } void appendToFile(string s) { ofstream myfile; myfile.open("List Levels.txt", ios::app); myfile << s; myfile.close(); } extern "C" DLLEXPORT int MdlMain (){ IterateLevels(); mdlDialog_cmdNumberQueue(FALSE, CMD_MDL_UNLOAD, mdlSystem_getCurrTaskID(), TRUE); return SUCCESS; } //Note that LevelID is the wrong type in the MDL Function Reference example int levels_process(LevelID levelID, void* args){ MSWChar levelName[512]; UInt32 id; mdlLevel_getName(levelName, 512, mdlModelRef_getActive(), levelID); appendToFile("The level name is "); appendToFile(levelName); appendToFile(","); if (SUCCESS == mdlLevel_getIdFromName(&id, mdlModelRef_getActive(), LEVEL_NULL_ID, levelName)){ appendToFile(" the id is "); appendToFile(id); appendToFile("\n"); } else { if (SUCCESS == mdlLevel_getIdFromName(&id, mdlLevelLibrary_getModelRef(), LEVEL_NULL_ID, levelName)){ //printf("the id is %ld in library \n", id); appendToFile(" the id is "); appendToFile(id); appendToFile("\n"); } } return SUCCESS; } void IterateLevels(){ LevelIteratorP iteratorP; int status; iteratorP = mdlLevelIterator_create(mdlModelRef_getActive()); status = mdlLevelIterator_setIterateType(iteratorP, LEVEL_ITERATE_TYPE_ALL_LEVELS); status = mdlLevelIterator_setIterateLibraryLevels(iteratorP, TRUE, LEVEL_ITERATE_TYPE_ALL_LEVELS); //if you want to report the levels in the ref files you need to combine the model iterator into this. mdlLevelIterator_traverse(iteratorP, levels_process, NULL); return; }