#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; static string tput(const char *s) { string ret = ""; string cmd = string("tput ") + s; FILE *fin = popen(cmd.c_str(),"r"); if (fin != NULL){ int c; while ((c=fgetc(fin))!=EOF){ ret += (char)c; } pclose (fin); } return ret; } struct VIEW{ string name; string description; VIEW(const char *_name, const char *_desc){ name = _name; description = _desc; } }; static vector readmodes() { glocal vector v; string optsdir = string(getenv("HOME")) + "/.Slic3r/modes"; (optsdir.c_str()); if (strcmp(basename,"pla.opts")!=0 && strcmp(basename,"abs.opts")!=0){ const char *pt = strchr(basename,'.'); if (pt != NULL){ glocal.v.push_back(string(basename,pt-basename)); } } sort(glocal.v.begin(),glocal.v.end()); return glocal.v; } enum VIEWMODE{ VIEW_STD,VIEW_TEST,VIEW_PRINT }; static string def = string(getenv("HOME")) + "/.Slic3r/viewsel.def"; struct OPTIONS{ string material; VIEWMODE viewmode; OPTIONS(){ material="pla"; viewmode = VIEW_STD; read (); } void save(); void read(); }; void OPTIONS::read() { glocal OPTIONS *o = this; (def.c_str(),true); if (strncmp(line,"material=",9)==0){ glocal.o->material = line+9; } return 0; } void OPTIONS::save() { glocal OPTIONS *o = this; (def.c_str(),false); fprintf (fout,"material=%s\n",glocal.o->material.c_str()); return 0; } static vector buildviews (const char *opt, const char *fname) { glocal vector v; string cmd = string("buildviews ") + opt + " " + fname; //printf ("buildviews: %s\n",cmd.c_str()); (cmd.c_str(),10,true); //printf ("line=%s\n",line); string word; const char *pt = str_copyword(word,line); glocal.v.push_back(VIEW(word.c_str(),pt)); return 0; return glocal.v; } static void viewsel_readopts(const char *cmd, map &result) { glocal map *result = &result; (cmd,20,true); line = str_skip(line); if (line[0] == '-' && line[1] == '-'){ line += 2; const char *pteq = strchr(line,'='); if (pteq != NULL){ string var = string(line,pteq-line); for (unsigned i=0; i } struct PRINTINFO{ int repeatx,repeaty; int offx,offy; PRINTINFO(){ repeatx = 1; repeaty = 1; offx = 50; offy = 50; } }; static void viewsel_create_header( const string &viewh, // Header to create const char *fname, const string &material, const vector &views, // Name of the views to present or print const PRINTINFO &aprint, bool printing, const vector &slicopts) { glocal const vector *views = &views; glocal const char *fname = fname; glocal const char *material = material.c_str(); glocal bool printing = printing; glocal const PRINTINFO *aprint = &aprint; glocal map result; { char tmp[1000]; snprintf (tmp,sizeof(tmp)-1,"export material=%s; ~/.Slic3r/printer.opts",glocal.material); viewsel_readopts(tmp,glocal.result); snprintf (tmp,sizeof(tmp)-1,"oscad-readopts -c -o viewsopts %s",glocal.fname); viewsel_readopts(tmp,glocal.result); snprintf (tmp,sizeof(tmp)-1,"oscad-readopts -c -o viewopts --optname2 %s %s",views[0].c_str(),glocal.fname); viewsel_readopts(tmp,glocal.result); for (unsigned i=0; i(viewh.c_str(),false); //fprintf (fout,"view=\"%s\";\n",(*glocal.views)[0].c_str()); fprintf (fout,"views=["); for (unsigned i=0; isize(); i++){ if (i > 0) fprintf (fout,","); fprintf (fout,"\"%s\"",(*glocal.views)[i].c_str()); } fprintf (fout,"];\n"); fprintf (fout,"vs_printing=%s;\n",glocal.printing ? "true" : "false"); fprintf (fout,"vs_material=\"%s\";\n",glocal.material); for (map::iterator it = glocal.result.begin(); it != glocal.result.end(); it++){ const char *sep = ""; if (!isdigit(it->second[0])) sep = "\""; fprintf (fout,"vs_%s=%s%s%s;\n",it->first.c_str(),sep,it->second.c_str(),sep); } fprintf (fout,"vs_repeatx=%d;\n",glocal.aprint->repeatx); fprintf (fout,"vs_repeaty=%d;\n",glocal.aprint->repeaty); fprintf (fout,"vs_repeat_offx=%d;\n",glocal.aprint->offx); fprintf (fout,"vs_repeat_offy=%d;\n",glocal.aprint->offy); fprintf (fout,"for(o=[0:len(views)-1]){\n"); fprintf (fout,"\tview=views[o];\n"); fprintf (fout,"\tfor (x=[0:vs_repeatx-1]){\n"); fprintf (fout,"\t\tfor (y=[0:vs_repeaty-1]){\n"); fprintf (fout,"\t\t\ttranslate([x*vs_repeat_offx,(o*vs_repeaty+y)*vs_repeat_offy,0]) main_view(view);\n"); fprintf (fout,"\t\t}\n"); fprintf (fout,"\t}\n"); fprintf (fout,"}\n"); return 0; } static void viewsel_create_header( const string &viewh, // Header to create const char *fname, const string &material, const vector &views, // Name of the views const string &opt, bool printing, const vector &slicopts) { PRINTINFO info; vector tb; int n = str_splitline(opt.c_str(),',',tb); for (int i=0; i views; views.push_back(view); string opt; vector sopts; viewsel_create_header(viewh,fname,material,views,opt,printing,sopts); } static bool viewsel_isnum(const char *pt) { bool ret = false; if (isdigit(*pt)){ while (isdigit(*pt)) pt++; ret = *pt == '\0' || *pt == ' ' || *pt == ',' || *pt == ':'; } return ret; } static int viewsel_parsecmd ( const char *cmd, const vector &menu, // Available views vector &views, string &opt, bool &printview, // Request the same view as if print was requested bool &print, // request an stl file and gcode const char *&pt, int level) // Recursion level { int ret = 0; printview = false; print = false; views.clear(); opt = ""; pt = cmd; if (*pt == '='){ printview = true; pt++; } if (*pt == '!'){ printview = true; print = true; pt++; } while (*pt > ' ' && *pt != ':'){ if (viewsel_isnum(pt)){ unsigned num = atoi(pt); pt = str_skipdig(pt); if (num < 1 || num > menu.size()){ printf ("\nInvalid selection %u\n",num); ret = -1; }else{ string view = menu[num-1].name; if (level == 0){ const char *ppt; bool nprintview; bool nprint; string nopt; vector nviews; if (viewsel_parsecmd(view.c_str(),menu,nviews,nopt,nprintview,nprint,ppt,1)==-1){ printf ("\nInvalid view %s\n",view.c_str()); }else{ for (unsigned i=0; i ' ' && *pt != ',' && *pt != ':'){ view += *pt++; } bool found = false; for (unsigned i=0; i ' ') opt += *pt++; } return ret; } static int viewsel_parsecmd ( const char *cmd, const vector &menu, // Available views vector &views, string &opt, bool &printview, bool &print, const char *&pt) { return viewsel_parsecmd(cmd,menu,views,opt,printview,print,pt,0); } static void viewsel_append (vector &allv, vector &v) { for (unsigned i=0; i int main (int argc, char *argv[]) { glocal const char *parse = NULL; glocal const char *scadfile = NULL; glocal const char *material = "pla"; glocal vector slicopts; glocal int ret = -1; glocal.ret = (argc,argv); setarg ('p',"parseview","Parse a view and generate all the variables",glocal.parse,false); setarg ('s',"scadfile","SCAD file to retrieve view options",glocal.scadfile,false); setarg ('o',"slicopt","Slic3r option files",glocal.slicopts,false); int ret = -1; if (glocal.parse != NULL && glocal.scadfile != NULL){ vector views; string opt; bool printview,print; const char *pt; OPTIONS options; vector v = buildviews("--list",glocal.scadfile); vector tests = buildviews("--listtest",glocal.scadfile); vector prints = buildviews("--listprint",glocal.scadfile); for (unsigned i=0; i int ret = -1; if (argc != 1){ usage(); }else if (!file_exist(argv[0])){ tlmp_error ("File %s does not exist\n",argv[0]); }else{ string bold = tput("bold"); string normal = tput("sgr0"); string clear = tput("clear"); glocal string viewh; glocal OPTIONS options; mkdir ("/tmp/viewsel",0770); { const char *start = strrchr(argv[0],'/'); if (start != NULL){ start++; }else{ start = argv[0]; } string base = start; const char *pt = strstr(start,".scad"); if (pt != NULL){ base = string(start,pt-start); } glocal.viewh = "/tmp/viewsel/" + base + ".h"; } ret = 0; while (1){ glocal vector views = buildviews("--list",argv[0]); glocal vector tests = buildviews("--listtest",argv[0]); glocal vector prints = buildviews("--listprint",argv[0]); glocal vector modes; glocal string view; if (!file_exist(glocal.viewh.c_str())){ glocal.view = glocal.views[0].name; viewsel_create_header (glocal.viewh,argv[0],glocal.options.material,glocal.view,false); }else{ (glocal.viewh.c_str(),true); if (strncmp(line,"views=[\"",8)==0){ const char *start = line +8; const char *end = strchr(start,'"'); if (end != NULL){ glocal.view = string(start,end-start); } } return 0; } glocal.modes = readmodes(); printf ("%s",clear.c_str()); printf ("Material %s%s%s\n",bold.c_str(),glocal.options.material.c_str(),normal.c_str()); printf ("Mode "); { unsigned width=0; for (unsigned i=0; i 70){ printf ("\n "); width = 0; } printf (" %s",mode); width += len; } printf ("\n\n"); } vector &v = glocal.options.viewmode == VIEW_TEST ? glocal.tests : (glocal.options.viewmode == VIEW_PRINT ? glocal.prints : glocal.views); static const char *tbmode[]={"Basic parts","Test views","Extra prints"}; printf ("%s%s%s\n",bold.c_str(),tbmode[glocal.options.viewmode],normal.c_str()); unsigned maxw = 0; for (unsigned i=0; i maxw) maxw = len; } maxw += 5; for (unsigned i=0; i ' '){ bool error = false; string opt; bool printview=false; bool print = false; vector views; const char *pt = NULL; vector allv; viewsel_append (allv,v); viewsel_append (allv,glocal.views); viewsel_append (allv,glocal.tests); viewsel_append (allv,glocal.prints); if (viewsel_parsecmd (sel,allv,views,opt,printview,print,pt)){ error = true; } if (views.size()==0){ printf ("\nNo view selected\n"); error = true; } if (error){ fgets(sel,sizeof(sel),stdin); }else{ glocal const char *newv = views[0].c_str(); auto words = str_splitline (pt,' '); if (!print){ glocal.view = glocal.newv; viewsel_create_header(glocal.viewh,argv[0],glocal.options.material,views,opt,printview,words); }else{ string cmd = "buildviews --slice --materialmode " + glocal.options.material; for (unsigned i=0; i 0) cmd += string(","); cmd += views[i]; } if (opt.size() > 0) cmd += string(":") + opt; printf ("cmd=%s\n",cmd.c_str()); system (cmd.c_str()); printf ("-------Done------\n"); fgets(sel,sizeof(sel),stdin); } } } } } return ret; return glocal.ret; }