OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
EdaDataFile.cpp
Go to the documentation of this file.
1#include "EdaDataFile.h"
2#include <fstream>
3#include <sstream>
4#include "str_utils.h"
5#include "../../enums.h"
6#include "edadatafile.pb.h"
7#include "ArchiveExtractor.h"
8#include "Logger.h"
9#include "../parse_info.h"
10#include "../parse_error.h"
11#include "../invalid_odb_error.h"
12#include "../../Text/Utf8Sanitizer.h"
13
14using namespace std::filesystem;
15using namespace Utils;
16
18{
19 EdaDataFile::EdaDataFile(bool logAllLineParsing)
20 : m_logAllLineParsing(logAllLineParsing)
21 {
22 }
23
25 {
26 m_layerNames.clear();
27 m_attributeNames.clear();
28 m_attributeTextValues.clear();
29 m_netRecords.clear();
30 m_netRecordsByName.clear();
31 m_packageRecords.clear();
32 m_packageRecordsByName.clear();
33 m_featureGroupRecords.clear();
34 m_propertyRecords.clear();
35 }
36
37 const std::filesystem::path& EdaDataFile::GetPath() const
38 {
39 return m_path;
40 }
41
42 const std::filesystem::path& EdaDataFile::GetDirectory() const
43 {
44 return m_directory;
45 }
46
47 const std::string& EdaDataFile::GetUnits() const
48 {
49 return m_units;
50 }
51
52 const std::string& EdaDataFile::GetSource() const
53 {
54 return m_source;
55 }
56
61
62 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord> EdaDataFile::NetRecord::SubnetRecord::to_protobuf() const
63 {
64 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord> pSubnetRecordMessage(new Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord);
65 pSubnetRecordMessage->set_type((Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord::Type) type);
66 if (type == Type::Toeprint)
67 {
68 pSubnetRecordMessage->set_componentnumber(componentNumber);
69 pSubnetRecordMessage->set_toeprintnumber(toeprintNumber);
70 pSubnetRecordMessage->set_side((Odb::Lib::Protobuf::BoardSide)side);
71 }
72 else if (type == Type::Plane)
73 {
74 pSubnetRecordMessage->set_filltype((Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord::FillType)fillType);
75 pSubnetRecordMessage->set_cutouttype((Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord::CutoutType) cutoutType);
76 pSubnetRecordMessage->set_fillsize(fillSize);
77 }
78
79 for (const auto& featureIdRecord : m_featureIdRecords)
80 {
81 auto pFeatureIdRecordMessage = pSubnetRecordMessage->add_featureidrecords();
82 pFeatureIdRecordMessage->CopyFrom(*featureIdRecord->to_protobuf());
83 }
84 return pSubnetRecordMessage;
85 }
86
87 void EdaDataFile::NetRecord::SubnetRecord::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord& message)
88 {
89 type = static_cast<Type>(message.type());
90
91 if (type == Type::Toeprint)
92 {
93 componentNumber = message.componentnumber();
94 toeprintNumber = message.toeprintnumber();
95 side = static_cast<BoardSide>(message.side());
96 }
97 else if (type == Type::Plane)
98 {
99 fillType = static_cast<FillType>(message.filltype());
100 cutoutType = static_cast<CutoutType>(message.cutouttype());
101 fillSize = message.fillsize();
102 index = message.index();
103 }
104
105 for (const auto& featureIdRecordMessage : message.featureidrecords())
106 {
107 auto pFeatureIdRecord = std::make_shared<FeatureIdRecord>();
108 pFeatureIdRecord->from_protobuf(featureIdRecordMessage);
109 m_featureIdRecords.push_back(pFeatureIdRecord);
110 }
111 }
112
118
119 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::NetRecord> EdaDataFile::NetRecord::to_protobuf() const
120 {
121 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::NetRecord> pNetRecordMessage(new Odb::Lib::Protobuf::EdaDataFile::NetRecord);
122 pNetRecordMessage->set_name(Odb::Lib::Text::ToUtf8(name));
123 //pNetRecordMessage->set_attributesidstring(attributesIdString);
124 pNetRecordMessage->set_index(index);
125 if (id.has_value())
126 {
127 pNetRecordMessage->set_id(*id);
128 }
129
130 for (const auto& propertyRecord : m_propertyRecords)
131 {
132 auto pPropertyRecordMessage = pNetRecordMessage->add_propertyrecords();
133 pPropertyRecordMessage->CopyFrom(*propertyRecord->to_protobuf());
134 }
135
136 for (const auto& subnetRecord : m_subnetRecords)
137 {
138 auto pSubnetRecordMessage = pNetRecordMessage->add_subnetrecords();
139 pSubnetRecordMessage->CopyFrom(*subnetRecord->to_protobuf());
140 }
141 for (const auto& kvAttributeAssignment : m_attributeLookupTable)
142 {
143 (*pNetRecordMessage->mutable_attributelookuptable())[Odb::Lib::Text::ToUtf8(kvAttributeAssignment.first)] = Odb::Lib::Text::ToUtf8(kvAttributeAssignment.second);
144 }
145 return pNetRecordMessage;
146 }
147
148 void EdaDataFile::NetRecord::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::NetRecord& message)
149 {
150 name = message.name();
151 //attributesIdString = message.attributesidstring();
152 index = message.index();
153 if (message.has_id())
154 {
155 id = message.id();
156 }
157
158 for (const auto& propertyRecordMessage : message.propertyrecords())
159 {
160 auto pPropertyRecord = std::make_shared<PropertyRecord>();
161 pPropertyRecord->from_protobuf(propertyRecordMessage);
162 m_propertyRecords.push_back(pPropertyRecord);
163 }
164
165 for (const auto& subnetRecordMessage : message.subnetrecords())
166 {
167 auto pSubnetRecord = std::make_shared<SubnetRecord>();
168 pSubnetRecord->from_protobuf(subnetRecordMessage);
169 m_subnetRecords.push_back(pSubnetRecord);
170 }
171
172 for (const auto& kvAttributeAssignment : message.attributelookuptable())
173 {
174 m_attributeLookupTable[kvAttributeAssignment.first] = kvAttributeAssignment.second;
175 }
176 }
177
178 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::FeatureIdRecord>
180 {
181 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::FeatureIdRecord> pFeatureIdRecordMessage(new Odb::Lib::Protobuf::EdaDataFile::FeatureIdRecord);
182 pFeatureIdRecordMessage->set_type((Odb::Lib::Protobuf::EdaDataFile::FeatureIdRecord::Type) type);
183 pFeatureIdRecordMessage->set_layernumber(layerNumber);
184 pFeatureIdRecordMessage->set_featurenumber(featureNumber);
185 return pFeatureIdRecordMessage;
186 }
187
188 void EdaDataFile::FeatureIdRecord::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::FeatureIdRecord& message)
189 {
190 type = static_cast<Type>(message.type());
191 layerNumber = message.layernumber();
192 featureNumber = message.featurenumber();
193 }
194
195 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::FeatureGroupRecord>
197 {
198 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::FeatureGroupRecord> pFeatureGroupRecordMessage(new Odb::Lib::Protobuf::EdaDataFile::FeatureGroupRecord);
199 pFeatureGroupRecordMessage->set_type(Odb::Lib::Text::ToUtf8(type));
200 for (const auto& featureIdRecord : m_featureIdRecords)
201 {
202 auto pFeatureIdRecordMessage = pFeatureGroupRecordMessage->add_featureidrecords();
203 pFeatureIdRecordMessage->CopyFrom(*featureIdRecord->to_protobuf());
204 }
205 for (const auto& propertyRecord : m_propertyRecords)
206 {
207 // PropertyRecord payloads belong in propertyrecords (field 2); the
208 // previous add_featureidrecords() here was a cross-type
209 // Message::CopyFrom (type confusion / UB) whenever a design carries
210 // feature-group property records.
211 auto pPropertyRecordMessage = pFeatureGroupRecordMessage->add_propertyrecords();
212 pPropertyRecordMessage->CopyFrom(*propertyRecord->to_protobuf());
213 }
214 return pFeatureGroupRecordMessage;
215 }
216
217 void EdaDataFile::FeatureGroupRecord::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::FeatureGroupRecord& message)
218 {
219 type = message.type();
220 for (const auto& featureIdRecordMessage : message.featureidrecords())
221 {
222 auto pFeatureIdRecord = std::make_shared<FeatureIdRecord>();
223 pFeatureIdRecord->from_protobuf(featureIdRecordMessage);
224 m_featureIdRecords.push_back(pFeatureIdRecord);
225 }
226 for (const auto& propertyRecordMessage : message.propertyrecords())
227 {
228 auto pPropertyRecord = std::make_shared<PropertyRecord>();
229 pPropertyRecord->from_protobuf(propertyRecordMessage);
230 m_propertyRecords.push_back(pPropertyRecord);
231 }
232 }
233
234 const std::vector<std::string>& EdaDataFile::GetLayerNames() const
235 {
236 return m_layerNames;
237 }
238
239 const std::vector<std::string>& EdaDataFile::GetAttributeNames() const
240 {
241 return m_attributeNames;
242 }
243
244 const std::vector<std::string>& EdaDataFile::GetAttributeTextValues() const
245 {
246 return m_attributeTextValues;
247 }
248
250 {
251 return m_netRecords;
252 }
253
255 {
256 return m_netRecordsByName;
257 }
258
260 {
261 return m_packageRecords;
262 }
263
265 {
266 return m_packageRecordsByName;
267 }
268
270 {
271 return m_featureGroupRecords;
272 }
273
278
279 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile> EdaDataFile::to_protobuf() const
280 {
281 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile> pEdaDataFileMessage(new Odb::Lib::Protobuf::EdaDataFile);
282 pEdaDataFileMessage->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
283 pEdaDataFileMessage->set_units(Odb::Lib::Text::ToUtf8(m_units));
284 pEdaDataFileMessage->set_source(Odb::Lib::Text::ToUtf8(m_source));
285 for (const auto& layerName : m_layerNames)
286 {
287 pEdaDataFileMessage->add_layernames(Odb::Lib::Text::ToUtf8(layerName));
288 }
289 for (const auto& attributeName : m_attributeNames)
290 {
291 pEdaDataFileMessage->add_attributenames(Odb::Lib::Text::ToUtf8(attributeName));
292 }
293 for (const auto& attrTextValue : m_attributeTextValues)
294 {
295 pEdaDataFileMessage->add_attributetextvalues(Odb::Lib::Text::ToUtf8(attrTextValue));
296 }
297 for (const auto& pNetRecord : m_netRecords)
298 {
299 auto pNetRecordMessage = pEdaDataFileMessage->add_netrecords();
300 pNetRecordMessage->CopyFrom(*pNetRecord->to_protobuf());
301 }
302 for (const auto& kvNetRecord : m_netRecordsByName)
303 {
304 (*pEdaDataFileMessage->mutable_netrecordsbyname())[Odb::Lib::Text::ToUtf8(kvNetRecord.first)] = *kvNetRecord.second->to_protobuf();
305 }
306 for (const auto& pPackageRecord : m_packageRecords)
307 {
308 auto pPackageRecordMessage = pEdaDataFileMessage->add_packagerecords();
309 pPackageRecordMessage->CopyFrom(*pPackageRecord->to_protobuf());
310 }
311 for (const auto& kvPackageRecord : m_packageRecordsByName)
312 {
313 (*pEdaDataFileMessage->mutable_packagerecordsbyname())[Odb::Lib::Text::ToUtf8(kvPackageRecord.first)] = *kvPackageRecord.second->to_protobuf();
314 }
315 for (const auto& pPropertyRecord : m_propertyRecords)
316 {
317 auto pPropertyRecordMessage = pEdaDataFileMessage->add_propertyrecords();
318 pPropertyRecordMessage->CopyFrom(*pPropertyRecord->to_protobuf());
319 }
320 for (const auto& pFeatureGroupRecord : m_featureGroupRecords)
321 {
322 auto pFeatureGroupRecordMessage = pEdaDataFileMessage->add_featuregrouprecords();
323 pFeatureGroupRecordMessage->CopyFrom(*pFeatureGroupRecord->to_protobuf());
324 }
325
326 return pEdaDataFileMessage;
327 }
328
329 void EdaDataFile::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile& message)
330 {
331 m_path = message.path();
332 m_units = message.units();
333 m_source = message.source();
334 for (const auto& layerName : message.layernames())
335 {
336 m_layerNames.push_back(layerName);
337 }
338 for (const auto& attributeName : message.attributenames())
339 {
340 m_attributeNames.push_back(attributeName);
341 }
342 for (const auto& attrTextValue : message.attributetextvalues())
343 {
344 m_attributeTextValues.push_back(attrTextValue);
345 }
346 for (const auto& netRecordMessage : message.netrecords())
347 {
348 auto pNetRecord = std::make_shared<NetRecord>();
349 pNetRecord->from_protobuf(netRecordMessage);
350 m_netRecords.push_back(pNetRecord);
351 }
352 for (const auto& kvNetRecordMessage : message.netrecordsbyname())
353 {
354 auto pNetRecord = std::make_shared<NetRecord>();
355 pNetRecord->from_protobuf(kvNetRecordMessage.second);
356 m_netRecordsByName[kvNetRecordMessage.first] = pNetRecord;
357 }
358 for (const auto& packageRecordMessage : message.packagerecords())
359 {
360 auto pPackageRecord = std::make_shared<PackageRecord>();
361 pPackageRecord->from_protobuf(packageRecordMessage);
362 m_packageRecords.push_back(pPackageRecord);
363 }
364 for (const auto& kvPackageRecordMessage : message.packagerecordsbyname())
365 {
366 auto pPackageRecord = std::make_shared<PackageRecord>();
367 pPackageRecord->from_protobuf(kvPackageRecordMessage.second);
368 m_packageRecordsByName[kvPackageRecordMessage.first] = pPackageRecord;
369 }
370 for (const auto& propertyRecordMessage : message.propertyrecords())
371 {
372 auto pPropertyRecord = std::make_shared<PropertyRecord>();
373 pPropertyRecord->from_protobuf(propertyRecordMessage);
374 m_propertyRecords.push_back(pPropertyRecord);
375 }
376 for (const auto& featureGroupRecordMessage : message.featuregrouprecords())
377 {
378 auto pFeatureGroupRecord = std::make_shared<FeatureGroupRecord>();
379 pFeatureGroupRecord->from_protobuf(featureGroupRecordMessage);
380 m_featureGroupRecords.push_back(pFeatureGroupRecord);
381 }
382 }
383
384 bool EdaDataFile::Save(std::ostream& os)
385 {
386 return true;
387 }
388
389 bool EdaDataFile::Parse(std::filesystem::path path)
390 {
391 std::ifstream edaDataFile;
392 int lineNumber = 0;
393 std::string line;
394
395 try
396 {
397 loginfo("checking for extraction...");
398
399 m_directory = path;
400 m_path = ArchiveExtractor::getUncompressedFilePath(m_directory.string(), EDADATA_FILENAME);
401
402 if (!std::filesystem::exists(m_path))
403 {
404 auto message = "eda/data file does not exist: [" + m_path.string() + "]";
405 throw invalid_odb_error(message.c_str());
406 }
407 else if (!std::filesystem::is_regular_file(m_path))
408 {
409 auto message = "eda/data is not a file: [" + m_path.string() + "]";
410 throw invalid_odb_error(message.c_str());
411 }
412
413 loginfo("any extraction complete, parsing data...");
414
415 edaDataFile.open(m_path.string(), std::ios::in);
416 if (!edaDataFile.is_open())
417 {
418 auto message = "unable to open eda/data file: [" + m_path.string() + "]";
419 throw invalid_odb_error(message.c_str());
420 }
421
422 std::shared_ptr<NetRecord> pCurrentNetRecord;
423 std::shared_ptr<NetRecord::SubnetRecord> pCurrentSubnetRecord;
424
425 std::shared_ptr<PackageRecord> pCurrentPackageRecord;
426 std::shared_ptr<PackageRecord::PinRecord> pCurrentPinRecord;
427
428 std::shared_ptr<PackageRecord::OutlineRecord> pCurrentContourOutlineRecord;
429 std::shared_ptr<ContourPolygon> pCurrentContourPolygon;
430
431 FeatureGroupRecord::shared_ptr pCurrentFeatureGroupRecord;
432
433 while (std::getline(edaDataFile, line))
434 {
435 // keep track of line number
436 lineNumber++;
437
438 // trim whitespace from beginning and end of line
439 Utils::str_trim(line);
440 if (!line.empty())
441 {
442 if (m_logAllLineParsing)
443 {
444 parse_info pi(m_path, line, lineNumber);
445 logdebug(pi.toString("Parsing line..."));
446 }
447
448 std::stringstream lineStream(line);
449
450 if (line.find(ATTRIBUTE_NAME_TOKEN) == 0 ||
451 line.find(std::string(COMMENT_TOKEN) + ATTRIBUTE_NAME_TOKEN) == 0) // backward compatibility dictates allowing comment character in front of attribute value token
452 {
453 // component attribute name line
454 std::string token;
455 // TODO: continue on failing line parse, to make a less strict/more robust parser (make a flag: enum ParseStrictness { strict, lax })
456 if (!std::getline(lineStream, token, ' '))
457 {
458 throw_parse_error(m_path, line, token, lineNumber);
459 }
460 else if (!std::getline(lineStream, token, ' '))
461 {
462 throw_parse_error(m_path, line, token, lineNumber);
463 }
464 m_attributeNames.push_back(token);
465 }
466 else if (line.find(ATTRIBUTE_VALUE_TOKEN) == 0 ||
467 line.find(std::string(COMMENT_TOKEN) + ATTRIBUTE_VALUE_TOKEN) == 0) // backward compatibility dictates allowing comment character in front of attribute value token
468 {
469 // component attribute text string values
470 std::string token;
471 if (!std::getline(lineStream, token, ' '))
472 {
473 throw_parse_error(m_path, line, token, lineNumber);
474 }
475 else if (!std::getline(lineStream, token, ' '))
476 {
477 throw_parse_error(m_path, line, token, lineNumber);
478 }
479 m_attributeTextValues.push_back(token);
480 }
481 else if (line.find(COMMENT_TOKEN) == 0)
482 {
483 // comment line
484 // TODO: attribute lines can begin with a comment for backward compatbility
485 }
486 else if (line.find(UNITS_TOKEN) == 0)
487 {
488 // units line
489 std::string token;
490 if (!std::getline(lineStream, token, '='))
491 {
492 throw_parse_error(m_path, line, token, lineNumber);
493 }
494 else if (!std::getline(lineStream, token, '='))
495 {
496 throw_parse_error(m_path, line, token, lineNumber);
497 }
498 m_units = token;
499 }
500 else if (line.find(HEADER_RECORD_TOKEN) == 0)
501 {
502 // component record line
503 std::string token;
504
505 lineStream >> token;
506 if (token != HEADER_RECORD_TOKEN)
507 {
508 throw_parse_error(m_path, line, token, lineNumber);
509 }
510
511 // read the rest of the line as the source
512 if (!std::getline(lineStream, m_source))
513 {
514 throw_parse_error(m_path, line, token, lineNumber);
515 }
516
517 Utils::str_trim(m_source);
518 }
519 else if (line.find(LAYER_NAMES_RECORD_TOKEN) == 0)
520 {
521 // component record line
522 std::string token;
523
524 lineStream >> token;
525 if (token != LAYER_NAMES_RECORD_TOKEN)
526 {
527 throw_parse_error(m_path, line, token, lineNumber);
528 }
529
530 while (lineStream >> token)
531 {
532 m_layerNames.push_back(token);
533 }
534 }
535 else if (line.find(PropertyRecord::RECORD_TOKEN) == 0)
536 {
537 // component property record line
538 std::string token;
539 if (!(lineStream >> token))
540 {
541 throw_parse_error(m_path, line, token, lineNumber);
542 }
543
544 if (token != PropertyRecord::RECORD_TOKEN)
545 {
546 throw_parse_error(m_path, line, token, lineNumber);
547 }
548
549 auto pPropertyRecord = std::make_shared<PropertyRecord>();
550
551 if (!(lineStream >> pPropertyRecord->name))
552 {
553 throw_parse_error(m_path, line, token, lineNumber);
554 }
555
556 if (!(lineStream >> token))
557 {
558 throw_parse_error(m_path, line, token, lineNumber);
559 }
560
561 if (!token.empty())
562 {
563 // handle case where the beginning of the value, after the opening single-quote, is whitespace...
564 if (token == "'")
565 {
566 // eat the single-quote and get the next token, i.e. the actual value (w/ space in front)
567 if (!(lineStream >> token))
568 {
569 throw_parse_error(m_path, line, token, lineNumber);
570 }
571 }
572
573 if (token.size() > 0 && token[0] == '\'')
574 {
575 // remove leading quote
576 token.erase(0, 1);
577 }
578
579 if (token.size() > 0 && token[token.size() - 1] == '\'')
580 {
581 // remove trailing quote
582 token.erase(token.size() - 1);
583 }
584
585 Utils::str_trim(token);
586 }
587
588 pPropertyRecord->value = token;
589
590 double f;
591 while (lineStream >> f)
592 {
593 pPropertyRecord->floatValues.push_back(f);
594 }
595
596 if (pCurrentNetRecord != nullptr)
597 {
598 pCurrentNetRecord->m_propertyRecords.push_back(pPropertyRecord);
599 }
600 else if (pCurrentPackageRecord != nullptr)
601 {
602 pCurrentPackageRecord->m_propertyRecords.push_back(pPropertyRecord);
603 }
604 else if (pCurrentFeatureGroupRecord != nullptr)
605 {
606 pCurrentFeatureGroupRecord->m_propertyRecords.push_back(pPropertyRecord);
607 }
608 else
609 {
610 // top-level PRP record
611 m_propertyRecords.push_back(pPropertyRecord);
612 }
613 }
614 else if (line.find(NET_RECORD_TOKEN) == 0)
615 {
616 // net record line
617 std::string token;
618
619 if (!(lineStream >> token))
620 {
621 throw_parse_error(m_path, line, token, lineNumber);
622 }
623
624 if (token != NET_RECORD_TOKEN)
625 {
626 throw_parse_error(m_path, line, token, lineNumber);
627 }
628
629 // finish current (previous) net record
630 if (pCurrentNetRecord != nullptr)
631 {
632 // finish up (any) current subnet record
633 if (pCurrentSubnetRecord != nullptr)
634 {
635 pCurrentNetRecord->m_subnetRecords.push_back(pCurrentSubnetRecord);
636 pCurrentSubnetRecord.reset();
637 }
638
639 m_netRecords.push_back(pCurrentNetRecord);
640 //m_netRecordsByName[pCurrentNetRecord->name] = pCurrentNetRecord;
641 pCurrentNetRecord.reset();
642 }
643
644 // net name
645 if (!std::getline(lineStream, token, ';'))
646 {
647 throw_parse_error(m_path, line, token, lineNumber);
648 }
649
650 // trim whitespace from beginning
651 str_trim(token);
652
653 // create new net record
654 pCurrentNetRecord = std::make_shared<NetRecord>();
655 pCurrentNetRecord->name = token;
656 pCurrentNetRecord->index = static_cast<unsigned>(m_netRecords.size());
657
658 std::string attrIdString;
659 lineStream >> attrIdString;
660 if (!pCurrentNetRecord->ParseAttributeLookupTable(attrIdString, pCurrentNetRecord->id))
661 {
662 throw_parse_error(m_path, line, token, lineNumber);
663 }
664 }
665 else if (line.find(NetRecord::SubnetRecord::RECORD_TOKEN) == 0)
666 {
667 // component record line
668 std::string token;
669
670 lineStream >> token;
672 {
673 throw_parse_error(m_path, line, token, lineNumber);
674 }
675
676 if (pCurrentNetRecord != nullptr)
677 {
678 // finish current (previous) subnet record
679 if (pCurrentSubnetRecord != nullptr)
680 {
681 pCurrentNetRecord->m_subnetRecords.push_back(pCurrentSubnetRecord);
682 pCurrentSubnetRecord.reset();
683 }
684 }
685
686 pCurrentSubnetRecord = std::make_shared<NetRecord::SubnetRecord>();
687 pCurrentSubnetRecord->index = static_cast<unsigned>(pCurrentNetRecord->m_subnetRecords.size());
688
689 // subnet type
690 lineStream >> token;
692 {
693 pCurrentSubnetRecord->type = NetRecord::SubnetRecord::Type::Via;
694 }
696 {
697 pCurrentSubnetRecord->type = NetRecord::SubnetRecord::Type::Trace;
698 }
700 {
701 pCurrentSubnetRecord->type = NetRecord::SubnetRecord::Type::Plane;
702
703 // fill type
704 lineStream >> token;
705 if (token == "S")
706 {
707 pCurrentSubnetRecord->fillType = NetRecord::SubnetRecord::FillType::Solid;
708 }
709 else if (token == "O")
710 {
711 pCurrentSubnetRecord->fillType = NetRecord::SubnetRecord::FillType::Outline;
712 }
713 else
714 {
715 throw_parse_error(m_path, line, token, lineNumber);
716 }
717
718 // cutout type
719 lineStream >> token;
720 if (token == "C")
721 {
722 pCurrentSubnetRecord->cutoutType = NetRecord::SubnetRecord::CutoutType::Circle;
723 }
724 else if (token == "R")
725 {
726 pCurrentSubnetRecord->cutoutType = NetRecord::SubnetRecord::CutoutType::Rectangle;
727 }
728 else if (token == "O")
729 {
730 pCurrentSubnetRecord->cutoutType = NetRecord::SubnetRecord::CutoutType::Octagon;
731 }
732 else if (token == "E")
733 {
734 pCurrentSubnetRecord->cutoutType = NetRecord::SubnetRecord::CutoutType::Exact;
735 }
736 else
737 {
738 throw_parse_error(m_path, line, token, lineNumber);
739 }
740
741 // fill size
742 lineStream >> pCurrentSubnetRecord->fillSize;
743 }
745 {
746 pCurrentSubnetRecord->type = NetRecord::SubnetRecord::Type::Toeprint;
747
748 // side
749 lineStream >> token;
750 if (token == "T")
751 {
752 pCurrentSubnetRecord->side = BoardSide::Top;
753 }
754 else if (token == "B")
755 {
756 pCurrentSubnetRecord->side = BoardSide::Bottom;
757 }
758 else
759 {
760 throw_parse_error(m_path, line, token, lineNumber);
761 }
762
763 // componentNumber
764 lineStream >> pCurrentSubnetRecord->componentNumber;
765
766 // toeprintNumber
767 lineStream >> pCurrentSubnetRecord->toeprintNumber;
768 }
769 else
770 {
771 throw_parse_error(m_path, line, token, lineNumber);
772 }
773 }
774 else if (line.find(FEATURE_ID_RECORD_TOKEN) == 0)
775 {
776 // component record line
777 std::string token;
778
779 if (!(lineStream >> token) || token != FEATURE_ID_RECORD_TOKEN)
780 {
781 throw_parse_error(m_path, line, token, lineNumber);
782 }
783
784 auto pFeatureIdRecord = std::make_shared<FeatureIdRecord>();
785
786 // type
787 if (!(lineStream >> token))
788 {
789 throw_parse_error(m_path, line, token, lineNumber);
790 }
791
792 if (token == "C")
793 {
794 pFeatureIdRecord->type = FeatureIdRecord::Type::Copper;
795 }
796 else if (token == "L")
797 {
798 pFeatureIdRecord->type = FeatureIdRecord::Type::Laminate;
799 }
800 else if (token == "H")
801 {
802 pFeatureIdRecord->type = FeatureIdRecord::Type::Hole;
803 }
804 else
805 {
806 throw_parse_error(m_path, line, token, lineNumber);
807 }
808
809 // layer number
810 if (!(lineStream >> pFeatureIdRecord->layerNumber))
811 {
812 throw_parse_error(m_path, line, token, lineNumber);
813 }
814
815 // feature number
816 if (!(lineStream >> pFeatureIdRecord->featureNumber))
817 {
818 throw_parse_error(m_path, line, token, lineNumber);
819 }
820
821 if (pCurrentNetRecord != nullptr &&
822 pCurrentSubnetRecord != nullptr)
823 {
824 pCurrentSubnetRecord->m_featureIdRecords.push_back(pFeatureIdRecord);
825 }
826 else if (pCurrentFeatureGroupRecord != nullptr)
827 {
828 pCurrentFeatureGroupRecord->m_featureIdRecords.push_back(pFeatureIdRecord);
829 }
830 else
831 {
832 throw_parse_error(m_path, line, token, lineNumber);
833 }
834 }
835 else if (line.find(PACKAGE_RECORD_TOKEN) == 0)
836 {
837 // package record line
838 std::string token;
839
840 if (!(lineStream >> token) || token != PACKAGE_RECORD_TOKEN)
841 {
842 throw_parse_error(m_path, line, token, lineNumber);
843 }
844
845 // finish current (previous) net record
846 if (pCurrentNetRecord != nullptr)
847 {
848 // finish up (any) current subnet record
849 if (pCurrentSubnetRecord != nullptr)
850 {
851 pCurrentNetRecord->m_subnetRecords.push_back(pCurrentSubnetRecord);
852 pCurrentSubnetRecord.reset();
853 }
854
855 m_netRecords.push_back(pCurrentNetRecord);
856 //m_netRecordsByName[pCurrentNetRecord->name] = pCurrentNetRecord;
857 pCurrentNetRecord.reset();
858 }
859 else if (pCurrentPackageRecord != nullptr)
860 {
861 // finish up any current (previous) pin records
862 if (pCurrentPinRecord != nullptr)
863 {
864 // check for overflow
865 if (pCurrentPackageRecord->m_pinRecords.size() > UINT_MAX)
866 {
867 throw_parse_error(m_path, line, token, lineNumber);
868 }
869 pCurrentPinRecord->index = static_cast<unsigned>(pCurrentPackageRecord->m_pinRecords.size());
870 pCurrentPackageRecord->m_pinRecords.push_back(pCurrentPinRecord);
871 pCurrentPinRecord.reset();
872 }
873
874 // finish up any current (previous) package records
875 m_packageRecords.push_back(pCurrentPackageRecord);
876 //m_packageRecordsByName[pCurrentPackageRecord->name] = pCurrentPackageRecord;
877 pCurrentPackageRecord.reset();
878 }
879
880 pCurrentPackageRecord = std::make_shared<PackageRecord>();
881 pCurrentPackageRecord->index = static_cast<unsigned int>(m_packageRecords.size());
882
883 // name
884 if (!(lineStream >> pCurrentPackageRecord->name))
885 {
886 throw_parse_error(m_path, line, token, lineNumber);
887 }
888
889 // pitch
890 if (!(lineStream >> pCurrentPackageRecord->pitch))
891 {
892 throw_parse_error(m_path, line, token, lineNumber);
893 }
894
895 // xmin, ymin
896 if (!(lineStream >> pCurrentPackageRecord->xMin >> pCurrentPackageRecord->yMin))
897 {
898 throw_parse_error(m_path, line, token, lineNumber);
899 }
900
901 // xmax
902 if (!(lineStream >> pCurrentPackageRecord->xMax))
903 {
904 throw_parse_error(m_path, line, token, lineNumber);
905 }
906
907 // ymax and attributes/ID string
908 if (!std::getline(lineStream, token, ';'))
909 {
910 throw_parse_error(m_path, line, token, lineNumber);
911 }
912
913 // ymax
914 pCurrentPackageRecord->yMax = std::stof(token);
915
916 std::string attrIdString;
917 lineStream >> attrIdString;
918 if (!pCurrentPackageRecord->ParseAttributeLookupTable(attrIdString, pCurrentPackageRecord->id))
919 {
920 throw_parse_error(m_path, line, token, lineNumber);
921 }
922 }
923 else if (line.find(PIN_RECORD_TOKEN) == 0)
924 {
925 // package pin record line
926 std::string token;
927
928 if (!(lineStream >> token) || token != PIN_RECORD_TOKEN)
929 {
930 throw_parse_error(m_path, line, token, lineNumber);
931 }
932
933 if (pCurrentPackageRecord != nullptr)
934 {
935 // finish up any current (previous) pin records
936 if (pCurrentPinRecord != nullptr)
937 {
938 // check for overflow
939 if (pCurrentPackageRecord->m_pinRecords.size() > UINT_MAX)
940 {
941 throw_parse_error(m_path, line, token, lineNumber);
942 }
943 pCurrentPinRecord->index = static_cast<unsigned>(pCurrentPackageRecord->m_pinRecords.size());
944 pCurrentPackageRecord->m_pinRecords.push_back(pCurrentPinRecord);
945 pCurrentPinRecord.reset();
946 }
947 }
948
949 pCurrentPinRecord = std::make_shared<PackageRecord::PinRecord>();
950 //pCurrentPinRecord->index = static_cast<unsigned>(pCurrentPackageRecord->m_pinRecords.size());
951
952 // name
953 lineStream >> pCurrentPinRecord->name;
954
955 // type
956 if (!(lineStream >> token))
957 {
958 throw_parse_error(m_path, line, token, lineNumber);
959 }
960
961 if (token == "T")
962 {
963 pCurrentPinRecord->type = PackageRecord::PinRecord::Type::ThroughHole;
964 }
965 else if (token == "B")
966 {
967 pCurrentPinRecord->type = PackageRecord::PinRecord::Type::Blind;
968 }
969 else if (token == "S")
970 {
971 pCurrentPinRecord->type = PackageRecord::PinRecord::Type::Surface;
972 }
973 else
974 {
975 logerror("unknown pin type: [" + token + "]");
976 throw_parse_error(m_path, line, token, lineNumber);
977 }
978
979 // xc, xy
980 if (!(lineStream >> pCurrentPinRecord->xCenter >> pCurrentPinRecord->yCenter))
981 {
982 throw_parse_error(m_path, line, token, lineNumber);
983 }
984
985 // finished hole size (fhs)
986 if (!(lineStream >> pCurrentPinRecord->finishedHoleSize))
987 {
988 throw_parse_error(m_path, line, token, lineNumber);
989 }
990
991 // electrical type
992 if (!(lineStream >> token))
993 {
994 throw_parse_error(m_path, line, token, lineNumber);
995 }
996
997 if (token == "E")
998 {
999 pCurrentPinRecord->electricalType = PackageRecord::PinRecord::ElectricalType::Electrical;
1000 }
1001 else if (token == "M")
1002 {
1003 pCurrentPinRecord->electricalType = PackageRecord::PinRecord::ElectricalType::NonElectrical;
1004 }
1005 else if (token == "U")
1006 {
1007 pCurrentPinRecord->electricalType = PackageRecord::PinRecord::ElectricalType::Undefined;
1008 }
1009 else
1010 {
1011 logerror("unknown pin electrical type: [" + token + "]");
1012 throw_parse_error(m_path, line, token, lineNumber);
1013 }
1014
1015 // mount type
1016 if (!(lineStream >> token))
1017 {
1018 throw_parse_error(m_path, line, token, lineNumber);
1019 }
1020
1021 if (token == "S")
1022 {
1023 pCurrentPinRecord->mountType = PackageRecord::PinRecord::MountType::Smt;
1024 }
1025 else if (token == "D")
1026 {
1027 pCurrentPinRecord->mountType = PackageRecord::PinRecord::MountType::RecommendedSmtPad;
1028 }
1029 else if (token == "T")
1030 {
1031 pCurrentPinRecord->mountType = PackageRecord::PinRecord::MountType::MT_ThroughHole;
1032 }
1033 else if (token == "R")
1034 {
1036 }
1037 else if (token == "P")
1038 {
1039 pCurrentPinRecord->mountType = PackageRecord::PinRecord::MountType::Pressfit;
1040 }
1041 else if (token == "N")
1042 {
1043 pCurrentPinRecord->mountType = PackageRecord::PinRecord::MountType::NonBoard;
1044 }
1045 else if (token == "H")
1046 {
1047 pCurrentPinRecord->mountType = PackageRecord::PinRecord::MountType::Hole;
1048 }
1049 else if (token == "U")
1050 {
1051 pCurrentPinRecord->mountType = PackageRecord::PinRecord::MountType::MT_Undefined;
1052 }
1053 else
1054 {
1055 logerror("unknown pin mount type: [" + token + "]");
1056 throw_parse_error(m_path, line, token, lineNumber);
1057 }
1058
1059 // ID=<id>
1060 // PIN record, ID field is optional: spec pg.31
1061 if (lineStream >> token)
1062 {
1063 std::stringstream idStream(token);
1064 if (!std::getline(idStream, token, '=') || token != "ID")
1065 {
1066 throw_parse_error(m_path, line, token, lineNumber);
1067 }
1068
1069 idStream >> pCurrentPinRecord->id;
1070 }
1071 else
1072 {
1073
1074#define SIMULATE_PARSE_ERROR 0
1075#if SIMULATE_PARSE_ERROR
1076 throw_parse_error(m_path, line, token, lineNumber);
1077#endif
1078
1079 pCurrentPinRecord->id = UINT_MAX;
1080 }
1081 }
1082 else if (line.find(FEATURE_GROUP_RECORD_TOKEN) == 0)
1083 {
1084 // feature group record line
1085 std::string token;
1086
1087 if (!(lineStream >> token))
1088 {
1089 throw_parse_error(m_path, line, token, lineNumber);
1090 }
1091
1092 if (token != FEATURE_GROUP_RECORD_TOKEN)
1093 {
1094 throw_parse_error(m_path, line, token, lineNumber);
1095 }
1096
1097 // finish current (previous) net record
1098 if (pCurrentNetRecord != nullptr)
1099 {
1100 // finish up (any) current subnet record
1101 if (pCurrentSubnetRecord != nullptr)
1102 {
1103 pCurrentNetRecord->m_subnetRecords.push_back(pCurrentSubnetRecord);
1104 pCurrentSubnetRecord.reset();
1105 }
1106
1107 m_netRecords.push_back(pCurrentNetRecord);
1108 //m_netRecordsByName[pCurrentNetRecord->name] = pCurrentNetRecord;
1109 pCurrentNetRecord.reset();
1110 }
1111 else if (pCurrentPackageRecord != nullptr)
1112 {
1113 // finish up any current (previous) pin records
1114 if (pCurrentPinRecord != nullptr)
1115 {
1116 // check for overflow
1117 if (pCurrentPackageRecord->m_pinRecords.size() > UINT_MAX)
1118 {
1119 throw_parse_error(m_path, line, token, lineNumber);
1120 }
1121 pCurrentPinRecord->index = static_cast<unsigned>(pCurrentPackageRecord->m_pinRecords.size());
1122 pCurrentPackageRecord->m_pinRecords.push_back(pCurrentPinRecord);
1123 pCurrentPinRecord.reset();
1124 }
1125
1126 // finish up any current (previous) package records
1127 m_packageRecords.push_back(pCurrentPackageRecord);
1128 //m_packageRecordsByName[pCurrentPackageRecord->name] = pCurrentPackageRecord;
1129 pCurrentPackageRecord.reset();
1130 }
1131 else if (pCurrentFeatureGroupRecord != nullptr)
1132 {
1133 m_featureGroupRecords.push_back(pCurrentFeatureGroupRecord);
1134 pCurrentFeatureGroupRecord.reset();
1135 }
1136
1137 pCurrentFeatureGroupRecord = std::make_shared<FeatureGroupRecord>();
1138
1139 if (!(lineStream >> pCurrentFeatureGroupRecord->type))
1140 {
1141 throw_parse_error(m_path, line, token, lineNumber);
1142 }
1143 }
1145 {
1146 std::string token;
1147 if (!(lineStream >> token))
1148 {
1149 throw_parse_error(m_path, line, token, lineNumber);
1150 }
1151
1153 {
1154 throw_parse_error(m_path, line, token, lineNumber);
1155 }
1156
1157 auto pOutlineRecord = std::make_shared<PackageRecord::OutlineRecord>();
1158 pOutlineRecord->type = PackageRecord::OutlineRecord::Type::Rectangle;
1159
1160 if (!(lineStream >> pOutlineRecord->lowerLeftX))
1161 {
1162 throw_parse_error(m_path, line, token, lineNumber);
1163 }
1164
1165 if (!(lineStream >> pOutlineRecord->lowerLeftY))
1166 {
1167 throw_parse_error(m_path, line, token, lineNumber);
1168 }
1169
1170 if (!(lineStream >> pOutlineRecord->width))
1171 {
1172 throw_parse_error(m_path, line, token, lineNumber);
1173 }
1174
1175 if (!(lineStream >> pOutlineRecord->height))
1176 {
1177 throw_parse_error(m_path, line, token, lineNumber);
1178 }
1179
1180 if (pCurrentPackageRecord != nullptr)
1181 {
1182 if (pCurrentPinRecord != nullptr)
1183 {
1184 pCurrentPinRecord->m_outlineRecords.push_back(pOutlineRecord);
1185 }
1186 else
1187 {
1188 pCurrentPackageRecord->m_outlineRecords.push_back(pOutlineRecord);
1189 }
1190 }
1191 else
1192 {
1193 throw_parse_error(m_path, line, token, lineNumber);
1194 }
1195 }
1197 {
1198 std::string token;
1199 if (!(lineStream >> token))
1200 {
1201 throw_parse_error(m_path, line, token, lineNumber);
1202 }
1203
1205 {
1206 throw_parse_error(m_path, line, token, lineNumber);
1207 }
1208
1209 auto pOutlineRecord = std::make_shared<PackageRecord::OutlineRecord>();
1210 pOutlineRecord->type = PackageRecord::OutlineRecord::Type::Circle;
1211
1212 if (!(lineStream >> pOutlineRecord->xCenter))
1213 {
1214 throw_parse_error(m_path, line, token, lineNumber);
1215 }
1216
1217 if (!(lineStream >> pOutlineRecord->yCenter))
1218 {
1219 throw_parse_error(m_path, line, token, lineNumber);
1220 }
1221
1222 if (!(lineStream >> pOutlineRecord->radius))
1223 {
1224 throw_parse_error(m_path, line, token, lineNumber);
1225 }
1226
1227 if (pCurrentPackageRecord != nullptr)
1228 {
1229 if (pCurrentPinRecord != nullptr)
1230 {
1231 pCurrentPinRecord->m_outlineRecords.push_back(pOutlineRecord);
1232 }
1233 else
1234 {
1235 pCurrentPackageRecord->m_outlineRecords.push_back(pOutlineRecord);
1236 }
1237 }
1238 else
1239 {
1240 throw_parse_error(m_path, line, token, lineNumber);
1241 }
1242 }
1244 {
1245 std::string token;
1246 if (!(lineStream >> token))
1247 {
1248 throw_parse_error(m_path, line, token, lineNumber);
1249 }
1250
1252 {
1253 throw_parse_error(m_path, line, token, lineNumber);
1254 }
1255
1256 auto pOutlineRecord = std::make_shared<PackageRecord::OutlineRecord>();
1257 pOutlineRecord->type = PackageRecord::OutlineRecord::Type::Square;
1258
1259 if (!(lineStream >> pOutlineRecord->xCenter))
1260 {
1261 throw_parse_error(m_path, line, token, lineNumber);
1262 }
1263
1264 if (!(lineStream >> pOutlineRecord->yCenter))
1265 {
1266 throw_parse_error(m_path, line, token, lineNumber);
1267 }
1268
1269 if (!(lineStream >> pOutlineRecord->halfSide))
1270 {
1271 throw_parse_error(m_path, line, token, lineNumber);
1272 }
1273
1274 if (pCurrentPackageRecord != nullptr)
1275 {
1276 if (pCurrentPinRecord != nullptr)
1277 {
1278 pCurrentPinRecord->m_outlineRecords.push_back(pOutlineRecord);
1279 }
1280 else
1281 {
1282 pCurrentPackageRecord->m_outlineRecords.push_back(pOutlineRecord);
1283 }
1284 }
1285 else
1286 {
1287 throw_parse_error(m_path, line, token, lineNumber);
1288 }
1289 }
1291 {
1292 std::string token;
1293 if (!(lineStream >> token))
1294 {
1295 throw_parse_error(m_path, line, token, lineNumber);
1296 }
1297
1299 {
1300 throw_parse_error(m_path, line, token, lineNumber);
1301 }
1302
1303 pCurrentContourOutlineRecord = std::make_shared<PackageRecord::OutlineRecord>();
1304 pCurrentContourOutlineRecord->type = PackageRecord::OutlineRecord::Type::Contour;
1305 }
1307 {
1308 std::string token;
1309 if (!(lineStream >> token))
1310 {
1311 throw_parse_error(m_path, line, token, lineNumber);
1312 }
1313
1315 {
1316 throw_parse_error(m_path, line, token, lineNumber);
1317 }
1318
1319 if (pCurrentPackageRecord != nullptr)
1320 {
1321 if (pCurrentPinRecord != nullptr)
1322 {
1323 pCurrentPinRecord->m_outlineRecords.push_back(pCurrentContourOutlineRecord);
1324 }
1325 else
1326 {
1327 pCurrentPackageRecord->m_outlineRecords.push_back(pCurrentContourOutlineRecord);
1328 }
1329 }
1330 else
1331 {
1332 throw_parse_error(m_path, line, token, lineNumber);
1333 }
1334
1335 pCurrentContourOutlineRecord.reset();
1336 }
1337 else if (line.find(ContourPolygon::BEGIN_RECORD_TOKEN) == 0)
1338 {
1339 std::string token;
1340 if (!(lineStream >> token))
1341 {
1342 throw_parse_error(m_path, line, token, lineNumber);
1343 }
1344
1346 {
1347 throw_parse_error(m_path, line, token, lineNumber);
1348 }
1349
1350 pCurrentContourPolygon = std::make_shared<ContourPolygon>();
1351
1352 if (!(lineStream >> pCurrentContourPolygon->xStart))
1353 {
1354 throw_parse_error(m_path, line, token, lineNumber);
1355 }
1356
1357 if (!(lineStream >> pCurrentContourPolygon->yStart))
1358 {
1359 throw_parse_error(m_path, line, token, lineNumber);
1360 }
1361
1362 if (!(lineStream >> token))
1363 {
1364 throw_parse_error(m_path, line, token, lineNumber);
1365 }
1366
1368 {
1369 pCurrentContourPolygon->type = ContourPolygon::Type::Island;
1370 }
1371 else if (token == ContourPolygon::HOLE_TYPE_TOKEN)
1372 {
1373 pCurrentContourPolygon->type = ContourPolygon::Type::Hole;
1374 }
1375 else
1376 {
1377 throw_parse_error(m_path, line, token, lineNumber);
1378 }
1379 }
1380 else if (line.find(ContourPolygon::END_RECORD_TOKEN) == 0)
1381 {
1382 std::string token;
1383 if (!(lineStream >> token))
1384 {
1385 throw_parse_error(m_path, line, token, lineNumber);
1386 }
1387
1389 {
1390 throw_parse_error(m_path, line, token, lineNumber);
1391 }
1392
1393 if (pCurrentContourOutlineRecord != nullptr)
1394 {
1395 pCurrentContourOutlineRecord->m_contourPolygons.push_back(pCurrentContourPolygon);
1396 pCurrentContourPolygon.reset();
1397 }
1398 else
1399 {
1400 throw_parse_error(m_path, line, token, lineNumber);
1401 }
1402 }
1403 else if (line.find(ContourPolygon::PolygonPart::ARC_RECORD_TOKEN) == 0)
1404 {
1405 std::string token;
1406 if (!(lineStream >> token))
1407 {
1408 throw_parse_error(m_path, line, token, lineNumber);
1409 }
1410
1412 {
1413 throw_parse_error(m_path, line, token, lineNumber);
1414 }
1415
1416 auto pPolygonPart = std::make_shared<ContourPolygon::PolygonPart>();
1417 pPolygonPart->type = ContourPolygon::PolygonPart::Type::Arc;
1418
1419 if (!(lineStream >> pPolygonPart->endX))
1420 {
1421 throw_parse_error(m_path, line, token, lineNumber);
1422 }
1423
1424 if (!(lineStream >> pPolygonPart->endY))
1425 {
1426 throw_parse_error(m_path, line, token, lineNumber);
1427 }
1428
1429 if (!(lineStream >> pPolygonPart->xCenter))
1430 {
1431 throw_parse_error(m_path, line, token, lineNumber);
1432 }
1433
1434 if (!(lineStream >> pPolygonPart->yCenter))
1435 {
1436 throw_parse_error(m_path, line, token, lineNumber);
1437 }
1438
1439 if (!(lineStream >> token))
1440 {
1441 throw_parse_error(m_path, line, token, lineNumber);
1442 }
1443
1444 if (token == "y" || token == "Y")
1445 {
1446 pPolygonPart->isClockwise = true;
1447 }
1448 else if (token == "n" || token == "N")
1449 {
1450 pPolygonPart->isClockwise = false;
1451 }
1452 else
1453 {
1454 throw_parse_error(m_path, line, token, lineNumber);
1455 }
1456
1457 if (pCurrentContourPolygon != nullptr)
1458 {
1459 pCurrentContourPolygon->m_polygonParts.push_back(pPolygonPart);
1460 }
1461 else
1462 {
1463 throw_parse_error(m_path, line, token, lineNumber);
1464 }
1465 }
1467 {
1468 std::string token;
1469 if (!(lineStream >> token))
1470 {
1471 throw_parse_error(m_path, line, token, lineNumber);
1472 }
1473
1475 {
1476 throw_parse_error(m_path, line, token, lineNumber);
1477 }
1478
1479 auto pPolygonPart = std::make_shared<ContourPolygon::PolygonPart>();
1480 pPolygonPart->type = ContourPolygon::PolygonPart::Type::Segment;
1481
1482 if (!(lineStream >> pPolygonPart->endX))
1483 {
1484 throw_parse_error(m_path, line, token, lineNumber);
1485 }
1486
1487 if (!(lineStream >> pPolygonPart->endY))
1488 {
1489 throw_parse_error(m_path, line, token, lineNumber);
1490 }
1491
1492 if (pCurrentContourPolygon != nullptr)
1493 {
1494 pCurrentContourPolygon->m_polygonParts.push_back(pPolygonPart);
1495 }
1496 else
1497 {
1498 throw_parse_error(m_path, line, token, lineNumber);
1499 }
1500 }
1501 else
1502 {
1503 // unrecognized record line
1504 parse_info pi(m_path, line, lineNumber);
1505 logwarn(pi.toString("unrecognized record line in EDADATA file:"));
1506 }
1507 }
1508 else
1509 {
1510 // empty line
1511 continue;
1512 }
1513 }
1514
1515 // finish current (previous) net record
1516 // this is the case where the last line of the file is not a net record (and there are no PKG records)
1517 if (pCurrentNetRecord != nullptr)
1518 {
1519 // finish current (previous) subnet record
1520 // case where we reach the end of the file before a new net record is encountered (and there are no PKG records)
1521 if (pCurrentSubnetRecord != nullptr)
1522 {
1523 pCurrentNetRecord->m_subnetRecords.push_back(pCurrentSubnetRecord);
1524 pCurrentSubnetRecord.reset();
1525 }
1526
1527 m_netRecords.push_back(pCurrentNetRecord);
1528 //m_netRecordsByName[pCurrentNetRecord->name] = pCurrentNetRecord;
1529 pCurrentNetRecord.reset();
1530 }
1531 else if (pCurrentPackageRecord != nullptr)
1532 {
1533 // finish up any current (previous) pin records
1534 if (pCurrentPinRecord != nullptr)
1535 {
1536 // check for overflow
1537 if (pCurrentPackageRecord->m_pinRecords.size() > UINT_MAX)
1538 {
1539 throw_parse_error(m_path, line, "", lineNumber);
1540 }
1541 pCurrentPinRecord->index = static_cast<unsigned>(pCurrentPackageRecord->m_pinRecords.size());
1542 pCurrentPackageRecord->m_pinRecords.push_back(pCurrentPinRecord);
1543 pCurrentPinRecord.reset();
1544 }
1545
1546 // finish up any current (previous) package records
1547 m_packageRecords.push_back(pCurrentPackageRecord);
1548 //m_packageRecordsByName[pCurrentPackageRecord->name] = pCurrentPackageRecord;
1549 pCurrentPackageRecord.reset();
1550 }
1551 else if (pCurrentFeatureGroupRecord != nullptr)
1552 {
1553 m_featureGroupRecords.push_back(pCurrentFeatureGroupRecord);
1554 pCurrentFeatureGroupRecord.reset();
1555 }
1556
1557 edaDataFile.close();
1558 }
1559 catch (parse_error& pe)
1560 {
1561 auto m = pe.toString("Parse Error:");
1562 logerror(m);
1563 // cleanup file
1564 edaDataFile.close();
1565 throw pe;
1566 }
1567 catch (std::exception& e)
1568 {
1569 parse_info pi(m_path, line, lineNumber);
1570 const auto m = pi.toString();
1571 logexception_msg(e, m);
1572 // cleanup file
1573 edaDataFile.close();
1574 throw e;
1575 }
1576
1577 return true;
1578 }
1579
1580 // Inherited via IProtoBuffable
1581 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::PackageRecord>
1583 {
1584 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::PackageRecord> pPackageRecordMessage(new Odb::Lib::Protobuf::EdaDataFile::PackageRecord);
1585 pPackageRecordMessage->set_name(Odb::Lib::Text::ToUtf8(name));
1586 pPackageRecordMessage->set_pitch(pitch);
1587 pPackageRecordMessage->set_xmin(xMin);
1588 pPackageRecordMessage->set_ymin(yMin);
1589 pPackageRecordMessage->set_xmax(xMax);
1590 pPackageRecordMessage->set_ymax(yMax);
1591 if (id.has_value())
1592 {
1593 pPackageRecordMessage->set_id(*id);
1594 }
1595 //pPackageRecordMessage->set_attributesidstring(attributesIdString);
1596 for (const auto& pinRecord : m_pinRecords)
1597 {
1598 auto pPinRecordMessage = pPackageRecordMessage->add_pinrecords();
1599 pPinRecordMessage->CopyFrom(*pinRecord->to_protobuf());
1600 }
1601 for (const auto& kvPinRecord : m_pinRecordsByName)
1602 {
1603 (*pPackageRecordMessage->mutable_pinrecordsbyname())[Odb::Lib::Text::ToUtf8(kvPinRecord.first)] = *kvPinRecord.second->to_protobuf();
1604 }
1605 for (const auto& propertyRecord : m_propertyRecords)
1606 {
1607 auto pPropertyRecordMessage = pPackageRecordMessage->add_propertyrecords();
1608 pPropertyRecordMessage->CopyFrom(*propertyRecord->to_protobuf());
1609 }
1610 for (const auto& outlineRecord : m_outlineRecords)
1611 {
1612 auto pOutlineRecordMessage = pPackageRecordMessage->add_outlinerecords();
1613 pOutlineRecordMessage->CopyFrom(*outlineRecord->to_protobuf());
1614 }
1615 for (const auto& kvAttributeAssignment : m_attributeLookupTable)
1616 {
1617 (*pPackageRecordMessage->mutable_attributelookuptable())[Odb::Lib::Text::ToUtf8(kvAttributeAssignment.first)] = Odb::Lib::Text::ToUtf8(kvAttributeAssignment.second);
1618 }
1619 return pPackageRecordMessage;
1620 }
1621
1622 void EdaDataFile::PackageRecord::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::PackageRecord& message)
1623 {
1624 name = message.name();
1625 pitch = message.pitch();
1626 xMin = message.xmin();
1627 yMin = message.ymin();
1628 xMax = message.xmax();
1629 yMax = message.ymax();
1630 if (message.has_id())
1631 {
1632 id = message.id();
1633 }
1634 //attributesIdString = message.attributesidstring();
1635
1636 for (const auto& pinRecordMessage : message.pinrecords())
1637 {
1638 auto pPinRecord = std::make_shared<PinRecord>();
1639 pPinRecord->from_protobuf(pinRecordMessage);
1640 m_pinRecords.push_back(pPinRecord);
1641 }
1642
1643 for (const auto& kvPinRecordMessage : message.pinrecordsbyname())
1644 {
1645 auto pPinRecord = std::make_shared<PinRecord>();
1646 pPinRecord->from_protobuf(kvPinRecordMessage.second);
1647 m_pinRecordsByName[kvPinRecordMessage.first] = pPinRecord;
1648 }
1649
1650 for (const auto& propertyRecordMessage : message.propertyrecords())
1651 {
1652 auto pPropertyRecord = std::make_shared<PropertyRecord>();
1653 pPropertyRecord->from_protobuf(propertyRecordMessage);
1654 m_propertyRecords.push_back(pPropertyRecord);
1655 }
1656
1657 for (const auto& outlineRecordMessage : message.outlinerecords())
1658 {
1659 auto pOutlineRecord = std::make_shared<OutlineRecord>();
1660 pOutlineRecord->from_protobuf(outlineRecordMessage);
1661 m_outlineRecords.push_back(pOutlineRecord);
1662 }
1663
1664 for (const auto& kvAttributeAssignment : message.attributelookuptable())
1665 {
1666 m_attributeLookupTable[kvAttributeAssignment.first] = kvAttributeAssignment.second;
1667 }
1668 }
1669
1670 // Inherited via IProtoBuffable
1671 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::PackageRecord::OutlineRecord> EdaDataFile::PackageRecord::OutlineRecord::to_protobuf() const
1672 {
1673 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::PackageRecord::OutlineRecord> pOutlineRecordMessage(new Odb::Lib::Protobuf::EdaDataFile::PackageRecord::OutlineRecord);
1674 pOutlineRecordMessage->set_type((Odb::Lib::Protobuf::EdaDataFile::PackageRecord::OutlineRecord::Type)type);
1675 pOutlineRecordMessage->set_lowerleftx(lowerLeftX);
1676 pOutlineRecordMessage->set_lowerlefty(lowerLeftY);
1677 pOutlineRecordMessage->set_width(width);
1678 pOutlineRecordMessage->set_height(height);
1679 pOutlineRecordMessage->set_xcenter(xCenter);
1680 pOutlineRecordMessage->set_ycenter(yCenter);
1681 pOutlineRecordMessage->set_radius(radius);
1682 pOutlineRecordMessage->set_halfside(halfSide);
1683 for (const auto& contourPolygon : m_contourPolygons)
1684 {
1685 auto pContourPolygonMessage = pOutlineRecordMessage->add_contourpolygons();
1686 pContourPolygonMessage->CopyFrom(*contourPolygon->to_protobuf());
1687 }
1688 return pOutlineRecordMessage;
1689 }
1690
1691 void EdaDataFile::PackageRecord::OutlineRecord::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::PackageRecord::OutlineRecord& message)
1692 {
1693 type = (Type)message.type();
1694 lowerLeftX = message.lowerleftx();
1695 lowerLeftY = message.lowerlefty();
1696 width = message.width();
1697 height = message.height();
1698 xCenter = message.xcenter();
1699 yCenter = message.ycenter();
1700 radius = message.radius();
1701 halfSide = message.halfside();
1702 for (const auto& contourPolygonMessage : message.contourpolygons())
1703 {
1704 auto pContourPolygon = std::make_shared<ContourPolygon>();
1705 pContourPolygon->from_protobuf(contourPolygonMessage);
1706 m_contourPolygons.push_back(pContourPolygon);
1707 }
1708 }
1709
1710 // Inherited via IProtoBuffable
1711 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord>
1713 {
1714 std::unique_ptr<Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord> pPinRecordMessage(new Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord);
1715 pPinRecordMessage->set_name(Odb::Lib::Text::ToUtf8(name));
1716 pPinRecordMessage->set_type((Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord::Type)type);
1717 pPinRecordMessage->set_xcenter(xCenter);
1718 pPinRecordMessage->set_ycenter(yCenter);
1719 pPinRecordMessage->set_finishedholesize(finishedHoleSize);
1720 pPinRecordMessage->set_electricaltype((Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord::ElectricalType)electricalType);
1721 pPinRecordMessage->set_mounttype((Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord::MountType)mountType);
1722 pPinRecordMessage->set_id(id);
1723 pPinRecordMessage->set_index(index);
1724 return pPinRecordMessage;
1725 }
1726
1727 void EdaDataFile::PackageRecord::PinRecord::from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord& message)
1728 {
1729 name = message.name();
1730 type = (Type)message.type();
1731 xCenter = message.xcenter();
1732 yCenter = message.ycenter();
1733 finishedHoleSize = message.finishedholesize();
1734 electricalType = (ElectricalType)message.electricaltype();
1735 mountType = (MountType)message.mounttype();
1736 id = message.id();
1737 index = message.index();
1738 }
1739}
#define logexception_msg(e, m)
Definition Logger.h:29
#define logerror(s)
Definition Logger.h:17
#define logwarn(s)
Definition Logger.h:20
#define loginfo(s)
Definition Logger.h:14
#define logdebug(s)
Definition Logger.h:23
std::map< std::string, std::string > m_attributeLookupTable
const PackageRecord::Vector & GetPackageRecords() const
const PackageRecord::StringMap & GetPackageRecordsByName() const
bool Save(std::ostream &os) override
const std::vector< std::string > & GetLayerNames() const
EdaDataFile(bool logAllLineParsing=false)
const std::filesystem::path & GetDirectory() const
const NetRecord::StringMap & GetNetRecordsByName() const
const NetRecord::Vector & GetNetRecords() const
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile &message) override
const FeatureGroupRecord::Vector & GetFeatureGroupRecords() const
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile > to_protobuf() const override
const std::string & GetUnits() const
const PropertyRecord::Vector & GetPropertyRecords() const
bool Parse(std::filesystem::path path)
const std::vector< std::string > & GetAttributeTextValues() const
const std::string & GetSource() const
const std::filesystem::path & GetPath() const
const std::vector< std::string > & GetAttributeNames() const
std::string toString(const std::string &message="") const
static std::filesystem::path getUncompressedFilePath(const std::filesystem::path &directory, const std::string &filename)
std::string ToUtf8(std::string_view input)
Converts input to valid UTF-8.
BoardSide
Definition enums.h:6
std::string & str_trim(std::string &s)
Definition str_utils.cpp:38
#define throw_parse_error(dataFile, dataLine, dataToken, dataLineNumber)
Definition parse_error.h:12
std::vector< FeatureGroupRecord::shared_ptr > Vector
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile::FeatureGroupRecord > to_protobuf() const override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::FeatureGroupRecord &message) override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::FeatureIdRecord &message) override
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile::FeatureIdRecord > to_protobuf() const override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord &message) override
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile::NetRecord::SubnetRecord > to_protobuf() const override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::NetRecord &message) override
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile::NetRecord > to_protobuf() const override
std::vector< std::shared_ptr< NetRecord > > Vector
std::map< std::string, std::shared_ptr< NetRecord > > StringMap
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile::PackageRecord::OutlineRecord > to_protobuf() const override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::PackageRecord::OutlineRecord &message) override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord &message) override
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile::PackageRecord::PinRecord > to_protobuf() const override
void from_protobuf(const Odb::Lib::Protobuf::EdaDataFile::PackageRecord &message) override
std::map< std::string, std::shared_ptr< PackageRecord > > StringMap
std::unique_ptr< Odb::Lib::Protobuf::EdaDataFile::PackageRecord > to_protobuf() const override
std::vector< std::shared_ptr< PackageRecord > > Vector
std::vector< std::shared_ptr< PropertyRecord > > Vector
static constexpr const char * RECORD_TOKEN
std::string toString(const std::string &message="") const
Definition parse_info.cpp:6