commit 0d70f0e5a6a3dabec8280f129224fa9704fef9c7 Author: DWW Date: Tue Aug 10 18:39:15 2021 +0300 Added code diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..44cb1c9 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + g++ -o gcode_slice main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b878bf8 --- /dev/null +++ b/main.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +using namespace std; + +#define DIR_TOP "./slic3r-config" +#define DIR_PRINT "print" +#define DIR_PRINTER "printer" +#define DIR_FILAMENT "filament" + +int check_folder_structure(void); +vector get_folder_entries(const char *folder); +vector get_choices(void); +string pick_option(const string& name, const vector& options); +void flush_input(void); +void usage(char *name); + +int main(int argc, char **argv) +{ + vector configurations; + vector::iterator it; + string command; + + // Check for stl file + if (argc < 2) + { + usage(argv[0]); + return -1; + } + + if (0 != check_folder_structure()) + { + return -1; + } + + // Iterate configurations and build command + configurations = get_choices(); + command = "slic3r --no-gui"; + for (it = configurations.begin(); it != configurations.end(); ++it) + { + command += " --load \""; + command += *it; + command += "\""; + } + + command += " \""; + command += argv[1]; + command += "\""; + + return system(command.c_str()); +} + +int check_folder_structure(void) +{ + string path; + + path = DIR_TOP; + if (0 != access(path.c_str(), F_OK)) + { + printf("'%s' does not exist\n", path.c_str()); + return -1; + } + + path = DIR_TOP; + path += "/"; + path += DIR_PRINT; + if (0 != access(path.c_str(), F_OK)) + { + printf("'%s' does not exist\n", path.c_str()); + return -1; + } + + path = DIR_TOP; + path += "/"; + path += DIR_PRINTER; + if (0 != access(path.c_str(), F_OK)) + { + printf("'%s' does not exist\n", path.c_str()); + return -1; + } + + path = DIR_TOP; + path += "/"; + path += DIR_FILAMENT; + if (0 != access(path.c_str(), F_OK)) + { + printf("'%s' does not exist\n", path.c_str()); + return -1; + } + + return 0; +} + +vector get_folder_entries(const char *folder) +{ + vector result; + DIR *dir; + struct dirent *entry; + string name; + + dir = opendir(folder); + if (NULL != dir) + { + entry = readdir(dir); + while (NULL != entry) + { + if ((DT_REG == entry->d_type) || (DT_LNK == entry->d_type)) + { + name = entry->d_name; + result.push_back(name); + } + entry = readdir(dir); + } + closedir(dir); + } + + sort(result.begin(), result.end()); + return result; +} + +vector get_choices(void) +{ + vector result; + vector options; + string folder; + string dirs[3]; + string pick; + int i; + + dirs[0] = DIR_PRINT; + dirs[1] = DIR_PRINTER; + dirs[2] = DIR_FILAMENT; + + for (i = 0; i < 3; ++i) + { + folder = DIR_TOP; + folder += "/"; + folder += dirs[i]; + options = get_folder_entries(folder.c_str()); + + pick = pick_option(dirs[i], options); + if (pick.empty()) + { + continue; + } + + folder += "/"; + folder += pick; + result.push_back(folder); + } + + return result; +} + +string pick_option(const string& name, const vector& options) +{ + vector::const_iterator it; + int i; + int error; + + printf("Available %s configurations:\n", name.c_str()); + printf("%-2d - Use slic3r defaults\n", 0); + i = 1; + for (it = options.begin(); it != options.end(); ++it) + { + printf("%-2d - %s\n", i, it->c_str()); + ++i; + } + +redo_choice: + printf("Choice: "); + error = scanf("%d", &i); + flush_input(); + if (1 != error) + { + printf("Could not understand choice. Try again.\n"); + goto redo_choice; + } + + if ((i < 0) || (i > options.size())) + { + printf("Number out of bounds. Try again.\n"); + goto redo_choice; + } + + if (0 == i) + { + return ""; + } + + return options[i - 1]; +} + +void flush_input() +{ + char c; + while((c = getchar()) != '\n' && c != EOF); +} + +void usage(char *name) +{ + printf("Usage:\n"); + printf("\t%s [stl file]\n", name); +}