-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
stepIndexer.js
345 lines (263 loc) · 8.21 KB
/
stepIndexer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
require('colors');
var util = require("util");
var assert = require('assert');
var Table = require('easy-table');
var fs = require("fs");
// http://www.steptools.com/support/stdev_docs/express/ap203/html/index.html
var waiting_file_type = 1;
var waiting_section = 2; // HEADER; or DATA;
var in_header_section = 3;
var in_data_section = 4;
var parsing_complete = 5;
var pattern_ISO_10303_21 = /ISO-10303-21;/;
var pattern_HEADER = /HEADER;/;
var pattern_DATA = /DATA;/;
var pattern_ENDSEC = /ENDSEC;/;
// pattern for simple step line #123=TOTO(....)
var pattern_simple_line = /^(\w+)\s*(\(.*\))\s*;/;
var pattern_startLine = /\#([0-9]+)\s*=\s*(.*)/; // something like # 123 = ...
/**
* this class reads and loads a raw step file in memory
*
* @param callback
* @constructor
*/
var StepIndexer = function (callback) {
"use strict";
this.filename ="";
this.lastLine = "";
this.entries = { };
this.types = {};
this.typesRev = {};
this.callback = callback;
this.curLine = "";
this.bad_STEP_file = false;
this.status = waiting_file_type;
this.header = []; // un parsed header
};
util.inherits(StepIndexer, require("stream"));
StepIndexer.prototype._getTypeIndex = function (strType) {
"use strict";
var me = this;
var typeIndex;
if (me.types.hasOwnProperty(strType)) {
typeIndex = me.types[strType];
} else {
//typeIndex = { key: me.typeCounter++ , array: []};
typeIndex = { key: strType, array: []};
me.types[strType] = typeIndex;
me.typesRev[typeIndex.key] = strType;
}
return typeIndex;
};
function trim(str) {
"use strict";
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
/**
* _splitComplexLine processes a complex step line.
* of the form
* IDENTIFIER(x,y,(x,y,e),y)IDENTIFIER2(1,2,#1232)....
*
* @param line
* @returns {Array}
* @private
*/
StepIndexer.prototype._splitComplexLine = function (line) {
"use strict";
var me = this;
var array = [];
var i = 1;
var n = line.length - 2;
var s;
while (i < n) {
s = i;
// skip identifier
while (line[i] !== '(' && i < n) {
i++;
if (i >= n) { break;}
}
var identifier = trim(line.substr(s, i - s));
// find balanced closing )
var parLevel = 1;
s = i;
i+=1;
while ((line[i] !== ')') || parLevel !== 1) {
if (line[i] === ')') { parLevel--;}
if (line[i] === '(') { parLevel++;}
i++;
if (i >= n) { break; }
}
var content = line.substr(s, i - s + 1);
i++;
if (identifier !== '' && identifier !== ')' ) {
var typeIndex = me._getTypeIndex(identifier);
array.push({ type: typeIndex.key, raw_data: content});
}
}
return array;
};
StepIndexer.prototype.write = function (data) {
"use strict";
var me = this;
if (me.bad_STEP_file) {
console.log("bad step");
return;
}
var lines = (this.lastLine + data.toString()).split(/\r\n|\n/);
this.lastLine = lines.splice(lines.length - 1)[0];
var matches,line_m,num,typeIndex,entry,type;
lines.forEach(function (line /*, index, array*/) {
if (me.bad_STEP_file) {
return;
}
if (me.status === waiting_file_type) {
if (pattern_ISO_10303_21.test(line)) {
me.status = waiting_section;
return;
} else {
me.bad_STEP_file = true;
// destroy the input stream
return;
}
}
if (me.status === waiting_section) {
if (pattern_HEADER.test(line)) {
me.status = in_header_section;
return;
} else if (pattern_DATA.test(line)) {
me.status = in_data_section;
return;
}
}
if (me.status === in_header_section) {
if (pattern_ENDSEC.test(line)) {
me.status = waiting_section;
return;
}
me.header.push(line);
} else if (me.status === in_data_section) {
if (pattern_ENDSEC.test(line)) {
me.status = parsing_complete;
return;
}
matches = pattern_startLine.exec(line);
if (matches) {
me.curNum = matches[1];
me.curLine = matches[2];
} else {
me.curLine += line;
}
if (me.curLine[me.curLine.length - 1] === ';') {
matches = pattern_simple_line.exec(me.curLine);
if (matches) {
// we have identify a simple form step line
// example:
// #1234=EDGE_LOOP('',(#234));
//
num = me.curNum;
type = matches[1];
line_m = matches[2];
// keep track of this
typeIndex = me._getTypeIndex(type);
typeIndex.array.push(num);
assert(typeIndex.key === type);
entry = {
_id: num,
type: typeIndex.key,
raw_data: line_m
};
me.entries[num] = entry;
} else {
// this is probably a complex form step line
// example:
// #1307=(NAMED_UNIT(*)SI_UNIT($,.STERADIAN.)SOLID_ANGLE_UNIT());
//
num = me.curNum;
entry = {
_id: num,
types: me._splitComplexLine(me.curLine)
};
me.entries[num] = entry;
entry.types.forEach(function (element) {
var type = element.type;
var typeIndex = me._getTypeIndex(type);
typeIndex.array.push(num);
});
}
}
}
});
};
StepIndexer.prototype.end = function (data) {
"use strict";
assert(data === undefined);
// console.log(" END with ", data);
var me = this;
if (me.status !== parsing_complete) {
me.bad_STEP_file = true;
me.callback(new Error("Invalid step file"));
} else {
me.callback();
}
};
/**
* return true if the file is a correct STEP file
* a correct STEP file starts with
* ISO-10303-21;
* HEADER;
*
* @param filename
* @param callback
*/
function check_STEP_file(filename, callback) {
"use strict";
// "ISO-10303-21;"
// "HEADER;"
var stream = fs.createReadStream(filename, {flags:"r"});
var fileData = "";
stream.on('data', function (data) {
fileData += data;
// The next lines should be improved
var lines = fileData.split("\n");
if (lines.length >= 2) {
stream.destroy();
if (!pattern_ISO_10303_21.test(lines[0])) {
my_callback(new Error("this file is not a STEP FILE : ISO_10303_21 missing"));
} else {
my_callback(null, lines[0]);
}
}
});
stream.on('error', function () {
my_callback('Error', null);
});
stream.on('end', function () {
my_callback('File end reached without finding line', null);
});
var callback_called = false;
function my_callback(err, data) {
if (!callback_called) {
callback_called = true;
callback(err, data);
}
}
}
function buildStepIndex(filename,callback) {
"use strict";
var input = fs.createReadStream(filename);
var step_index = new StepIndexer(function (err) {
input.destroy();
step_index.write = undefined ; // function no longer required
if (err) {
callback(err);
return;
}
callback(null,step_index);
});
step_index.filename = filename;
input.pipe(step_index);
}
exports.StepIndexer = StepIndexer;
exports.check_STEP_file = check_STEP_file;
exports.buildStepIndex = buildStepIndex;