tool_parse.cc
1 // Copyright (C) 2009-2010 Jeff Epler <jepler@unpythonic.net> 2 // 3 // This program is free software; you can redistribute it and/or modify 4 // it under the terms of the GNU General Public License as published by 5 // the Free Software Foundation; either version 2 of the License, or 6 // (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU General Public License for more details. 12 // 13 // You should have received a copy of the GNU General Public License 14 // along with this program; if not, write to the Free Software 15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 #include <stdio.h> 17 #include <string.h> 18 #include <ctype.h> 19 #include "emcglb.h" 20 #include "emctool.h" 21 #include "tool_parse.h" 22 23 24 /******************************************************************** 25 * 26 * Description: saveToolTable(const char *filename, CANON_TOOL_TABLE toolTable[]) 27 * Saves the tool table from toolTable[] array into file filename. 28 * Array is CANON_TOOL_MAX + 1 entries, since 0 is included. 29 * 30 * Return Value: Zero on success or -1 if file not found. 31 * 32 * Side Effects: Default setting used if the parameter not found in 33 * the ini file. 34 * 35 * Called By: ioControl 36 * 37 ********************************************************************/ 38 int saveToolTable(const char *filename, 39 CANON_TOOL_TABLE toolTable[], 40 char *ttcomments[CANON_POCKETS_MAX], 41 int random_toolchanger) 42 { 43 int pocket; 44 FILE *fp; 45 const char *name; 46 int start_pocket; 47 48 // check filename 49 if (filename[0] == 0) { 50 name = tool_table_file; 51 } else { 52 // point to name provided 53 name = filename; 54 } 55 56 // open tool table file 57 if (NULL == (fp = fopen(name, "w"))) { 58 // can't open file 59 return -1; 60 } 61 62 if(random_toolchanger) { 63 start_pocket = 0; 64 } else { 65 start_pocket = 1; 66 } 67 for (pocket = start_pocket; pocket < CANON_POCKETS_MAX; pocket++) { 68 if (toolTable[pocket].toolno != -1) { 69 fprintf(fp, "T%d P%d", toolTable[pocket].toolno, random_toolchanger? pocket: toolTable[pocket].pocketno); 70 if (toolTable[pocket].diameter) fprintf(fp, " D%f", toolTable[pocket].diameter); 71 if (toolTable[pocket].offset.tran.x) fprintf(fp, " X%+f", toolTable[pocket].offset.tran.x); 72 if (toolTable[pocket].offset.tran.y) fprintf(fp, " Y%+f", toolTable[pocket].offset.tran.y); 73 if (toolTable[pocket].offset.tran.z) fprintf(fp, " Z%+f", toolTable[pocket].offset.tran.z); 74 if (toolTable[pocket].offset.a) fprintf(fp, " A%+f", toolTable[pocket].offset.a); 75 if (toolTable[pocket].offset.b) fprintf(fp, " B%+f", toolTable[pocket].offset.b); 76 if (toolTable[pocket].offset.c) fprintf(fp, " C%+f", toolTable[pocket].offset.c); 77 if (toolTable[pocket].offset.u) fprintf(fp, " U%+f", toolTable[pocket].offset.u); 78 if (toolTable[pocket].offset.v) fprintf(fp, " V%+f", toolTable[pocket].offset.v); 79 if (toolTable[pocket].offset.w) fprintf(fp, " W%+f", toolTable[pocket].offset.w); 80 if (toolTable[pocket].frontangle) fprintf(fp, " I%+f", toolTable[pocket].frontangle); 81 if (toolTable[pocket].backangle) fprintf(fp, " J%+f", toolTable[pocket].backangle); 82 if (toolTable[pocket].orientation) fprintf(fp, " Q%d", toolTable[pocket].orientation); 83 fprintf(fp, " ;%s\n", ttcomments[pocket]); 84 } 85 } 86 87 fclose(fp); 88 return 0; 89 } 90 91 int loadToolTable(const char *filename, 92 CANON_TOOL_TABLE toolTable[], 93 char *ttcomments[], 94 int random_toolchanger) 95 { 96 int fakepocket = 0; 97 int realpocket = 0; 98 int t; 99 FILE *fp; 100 char buffer[CANON_TOOL_ENTRY_LEN]; 101 char orig_line[CANON_TOOL_ENTRY_LEN]; 102 int pocket = 0; 103 104 if(!filename) return -1; 105 106 // open tool table file 107 if (NULL == (fp = fopen(filename, "r"))) { 108 // can't open file 109 return -1; 110 } 111 // clear out tool table 112 for (t = random_toolchanger? 0: 1; t < CANON_POCKETS_MAX; t++) { 113 toolTable[t].toolno = -1; 114 toolTable[t].pocketno = -1; 115 ZERO_EMC_POSE(toolTable[t].offset); 116 toolTable[t].diameter = 0.0; 117 toolTable[t].frontangle = 0.0; 118 toolTable[t].backangle = 0.0; 119 toolTable[t].orientation = 0; 120 if(ttcomments) ttcomments[t][0] = '\0'; 121 } 122 123 /* 124 Override 0's with codes from tool file 125 File format is: 126 127 <header> 128 <pocket # 0..CANON_TOOL_MAX> <FMS id> <length> <diameter> 129 ... 130 131 */ 132 133 while (!feof(fp)) { 134 const char *token; 135 char *buff, *comment; 136 int toolno, orientation, valid = 1; 137 EmcPose offset; // tlo 138 double diameter, frontangle, backangle; 139 140 // for nonrandom machines, just read the tools into pockets 1..n 141 // no matter their tool numbers. NB leave the spindle pocket 0 142 // unchanged/empty. 143 144 if (NULL == fgets(buffer, CANON_TOOL_ENTRY_LEN, fp)) { 145 break; 146 } 147 strcpy(orig_line, buffer); 148 149 toolno = -1; 150 diameter = frontangle = backangle = 0.0; 151 orientation = 0; 152 ZERO_EMC_POSE(offset); 153 buff = strtok(buffer, ";"); 154 comment = strtok(NULL, "\n"); 155 156 token = strtok(buff, " "); 157 while (token != NULL) { 158 switch (toupper(token[0])) { 159 case 'T': 160 if (sscanf(&token[1], "%d", &toolno) != 1) 161 valid = 0; 162 break; 163 case 'P': 164 if (sscanf(&token[1], "%d", &pocket) != 1) { 165 valid = 0; 166 break; 167 } 168 realpocket = pocket; 169 if (!random_toolchanger) { 170 fakepocket++; 171 if (fakepocket >= CANON_POCKETS_MAX) { 172 printf("too many tools. skipping tool %d\n", toolno); 173 valid = 0; 174 break; 175 } 176 pocket = fakepocket; 177 } 178 if (pocket < 0 || pocket >= CANON_POCKETS_MAX) { 179 printf("max pocket number is %d. skipping tool %d\n", CANON_POCKETS_MAX - 1, toolno); 180 valid = 0; 181 break; 182 } 183 break; 184 case 'D': 185 if (sscanf(&token[1], "%lf", &diameter) != 1) 186 valid = 0; 187 break; 188 case 'X': 189 if (sscanf(&token[1], "%lf", &offset.tran.x) != 1) 190 valid = 0; 191 break; 192 case 'Y': 193 if (sscanf(&token[1], "%lf", &offset.tran.y) != 1) 194 valid = 0; 195 break; 196 case 'Z': 197 if (sscanf(&token[1], "%lf", &offset.tran.z) != 1) 198 valid = 0; 199 break; 200 case 'A': 201 if (sscanf(&token[1], "%lf", &offset.a) != 1) 202 valid = 0; 203 break; 204 case 'B': 205 if (sscanf(&token[1], "%lf", &offset.b) != 1) 206 valid = 0; 207 break; 208 case 'C': 209 if (sscanf(&token[1], "%lf", &offset.c) != 1) 210 valid = 0; 211 break; 212 case 'U': 213 if (sscanf(&token[1], "%lf", &offset.u) != 1) 214 valid = 0; 215 break; 216 case 'V': 217 if (sscanf(&token[1], "%lf", &offset.v) != 1) 218 valid = 0; 219 break; 220 case 'W': 221 if (sscanf(&token[1], "%lf", &offset.w) != 1) 222 valid = 0; 223 break; 224 case 'I': 225 if (sscanf(&token[1], "%lf", &frontangle) != 1) 226 valid = 0; 227 break; 228 case 'J': 229 if (sscanf(&token[1], "%lf", &backangle) != 1) 230 valid = 0; 231 break; 232 case 'Q': 233 if (sscanf(&token[1], "%d", &orientation) != 1) 234 valid = 0; 235 break; 236 default: 237 if (strncmp(token, "\n", 1) != 0) 238 valid = 0; 239 break; 240 } 241 token = strtok(NULL, " "); 242 } 243 if (valid) { 244 toolTable[pocket].toolno = toolno; 245 toolTable[pocket].pocketno = realpocket; 246 toolTable[pocket].offset = offset; 247 toolTable[pocket].diameter = diameter; 248 toolTable[pocket].frontangle = frontangle; 249 toolTable[pocket].backangle = backangle; 250 toolTable[pocket].orientation = orientation; 251 252 if (ttcomments && comment) 253 strcpy(ttcomments[pocket], comment); 254 } else { 255 fprintf(stderr, "Unrecognized line skipped: %s", orig_line); 256 } 257 if (!random_toolchanger && toolTable[0].toolno == toolTable[pocket].toolno) { 258 toolTable[0] = toolTable[pocket]; 259 } 260 } 261 262 // close the file 263 fclose(fp); 264 265 return 0; 266 }