Line data Source code
1 : #include "FeaturesFile.h"
2 : #include "ArchiveExtractor.h"
3 : #include <fstream>
4 : #include "Logger.h"
5 : #include "../invalid_odb_error.h"
6 : #include "str_utils.h"
7 : #include "../parse_error.h"
8 : #include "SymbolName.h"
9 : #include "equals_within.h"
10 : #include "macros.h"
11 : #include "../../Text/Utf8Sanitizer.h"
12 :
13 : #include <algorithm>
14 : #include <cctype>
15 :
16 : namespace Odb::Lib::FileModel::Design
17 : {
18 : namespace
19 : {
20 9272 : void apply_default_units_to_symbols(const std::string &rawUnits, SymbolName::Vector &symbols)
21 : {
22 9272 : const auto inferred = Odb::Lib::FileModel::Design::inferred_unit_type_from_features_units(rawUnits);
23 9272 : if (inferred == UnitType::None)
24 0 : return;
25 9274 : for (const auto &sym : symbols)
26 : {
27 2 : if (sym)
28 : {
29 2 : sym->ApplyDefaultUnitTypeIfNone(inferred);
30 : }
31 : }
32 : }
33 : }
34 :
35 10006 : FeaturesFile::FeaturesFile()
36 30018 : : m_path(""), m_directory(""), m_numFeatures(0), m_id(static_cast<unsigned>(-1))
37 : {
38 10006 : }
39 :
40 7936 : FeaturesFile::~FeaturesFile()
41 : {
42 7936 : m_featureRecords.clear();
43 7919 : m_attributeNames.clear();
44 7919 : m_attributeTextValues.clear();
45 7919 : m_symbolNamesByName.clear();
46 7919 : m_symbolNames.clear();
47 7919 : }
48 :
49 9467 : bool FeaturesFile::Parse(std::filesystem::path directory, const std::string &alternateFilename /*= ""*/)
50 : {
51 9467 : std::ifstream featuresFile;
52 9467 : int lineNumber = 0;
53 9467 : std::string line;
54 :
55 : try
56 : {
57 9467 : m_directory = directory;
58 :
59 37868 : loginfo("checking for extraction...");
60 :
61 9467 : std::vector<std::string> filenames;
62 9467 : if (alternateFilename.empty())
63 : {
64 28101 : std::copy(std::begin(FEATURES_FILENAMES), std::end(FEATURES_FILENAMES), std::back_inserter(filenames));
65 : }
66 : else
67 : {
68 100 : filenames.push_back(alternateFilename);
69 : }
70 :
71 9467 : std::filesystem::path featuresFilePath;
72 9467 : for (const auto &featuresFilename : filenames)
73 : {
74 18934 : loginfo("trying features file: [" + featuresFilename + "]...");
75 :
76 9467 : featuresFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, featuresFilename);
77 9467 : if (exists(featuresFilePath) && is_regular_file(featuresFilePath))
78 : {
79 18934 : loginfo("found features file: [" + featuresFilePath.string() + "]");
80 9467 : break;
81 : }
82 : }
83 :
84 9467 : m_path = featuresFilePath;
85 :
86 37868 : loginfo("any extraction complete, parsing data...");
87 :
88 9467 : if (!std::filesystem::exists(m_path))
89 : {
90 0 : auto message = "features file does not exist: [" + m_path.string() + "]";
91 0 : throw invalid_odb_error(message.c_str());
92 0 : }
93 9467 : else if (!std::filesystem::is_regular_file(m_path))
94 : {
95 0 : auto message = "features is not a file: [" + m_path.string() + "]";
96 0 : throw invalid_odb_error(message.c_str());
97 0 : }
98 :
99 9467 : featuresFile.open(m_path.string(), std::ios::in);
100 9467 : if (!featuresFile.is_open())
101 : {
102 0 : auto message = "unable to open features file: [" + m_path.string() + "]";
103 0 : throw invalid_odb_error(message.c_str());
104 0 : }
105 :
106 9467 : std::shared_ptr<FeatureRecord> pCurrentFeatureRecord;
107 9467 : std::shared_ptr<ContourPolygon> pCurrentContourPolygon;
108 :
109 32307290 : while (std::getline(featuresFile, line))
110 : {
111 32297823 : lineNumber++;
112 :
113 : // trim whitespace from beginning and end of line
114 32297823 : Utils::str_trim(line);
115 32297823 : if (!line.empty())
116 : {
117 32288613 : std::stringstream lineStream(line);
118 : // char firstChar = line[0];
119 :
120 32288613 : if (line.find(COMMENT_TOKEN) == 0)
121 : {
122 : // comment line
123 : }
124 32192811 : else if (line.find(UNITS_TOKEN) == 0)
125 : {
126 : // units line
127 9272 : std::string token;
128 9272 : if (!std::getline(lineStream, token, '='))
129 : {
130 0 : throw_parse_error(m_path, line, token, lineNumber);
131 : }
132 9272 : else if (!std::getline(lineStream, token, '='))
133 : {
134 0 : throw_parse_error(m_path, line, token, lineNumber);
135 : }
136 :
137 9272 : m_units = token;
138 9272 : apply_default_units_to_symbols(m_units, m_symbolNames);
139 9272 : }
140 32183539 : else if (line.find("U") == 0)
141 : {
142 0 : std::string token;
143 0 : if (!std::getline(lineStream, token, ' '))
144 : {
145 0 : throw_parse_error(m_path, line, token, lineNumber);
146 : }
147 0 : else if (!std::getline(lineStream, token, ' '))
148 : {
149 0 : throw_parse_error(m_path, line, token, lineNumber);
150 : }
151 :
152 0 : m_units = token;
153 0 : apply_default_units_to_symbols(m_units, m_symbolNames);
154 0 : }
155 32183539 : else if (line.find(ID_TOKEN) == 0)
156 : {
157 9267 : std::string token;
158 9267 : if (!std::getline(lineStream, token, '='))
159 : {
160 0 : throw_parse_error(m_path, line, token, lineNumber);
161 : }
162 9267 : else if (!std::getline(lineStream, token, '='))
163 : {
164 0 : throw_parse_error(m_path, line, token, lineNumber);
165 : }
166 9267 : m_id = std::stoul(token);
167 9267 : }
168 32174272 : else if (line.find(NUM_FEATURES_TOKEN) == 0)
169 : {
170 : // component record line
171 9267 : std::string token;
172 :
173 9267 : if (!(lineStream >> token))
174 : {
175 0 : throw_parse_error(m_path, line, token, lineNumber);
176 : }
177 :
178 9267 : if (token != NUM_FEATURES_TOKEN)
179 : {
180 0 : throw_parse_error(m_path, line, token, lineNumber);
181 : }
182 :
183 9267 : if (!(lineStream >> m_numFeatures))
184 : {
185 0 : throw_parse_error(m_path, line, token, lineNumber);
186 : }
187 9267 : }
188 32165005 : else if (line.find(ATTRIBUTE_NAME_TOKEN) == 0)
189 : {
190 : // component attribute name line
191 58442 : std::string token;
192 : // TODO: continue on failing line parse, to make a less strict/more robust parser (make a flag: enum ParseStrictness { strict, lax })
193 58442 : if (!std::getline(lineStream, token, ' '))
194 : {
195 0 : throw_parse_error(m_path, line, token, lineNumber);
196 : }
197 58442 : else if (!std::getline(lineStream, token, ' '))
198 : {
199 0 : throw_parse_error(m_path, line, token, lineNumber);
200 : }
201 58442 : m_attributeNames.push_back(token);
202 58442 : }
203 32106563 : else if (line.find(ATTRIBUTE_VALUE_TOKEN) == 0)
204 : {
205 : // component attribute text string values
206 471575 : std::string token;
207 471575 : if (!std::getline(lineStream, token, ' '))
208 : {
209 0 : throw_parse_error(m_path, line, token, lineNumber);
210 : }
211 471575 : else if (!std::getline(lineStream >> std::ws, token))
212 : {
213 0 : throw_parse_error(m_path, line, token, lineNumber);
214 : }
215 471575 : m_attributeTextValues.push_back(token);
216 471575 : }
217 31634988 : else if (line.find(SYMBOL_NAME_TOKEN) == 0)
218 : {
219 : // component attribute text string values
220 45727 : auto pSymbolName = std::make_shared<SymbolName>();
221 45727 : if (!pSymbolName->SymbolName::Parse(m_path, line, lineNumber))
222 : {
223 0 : throw_parse_error(m_path, line, "", lineNumber);
224 : }
225 45727 : pSymbolName->ApplyDefaultUnitTypeIfNone(Odb::Lib::FileModel::Design::inferred_unit_type_from_features_units(m_units));
226 45727 : m_symbolNamesByName[pSymbolName->GetName()] = pSymbolName;
227 45727 : m_symbolNames.push_back(pSymbolName);
228 45727 : }
229 31589261 : else if (line.find(FeatureRecord::LINE_TOKEN) == 0)
230 : {
231 21452971 : std::string token;
232 21452971 : if (!(lineStream >> token) || token != FeatureRecord::LINE_TOKEN)
233 : {
234 0 : throw_parse_error(m_path, line, token, lineNumber);
235 : }
236 :
237 21452971 : auto pFeatureRecord = std::make_shared<FeatureRecord>();
238 21452971 : pFeatureRecord->type = FeatureRecord::Type::Line;
239 :
240 21452971 : if (!(lineStream >> pFeatureRecord->xs))
241 : {
242 0 : throw_parse_error(m_path, line, token, lineNumber);
243 : }
244 :
245 21452971 : if (!(lineStream >> pFeatureRecord->ys))
246 : {
247 0 : throw_parse_error(m_path, line, token, lineNumber);
248 : }
249 :
250 21452971 : if (!(lineStream >> pFeatureRecord->xe))
251 : {
252 0 : throw_parse_error(m_path, line, token, lineNumber);
253 : }
254 :
255 21452971 : if (!(lineStream >> pFeatureRecord->ye))
256 : {
257 0 : throw_parse_error(m_path, line, token, lineNumber);
258 : }
259 :
260 21452971 : if (!(lineStream >> pFeatureRecord->sym_num))
261 : {
262 0 : throw_parse_error(m_path, line, token, lineNumber);
263 : }
264 :
265 : char polarity;
266 21452971 : if (!(lineStream >> polarity))
267 : {
268 0 : throw_parse_error(m_path, line, token, lineNumber);
269 : }
270 21452971 : switch (polarity)
271 : {
272 21452971 : case 'P':
273 21452971 : pFeatureRecord->polarity = Polarity::Positive;
274 21452971 : break;
275 0 : case 'N':
276 0 : pFeatureRecord->polarity = Polarity::Negative;
277 0 : break;
278 0 : default:
279 0 : throw_parse_error(m_path, line, token, lineNumber);
280 : }
281 :
282 21452971 : if (!(lineStream >> pFeatureRecord->dcode))
283 : {
284 0 : throw_parse_error(m_path, line, token, lineNumber);
285 : }
286 :
287 42905942 : std::string attrIdString;
288 21452971 : lineStream >> attrIdString;
289 :
290 21452971 : if (!pFeatureRecord->ParseAttributeLookupTable(attrIdString, pFeatureRecord->id))
291 : {
292 0 : throw_parse_error(m_path, line, token, lineNumber);
293 : }
294 :
295 21452971 : m_featureRecords.push_back(pFeatureRecord);
296 21452971 : }
297 10136290 : else if (line.find(FeatureRecord::PAD_TOKEN) == 0)
298 : {
299 2688302 : std::string token;
300 2688302 : if (!(lineStream >> token) || token != FeatureRecord::PAD_TOKEN)
301 : {
302 0 : throw_parse_error(m_path, line, token, lineNumber);
303 : }
304 :
305 2688302 : auto pFeatureRecord = std::make_shared<FeatureRecord>();
306 2688302 : pFeatureRecord->type = FeatureRecord::Type::Pad;
307 :
308 2688302 : if (!(lineStream >> pFeatureRecord->x))
309 : {
310 0 : throw_parse_error(m_path, line, token, lineNumber);
311 : }
312 :
313 2688302 : if (!(lineStream >> pFeatureRecord->y))
314 : {
315 0 : throw_parse_error(m_path, line, token, lineNumber);
316 : }
317 :
318 2688302 : if (!(lineStream >> pFeatureRecord->apt_def_symbol_num))
319 : {
320 0 : throw_parse_error(m_path, line, token, lineNumber);
321 : }
322 2688302 : if (pFeatureRecord->apt_def_symbol_num == -1)
323 : {
324 1 : if (!(lineStream >> pFeatureRecord->apt_def_resize_factor))
325 : {
326 0 : throw_parse_error(m_path, line, token, lineNumber);
327 : }
328 : }
329 :
330 : // Contract: Pads reference a symbol by FeatureRecord.sym_num.
331 : // In ODB++ pad records this comes from apt_def_symbol_num; keep sym_num unset when apt_def_symbol_num == -1.
332 2688302 : if (pFeatureRecord->apt_def_symbol_num >= 0 && pFeatureRecord->sym_num == -1)
333 : {
334 2688301 : pFeatureRecord->sym_num = pFeatureRecord->apt_def_symbol_num;
335 : }
336 :
337 : char polarity;
338 2688302 : if (!(lineStream >> polarity))
339 : {
340 0 : throw_parse_error(m_path, line, token, lineNumber);
341 : }
342 2688302 : switch (polarity)
343 : {
344 2688302 : case 'P':
345 2688302 : pFeatureRecord->polarity = Polarity::Positive;
346 2688302 : break;
347 0 : case 'N':
348 0 : pFeatureRecord->polarity = Polarity::Negative;
349 0 : break;
350 0 : default:
351 0 : throw_parse_error(m_path, line, token, lineNumber);
352 : }
353 :
354 2688302 : if (!(lineStream >> pFeatureRecord->dcode))
355 : {
356 0 : throw_parse_error(m_path, line, token, lineNumber);
357 : }
358 :
359 2688302 : if (!(lineStream >> pFeatureRecord->orient_def))
360 : {
361 0 : throw_parse_error(m_path, line, token, lineNumber);
362 : }
363 :
364 5376604 : std::string attrIdString;
365 2688302 : lineStream >> attrIdString;
366 :
367 2688302 : if (!pFeatureRecord->ParseAttributeLookupTable(attrIdString, pFeatureRecord->id))
368 : {
369 0 : throw_parse_error(m_path, line, token, lineNumber);
370 : }
371 :
372 2688302 : m_featureRecords.push_back(pFeatureRecord);
373 2688302 : }
374 7447988 : else if (line.find(FeatureRecord::TEXT_TOKEN) == 0)
375 : {
376 0 : std::string token;
377 0 : if (!(lineStream >> token) || token != FeatureRecord::TEXT_TOKEN)
378 : {
379 0 : throw_parse_error(m_path, line, token, lineNumber);
380 : }
381 :
382 0 : auto pFeatureRecord = std::make_shared<FeatureRecord>();
383 0 : pFeatureRecord->type = FeatureRecord::Type::Text;
384 :
385 0 : if (!(lineStream >> pFeatureRecord->x))
386 : {
387 0 : throw_parse_error(m_path, line, token, lineNumber);
388 : }
389 :
390 0 : if (!(lineStream >> pFeatureRecord->y))
391 : {
392 0 : throw_parse_error(m_path, line, token, lineNumber);
393 : }
394 :
395 0 : if (!(lineStream >> pFeatureRecord->font))
396 : {
397 0 : throw_parse_error(m_path, line, token, lineNumber);
398 : }
399 :
400 : char polarity;
401 0 : if (!(lineStream >> polarity))
402 : {
403 0 : throw_parse_error(m_path, line, token, lineNumber);
404 : }
405 0 : switch (polarity)
406 : {
407 0 : case 'P':
408 0 : pFeatureRecord->polarity = Polarity::Positive;
409 0 : break;
410 0 : case 'N':
411 0 : pFeatureRecord->polarity = Polarity::Negative;
412 0 : break;
413 0 : default:
414 0 : throw_parse_error(m_path, line, token, lineNumber);
415 : }
416 :
417 0 : if (!(lineStream >> pFeatureRecord->orient_def))
418 : {
419 0 : throw_parse_error(m_path, line, token, lineNumber);
420 : }
421 :
422 0 : if (pFeatureRecord->orient_def == 8 ||
423 0 : pFeatureRecord->orient_def == 9)
424 : {
425 0 : if (!(lineStream >> pFeatureRecord->orient_def_rotation))
426 : {
427 0 : throw_parse_error(m_path, line, token, lineNumber);
428 : }
429 : }
430 :
431 0 : if (!(lineStream >> pFeatureRecord->xsize))
432 : {
433 0 : throw_parse_error(m_path, line, token, lineNumber);
434 : }
435 :
436 0 : if (!(lineStream >> pFeatureRecord->ysize))
437 : {
438 0 : throw_parse_error(m_path, line, token, lineNumber);
439 : }
440 :
441 0 : if (!(lineStream >> pFeatureRecord->width_factor))
442 : {
443 0 : throw_parse_error(m_path, line, token, lineNumber);
444 : }
445 :
446 0 : if (!(lineStream >> std::quoted(pFeatureRecord->text, '\'')))
447 : {
448 0 : throw_parse_error(m_path, line, token, lineNumber);
449 : }
450 :
451 0 : if (!(lineStream >> pFeatureRecord->version))
452 : {
453 0 : throw_parse_error(m_path, line, token, lineNumber);
454 : }
455 :
456 0 : std::string attrIdString;
457 0 : lineStream >> attrIdString;
458 :
459 0 : if (!pFeatureRecord->ParseAttributeLookupTable(attrIdString, pFeatureRecord->id))
460 : {
461 0 : throw_parse_error(m_path, line, token, lineNumber);
462 : }
463 :
464 0 : m_featureRecords.push_back(pFeatureRecord);
465 0 : }
466 7447988 : else if (line.find(FeatureRecord::ARC_TOKEN) == 0)
467 : {
468 64846 : std::string token;
469 64846 : if (!(lineStream >> token) || token != FeatureRecord::ARC_TOKEN)
470 : {
471 0 : throw_parse_error(m_path, line, token, lineNumber);
472 : }
473 :
474 64846 : auto pFeatureRecord = std::make_shared<FeatureRecord>();
475 64846 : pFeatureRecord->type = FeatureRecord::Type::Arc;
476 :
477 64846 : if (!(lineStream >> pFeatureRecord->xs))
478 : {
479 0 : throw_parse_error(m_path, line, token, lineNumber);
480 : }
481 :
482 64846 : if (!(lineStream >> pFeatureRecord->ys))
483 : {
484 0 : throw_parse_error(m_path, line, token, lineNumber);
485 : }
486 :
487 64846 : if (!(lineStream >> pFeatureRecord->xe))
488 : {
489 0 : throw_parse_error(m_path, line, token, lineNumber);
490 : }
491 :
492 64846 : if (!(lineStream >> pFeatureRecord->ye))
493 : {
494 0 : throw_parse_error(m_path, line, token, lineNumber);
495 : }
496 :
497 64846 : if (!(lineStream >> pFeatureRecord->xc))
498 : {
499 0 : throw_parse_error(m_path, line, token, lineNumber);
500 : }
501 :
502 64846 : if (!(lineStream >> pFeatureRecord->yc))
503 : {
504 0 : throw_parse_error(m_path, line, token, lineNumber);
505 : }
506 :
507 64846 : if (!(lineStream >> pFeatureRecord->sym_num))
508 : {
509 0 : throw_parse_error(m_path, line, token, lineNumber);
510 : }
511 :
512 : char polarity;
513 64846 : if (!(lineStream >> polarity))
514 : {
515 0 : throw_parse_error(m_path, line, token, lineNumber);
516 : }
517 64846 : switch (polarity)
518 : {
519 64846 : case 'P':
520 64846 : pFeatureRecord->polarity = Polarity::Positive;
521 64846 : break;
522 0 : case 'N':
523 0 : pFeatureRecord->polarity = Polarity::Negative;
524 0 : break;
525 0 : default:
526 0 : throw_parse_error(m_path, line, token, lineNumber);
527 : }
528 :
529 64846 : if (!(lineStream >> pFeatureRecord->dcode))
530 : {
531 0 : throw_parse_error(m_path, line, token, lineNumber);
532 : }
533 :
534 : char cw;
535 64846 : if (!(lineStream >> cw))
536 : {
537 0 : throw_parse_error(m_path, line, token, lineNumber);
538 : }
539 64846 : switch (cw)
540 : {
541 40070 : case 'Y':
542 40070 : pFeatureRecord->cw = true;
543 40070 : break;
544 24776 : case 'N':
545 24776 : pFeatureRecord->cw = false;
546 24776 : break;
547 0 : default:
548 0 : throw_parse_error(m_path, line, token, lineNumber);
549 : }
550 :
551 129692 : std::string attrIdString;
552 64846 : lineStream >> attrIdString;
553 :
554 64846 : if (!pFeatureRecord->ParseAttributeLookupTable(attrIdString, pFeatureRecord->id))
555 : {
556 0 : throw_parse_error(m_path, line, token, lineNumber);
557 : }
558 :
559 64846 : m_featureRecords.push_back(pFeatureRecord);
560 64846 : }
561 7383142 : else if (line.find(FeatureRecord::BARCODE_TOKEN) == 0)
562 : {
563 0 : std::string token;
564 0 : if (!(lineStream >> token) || token != FeatureRecord::BARCODE_TOKEN)
565 : {
566 0 : throw_parse_error(m_path, line, token, lineNumber);
567 : }
568 :
569 0 : auto pFeatureRecord = std::make_shared<FeatureRecord>();
570 0 : pFeatureRecord->type = FeatureRecord::Type::Barcode;
571 :
572 : // TODO: barcode feature record type
573 :
574 : // std::string attrIdString;
575 : // lineStream >> attrIdString;
576 :
577 : // if (!pFeatureRecord->ParseAttributeLookupTable(attrIdString, pFeatureRecord->id))
578 : //{
579 : // throw_parse_error(m_path, line, token, lineNumber);
580 : // }
581 :
582 0 : m_featureRecords.push_back(pFeatureRecord);
583 0 : }
584 7809910 : else if (line.find(FeatureRecord::SURFACE_START_TOKEN) == 0 &&
585 7809910 : line.size() > 1 && line[1] == ' ')
586 : {
587 213384 : std::string token;
588 213384 : if (!(lineStream >> token) || token != FeatureRecord::SURFACE_START_TOKEN)
589 : {
590 0 : throw_parse_error(m_path, line, token, lineNumber);
591 : }
592 :
593 213384 : pCurrentFeatureRecord = std::make_shared<FeatureRecord>();
594 213384 : pCurrentFeatureRecord->type = FeatureRecord::Type::Surface;
595 :
596 : char polarity;
597 213384 : if (!(lineStream >> polarity))
598 : {
599 0 : throw_parse_error(m_path, line, token, lineNumber);
600 : }
601 213384 : switch (polarity)
602 : {
603 213384 : case 'P':
604 213384 : pCurrentFeatureRecord->polarity = Polarity::Positive;
605 213384 : break;
606 0 : case 'N':
607 0 : pCurrentFeatureRecord->polarity = Polarity::Negative;
608 0 : break;
609 0 : default:
610 0 : throw_parse_error(m_path, line, token, lineNumber);
611 : }
612 :
613 213384 : if (!(lineStream >> pCurrentFeatureRecord->dcode))
614 : {
615 0 : throw_parse_error(m_path, line, token, lineNumber);
616 : }
617 :
618 426768 : std::string attrIdString;
619 213384 : lineStream >> attrIdString;
620 :
621 213384 : if (!pCurrentFeatureRecord->ParseAttributeLookupTable(attrIdString, pCurrentFeatureRecord->id))
622 : {
623 0 : throw_parse_error(m_path, line, token, lineNumber);
624 : }
625 213384 : }
626 7169758 : else if (line.find(FeatureRecord::SURFACE_END_TOKEN) == 0)
627 : {
628 213384 : std::string token;
629 213384 : if (!(lineStream >> token) || token != FeatureRecord::SURFACE_END_TOKEN)
630 : {
631 0 : throw_parse_error(m_path, line, token, lineNumber);
632 : }
633 :
634 213384 : if (pCurrentFeatureRecord != nullptr)
635 : {
636 213384 : m_featureRecords.push_back(pCurrentFeatureRecord);
637 213384 : pCurrentFeatureRecord.reset();
638 : }
639 : else
640 : {
641 0 : throw_parse_error(m_path, line, token, lineNumber);
642 : }
643 213384 : }
644 6956374 : else if (line.find(ContourPolygon::BEGIN_RECORD_TOKEN) == 0)
645 : {
646 541860 : std::string token;
647 541860 : if (!(lineStream >> token))
648 : {
649 0 : throw_parse_error(m_path, line, token, lineNumber);
650 : }
651 :
652 541860 : if (token != ContourPolygon::BEGIN_RECORD_TOKEN)
653 : {
654 0 : throw_parse_error(m_path, line, token, lineNumber);
655 : }
656 :
657 541860 : pCurrentContourPolygon = std::make_shared<ContourPolygon>();
658 :
659 541860 : if (!(lineStream >> pCurrentContourPolygon->xStart))
660 : {
661 0 : throw_parse_error(m_path, line, token, lineNumber);
662 : }
663 :
664 541860 : if (!(lineStream >> pCurrentContourPolygon->yStart))
665 : {
666 0 : throw_parse_error(m_path, line, token, lineNumber);
667 : }
668 :
669 541860 : if (!(lineStream >> token))
670 : {
671 0 : throw_parse_error(m_path, line, token, lineNumber);
672 : }
673 :
674 541860 : if (token == ContourPolygon::ISLAND_TYPE_TOKEN)
675 : {
676 214824 : pCurrentContourPolygon->type = ContourPolygon::Type::Island;
677 : }
678 327036 : else if (token == ContourPolygon::HOLE_TYPE_TOKEN)
679 : {
680 327036 : pCurrentContourPolygon->type = ContourPolygon::Type::Hole;
681 : }
682 : else
683 : {
684 0 : throw_parse_error(m_path, line, token, lineNumber);
685 : }
686 541860 : }
687 6414514 : else if (line.find(ContourPolygon::END_RECORD_TOKEN) == 0)
688 : {
689 541860 : std::string token;
690 541860 : if (!(lineStream >> token))
691 : {
692 0 : throw_parse_error(m_path, line, token, lineNumber);
693 : }
694 :
695 541860 : if (token != ContourPolygon::END_RECORD_TOKEN)
696 : {
697 0 : throw_parse_error(m_path, line, token, lineNumber);
698 : }
699 :
700 541860 : if (pCurrentFeatureRecord != nullptr)
701 : {
702 541860 : pCurrentFeatureRecord->m_contourPolygons.push_back(pCurrentContourPolygon);
703 541860 : pCurrentContourPolygon.reset();
704 : }
705 : else
706 : {
707 0 : throw_parse_error(m_path, line, token, lineNumber);
708 : }
709 541860 : }
710 5872654 : else if (line.find(ContourPolygon::PolygonPart::ARC_RECORD_TOKEN) == 0)
711 : {
712 3647058 : std::string token;
713 3647058 : if (!(lineStream >> token))
714 : {
715 0 : throw_parse_error(m_path, line, token, lineNumber);
716 : }
717 :
718 3647058 : if (token != ContourPolygon::PolygonPart::ARC_RECORD_TOKEN)
719 : {
720 0 : throw_parse_error(m_path, line, token, lineNumber);
721 : }
722 :
723 3647058 : auto pPolygonPart = std::make_shared<ContourPolygon::PolygonPart>();
724 3647058 : pPolygonPart->type = ContourPolygon::PolygonPart::Type::Arc;
725 :
726 3647058 : if (!(lineStream >> pPolygonPart->endX))
727 : {
728 0 : throw_parse_error(m_path, line, token, lineNumber);
729 : }
730 :
731 3647058 : if (!(lineStream >> pPolygonPart->endY))
732 : {
733 0 : throw_parse_error(m_path, line, token, lineNumber);
734 : }
735 :
736 3647058 : if (!(lineStream >> pPolygonPart->xCenter))
737 : {
738 0 : throw_parse_error(m_path, line, token, lineNumber);
739 : }
740 :
741 3647058 : if (!(lineStream >> pPolygonPart->yCenter))
742 : {
743 0 : throw_parse_error(m_path, line, token, lineNumber);
744 : }
745 :
746 3647058 : if (!(lineStream >> token))
747 : {
748 0 : throw_parse_error(m_path, line, token, lineNumber);
749 : }
750 :
751 3647058 : if (token == "y" || token == "Y")
752 : {
753 1626255 : pPolygonPart->isClockwise = true;
754 : }
755 2020803 : else if (token == "n" || token == "N")
756 : {
757 2020803 : pPolygonPart->isClockwise = false;
758 : }
759 : else
760 : {
761 0 : throw_parse_error(m_path, line, token, lineNumber);
762 : }
763 :
764 3647058 : if (pCurrentContourPolygon != nullptr)
765 : {
766 3647058 : pCurrentContourPolygon->m_polygonParts.push_back(pPolygonPart);
767 : }
768 : else
769 : {
770 0 : throw_parse_error(m_path, line, token, lineNumber);
771 : }
772 3647058 : }
773 2225596 : else if (line.find(ContourPolygon::PolygonPart::SEGMENT_RECORD_TOKEN) == 0)
774 : {
775 2225596 : std::string token;
776 2225596 : if (!(lineStream >> token))
777 : {
778 0 : throw_parse_error(m_path, line, token, lineNumber);
779 : }
780 :
781 2225596 : if (token != ContourPolygon::PolygonPart::SEGMENT_RECORD_TOKEN)
782 : {
783 0 : throw_parse_error(m_path, line, token, lineNumber);
784 : }
785 :
786 2225596 : auto pPolygonPart = std::make_shared<ContourPolygon::PolygonPart>();
787 2225596 : pPolygonPart->type = ContourPolygon::PolygonPart::Type::Segment;
788 :
789 2225596 : if (!(lineStream >> pPolygonPart->endX))
790 : {
791 0 : throw_parse_error(m_path, line, token, lineNumber);
792 : }
793 :
794 2225596 : if (!(lineStream >> pPolygonPart->endY))
795 : {
796 0 : throw_parse_error(m_path, line, token, lineNumber);
797 : }
798 :
799 2225596 : if (pCurrentContourPolygon != nullptr)
800 : {
801 2225596 : pCurrentContourPolygon->m_polygonParts.push_back(pPolygonPart);
802 : }
803 : else
804 : {
805 0 : throw_parse_error(m_path, line, token, lineNumber);
806 : }
807 2225596 : }
808 : else
809 : {
810 : // unrecognized record line
811 0 : parse_info pi(m_path, line, lineNumber);
812 0 : logwarn(pi.toString("unrecognized record line in features file:"));
813 0 : }
814 32288613 : }
815 : }
816 :
817 : // do we have a current feature record? (finish up the last record in the file- i.e. ran out of file)
818 9467 : if (pCurrentFeatureRecord != nullptr)
819 : {
820 : // finish it up and add it to the list
821 0 : m_featureRecords.push_back(pCurrentFeatureRecord);
822 0 : pCurrentFeatureRecord.reset();
823 : }
824 :
825 9467 : featuresFile.close();
826 9467 : }
827 0 : catch (parse_error &pe)
828 : {
829 0 : auto m = pe.toString("Parse Error:");
830 0 : logerror(m);
831 0 : featuresFile.close();
832 0 : throw pe;
833 0 : }
834 0 : catch (std::exception &e)
835 : {
836 0 : parse_info pi(m_path, line, lineNumber);
837 0 : const auto m = pi.toString();
838 0 : logexception_msg(e, m);
839 0 : featuresFile.close();
840 0 : throw e;
841 0 : }
842 :
843 9467 : return true;
844 9467 : }
845 :
846 9 : std::string FeaturesFile::GetUnits() const
847 : {
848 9 : return m_units;
849 : }
850 :
851 0 : std::filesystem::path FeaturesFile::GetPath()
852 : {
853 0 : return m_path;
854 : }
855 :
856 0 : std::filesystem::path FeaturesFile::GetDirectory()
857 : {
858 0 : return m_directory;
859 : }
860 :
861 0 : int FeaturesFile::GetNumFeatures() const
862 : {
863 0 : return m_numFeatures;
864 : }
865 :
866 0 : unsigned int FeaturesFile::GetId() const
867 : {
868 0 : return m_id;
869 : }
870 :
871 2 : const SymbolName::StringMap &FeaturesFile::GetSymbolNamesByName() const
872 : {
873 2 : return m_symbolNamesByName;
874 : }
875 :
876 10 : const SymbolName::Vector &FeaturesFile::GetSymbolNames() const
877 : {
878 10 : return m_symbolNames;
879 : }
880 :
881 7390 : const FeaturesFile::FeatureRecord::Vector &FeaturesFile::GetFeatureRecords() const
882 : {
883 7390 : return m_featureRecords;
884 : }
885 :
886 7397 : std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile> FeaturesFile::to_protobuf() const
887 : {
888 7397 : std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile> pFeaturesFileMessage(new Odb::Lib::Protobuf::FeaturesFile);
889 7397 : pFeaturesFileMessage->set_id(m_id);
890 7397 : pFeaturesFileMessage->set_numfeatures(m_numFeatures);
891 14794 : pFeaturesFileMessage->set_units(Odb::Lib::Text::ToUtf8(m_units));
892 14485316 : for (const auto &pFeatureRecord : m_featureRecords)
893 : {
894 14477919 : pFeaturesFileMessage->add_featurerecords()->CopyFrom(*pFeatureRecord->to_protobuf());
895 : }
896 7397 : FillSymbolNames(*pFeaturesFileMessage);
897 7397 : return pFeaturesFileMessage;
898 0 : }
899 :
900 368 : std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile> FeaturesFile::to_protobuf_skeleton() const
901 : {
902 368 : std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile> pFeaturesFileMessage(new Odb::Lib::Protobuf::FeaturesFile);
903 368 : pFeaturesFileMessage->set_id(m_id);
904 368 : pFeaturesFileMessage->set_numfeatures(m_numFeatures);
905 736 : pFeaturesFileMessage->set_units(Odb::Lib::Text::ToUtf8(m_units));
906 : // featureRecords deliberately omitted (M6 streaming skeleton).
907 368 : FillSymbolNames(*pFeaturesFileMessage);
908 368 : return pFeaturesFileMessage;
909 0 : }
910 :
911 7765 : void FeaturesFile::FillSymbolNames(Odb::Lib::Protobuf::FeaturesFile &featuresFileMessage) const
912 : {
913 49962 : for (const auto &kvSymbolName : m_symbolNamesByName)
914 : {
915 42197 : (*featuresFileMessage.mutable_symbolnamesbyname())[Odb::Lib::Text::ToUtf8(kvSymbolName.first)] = *kvSymbolName.second->to_protobuf();
916 : }
917 49962 : for (const auto &symbolName : m_symbolNames)
918 : {
919 42197 : featuresFileMessage.add_symbolnames()->CopyFrom(*symbolName->to_protobuf());
920 : }
921 7765 : }
922 :
923 536 : void FeaturesFile::from_protobuf(const Odb::Lib::Protobuf::FeaturesFile &message)
924 : {
925 536 : m_id = message.id();
926 536 : m_numFeatures = message.numfeatures();
927 536 : m_units = message.units();
928 1299401 : for (const auto &featureRecordMessage : message.featurerecords())
929 : {
930 1298865 : std::shared_ptr<FeatureRecord> pFeatureRecord(new FeatureRecord);
931 1298865 : pFeatureRecord->from_protobuf(featureRecordMessage);
932 1298865 : m_featureRecords.push_back(pFeatureRecord);
933 1298865 : }
934 536 : if (message.symbolnames().size() > 0)
935 : {
936 : // create both collections from symbolNames
937 2586 : for (const auto &symbolNameMessage : message.symbolnames())
938 : {
939 2363 : auto pSymbolName = std::make_shared<SymbolName>();
940 2363 : pSymbolName->from_protobuf(symbolNameMessage);
941 2363 : m_symbolNames.push_back(pSymbolName);
942 2363 : m_symbolNamesByName[pSymbolName->GetName()] = pSymbolName;
943 2363 : }
944 : }
945 313 : else if (message.symbolnamesbyname().size() > 0)
946 : {
947 : // create both collections from symbolsByName
948 0 : for (const auto &kvSymbolNameMessage : message.symbolnamesbyname())
949 : {
950 0 : auto pSymbolName = std::make_shared<SymbolName>();
951 0 : pSymbolName->from_protobuf(kvSymbolNameMessage.second);
952 0 : m_symbolNamesByName[kvSymbolNameMessage.first] = pSymbolName;
953 0 : m_symbolNames.push_back(pSymbolName);
954 0 : }
955 : }
956 536 : }
957 :
958 0 : bool FeaturesFile::Save(std::ostream &os)
959 : {
960 0 : return true;
961 : }
962 :
963 21739711 : FeaturesFile::FeatureRecord::~FeatureRecord()
964 : {
965 20440846 : m_contourPolygons.clear();
966 21739710 : }
967 :
968 0 : const ContourPolygon::Vector &FeaturesFile::FeatureRecord::GetContourPolygons() const
969 : {
970 0 : return m_contourPolygons;
971 : }
972 :
973 15611929 : std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile::FeatureRecord> Odb::Lib::FileModel::Design::FeaturesFile::FeatureRecord::to_protobuf() const
974 : {
975 15611929 : std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile::FeatureRecord> pFeatureRecordMessage(new Odb::Lib::Protobuf::FeaturesFile::FeatureRecord);
976 15611929 : pFeatureRecordMessage->set_apt_def_resize_factor(apt_def_resize_factor);
977 15611929 : pFeatureRecordMessage->set_xc(xc);
978 15611929 : pFeatureRecordMessage->set_yc(yc);
979 15611929 : pFeatureRecordMessage->set_cw(cw);
980 31223858 : pFeatureRecordMessage->set_font(Odb::Lib::Text::ToUtf8(font));
981 15611929 : pFeatureRecordMessage->set_xsize(xsize);
982 15611929 : pFeatureRecordMessage->set_ysize(ysize);
983 15611929 : pFeatureRecordMessage->set_width_factor(width_factor);
984 31223858 : pFeatureRecordMessage->set_text(Odb::Lib::Text::ToUtf8(text));
985 15611929 : pFeatureRecordMessage->set_version(version);
986 15611929 : if (sym_num >= 0)
987 : {
988 15493492 : pFeatureRecordMessage->set_sym_num(sym_num);
989 : }
990 15611929 : pFeatureRecordMessage->set_polarity(static_cast<Odb::Lib::Protobuf::Polarity>(polarity));
991 15611929 : pFeatureRecordMessage->set_dcode(dcode);
992 15611929 : if (id.has_value())
993 : {
994 15611811 : pFeatureRecordMessage->set_id(*id);
995 : }
996 15611929 : pFeatureRecordMessage->set_orient_def(orient_def);
997 15611929 : pFeatureRecordMessage->set_orient_def_rotation(orient_def_rotation);
998 15611929 : pFeatureRecordMessage->set_type(static_cast<Odb::Lib::Protobuf::FeaturesFile::FeatureRecord::Type>(type));
999 15611929 : pFeatureRecordMessage->set_xs(xs);
1000 15611929 : pFeatureRecordMessage->set_ys(ys);
1001 15611929 : pFeatureRecordMessage->set_xe(xe);
1002 15611929 : pFeatureRecordMessage->set_ye(ye);
1003 15611929 : pFeatureRecordMessage->set_x(x);
1004 15611929 : pFeatureRecordMessage->set_y(y);
1005 15611929 : if (apt_def_symbol_num >= 0)
1006 : {
1007 2132572 : pFeatureRecordMessage->set_apt_def_symbol_num(apt_def_symbol_num);
1008 : }
1009 16098503 : for (const auto &pContourPolygon : m_contourPolygons)
1010 : {
1011 486574 : pFeatureRecordMessage->add_contourpolygons()->CopyFrom(*pContourPolygon->to_protobuf());
1012 : }
1013 54493234 : for (const auto &kvAttributeAssignment : m_attributeLookupTable)
1014 : {
1015 38881305 : (*pFeatureRecordMessage->mutable_attributelookuptable())[Odb::Lib::Text::ToUtf8(kvAttributeAssignment.first)] = Odb::Lib::Text::ToUtf8(kvAttributeAssignment.second);
1016 : }
1017 15611929 : return pFeatureRecordMessage;
1018 0 : }
1019 :
1020 1298869 : void Odb::Lib::FileModel::Design::FeaturesFile::FeatureRecord::from_protobuf(const Odb::Lib::Protobuf::FeaturesFile::FeatureRecord &message)
1021 : {
1022 1298869 : apt_def_resize_factor = message.apt_def_resize_factor();
1023 1298869 : xc = message.xc();
1024 1298869 : yc = message.yc();
1025 1298869 : cw = message.cw();
1026 1298869 : font = message.font();
1027 1298869 : xsize = message.xsize();
1028 1298869 : ysize = message.ysize();
1029 1298869 : width_factor = message.width_factor();
1030 1298869 : text = message.text();
1031 1298869 : version = message.version();
1032 1298869 : sym_num = message.has_sym_num() ? message.sym_num() : -1;
1033 1298869 : polarity = static_cast<Polarity>(message.polarity());
1034 1298869 : dcode = message.dcode();
1035 1298869 : if (message.has_id())
1036 : {
1037 1298851 : id = message.id();
1038 : }
1039 1298869 : orient_def = message.orient_def();
1040 1298869 : orient_def_rotation = message.orient_def_rotation();
1041 1298869 : type = static_cast<Type>(message.type());
1042 1298869 : xs = message.xs();
1043 1298869 : ys = message.ys();
1044 1298869 : xe = message.xe();
1045 1298869 : ye = message.ye();
1046 1298869 : x = message.x();
1047 1298869 : y = message.y();
1048 1298869 : apt_def_symbol_num = message.has_apt_def_symbol_num() ? message.apt_def_symbol_num() : -1;
1049 1326939 : for (const auto &contourPolygonMessage : message.contourpolygons())
1050 : {
1051 28070 : std::shared_ptr<ContourPolygon> pContourPolygon(new ContourPolygon);
1052 28070 : pContourPolygon->from_protobuf(contourPolygonMessage);
1053 28070 : m_contourPolygons.push_back(pContourPolygon);
1054 28070 : }
1055 7005795 : for (const auto &kvAttributeAssignment : message.attributelookuptable())
1056 : {
1057 2853463 : m_attributeLookupTable[kvAttributeAssignment.first] = kvAttributeAssignment.second;
1058 : }
1059 1298869 : }
1060 :
1061 4 : SymbolName::Vector collect_symbols(const FeaturesFile &featuresFile)
1062 : {
1063 4 : const auto &vec = featuresFile.GetSymbolNames();
1064 4 : if (!vec.empty())
1065 2 : return vec;
1066 :
1067 2 : SymbolName::Vector collected;
1068 2 : for (const auto &kv : featuresFile.GetSymbolNamesByName())
1069 : {
1070 0 : collected.push_back(kv.second);
1071 : }
1072 2 : return collected;
1073 2 : }
1074 :
1075 54999 : UnitType inferred_unit_type_from_features_units(const std::string &rawUnits)
1076 : {
1077 54999 : if (rawUnits.empty())
1078 4 : return UnitType::None;
1079 54995 : std::string u = rawUnits;
1080 54995 : std::transform(u.begin(), u.end(), u.begin(), [](unsigned char c)
1081 219976 : { return static_cast<char>(std::tolower(c)); });
1082 54995 : if (u == "mm" || u == "millimeter" || u == "millimeters" || u == "micron" || u == "microns" || u == "um" || u == "µm")
1083 : {
1084 2 : return UnitType::Metric;
1085 : }
1086 54993 : if (u == "inch" || u == "inches" || u == "in" || u == "mil" || u == "mils")
1087 : {
1088 54993 : return UnitType::Imperial;
1089 : }
1090 0 : return UnitType::None;
1091 54995 : }
1092 : }
|