GCC Code Coverage Report
Directory: emc/rs274ngc/ Exec Total Coverage
File: emc/rs274ngc/tool_parse.cc Lines: 79 120 65.8 %
Date: 2016-10-27 Branches: 60 114 52.6 %

Line Exec Source
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
267
static bool scan_old_style(
24
	char *buffer,
25
	CANON_TOOL_TABLE toolTable[],
26
	int fms[],
27
	char *ttcomments[],
28
	int random_toolchanger,
29
	int &fakepocket) {
30
    int scanned, toolno, pocket, orientation;
31
    double zoffset, xoffset, diameter, frontangle, backangle;
32
    char comment[CANON_TOOL_ENTRY_LEN];
33
34
267
    if((scanned = sscanf(buffer, "%d %d %lf %lf %lf %lf %lf %d %[^\n]",
35
			 &toolno, &pocket, &zoffset, &xoffset, &diameter,
36
279
			 &frontangle, &backangle, &orientation, comment)) &&
37
12
       (scanned == 8 || scanned == 9)) {
38
	if(!random_toolchanger) {
39
	    fakepocket++;
40
	    if(fakepocket >= CANON_POCKETS_MAX) {
41
		printf("too many tools. skipping tool %d\n", toolno);
42
		return true;
43
	    }
44
	    if(fms) fms[fakepocket] = pocket;
45
	    pocket = fakepocket;
46
	}
47
	if (pocket < 0 || pocket >= CANON_POCKETS_MAX) {
48
	    printf("max pocket number is %d. skipping tool %d\n", CANON_POCKETS_MAX-1, toolno);
49
	    return true;
50
	} else {
51
	    /* lathe tool */
52
	    toolTable[pocket].toolno = toolno;
53
	    toolTable[pocket].offset.tran.z = zoffset;
54
	    toolTable[pocket].offset.tran.x = xoffset;
55
	    toolTable[pocket].diameter = diameter;
56
57
	    toolTable[pocket].frontangle = frontangle;
58
	    toolTable[pocket].backangle = backangle;
59
	    toolTable[pocket].orientation = orientation;
60
	    if(ttcomments && scanned == 9) strcpy(ttcomments[pocket], comment);
61
	    return true;
62
	}
63
267
    } else if ((scanned = sscanf(buffer, "%d %d %lf %lf %[^\n]",
64
279
				 &toolno, &pocket, &zoffset, &diameter, comment)) &&
65
12
	       (scanned == 4 || scanned == 5)) {
66
10
	if(!random_toolchanger) {
67
10
	    fakepocket++;
68
10
	    if(fakepocket >= CANON_POCKETS_MAX) {
69
		printf("too many tools. skipping tool %d\n", toolno);
70
		return true;
71
	    }
72
10
	    if(fms) fms[fakepocket] = pocket;
73
10
	    pocket = fakepocket;
74
	}
75
10
	if (pocket < 0 || pocket >= CANON_POCKETS_MAX) {
76
	    printf("max pocket number is %d. skipping tool %d\n", CANON_POCKETS_MAX-1, toolno);
77
	    return true;
78
	} else {
79
	    /* mill tool */
80
10
	    toolTable[pocket].toolno = toolno;
81
10
	    toolTable[pocket].offset.tran.z = zoffset;
82
10
	    toolTable[pocket].diameter = diameter;
83
84
	    // these aren't used on a mill
85
10
	    toolTable[pocket].frontangle = toolTable[pocket].backangle = 0.0;
86
10
	    toolTable[pocket].offset.tran.x = 0.0;
87
10
	    toolTable[pocket].orientation = 0;
88
10
	    if(ttcomments && scanned == 5) strcpy(ttcomments[pocket], comment);
89
	    return true;
90
	}
91
    }
92
    return false;
93
}
94
95
68
int loadToolTable(const char *filename,
96
			 CANON_TOOL_TABLE toolTable[],
97
			 int fms[],
98
			 char *ttcomments[],
99
			 int random_toolchanger)
100
{
101
68
    int fakepocket = 0;
102
    int t;
103
    FILE *fp;
104
    char buffer[CANON_TOOL_ENTRY_LEN];
105
    char orig_line[CANON_TOOL_ENTRY_LEN];
106
68
    int pocket = 0;
107
108
68
    if(!filename) return -1;
109
110
    // open tool table file
111
68
    if (NULL == (fp = fopen(filename, "r"))) {
112
	// can't open file
113
	return -1;
114
    }
115
    // clear out tool table
116
3808
    for (t = random_toolchanger? 0: 1; t < CANON_POCKETS_MAX; t++) {
117
3740
        toolTable[t].toolno = -1;
118
3740
        ZERO_EMC_POSE(toolTable[t].offset);
119
3740
        toolTable[t].diameter = 0.0;
120
3740
        toolTable[t].frontangle = 0.0;
121
3740
        toolTable[t].backangle = 0.0;
122
3740
        toolTable[t].orientation = 0;
123
3740
        if(fms) fms[t] = 0;
124
3740
        if(ttcomments) ttcomments[t][0] = '\0';
125
    }
126
127
    /*
128
      Override 0's with codes from tool file
129
      File format is:
130
131
      <header>
132
      <pocket # 0..CANON_TOOL_MAX> <FMS id> <length> <diameter>
133
      ...
134
135
    */
136
137
335
    while (!feof(fp)) {
138
        const char *token;
139
        char *buff, *comment;
140
335
        int toolno, orientation, valid = 1;
141
        EmcPose offset;  // tlo
142
        double diameter, frontangle, backangle;
143
144
        // for nonrandom machines, just read the tools into pockets 1..n
145
        // no matter their tool numbers.  NB leave the spindle pocket 0
146
        // unchanged/empty.
147
148
335
        if (NULL == fgets(buffer, CANON_TOOL_ENTRY_LEN, fp)) {
149
            break;
150
        }
151
267
        strcpy(orig_line, buffer);
152
153
267
        if(scan_old_style(buffer, toolTable, fms, ttcomments,
154
10
                                random_toolchanger, fakepocket)) continue;
155
156
257
        toolno = -1;
157
257
        diameter = frontangle = backangle = 0.0;
158
257
        orientation = 0;
159
257
        ZERO_EMC_POSE(offset);
160
257
        buff = strtok(buffer, ";");
161
257
        comment = strtok(NULL, "\n");
162
163
257
        token = strtok(buff, " ");
164
1478
        while (token != NULL) {
165
964
            switch (toupper(token[0])) {
166
            case 'T':
167
253
                if (sscanf(&token[1], "%d", &toolno) != 1)
168
                    valid = 0;
169
                break;
170
            case 'P':
171
255
                if (sscanf(&token[1], "%d", &pocket) != 1) {
172
                    valid = 0;
173
                    break;
174
                }
175
253
                if (!random_toolchanger) {
176
253
                    fakepocket++;
177
253
                    if (fakepocket >= CANON_POCKETS_MAX) {
178
                        printf("too many tools. skipping tool %d\n", toolno);
179
                        valid = 0;
180
                        break;
181
                    }
182
253
                    if(fms) fms[fakepocket] = pocket;
183
253
                    pocket = fakepocket;
184
                }
185
253
                if (pocket < 0 || pocket >= CANON_POCKETS_MAX) {
186
                    printf("max pocket number is %d. skipping tool %d\n", CANON_POCKETS_MAX - 1, toolno);
187
                    valid = 0;
188
                    break;
189
                }
190
                break;
191
            case 'D':
192
189
                if (sscanf(&token[1], "%lf", &diameter) != 1)
193
2
                    valid = 0;
194
                break;
195
            case 'X':
196
3
                if (sscanf(&token[1], "%lf", &offset.tran.x) != 1)
197
                    valid = 0;
198
                break;
199
            case 'Y':
200
3
                if (sscanf(&token[1], "%lf", &offset.tran.y) != 1)
201
                    valid = 0;
202
                break;
203
            case 'Z':
204
253
                if (sscanf(&token[1], "%lf", &offset.tran.z) != 1)
205
                    valid = 0;
206
                break;
207
            case 'A':
208
                if (sscanf(&token[1], "%lf", &offset.a) != 1)
209
                    valid = 0;
210
                break;
211
            case 'B':
212
                if (sscanf(&token[1], "%lf", &offset.b) != 1)
213
                    valid = 0;
214
                break;
215
            case 'C':
216
2
                if (sscanf(&token[1], "%lf", &offset.c) != 1)
217
2
                    valid = 0;
218
                break;
219
            case 'U':
220
                if (sscanf(&token[1], "%lf", &offset.u) != 1)
221
                    valid = 0;
222
                break;
223
            case 'V':
224
                if (sscanf(&token[1], "%lf", &offset.v) != 1)
225
                    valid = 0;
226
                break;
227
            case 'W':
228
                if (sscanf(&token[1], "%lf", &offset.w) != 1)
229
                    valid = 0;
230
                break;
231
            case 'I':
232
                if (sscanf(&token[1], "%lf", &frontangle) != 1)
233
                    valid = 0;
234
                break;
235
            case 'J':
236
                if (sscanf(&token[1], "%lf", &backangle) != 1)
237
                    valid = 0;
238
                break;
239
            case 'Q':
240
                if (sscanf(&token[1], "%d", &orientation) != 1)
241
                    valid = 0;
242
                break;
243
            default:
244
6
                if (strncmp(token, "\n", 1) != 0)
245
4
                    valid = 0;
246
                break;
247
            }
248
964
            token = strtok(NULL, " ");
249
        }
250
257
        if (valid) {
251
255
            toolTable[pocket].toolno = toolno;
252
255
            toolTable[pocket].offset = offset;
253
255
            toolTable[pocket].diameter = diameter;
254
255
            toolTable[pocket].frontangle = frontangle;
255
255
            toolTable[pocket].backangle = backangle;
256
255
            toolTable[pocket].orientation = orientation;
257
258
255
            if (ttcomments && comment)
259
                strcpy(ttcomments[pocket], comment);
260
        } else {
261
2
            fprintf(stderr, "Unrecognized line skipped: %s", orig_line);
262
        }
263
257
        if (!random_toolchanger && toolTable[0].toolno == toolTable[pocket].toolno) {
264
4
            toolTable[0] = toolTable[pocket];
265
        }
266
    }
267
268
    // close the file
269
68
    fclose(fp);
270
271
    return 0;
272
}