KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pads_sch_parser.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2025 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "pads_sch_parser.h"
21
22#include <io/pads/pads_common.h>
23#include <ki_exception.h>
24#include <reporter.h>
25
26#include <algorithm>
27#include <cmath>
28#include <fstream>
29#include <sstream>
30#include <regex>
31
32namespace PADS_SCH
33{
34
35VECTOR2I padsSchArcMidpoint( const VECTOR2I& aStart, const VECTOR2I& aEnd, const VECTOR2I& aCenter )
36{
37 double sx = aStart.x - aCenter.x;
38 double sy = aStart.y - aCenter.y;
39 double ex = aEnd.x - aCenter.x;
40 double ey = aEnd.y - aCenter.y;
41 double radius = std::sqrt( sx * sx + sy * sy );
42
43 double mx = sx + ex;
44 double my = sy + ey;
45 double mlen = std::sqrt( mx * mx + my * my );
46
47 VECTOR2I midPt;
48
49 if( mlen > 0.001 )
50 {
51 midPt.x = aCenter.x + static_cast<int>( radius * mx / mlen );
52 midPt.y = aCenter.y + static_cast<int>( radius * my / mlen );
53 }
54 else
55 {
56 midPt.x = aCenter.x + static_cast<int>( -sy * radius / std::max( radius, 1.0 ) );
57 midPt.y = aCenter.y + static_cast<int>( sx * radius / std::max( radius, 1.0 ) );
58 }
59
60 return midPt;
61}
62
63
65 m_reporter( nullptr ),
66 m_lineNumber( 0 ),
68{
69}
70
71
75
76
77bool PADS_SCH_PARSER::isSectionMarker( const std::string& aLine ) const
78{
79 if( aLine.size() < 3 || aLine[0] != '*' )
80 return false;
81
82 size_t endPos = aLine.find( '*', 1 );
83 return endPos != std::string::npos && endPos > 1;
84}
85
86
87std::string PADS_SCH_PARSER::extractSectionName( const std::string& aLine ) const
88{
89 if( aLine.size() < 3 || aLine[0] != '*' )
90 return "";
91
92 size_t endPos = aLine.find( '*', 1 );
93
94 if( endPos == std::string::npos || endPos <= 1 )
95 return "";
96
97 return aLine.substr( 1, endPos - 1 );
98}
99
100
101bool PADS_SCH_PARSER::Parse( const std::string& aFileName )
102{
105 m_symbolDefs.clear();
106 m_partPlacements.clear();
107 m_signals.clear();
108 m_offPageConnectors.clear();
109 m_buses.clear();
110 m_partTypes.clear();
111 m_tiedDots.clear();
112 m_sheetHeaders.clear();
113 m_textItems.clear();
114 m_linesItems.clear();
115 m_netNameLabels.clear();
116 m_lineNumber = 0;
117 m_currentSheet = 0;
118
119 std::ifstream file( aFileName );
120
121 if( !file.is_open() )
122 {
123 if( m_reporter )
124 m_reporter->Report( wxString::Format( "Cannot open file: %s", aFileName ), RPT_SEVERITY_ERROR );
125
126 return false;
127 }
128
129 std::vector<std::string> lines;
130 std::string line;
131
132 while( std::getline( file, line ) )
133 {
134 if( !line.empty() && line.back() == '\r' )
135 line.pop_back();
136
137 lines.push_back( line );
138 }
139
140 file.close();
141
142 if( lines.empty() )
143 {
144 if( m_reporter )
145 m_reporter->Report( "File is empty", RPT_SEVERITY_ERROR );
146
147 return false;
148 }
149
150 if( !parseHeader( lines[0] ) )
151 {
152 if( m_reporter )
153 m_reporter->Report( "Invalid PADS Logic file header", RPT_SEVERITY_ERROR );
154
155 return false;
156 }
157
158 m_header.valid = true;
159
160 for( size_t i = 1; i < lines.size(); i++ )
161 {
162 m_lineNumber = static_cast<int>( i + 1 );
163 const std::string& currentLine = lines[i];
164
165 if( currentLine.empty() )
166 continue;
167
168 if( currentLine.find( "*REMARK*" ) == 0 )
169 continue;
170
171 if( !isSectionMarker( currentLine ) )
172 continue;
173
174 std::string sectionName = extractSectionName( currentLine );
175
176 if( sectionName == "SCH" )
177 {
178 i = parseSectionSCH( lines, i );
179 }
180 else if( sectionName == "CAM" || sectionName == "MISC" )
181 {
182 i = skipBraceDelimitedSection( lines, i );
183 }
184 else if( sectionName == "FIELDS" )
185 {
186 i = parseSectionFIELDS( lines, i );
187 }
188 else if( sectionName == "SHT" )
189 {
190 i = parseSectionSHT( lines, i );
191 }
192 else if( sectionName == "CAE" )
193 {
194 i = parseSectionCAE( lines, i );
195 }
196 else if( sectionName == "TEXT" )
197 {
198 i = parseSectionTEXT( lines, i );
199 }
200 else if( sectionName == "LINES" )
201 {
202 i = parseSectionLINES( lines, i );
203 }
204 else if( sectionName == "CAEDECAL" )
205 {
206 i = parseSectionCAEDECAL( lines, i );
207 }
208 else if( sectionName == "PARTTYPE" )
209 {
210 i = parseSectionPARTTYPE( lines, i );
211 }
212 else if( sectionName == "PART" )
213 {
214 i = parseSectionPART( lines, i );
215 }
216 else if( sectionName == "BUSSES" )
217 {
218 i = parseSectionBUSSES( lines, i );
219 }
220 else if( sectionName == "OFFPAGE REFS" )
221 {
222 i = parseSectionOFFPAGEREFS( lines, i );
223 }
224 else if( sectionName == "TIEDOTS" )
225 {
226 i = parseSectionTIEDOTS( lines, i );
227 }
228 else if( sectionName == "CONNECTION" )
229 {
230 i = parseSectionCONNECTION( lines, i );
231 }
232 else if( sectionName == "NETNAMES" )
233 {
234 i = parseSectionNETNAMES( lines, i );
235 }
236 else if( sectionName == "END" )
237 {
238 break;
239 }
240 }
241
243
244 return true;
245}
246
247
249{
250 // Pin data is applied at symbol build time via GATE_DEF::pins rather than mutated on the
251 // shared SYMBOL_DEF, since several PARTTYPEs can reference one decal with different pin
252 // mappings.
253
254 for( auto& part : m_partPlacements )
255 {
256 for( auto& attr : part.attributes )
257 {
258 if( attr.name == "Ref.Des." && attr.value.empty() )
259 attr.value = part.reference;
260
261 auto ovr = part.attr_overrides.find( attr.name );
262
263 if( ovr != part.attr_overrides.end() && attr.value.empty() )
264 attr.value = ovr->second;
265 }
266 }
267}
268
269
270bool PADS_SCH_PARSER::CheckFileHeader( const std::string& aFileName )
271{
272 std::ifstream file( aFileName );
273
274 if( !file.is_open() )
275 return false;
276
277 std::string firstLine;
278
279 if( !std::getline( file, firstLine ) )
280 return false;
281
282 if( !firstLine.empty() && firstLine.back() == '\r' )
283 firstLine.pop_back();
284
285 if( firstLine.find( "*PADS-LOGIC" ) == 0 )
286 return true;
287
288 if( firstLine.find( "*PADS-POWERLOGIC" ) == 0 )
289 return true;
290
291 return false;
292}
293
294
295bool PADS_SCH_PARSER::parseHeader( const std::string& aLine )
296{
297 if( aLine.empty() || aLine[0] != '*' )
298 return false;
299
300 size_t endPos = aLine.find( '*', 1 );
301
302 if( endPos == std::string::npos )
303 return false;
304
305 std::string headerTag = aLine.substr( 1, endPos - 1 );
306
307 // The optional trailing suffix is an ANSI code page for non-ASCII strings.
308 std::regex headerRegex( R"(PADS-(POWER)?LOGIC-V(\d+\.\d+)(?:-([A-Za-z0-9]+))?)" );
309 std::smatch match;
310
311 if( !std::regex_match( headerTag, match, headerRegex ) )
312 return false;
313
314 if( match[1].matched )
315 m_header.product = "PADS-POWERLOGIC";
316 else
317 m_header.product = "PADS-LOGIC";
318
319 m_header.version = "V" + match[2].str();
320
321 if( match[3].matched )
322 m_header.codepage = match[3].str();
323
324 if( endPos + 1 < aLine.size() )
325 {
326 std::string desc = aLine.substr( endPos + 1 );
327 size_t start = desc.find_first_not_of( ' ' );
328
329 if( start != std::string::npos )
330 m_header.description = desc.substr( start );
331 }
332
333 return true;
334}
335
336
337size_t PADS_SCH_PARSER::parseSectionSCH( const std::vector<std::string>& aLines, size_t aStartLine )
338{
339 size_t i = aStartLine + 1;
340
341 while( i < aLines.size() )
342 {
343 const std::string& line = aLines[i];
344
345 if( isSectionMarker( line ) )
346 return i - 1;
347
348 if( line.empty() )
349 {
350 i++;
351 continue;
352 }
353
354 std::istringstream iss( line );
355 std::string keyword;
356 iss >> keyword;
357
358 if( keyword == "UNITS" )
359 {
360 int unitsVal = 0;
361 iss >> unitsVal;
362
363 switch( unitsVal )
364 {
365 case 0: m_parameters.units = UNIT_TYPE::MILS; break;
366 case 1: m_parameters.units = UNIT_TYPE::METRIC; break;
367 case 2: m_parameters.units = UNIT_TYPE::INCHES; break;
368 default: m_parameters.units = UNIT_TYPE::MILS; break;
369 }
370 }
371 else if( keyword == "CUR" )
372 {
373 std::string second;
374 iss >> second;
375
376 if( second == "SHEET" )
377 iss >> m_parameters.cur_sheet;
378 }
379 else if( keyword == "SHEET" )
380 {
381 std::string second;
382 iss >> second;
383
384 // Accept either SIZE SIZECODE as two tokens, or concatentated as a single token
385 if( second.starts_with( "SIZE" ) )
386 {
387 std::string sizeCode;
388
389 if( second == "SIZE" )
390 iss >> sizeCode;
391 else
392 sizeCode = second.substr( 4 );
393
394 m_parameters.sheet_size.name = sizeCode;
395
396 if( sizeCode == "A" )
397 {
398 m_parameters.sheet_size.width = 11000.0;
399 m_parameters.sheet_size.height = 8500.0;
400 }
401 else if( sizeCode == "B" )
402 {
403 m_parameters.sheet_size.width = 17000.0;
404 m_parameters.sheet_size.height = 11000.0;
405 }
406 else if( sizeCode == "C" )
407 {
408 m_parameters.sheet_size.width = 22000.0;
409 m_parameters.sheet_size.height = 17000.0;
410 }
411 else if( sizeCode == "D" )
412 {
413 m_parameters.sheet_size.width = 34000.0;
414 m_parameters.sheet_size.height = 22000.0;
415 }
416 else if( sizeCode == "E" )
417 {
418 m_parameters.sheet_size.width = 44000.0;
419 m_parameters.sheet_size.height = 34000.0;
420 }
421 else if( sizeCode == "A0" )
422 {
423 m_parameters.sheet_size.width = 46811.0;
424 m_parameters.sheet_size.height = 33110.0;
425 }
426 else if( sizeCode == "A1" )
427 {
428 m_parameters.sheet_size.width = 33110.0;
429 m_parameters.sheet_size.height = 23386.0;
430 }
431 else if( sizeCode == "A2" )
432 {
433 m_parameters.sheet_size.width = 23386.0;
434 m_parameters.sheet_size.height = 16535.0;
435 }
436 else if( sizeCode == "A3" )
437 {
438 m_parameters.sheet_size.width = 16535.0;
439 m_parameters.sheet_size.height = 11693.0;
440 }
441 else if( sizeCode == "A4" )
442 {
443 m_parameters.sheet_size.width = 11693.0;
444 m_parameters.sheet_size.height = 8268.0;
445 }
446 }
447 }
448 else if( keyword == "USERGRID" )
449 {
450 iss >> m_parameters.grid_x >> m_parameters.grid_y;
451 }
452 else if( keyword == "LINEWIDTH" )
453 {
454 iss >> m_parameters.line_width;
455 }
456 else if( keyword == "CONNWIDTH" )
457 {
458 iss >> m_parameters.conn_width;
459 }
460 else if( keyword == "BUSWIDTH" )
461 {
462 iss >> m_parameters.bus_width;
463 }
464 else if( keyword == "BUSANGLE" )
465 {
466 iss >> m_parameters.bus_angle;
467 }
468 else if( keyword == "TEXTSIZE" )
469 {
470 iss >> m_parameters.text_h >> m_parameters.text_w;
471 m_parameters.text_size = m_parameters.text_h;
472 }
473 else if( keyword == "PINNAMESIZE" )
474 {
475 iss >> m_parameters.pin_name_h >> m_parameters.pin_name_w;
476 }
477 else if( keyword == "REFNAMESIZE" )
478 {
479 iss >> m_parameters.ref_name_h >> m_parameters.ref_name_w;
480 }
481 else if( keyword == "PARTNAMESIZE" )
482 {
483 iss >> m_parameters.part_name_h >> m_parameters.part_name_w;
484 }
485 else if( keyword == "PINNOSIZE" )
486 {
487 iss >> m_parameters.pin_no_h >> m_parameters.pin_no_w;
488 }
489 else if( keyword == "NETNAMESIZE" )
490 {
491 iss >> m_parameters.net_name_h >> m_parameters.net_name_w;
492 }
493 else if( keyword == "DOTGRID" )
494 {
495 iss >> m_parameters.dot_grid;
496 }
497 else if( keyword == "TIEDOTSIZE" )
498 {
499 iss >> m_parameters.tied_dot_size;
500 }
501 else if( keyword == "REAL" )
502 {
503 std::string second;
504 iss >> second;
505
506 if( second == "WIDTH" )
507 iss >> m_parameters.real_width;
508 }
509 else if( keyword == "FONT" )
510 {
511 std::string second;
512 iss >> second;
513
514 if( second == "MODE" )
515 iss >> m_parameters.font_mode;
516 }
517 else if( keyword == "DEFAULT" )
518 {
519 std::string second;
520 iss >> second;
521
522 if( second == "FONT" )
523 {
524 std::string rest;
525 std::getline( iss, rest );
526 size_t start = rest.find( '"' );
527
528 if( start != std::string::npos )
529 {
530 size_t end = rest.find( '"', start + 1 );
531
532 if( end != std::string::npos )
533 m_parameters.default_font = rest.substr( start + 1, end - start - 1 );
534 }
535 }
536 }
537 else if( keyword == "BORDER" )
538 {
539 std::string second;
540 iss >> second;
541
542 if( second == "NAME" )
543 {
544 std::string name;
545 iss >> name;
546 m_parameters.border_template = name;
547 }
548 else
549 {
550 m_parameters.border_template = second;
551 }
552 }
553 else if( keyword == "JOBNAME" )
554 {
555 std::string rest;
556 std::getline( iss, rest );
557 size_t start = rest.find_first_not_of( " \t" );
558
559 if( start != std::string::npos )
560 {
561 rest = rest.substr( start );
562
563 if( rest.size() >= 2 && rest.front() == '"' && rest.back() == '"' )
564 rest = rest.substr( 1, rest.size() - 2 );
565
566 m_parameters.job_name = rest;
567 }
568 }
569 // Color and other numeric keywords we ignore
570 else if( keyword == "NTXCOL" || keyword == "HIRCOL" || keyword == "LINCOL" || keyword == "TXTCOL"
571 || keyword == "CONCOL" || keyword == "BUSCOL" || keyword == "PTXCOL" || keyword == "COMCOL"
572 || keyword == "NMCOL" || keyword == "PNMCOL" || keyword == "PINCOL" || keyword == "NETCOL"
573 || keyword == "FBGCOL" || keyword == "FIELDCOL" || keyword == "NNVISPWRGND" || keyword == "PCBFLAGS"
574 || keyword == "JOBTIME" || keyword == "BACKUPTIME" || keyword == "OFFREFVIEW" || keyword == "OFFREFNUM"
575 || keyword == "SHEETNUMSEP" )
576 {
577 }
578
579 i++;
580 }
581
582 return aLines.size() - 1;
583}
584
585
586size_t PADS_SCH_PARSER::parseSectionFIELDS( const std::vector<std::string>& aLines, size_t aStartLine )
587{
588 size_t i = aStartLine + 1;
589
590 while( i < aLines.size() )
591 {
592 const std::string& line = aLines[i];
593
594 if( isSectionMarker( line ) )
595 return i - 1;
596
597 if( line.empty() || line[0] != '"' )
598 {
599 i++;
600 continue;
601 }
602
603 // Line is "Field Name" followed by optional value text
604 size_t closeQuote = line.find( '"', 1 );
605
606 if( closeQuote != std::string::npos )
607 {
608 std::string fieldName = line.substr( 1, closeQuote - 1 );
609 std::string fieldValue;
610
611 if( closeQuote + 1 < line.size() )
612 {
613 fieldValue = line.substr( closeQuote + 1 );
614 size_t start = fieldValue.find_first_not_of( " \t" );
615
616 if( start != std::string::npos )
617 fieldValue = fieldValue.substr( start );
618 else
619 fieldValue.clear();
620 }
621
622 m_parameters.fields[fieldName] = fieldValue;
623 }
624
625 i++;
626 }
627
628 return aLines.size() - 1;
629}
630
631
632size_t PADS_SCH_PARSER::parseSectionSHT( const std::vector<std::string>& aLines, size_t aStartLine )
633{
634 // Fields: sheet_num sheet_name parent_num parent_name
635 const std::string& line = aLines[aStartLine];
636 std::string afterMarker = line.substr( line.find( '*', 1 ) + 1 );
637
638 std::istringstream iss( afterMarker );
639 SHEET_HEADER header;
640
641 iss >> header.sheet_num >> header.sheet_name >> header.parent_num >> header.parent_name;
642
643 m_currentSheet = header.sheet_num;
644 m_sheetHeaders.push_back( header );
645
646 return aStartLine;
647}
648
649
650size_t PADS_SCH_PARSER::parseSectionCAE( const std::vector<std::string>& aLines, size_t aStartLine )
651{
652 // The CAE section holds only viewport parameters, so skip it
653 size_t i = aStartLine + 1;
654
655 while( i < aLines.size() )
656 {
657 const std::string& line = aLines[i];
658
659 if( isSectionMarker( line ) )
660 return i - 1;
661
662 i++;
663 }
664
665 return aLines.size() - 1;
666}
667
668
669size_t PADS_SCH_PARSER::parseSectionTEXT( const std::vector<std::string>& aLines, size_t aStartLine )
670{
671 size_t i = aStartLine + 1;
672
673 while( i < aLines.size() )
674 {
675 const std::string& line = aLines[i];
676
677 if( line.empty() )
678 {
679 i++;
680 continue;
681 }
682
683 if( isSectionMarker( line ) )
684 return i - 1;
685
686 // A text item spans two lines: an attribute line then a content line
687 TEXT_ITEM item;
689 std::istringstream iss( line );
690
691 int x = 0, y = 0;
692 iss >> x >> y >> item.rotation >> item.justification >> item.height >> item.width_factor >> item.attr_flag;
693
694 item.position.x = x;
695 item.position.y = y;
696
697 std::string rest;
698 std::getline( iss, rest );
699 size_t qStart = rest.find( '"' );
700
701 if( qStart != std::string::npos )
702 {
703 size_t qEnd = rest.find( '"', qStart + 1 );
704
705 if( qEnd != std::string::npos )
706 item.font_name = rest.substr( qStart + 1, qEnd - qStart - 1 );
707 }
708
709 i++;
710
711 if( i < aLines.size() )
712 item.content = aLines[i];
713
714 m_textItems.push_back( item );
715 i++;
716 }
717
718 return aLines.size() - 1;
719}
720
721
722size_t PADS_SCH_PARSER::parseSectionLINES( const std::vector<std::string>& aLines, size_t aStartLine )
723{
724 size_t i = aStartLine + 1;
725
726 while( i < aLines.size() )
727 {
728 const std::string& line = aLines[i];
729
730 if( line.empty() )
731 {
732 i++;
733 continue;
734 }
735
736 if( isSectionMarker( line ) )
737 return i - 1;
738
739 // LINES item header: name LINES x y param1 param2
740 if( line.find( "LINES" ) != std::string::npos )
741 {
742 LINES_ITEM item;
743 std::istringstream iss( line );
744 std::string name, keyword;
745 int x = 0, y = 0;
746
747 iss >> name >> keyword >> x >> y >> item.param1 >> item.param2;
748
749 if( keyword == "LINES" )
750 {
751 item.name = name;
752 item.origin.x = x;
753 item.origin.y = y;
755
756 i++;
757
758 // Read primitives and embedded text until the next LINES item or section
759 while( i < aLines.size() )
760 {
761 const std::string& pline = aLines[i];
762
763 if( pline.empty() )
764 {
765 i++;
766 continue;
767 }
768
769 if( isSectionMarker( pline ) )
770 break;
771
772 if( pline.find( "LINES" ) != std::string::npos )
773 {
774 std::istringstream tiss( pline );
775 std::string tname, tkw;
776 tiss >> tname >> tkw;
777
778 if( tkw == "LINES" )
779 break;
780 }
781
782 std::istringstream piss( pline );
783 std::string firstToken;
784 piss >> firstToken;
785
786 if( firstToken == "OPEN" || firstToken == "CLOSED" || firstToken == "CIRCLE"
787 || firstToken == "COPCLS" )
788 {
789 SYMBOL_GRAPHIC graphic;
790 i = parseGraphicPrimitive( aLines, i, graphic );
791 item.primitives.push_back( graphic );
792 i++;
793 continue;
794 }
795
796 // A leading number marks a text item (coordinate attribute line)
797 bool isNumber =
798 !firstToken.empty()
799 && ( std::isdigit( firstToken[0] ) || firstToken[0] == '-' || firstToken[0] == '+' );
800
801 if( isNumber && pline.find( '"' ) != std::string::npos )
802 {
804 text.sheet_number = m_currentSheet;
805 std::istringstream tiss( pline );
806 int tx = 0, ty = 0;
807
808 tiss >> tx >> ty >> text.rotation >> text.justification >> text.height >> text.width_factor;
809
810 text.position.x = tx;
811 text.position.y = ty;
812
813 std::string trest;
814 std::getline( tiss, trest );
815 size_t qStart = trest.find( '"' );
816
817 if( qStart != std::string::npos )
818 {
819 size_t qEnd = trest.find( '"', qStart + 1 );
820
821 if( qEnd != std::string::npos )
822 text.font_name = trest.substr( qStart + 1, qEnd - qStart - 1 );
823 }
824
825 i++;
826
827 if( i < aLines.size() )
828 text.content = aLines[i];
829
830 item.texts.push_back( text );
831 }
832
833 i++;
834 }
835
836 m_linesItems.push_back( item );
837 continue;
838 }
839 }
840
841 i++;
842 }
843
844 return aLines.size() - 1;
845}
846
847
848size_t PADS_SCH_PARSER::parseSectionCAEDECAL( const std::vector<std::string>& aLines, size_t aStartLine )
849{
850 size_t i = aStartLine + 1;
851
852 while( i < aLines.size() )
853 {
854 const std::string& line = aLines[i];
855
856 if( line.empty() )
857 {
858 i++;
859 continue;
860 }
861
862 if( isSectionMarker( line ) )
863 break;
864
865 SYMBOL_DEF symbol;
866 i = parseSymbolDef( aLines, i, symbol );
867
868 if( !symbol.name.empty() )
869 m_symbolDefs.push_back( std::move( symbol ) );
870
871 i++;
872 }
873
874 // Resolve pin lengths from pin-decal geometry. The SHORT/LONG name heuristic covers only
875 // standard decals; custom names need their length measured from the graphics.
876 std::map<std::string, double> pinDecalLengths;
877
878 for( const auto& sym : m_symbolDefs )
879 {
880 if( !sym.is_pin_decal )
881 continue;
882
883 for( const auto& graphic : sym.graphics )
884 {
885 if( graphic.type != GRAPHIC_TYPE::LINE && graphic.type != GRAPHIC_TYPE::POLYLINE )
886 continue;
887
888 if( graphic.points.size() < 2 )
889 continue;
890
891 const auto& first = graphic.points.front().coord;
892 const auto& last = graphic.points.back().coord;
893 double dx = last.x - first.x;
894 double dy = last.y - first.y;
895 pinDecalLengths[sym.name] = std::sqrt( dx * dx + dy * dy );
896 break;
897 }
898 }
899
900 for( auto& sym : m_symbolDefs )
901 {
902 if( sym.is_pin_decal )
903 continue;
904
905 for( auto& pin : sym.pins )
906 {
907 if( pin.pin_decal_name.empty() )
908 continue;
909
910 auto it = pinDecalLengths.find( pin.pin_decal_name );
911
912 if( it != pinDecalLengths.end() )
913 pin.length = it->second;
914 }
915 }
916
917 return i > 0 ? i - 1 : aLines.size() - 1;
918}
919
920
921size_t PADS_SCH_PARSER::parseSymbolDef( const std::vector<std::string>& aLines, size_t aStartLine, SYMBOL_DEF& aSymbol )
922{
923 if( aStartLine >= aLines.size() )
924 return aStartLine;
925
926 const std::string& headerLine = aLines[aStartLine];
927
928 // Header fields: name f1 f2 height width h2 w2 num_attrs num_pieces has_polarity num_pins
929 // pin_origin_code is_pin_decal
930 std::istringstream iss( headerLine );
931 std::string name;
932 iss >> name;
933
934 if( name.empty() )
935 return aStartLine;
936
937 aSymbol.name = name;
938
939 std::vector<std::string> tokens;
940 std::string token;
941
942 while( iss >> token )
943 tokens.push_back( token );
944
945 if( tokens.size() >= 12 )
946 {
947 aSymbol.f1 = PADS_COMMON::ParseInt( tokens[0], 0, "CAEDECAL header" );
948 aSymbol.f2 = PADS_COMMON::ParseInt( tokens[1], 0, "CAEDECAL header" );
949 aSymbol.height = PADS_COMMON::ParseInt( tokens[2], 0, "CAEDECAL header" );
950 aSymbol.width = PADS_COMMON::ParseInt( tokens[3], 0, "CAEDECAL header" );
951 aSymbol.h2 = PADS_COMMON::ParseInt( tokens[4], 0, "CAEDECAL header" );
952 aSymbol.w2 = PADS_COMMON::ParseInt( tokens[5], 0, "CAEDECAL header" );
953 aSymbol.num_attrs = PADS_COMMON::ParseInt( tokens[6], 0, "CAEDECAL header" );
954 aSymbol.num_pieces = PADS_COMMON::ParseInt( tokens[7], 0, "CAEDECAL header" );
955 aSymbol.has_polarity = PADS_COMMON::ParseInt( tokens[8], 0, "CAEDECAL header" );
956 aSymbol.num_pins = PADS_COMMON::ParseInt( tokens[9], 0, "CAEDECAL header" );
957 aSymbol.pin_origin_code = PADS_COMMON::ParseInt( tokens[10], 0, "CAEDECAL header" );
958 aSymbol.is_pin_decal = PADS_COMMON::ParseInt( tokens[11], 0, "CAEDECAL header" );
959 }
960 else
961 {
962 // Simplified format: name num_pieces num_pins gate_count
963 if( tokens.size() >= 3 )
964 {
965 aSymbol.num_pieces = PADS_COMMON::ParseInt( tokens[0], 0, "CAEDECAL simplified" );
966 aSymbol.num_pins = PADS_COMMON::ParseInt( tokens[1], 0, "CAEDECAL simplified" );
967 aSymbol.gate_count = PADS_COMMON::ParseInt( tokens[2], 0, "CAEDECAL simplified" );
968 }
969 else
970 {
971 return aStartLine;
972 }
973
974 size_t idx = aStartLine + 1;
975
976 for( int p = 0; p < aSymbol.num_pieces && idx < aLines.size(); p++ )
977 {
978 const std::string& gline = aLines[idx];
979
980 if( gline.empty() || isSectionMarker( gline ) )
981 break;
982
983 SYMBOL_GRAPHIC graphic;
984 std::istringstream giss( gline );
985 std::string typeStr;
986 giss >> typeStr;
987
988 if( typeStr == "OPEN" || typeStr == "LINE" )
989 {
990 graphic.type = GRAPHIC_TYPE::LINE;
991 POINT p1, p2;
992 giss >> p1.x >> p1.y >> p2.x >> p2.y;
993 graphic.points.push_back( { p1, std::nullopt } );
994 graphic.points.push_back( { p2, std::nullopt } );
995 giss >> graphic.line_width;
996 }
997 else if( typeStr == "CLOSED" || typeStr == "RECT" )
998 {
1000 POINT p1, p2;
1001 giss >> p1.x >> p1.y >> p2.x >> p2.y;
1002 graphic.points.push_back( { p1, std::nullopt } );
1003 graphic.points.push_back( { p2, std::nullopt } );
1004 giss >> graphic.line_width;
1005 }
1006 else if( typeStr == "CIRCLE" )
1007 {
1008 graphic.type = GRAPHIC_TYPE::CIRCLE;
1009 giss >> graphic.center.x >> graphic.center.y >> graphic.radius;
1010 giss >> graphic.line_width;
1011 }
1012
1013 aSymbol.graphics.push_back( graphic );
1014 idx++;
1015 }
1016
1017 for( int p = 0; p < aSymbol.num_pins && idx < aLines.size(); p++ )
1018 {
1019 const std::string& pline = aLines[idx];
1020
1021 if( pline.empty() || isSectionMarker( pline ) )
1022 break;
1023
1025 std::istringstream piss( pline );
1026 double orientation = 0;
1027
1028 piss >> pin.position.x >> pin.position.y >> orientation >> pin.length;
1029 piss >> pin.number >> pin.name;
1030
1031 pin.rotation = orientation;
1032
1033 std::string typeStr;
1034
1035 if( piss >> typeStr )
1036 pin.type = parsePinType( typeStr );
1037
1038 aSymbol.pins.push_back( pin );
1039 idx++;
1040 }
1041
1042 return idx - 1;
1043 }
1044
1045 size_t idx = aStartLine + 1;
1046
1047 if( idx < aLines.size() && aLines[idx].find( "TIMESTAMP" ) == 0 )
1048 {
1049 std::istringstream tiss( aLines[idx] );
1050 std::string kw;
1051 tiss >> kw >> aSymbol.timestamp;
1052 idx++;
1053 }
1054
1055 // Two optional font-name lines, absent in older formats
1056 if( idx < aLines.size() && aLines[idx].size() >= 2 && aLines[idx][0] == '"' )
1057 {
1058 size_t qEnd = aLines[idx].find( '"', 1 );
1059
1060 if( qEnd != std::string::npos )
1061 aSymbol.font1 = aLines[idx].substr( 1, qEnd - 1 );
1062
1063 idx++;
1064 }
1065
1066 if( idx < aLines.size() && aLines[idx].size() >= 2 && aLines[idx][0] == '"' )
1067 {
1068 size_t qEnd = aLines[idx].find( '"', 1 );
1069
1070 if( qEnd != std::string::npos )
1071 aSymbol.font2 = aLines[idx].substr( 1, qEnd - 1 );
1072
1073 idx++;
1074 }
1075
1076 // Attribute label pairs, each a position line then a name line
1077 for( int a = 0; a < aSymbol.num_attrs && idx + 1 < aLines.size(); a++ )
1078 {
1079 CAEDECAL_ATTR attr;
1080 std::istringstream aiss( aLines[idx] );
1081 int x = 0, y = 0;
1082
1083 aiss >> x >> y >> attr.angle >> attr.justification >> attr.height >> attr.width;
1084 attr.position.x = x;
1085 attr.position.y = y;
1086
1087 std::string rest;
1088 std::getline( aiss, rest );
1089 size_t qStart = rest.find( '"' );
1090
1091 if( qStart != std::string::npos )
1092 {
1093 size_t qEnd = rest.find( '"', qStart + 1 );
1094
1095 if( qEnd != std::string::npos )
1096 attr.font_name = rest.substr( qStart + 1, qEnd - qStart - 1 );
1097 }
1098
1099 idx++;
1100
1101 if( idx < aLines.size() )
1102 attr.attr_name = aLines[idx];
1103
1104 aSymbol.attrs.push_back( attr );
1105 idx++;
1106 }
1107
1108 for( int p = 0; p < aSymbol.num_pieces && idx < aLines.size(); p++ )
1109 {
1110 if( aLines[idx].empty() )
1111 {
1112 idx++;
1113 p--;
1114 continue;
1115 }
1116
1117 SYMBOL_GRAPHIC graphic;
1118 idx = parseGraphicPrimitive( aLines, idx, graphic );
1119 aSymbol.graphics.push_back( graphic );
1120 idx++;
1121 }
1122
1123 // Embedded text labels sit between graphics and pins. Scan until a T-prefixed pin line; a
1124 // blank line ends the entry, which matters for entries with num_pins == 0 like pin decals.
1125 while( idx < aLines.size() )
1126 {
1127 const std::string& tline = aLines[idx];
1128
1129 if( tline.empty() )
1130 break;
1131
1132 if( isSectionMarker( tline ) )
1133 break;
1134
1135 // A pin T-line starts with T followed by a digit or minus sign
1136 if( tline.size() > 1 && tline[0] == 'T'
1137 && ( std::isdigit( static_cast<unsigned char>( tline[1] ) ) || tline[1] == '-' ) )
1138 {
1139 break;
1140 }
1141
1143 std::istringstream tiss( tline );
1144 int tx = 0, ty = 0;
1145
1146 tiss >> tx >> ty >> text.rotation >> text.justification;
1147
1148 int height = 0, width = 0;
1149 tiss >> height >> width;
1150 text.size = height;
1151 text.width_factor = width;
1152
1153 text.position.x = tx;
1154 text.position.y = ty;
1155
1156 idx++;
1157
1158 if( idx < aLines.size() )
1159 {
1160 text.content = aLines[idx];
1161 idx++;
1162 }
1163
1164 aSymbol.texts.push_back( text );
1165 }
1166
1167 // Each pin is a T line followed by a P line
1168 for( int p = 0; p < aSymbol.num_pins && idx < aLines.size(); p++ )
1169 {
1170 const std::string& tLine = aLines[idx];
1171
1172 if( tLine.empty() )
1173 {
1174 idx++;
1175 p--;
1176 continue;
1177 }
1178
1179 if( isSectionMarker( tLine ) )
1180 break;
1181
1183
1184 // T line: T<x> y angle side pn_x pn_y pn_angle pn_just pl_x pl_y pl_angle pl_just
1185 // pin_decal_name
1186 if( tLine.size() > 1 && tLine[0] == 'T' )
1187 {
1188 std::string tContent = tLine.substr( 1 );
1189 std::istringstream tiss( tContent );
1190 int tx = 0, ty = 0, angle = 0, pnx = 0, pny = 0, plx = 0, ply = 0;
1191
1192 tiss >> tx >> ty >> angle >> pin.side >> pnx >> pny >> pin.pn_angle >> pin.pn_just >> plx >> ply
1193 >> pin.pl_angle >> pin.pl_just >> pin.pin_decal_name;
1194
1195 pin.position.x = tx;
1196 pin.position.y = ty;
1197 pin.rotation = angle;
1198 pin.pn_offset.x = pnx;
1199 pin.pn_offset.y = pny;
1200 pin.pl_offset.x = plx;
1201 pin.pl_offset.y = ply;
1202
1203 // The pin decal name encodes inverted/clock styles
1204 if( pin.pin_decal_name == "PINB" || pin.pin_decal_name == "PINORB" || pin.pin_decal_name == "PCLKB"
1205 || pin.pin_decal_name == "PINIEB" || pin.pin_decal_name == "PINCLKB" )
1206 {
1207 pin.inverted = true;
1208 }
1209
1210 if( pin.pin_decal_name == "PCLK" || pin.pin_decal_name == "PCLKB" || pin.pin_decal_name == "PINCLK"
1211 || pin.pin_decal_name == "PINCLKB" )
1212 {
1213 pin.clock = true;
1214 }
1215
1216 // A self-contained pin decal (no name) draws its own graphics, so its stub length
1217 // is zero; named decals map to a fixed stub length.
1218 if( pin.pin_decal_name.empty() )
1219 pin.length = 0.0;
1220 else if( pin.pin_decal_name.find( "SHORT" ) != std::string::npos )
1221 pin.length = 100.0;
1222 else if( pin.pin_decal_name.find( "LONG" ) != std::string::npos )
1223 pin.length = 300.0;
1224 }
1225
1226 idx++;
1227
1228 // P line: P<x1> y1 angle1 just1 x2 y2 angle2 just2 flags
1229 if( idx < aLines.size() )
1230 {
1231 const std::string& pLine = aLines[idx];
1232
1233 if( pLine.size() > 1 && pLine[0] == 'P' )
1234 {
1235 std::string pContent = pLine.substr( 1 );
1236 std::istringstream piss( pContent );
1237 int px1 = 0, py1 = 0, px2 = 0, py2 = 0;
1238
1239 piss >> px1 >> py1 >> pin.p_angle1 >> pin.p_just1 >> px2 >> py2 >> pin.p_angle2 >> pin.p_just2
1240 >> pin.p_flags;
1241
1242 pin.p_position1.x = px1;
1243 pin.p_position1.y = py1;
1244 pin.p_position2.x = px2;
1245 pin.p_position2.y = py2;
1246
1247 // Pin name hidden if flags bit 128 set
1248 if( pin.p_flags & 128 )
1249 pin.name = "";
1250 }
1251
1252 idx++;
1253 }
1254
1255 // Placeholder number; the real assignment comes from the PARTTYPE section
1256 pin.number = std::to_string( p + 1 );
1257
1258 aSymbol.pins.push_back( pin );
1259 }
1260
1261 return idx > 0 ? idx - 1 : 0;
1262}
1263
1264
1265size_t PADS_SCH_PARSER::parseGraphicPrimitive( const std::vector<std::string>& aLines, size_t aStartLine,
1266 SYMBOL_GRAPHIC& aGraphic )
1267{
1268 if( aStartLine >= aLines.size() )
1269 return aStartLine;
1270
1271 const std::string& headerLine = aLines[aStartLine];
1272 std::istringstream iss( headerLine );
1273 std::string typeStr;
1274 int pointCount = 0, lineWidth = 0, lineStyle = 255;
1275
1276 iss >> typeStr >> pointCount >> lineWidth >> lineStyle;
1277
1278 aGraphic.line_width = lineWidth;
1279 aGraphic.line_style = lineStyle;
1280
1281 if( typeStr == "OPEN" )
1282 {
1283 aGraphic.type = GRAPHIC_TYPE::POLYLINE;
1284 aGraphic.filled = false;
1285 }
1286 else if( typeStr == "CLOSED" )
1287 {
1288 aGraphic.type = GRAPHIC_TYPE::RECTANGLE;
1289 aGraphic.filled = false;
1290 }
1291 else if( typeStr == "CIRCLE" )
1292 {
1293 aGraphic.type = GRAPHIC_TYPE::CIRCLE;
1294 aGraphic.filled = false;
1295 }
1296 else if( typeStr == "COPCLS" )
1297 {
1298 aGraphic.type = GRAPHIC_TYPE::RECTANGLE;
1299 aGraphic.filled = true;
1300 }
1301
1302 size_t idx = aStartLine + 1;
1303
1304 for( int p = 0; p < pointCount && idx < aLines.size(); p++ )
1305 {
1306 const std::string& ptLine = aLines[idx];
1307
1308 if( ptLine.empty() || isSectionMarker( ptLine ) )
1309 break;
1310
1311 std::istringstream piss( ptLine );
1312 GRAPHIC_POINT gpt;
1313 piss >> gpt.coord.x >> gpt.coord.y;
1314
1315 std::vector<std::string> extraTokens;
1316 std::string tok;
1317
1318 while( piss >> tok )
1319 extraTokens.push_back( tok );
1320
1321 if( extraTokens.size() >= 6 )
1322 {
1323 ARC_DATA arcData;
1324 arcData.bulge = PADS_COMMON::ParseDouble( extraTokens[0], 0.0, "arc data" );
1325 arcData.angle = PADS_COMMON::ParseDouble( extraTokens[1], 0.0, "arc data" );
1326 arcData.bbox_x1 = PADS_COMMON::ParseDouble( extraTokens[2], 0.0, "arc data" );
1327 arcData.bbox_y1 = PADS_COMMON::ParseDouble( extraTokens[3], 0.0, "arc data" );
1328 arcData.bbox_x2 = PADS_COMMON::ParseDouble( extraTokens[4], 0.0, "arc data" );
1329 arcData.bbox_y2 = PADS_COMMON::ParseDouble( extraTokens[5], 0.0, "arc data" );
1330 gpt.arc = arcData;
1331 }
1332
1333 aGraphic.points.push_back( gpt );
1334
1335 idx++;
1336 }
1337
1338 // A closed polygon reduces to two corner points when it is an axis-aligned rectangle;
1339 // otherwise it becomes a POLYLINE.
1340 if( aGraphic.type == GRAPHIC_TYPE::RECTANGLE && aGraphic.points.size() >= 4 )
1341 {
1342 std::set<double> uniqueX, uniqueY;
1343
1344 for( const auto& pt : aGraphic.points )
1345 {
1346 uniqueX.insert( pt.coord.x );
1347 uniqueY.insert( pt.coord.y );
1348 }
1349
1350 bool isRect = ( uniqueX.size() == 2 && uniqueY.size() == 2 );
1351
1352 if( isRect )
1353 {
1354 double minX = *uniqueX.begin();
1355 double maxX = *uniqueX.rbegin();
1356 double minY = *uniqueY.begin();
1357 double maxY = *uniqueY.rbegin();
1358
1359 aGraphic.points.clear();
1360 aGraphic.points.push_back( { { minX, minY }, std::nullopt } );
1361 aGraphic.points.push_back( { { maxX, maxY }, std::nullopt } );
1362 }
1363 else
1364 {
1365 aGraphic.type = GRAPHIC_TYPE::POLYLINE;
1366 }
1367 }
1368
1369 // A two-point circle gives its diameter; derive center and radius
1370 if( aGraphic.type == GRAPHIC_TYPE::CIRCLE && aGraphic.points.size() == 2 )
1371 {
1372 aGraphic.center.x = ( aGraphic.points[0].coord.x + aGraphic.points[1].coord.x ) / 2.0;
1373 aGraphic.center.y = ( aGraphic.points[0].coord.y + aGraphic.points[1].coord.y ) / 2.0;
1374 double dx = aGraphic.points[1].coord.x - aGraphic.points[0].coord.x;
1375 double dy = aGraphic.points[1].coord.y - aGraphic.points[0].coord.y;
1376 aGraphic.radius = std::sqrt( dx * dx + dy * dy ) / 2.0;
1377 }
1378
1379 return idx > 0 ? idx - 1 : aStartLine;
1380}
1381
1382
1383size_t PADS_SCH_PARSER::parseSectionPARTTYPE( const std::vector<std::string>& aLines, size_t aStartLine )
1384{
1385 size_t i = aStartLine + 1;
1386
1387 while( i < aLines.size() )
1388 {
1389 const std::string& line = aLines[i];
1390
1391 if( line.empty() )
1392 {
1393 i++;
1394 continue;
1395 }
1396
1397 if( isSectionMarker( line ) )
1398 return i - 1;
1399
1400 // Header fields: name category num_physical num_sigpins unused num_swap_groups
1401 PARTTYPE_DEF pt;
1402 std::istringstream iss( line );
1403 iss >> pt.name >> pt.category >> pt.num_physical >> pt.num_sigpins >> pt.unused >> pt.num_swap_groups;
1404
1405 if( pt.name.empty() )
1406 {
1407 i++;
1408 continue;
1409 }
1410
1411 // PADS marks connector part types with a "CN" or "CON" category. Connectors
1412 // number their pins regardless of the gate keyword that follows, so flag them
1413 // here rather than only in the per-keyword branches below.
1414 if( pt.category == "CN" || pt.category == "CON" )
1415 pt.is_connector = true;
1416
1417 i++;
1418
1419 if( i < aLines.size() && aLines[i].find( "TIMESTAMP" ) == 0 )
1420 {
1421 std::istringstream tiss( aLines[i] );
1422 std::string kw;
1423 tiss >> kw >> pt.timestamp;
1424 i++;
1425 }
1426
1427 bool isSpecial = ( pt.name == "$GND_SYMS" || pt.name == "$PWR_SYMS" || pt.name == "$OSR_SYMS" );
1428
1429 // Older files use "G:decal swap num_pins" gate lines; newer ones use
1430 // "GATE num_variants num_pins swap" followed by decal lines. A leading "G:" marks the
1431 // older form.
1432 bool isV52Gates = ( i < aLines.size() && aLines[i].size() >= 3 && aLines[i][0] == 'G' && aLines[i][1] == ':' );
1433
1434 if( isSpecial && !isV52Gates )
1435 {
1436 // Newer special-symbol form: keyword num_variants, then variant lines
1437 if( i < aLines.size() )
1438 {
1439 std::istringstream siss( aLines[i] );
1440 int numVariants = 0;
1441 siss >> pt.special_keyword >> numVariants;
1442 i++;
1443
1444 for( int v = 0; v < numVariants && i < aLines.size(); v++ )
1445 {
1447 std::istringstream viss( aLines[i] );
1448 viss >> sv.decal_name >> sv.pin_type;
1449
1450 std::string rest;
1451
1452 if( viss >> rest )
1453 sv.net_suffix = rest;
1454
1455 pt.special_variants.push_back( sv );
1456 i++;
1457 }
1458 }
1459 }
1460 else if( i < aLines.size() )
1461 {
1462 if( isSpecial )
1463 {
1464 if( pt.name == "$GND_SYMS" )
1465 pt.special_keyword = "GND";
1466 else if( pt.name == "$PWR_SYMS" )
1467 pt.special_keyword = "PWR";
1468 else
1469 pt.special_keyword = "OSR";
1470 }
1471
1472 // Standard parts hold GATE, CONN or G: blocks
1473 while( i < aLines.size() )
1474 {
1475 const std::string& gline = aLines[i];
1476
1477 if( gline.empty() )
1478 {
1479 break;
1480 }
1481
1482 if( isSectionMarker( gline ) )
1483 break;
1484
1485 std::istringstream giss( gline );
1486 std::string keyword;
1487 giss >> keyword;
1488
1489 if( keyword == "GATE" )
1490 {
1491 GATE_DEF gate;
1492 giss >> gate.num_decal_variants >> gate.num_pins >> gate.swap_flag;
1493 i++;
1494
1495 for( int d = 0; d < gate.num_decal_variants && i < aLines.size(); d++ )
1496 {
1497 if( aLines[i].empty() || isSectionMarker( aLines[i] ) )
1498 break;
1499
1500 gate.decal_names.push_back( aLines[i] );
1501 i++;
1502 }
1503
1504 for( int p = 0; p < gate.num_pins && i < aLines.size(); p++ )
1505 {
1506 if( aLines[i].empty() || isSectionMarker( aLines[i] ) )
1507 break;
1508
1510 std::istringstream piss( aLines[i] );
1511 std::string pinType;
1512
1513 piss >> pin.pin_id >> pin.swap_group >> pinType;
1514
1515 if( !pinType.empty() )
1516 pin.pin_type = pinType[0];
1517
1518 std::string pinName;
1519
1520 if( piss >> pinName )
1521 pin.pin_name = pinName;
1522
1523 gate.pins.push_back( pin );
1524 i++;
1525 }
1526
1527 pt.gates.push_back( gate );
1528 continue;
1529 }
1530 else if( keyword.size() >= 3 && keyword[0] == 'G' && keyword[1] == ':' )
1531 {
1532 // Gate form G:decal1[:decal2:...] swap_flag num_pins, with dot-separated
1533 // pin fields packed multiple per line.
1534 if( pt.category == "CON" )
1535 pt.is_connector = true;
1536
1537 GATE_DEF gate;
1538
1539 std::string decalStr = keyword.substr( 2 );
1540 std::istringstream diss( decalStr );
1541 std::string decalName;
1542
1543 while( std::getline( diss, decalName, ':' ) )
1544 {
1545 if( !decalName.empty() )
1546 gate.decal_names.push_back( decalName );
1547 }
1548
1549 gate.num_decal_variants = static_cast<int>( gate.decal_names.size() );
1550 giss >> gate.swap_flag >> gate.num_pins;
1551 i++;
1552
1553 int pinsRead = 0;
1554
1555 while( pinsRead < gate.num_pins && i < aLines.size() )
1556 {
1557 const std::string& pline = aLines[i];
1558
1559 if( pline.empty() || isSectionMarker( pline ) )
1560 break;
1561
1562 if( ( pline[0] == 'G' && pline.size() >= 2 && pline[1] == ':' ) || pline.find( "SIGPIN" ) == 0 )
1563 {
1564 break;
1565 }
1566
1567 std::istringstream piss( pline );
1568 std::string pinToken;
1569
1570 while( piss >> pinToken && pinsRead < gate.num_pins )
1571 {
1573 std::vector<std::string> fields;
1574 std::istringstream fiss( pinToken );
1575 std::string field;
1576
1577 while( std::getline( fiss, field, '.' ) )
1578 fields.push_back( field );
1579
1580 if( fields.size() >= 1 )
1581 pin.pin_id = fields[0];
1582
1583 if( fields.size() >= 2 )
1584 {
1585 pin.swap_group = PADS_COMMON::ParseInt( fields[1], 0, "V5.2 pin" );
1586 }
1587
1588 if( fields.size() >= 3 && !fields[2].empty() )
1589 pin.pin_type = fields[2][0];
1590
1591 if( fields.size() >= 4 )
1592 pin.pin_name = fields[3];
1593
1594 gate.pins.push_back( pin );
1595 pinsRead++;
1596 }
1597
1598 i++;
1599 }
1600
1601 if( isSpecial )
1602 {
1604 sv.decal_name = gate.decal_names.empty() ? "" : gate.decal_names[0];
1605
1606 if( !gate.pins.empty() )
1607 sv.pin_type = std::string( 1, gate.pins[0].pin_type );
1608
1609 pt.special_variants.push_back( sv );
1610 }
1611
1612 pt.gates.push_back( gate );
1613 continue;
1614 }
1615 else if( keyword == "CONN" )
1616 {
1617 pt.is_connector = true;
1618 GATE_DEF gate;
1619 int numPins = 0;
1620 giss >> gate.num_decal_variants >> numPins;
1621 gate.num_pins = numPins;
1622 i++;
1623
1624 for( int d = 0; d < gate.num_decal_variants && i < aLines.size(); d++ )
1625 {
1626 if( aLines[i].empty() || isSectionMarker( aLines[i] ) )
1627 break;
1628
1629 std::istringstream diss( aLines[i] );
1630 std::string decalName, pinType;
1631 diss >> decalName >> pinType;
1632 gate.decal_names.push_back( decalName );
1633 i++;
1634 }
1635
1636 for( int p = 0; p < numPins && i < aLines.size(); p++ )
1637 {
1638 if( aLines[i].empty() || isSectionMarker( aLines[i] ) )
1639 break;
1640
1642 std::istringstream piss( aLines[i] );
1643 std::string pinType;
1644
1645 piss >> pin.pin_id >> pin.swap_group >> pinType;
1646
1647 if( !pinType.empty() )
1648 pin.pin_type = pinType[0];
1649
1650 gate.pins.push_back( pin );
1651 i++;
1652 }
1653
1654 pt.gates.push_back( gate );
1655 continue;
1656 }
1657 else if( keyword == "SIGPIN" )
1658 {
1660 std::string token;
1661 giss >> token;
1662
1663 // Older files pack the fields dot-separated (e.g. "1.50.DGND"); newer ones
1664 // use space-separated "pin_number net_name".
1665 if( token.find( '.' ) != std::string::npos )
1666 {
1667 std::vector<std::string> fields;
1668 std::istringstream fiss( token );
1669 std::string field;
1670
1671 while( std::getline( fiss, field, '.' ) )
1672 fields.push_back( field );
1673
1674 if( fields.size() >= 1 )
1675 sp.pin_number = fields[0];
1676
1677 if( fields.size() >= 3 )
1678 sp.net_name = fields.back();
1679 }
1680 else
1681 {
1682 sp.pin_number = token;
1683 giss >> sp.net_name;
1684 }
1685
1686 pt.sigpins.push_back( sp );
1687 i++;
1688 continue;
1689 }
1690 else
1691 {
1692 // Swap-group or unrecognized line; keep it
1693 pt.swap_lines.push_back( gline );
1694 i++;
1695 continue;
1696 }
1697 }
1698 }
1699
1700 m_partTypes[pt.name] = pt;
1701 }
1702
1703 return aLines.size() - 1;
1704}
1705
1706
1707size_t PADS_SCH_PARSER::parseSectionPART( const std::vector<std::string>& aLines, size_t aStartLine )
1708{
1709 size_t i = aStartLine + 1;
1710
1711 while( i < aLines.size() && aLines[i].empty() )
1712 i++;
1713
1714 while( i < aLines.size() )
1715 {
1716 const std::string& line = aLines[i];
1717
1718 if( line.empty() )
1719 {
1720 i++;
1721 continue;
1722 }
1723
1724 if( isSectionMarker( line ) )
1725 return i - 1;
1726
1727 // A part header begins with its alpha reference designator
1728 if( std::isalpha( static_cast<unsigned char>( line[0] ) ) )
1729 {
1730 PART_PLACEMENT part;
1731 i = parsePartPlacement( aLines, i, part );
1732
1733 if( !part.reference.empty() )
1734 {
1736 m_partPlacements.push_back( std::move( part ) );
1737 }
1738
1739 i++;
1740 continue;
1741 }
1742
1743 i++;
1744 }
1745
1746 return aLines.size() - 1;
1747}
1748
1749
1750size_t PADS_SCH_PARSER::parsePartPlacement( const std::vector<std::string>& aLines, size_t aStartLine,
1751 PART_PLACEMENT& aPart )
1752{
1753 if( aStartLine >= aLines.size() )
1754 return aStartLine;
1755
1756 const std::string& headerLine = aLines[aStartLine];
1757 std::istringstream iss( headerLine );
1758
1759 // Two header forms, distinguished by whether the third token is numeric:
1760 // Normal: ref part_type x y angle mirror h1 w1 h2 w2 attrs disp pins u1 gate u2
1761 // Power: ref net_name $part_type x y angle mirror variant_index
1762 std::string refdes, partType;
1763 int x = 0, y = 0, angleCode = 0, mirrorFlag = 0;
1764
1765 iss >> refdes >> partType;
1766
1767 if( !( iss >> x ) )
1768 {
1769 // Non-numeric third field (e.g. "$PWR_SYMS") means a power symbol entry
1770 iss.clear();
1771 std::string actualPartType;
1772 iss >> actualPartType >> x >> y >> angleCode >> mirrorFlag;
1773
1774 aPart.power_net_name = partType;
1775 partType = actualPartType;
1776 }
1777 else
1778 {
1779 iss >> y >> angleCode >> mirrorFlag;
1780 }
1781
1782 aPart.reference = refdes;
1783 aPart.part_type = partType;
1784 aPart.symbol_name = partType;
1785 aPart.position.x = x;
1786 aPart.position.y = y;
1787
1788 switch( angleCode )
1789 {
1790 case 0: aPart.rotation = 0.0; break;
1791 case 1: aPart.rotation = 90.0; break;
1792 case 2: aPart.rotation = 180.0; break;
1793 case 3: aPart.rotation = 270.0; break;
1794 default: aPart.rotation = angleCode; break;
1795 }
1796
1797 aPart.mirror_flags = mirrorFlag;
1798
1799 if( !aPart.power_net_name.empty() )
1800 {
1801 // The remaining field is the variant index
1802 int variantIdx = 0;
1803
1804 if( iss >> variantIdx )
1805 aPart.gate_index = variantIdx;
1806 }
1807 else
1808 {
1809 int numAttrs = 0, numDisplayedValues = 0, numPins = 0, unused1 = 0, gateIdx = 0;
1810 int unused2 = 0;
1811
1812 if( iss >> aPart.h1 >> aPart.w1 >> aPart.h2 >> aPart.w2 >> numAttrs >> numDisplayedValues >> numPins >> unused1
1813 >> gateIdx >> unused2 )
1814 {
1815 aPart.num_attrs = numAttrs;
1816 aPart.num_displayed_values = numDisplayedValues;
1817 aPart.num_pins = numPins;
1818 aPart.gate_index = gateIdx;
1819 aPart.gate_number = gateIdx + 1;
1820 }
1821 else
1822 {
1823 // Simplified format: ref part_type x y angle mirror sheet gate
1824 std::istringstream iss2( headerLine );
1825 std::string dummy;
1826 iss2 >> dummy >> dummy >> x >> y;
1827
1828 double rotDeg = 0;
1829 iss2 >> rotDeg;
1830 aPart.rotation = rotDeg;
1831
1832 std::string mirrorStr;
1833
1834 if( iss2 >> mirrorStr )
1835 {
1836 if( mirrorStr == "M" || mirrorStr == "Y" || mirrorStr == "1" )
1837 aPart.mirror_flags = 1;
1838 }
1839
1840 iss2 >> aPart.sheet_number >> aPart.gate_number;
1841 }
1842 }
1843
1844 // Multi-gate components encode the gate in the reference suffix (U17-A, U1.B)
1845 size_t sepPos = refdes.rfind( '-' );
1846
1847 if( sepPos == std::string::npos )
1848 sepPos = refdes.rfind( '.' );
1849
1850 if( sepPos != std::string::npos && sepPos + 1 < refdes.size() )
1851 {
1852 char gateLetter = refdes[sepPos + 1];
1853
1854 if( std::isalpha( static_cast<unsigned char>( gateLetter ) ) )
1855 {
1856 // Derive the gate index from the letter only when the header gave none
1857 if( aPart.gate_index == 0 )
1858 {
1859 aPart.gate_index = std::toupper( static_cast<unsigned char>( gateLetter ) ) - 'A';
1860 aPart.gate_number = aPart.gate_index + 1;
1861 }
1862 }
1863 }
1864
1865 auto partTypeDefinition = m_partTypes.find( aPart.part_type );
1866
1867 if( partTypeDefinition != m_partTypes.end() )
1868 {
1869 const PARTTYPE_DEF& definition = partTypeDefinition->second;
1870
1871 if( aPart.gate_index >= 0 && static_cast<size_t>( aPart.gate_index ) < definition.gates.size()
1872 && !definition.gates[aPart.gate_index].decal_names.empty() )
1873 {
1874 aPart.decal_name = definition.gates[aPart.gate_index].decal_names.front();
1875 }
1876 else if( definition.is_connector && !definition.special_variants.empty() )
1877 {
1878 aPart.decal_name = definition.special_variants.front().decal_name;
1879 }
1880 }
1881
1882 size_t i = aStartLine + 1;
1883
1884 // Full format carries font lines, attribute labels, overrides and pin overrides
1885 if( aPart.num_attrs > 0 || aPart.num_displayed_values > 0 )
1886 {
1887 if( i < aLines.size() )
1888 {
1889 const std::string& fl = aLines[i];
1890
1891 if( fl.size() >= 2 && fl[0] == '"' )
1892 {
1893 size_t qEnd = fl.find( '"', 1 );
1894
1895 if( qEnd != std::string::npos )
1896 aPart.font1 = fl.substr( 1, qEnd - 1 );
1897
1898 i++;
1899 }
1900 }
1901
1902 if( i < aLines.size() )
1903 {
1904 const std::string& fl = aLines[i];
1905
1906 if( fl.size() >= 2 && fl[0] == '"' )
1907 {
1908 size_t qEnd = fl.find( '"', 1 );
1909
1910 if( qEnd != std::string::npos )
1911 aPart.font2 = fl.substr( 1, qEnd - 1 );
1912
1913 i++;
1914 }
1915 }
1916
1917 for( int a = 0; a < aPart.num_attrs && i + 1 < aLines.size(); a++ )
1918 {
1919 PART_ATTRIBUTE attr;
1920 std::istringstream aiss( aLines[i] );
1921 int ax = 0, ay = 0, angle = 0, disp = 0, h = 0, w = 0, vis = 0;
1922
1923 aiss >> ax >> ay >> angle >> disp >> h >> w >> vis;
1924
1925 attr.position.x = ax;
1926 attr.position.y = ay;
1927 attr.rotation = angle;
1928 attr.justification = disp;
1929 attr.height = h;
1930 attr.width = w;
1931 attr.size = h;
1932 attr.visibility = vis;
1933
1934 // PADS uses bit 3 (value 8) of the attribute display flag to mark a label
1935 // hidden. The lower bits select what is displayed (name and/or value), so a
1936 // non-zero flag such as 1 or 3 still denotes a visible label.
1937 attr.visible = ( ( vis & 0x8 ) == 0 );
1938
1939 std::string rest;
1940 std::getline( aiss, rest );
1941 size_t qStart = rest.find( '"' );
1942
1943 if( qStart != std::string::npos )
1944 {
1945 size_t qEnd = rest.find( '"', qStart + 1 );
1946
1947 if( qEnd != std::string::npos )
1948 attr.font_name = rest.substr( qStart + 1, qEnd - qStart - 1 );
1949 }
1950
1951 i++;
1952
1953 if( i < aLines.size() )
1954 attr.name = aLines[i];
1955
1956 aPart.attributes.push_back( attr );
1957 i++;
1958 }
1959
1960 // Each displayed-value override is a position line then a "name" value line
1961 for( int d = 0; d < aPart.num_displayed_values && i < aLines.size(); d++ )
1962 {
1963 if( !aLines[i].empty() && std::isdigit( static_cast<unsigned char>( aLines[i][0] ) ) )
1964 {
1965 i++;
1966 }
1967
1968 if( i >= aLines.size() )
1969 break;
1970
1971 const std::string& valLine = aLines[i];
1972
1973 if( valLine.size() > 2 && valLine[0] == '"' )
1974 {
1975 size_t closeQ = valLine.find( '"', 1 );
1976
1977 if( closeQ != std::string::npos )
1978 {
1979 std::string attrName = valLine.substr( 1, closeQ - 1 );
1980 std::string attrValue;
1981
1982 if( closeQ + 1 < valLine.size() )
1983 {
1984 attrValue = valLine.substr( closeQ + 1 );
1985 size_t start = attrValue.find_first_not_of( " \t" );
1986
1987 if( start != std::string::npos )
1988 attrValue = attrValue.substr( start );
1989 else
1990 attrValue.clear();
1991 }
1992
1993 aPart.attr_overrides[attrName] = attrValue;
1994 }
1995 }
1996
1997 i++;
1998 }
1999
2000 // Populate attr.value from the overrides
2001 for( auto& attr : aPart.attributes )
2002 {
2003 auto it = aPart.attr_overrides.find( attr.name );
2004
2005 if( it != aPart.attr_overrides.end() )
2006 attr.value = it->second;
2007 }
2008
2009 // Pin override lines
2010 while( i < aLines.size() )
2011 {
2012 const std::string& pline = aLines[i];
2013
2014 if( pline.empty() )
2015 break;
2016
2017 if( isSectionMarker( pline ) )
2018 return i - 1;
2019
2020 // An alpha start is the next part
2021 if( std::isalpha( static_cast<unsigned char>( pline[0] ) ) )
2022 return i - 1;
2023
2024 // Pin override: index height width angle justification
2025 if( std::isdigit( static_cast<unsigned char>( pline[0] ) ) )
2026 {
2028 std::istringstream poiss( pline );
2029 int pinIdx = 0;
2030 poiss >> pinIdx >> po.height >> po.width >> po.angle >> po.justification;
2031 aPart.pin_overrides.push_back( po );
2032 }
2033
2034 i++;
2035 }
2036 }
2037 else
2038 {
2039 // Simplified format: @-prefixed attribute lines
2040 while( i < aLines.size() )
2041 {
2042 const std::string& attrLine = aLines[i];
2043
2044 if( attrLine.empty() )
2045 break;
2046
2047 if( isSectionMarker( attrLine ) )
2048 return i - 1;
2049
2050 if( attrLine[0] == '@' )
2051 {
2052 PART_ATTRIBUTE attr;
2053 std::istringstream aiss( attrLine.substr( 1 ) );
2054 aiss >> attr.name;
2055
2056 std::string rest;
2057 std::getline( aiss, rest );
2058 size_t start = rest.find_first_not_of( " \t" );
2059
2060 if( start != std::string::npos )
2061 {
2062 rest = rest.substr( start );
2063
2064 if( !rest.empty() && rest[0] == '"' )
2065 {
2066 size_t endQuote = rest.find( '"', 1 );
2067
2068 if( endQuote != std::string::npos )
2069 {
2070 attr.value = rest.substr( 1, endQuote - 1 );
2071 rest = rest.substr( endQuote + 1 );
2072 }
2073 }
2074 else
2075 {
2076 std::istringstream viss( rest );
2077 viss >> attr.value;
2078 std::getline( viss, rest );
2079 }
2080
2081 std::istringstream piss( rest );
2082 piss >> attr.position.x >> attr.position.y >> attr.rotation >> attr.size;
2083
2084 std::string visStr;
2085
2086 if( piss >> visStr )
2087 attr.visible = ( visStr != "N" && visStr != "0" && visStr != "H" );
2088 }
2089
2090 aPart.attributes.push_back( attr );
2091 i++;
2092 }
2093 else if( std::isalpha( static_cast<unsigned char>( attrLine[0] ) ) )
2094 {
2095 return i - 1;
2096 }
2097 else
2098 {
2099 i++;
2100 }
2101 }
2102 }
2103
2104 return i - 1;
2105}
2106
2107
2108size_t PADS_SCH_PARSER::parseSectionBUSSES( const std::vector<std::string>& aLines, size_t aStartLine )
2109{
2110 constexpr long long MAX_BUS_POINTS = 1'000'000;
2111 size_t i = aStartLine + 1;
2112
2113 // This grammar is inferred rather than observed, and nothing outside the tests reads
2114 // GetBuses(), so a record we cannot read drops that bus instead of the whole import
2115 auto unsupported =
2116 [&]( size_t aLine, const wxString& aDetail )
2117 {
2118 if( m_reporter )
2119 {
2120 m_reporter->Report( wxString::Format( _( "BUSSES line %llu: unsupported record, %s. "
2121 "The bus was skipped." ),
2122 static_cast<unsigned long long>( aLine + 1 ),
2123 aDetail ),
2125 }
2126 };
2127
2128 while( i < aLines.size() )
2129 {
2130 const std::string& line = aLines[i];
2131
2132 if( isSectionMarker( line ) )
2133 return i - 1;
2134
2135 if( line.empty() )
2136 {
2137 ++i;
2138 continue;
2139 }
2140
2141 BUS_DEF bus;
2142 long long parsedPointCount = -1;
2143 std::istringstream header( line );
2144 std::string extra;
2145
2146 if( !( header >> bus.handle >> bus.name >> parsedPointCount ) || header >> extra
2147 || !bus.handle.starts_with( "@@@B" ) || bus.handle.size() == 4
2148 || parsedPointCount < 0 || parsedPointCount > MAX_BUS_POINTS )
2149 {
2150 unsupported( i, wxS( "invalid bus header or point count" ) );
2151 ++i;
2152 continue;
2153 }
2154
2155 const size_t pointCount = static_cast<size_t>( parsedPointCount );
2156 bool busMalformed = false;
2158
2159 if( !bus.name.empty() )
2160 bus.aliases.push_back( bus.name );
2161
2162 ++i;
2163
2164 while( i < aLines.size() && bus.path.size() < pointCount )
2165 {
2166 // Leave the marker or the next handle in place so the outer loop resynchronizes on it
2167 if( isSectionMarker( aLines[i] ) || aLines[i].starts_with( "@@@B" ) )
2168 break;
2169
2170 if( aLines[i].empty() )
2171 {
2172 ++i;
2173 continue;
2174 }
2175
2176 POINT point;
2177 std::istringstream coordinates( aLines[i] );
2178
2179 // A later line must not complete a bus whose earlier point was unreadable
2180 if( !( coordinates >> point.x >> point.y ) || coordinates >> extra )
2181 {
2182 busMalformed = true;
2183 ++i;
2184 continue;
2185 }
2186
2187 bus.path.push_back( point );
2188
2189 ++i;
2190 }
2191
2192 if( busMalformed || bus.path.size() != pointCount )
2193 {
2194 unsupported( i == aLines.size() ? i - 1 : i,
2195 busMalformed ? wxS( "invalid bus point" )
2196 : wxS( "bus ended before its declared point count" ) );
2197 continue;
2198 }
2199
2200 m_buses.push_back( std::move( bus ) );
2201 }
2202
2203 return aLines.size() - 1;
2204}
2205
2206
2207size_t PADS_SCH_PARSER::parseSectionOFFPAGEREFS( const std::vector<std::string>& aLines, size_t aStartLine )
2208{
2209 size_t i = aStartLine + 1;
2210
2211 while( i < aLines.size() )
2212 {
2213 const std::string& line = aLines[i];
2214
2215 if( line.empty() )
2216 {
2217 i++;
2218 continue;
2219 }
2220
2221 if( isSectionMarker( line ) )
2222 return i - 1;
2223
2224 // Fields: @@@O<id> net_name symbol_lib x y rotation flags1 flags2
2225 if( line.find( "@@@O" ) == 0 )
2226 {
2228 std::istringstream iss( line );
2229 std::string idToken;
2230 iss >> idToken;
2231
2232 if( idToken.size() > 4 )
2233 opc.id = PADS_COMMON::ParseInt( idToken.substr( 4 ), 0, "OPC id" );
2234
2235 int x = 0, y = 0;
2236 iss >> opc.signal_name >> opc.symbol_lib >> x >> y >> opc.rotation >> opc.flags1 >> opc.flags2;
2237
2238 opc.position.x = x;
2239 opc.position.y = y;
2241
2242 m_offPageConnectors.push_back( opc );
2243
2244 auto bus = std::find_if( m_buses.begin(), m_buses.end(),
2245 [&]( const BUS_DEF& aBus )
2246 {
2247 return aBus.handle == opc.symbol_lib && aBus.sheet_number == m_currentSheet;
2248 } );
2249
2250 if( bus != m_buses.end() )
2251 {
2252 bus->entries.push_back( { opc.signal_name, opc.position, opc.rotation } );
2253
2254 if( std::find( bus->member_nets.begin(), bus->member_nets.end(), opc.signal_name )
2255 == bus->member_nets.end() )
2256 {
2257 bus->member_nets.push_back( opc.signal_name );
2258 }
2259 }
2260 }
2261
2262 i++;
2263 }
2264
2265 return aLines.size() - 1;
2266}
2267
2268
2269size_t PADS_SCH_PARSER::parseSectionTIEDOTS( const std::vector<std::string>& aLines, size_t aStartLine )
2270{
2271 size_t i = aStartLine + 1;
2272
2273 while( i < aLines.size() )
2274 {
2275 const std::string& line = aLines[i];
2276
2277 if( line.empty() )
2278 {
2279 i++;
2280 continue;
2281 }
2282
2283 if( isSectionMarker( line ) )
2284 return i - 1;
2285
2286 // Fields: @@@D<id> x y
2287 if( line.find( "@@@D" ) == 0 )
2288 {
2289 TIED_DOT dot;
2290 std::istringstream iss( line );
2291 std::string idToken;
2292 iss >> idToken;
2293
2294 if( idToken.size() > 4 )
2295 dot.id = PADS_COMMON::ParseInt( idToken.substr( 4 ), 0, "TIEDOT id" );
2296
2297 int x = 0, y = 0;
2298 iss >> x >> y;
2299 dot.position.x = x;
2300 dot.position.y = y;
2302
2303 m_tiedDots.push_back( dot );
2304 }
2305
2306 i++;
2307 }
2308
2309 return aLines.size() - 1;
2310}
2311
2312
2313size_t PADS_SCH_PARSER::parseSectionCONNECTION( const std::vector<std::string>& aLines, size_t aStartLine )
2314{
2315 // SIGNAL blocks follow inside the CONNECTION section
2316 size_t i = aStartLine + 1;
2317
2318 while( i < aLines.size() )
2319 {
2320 const std::string& line = aLines[i];
2321
2322 if( line.empty() )
2323 {
2324 i++;
2325 continue;
2326 }
2327
2328 // A non-SIGNAL marker ends the CONNECTION section
2329 if( isSectionMarker( line ) )
2330 {
2331 std::string secName = extractSectionName( line );
2332
2333 if( secName == "SIGNAL" )
2334 {
2335 SCH_SIGNAL signal;
2336 i = parseSignalDef( aLines, i, signal );
2337
2338 if( !signal.name.empty() )
2339 {
2340 for( auto& wire : signal.wires )
2341 wire.sheet_number = m_currentSheet;
2342
2343 // Build pin connections from wire endpoint references
2344 for( const auto& wire : signal.wires )
2345 {
2346 for( const auto& ep : { wire.endpoint_a, wire.endpoint_b } )
2347 {
2348 if( ep.find( '.' ) != std::string::npos && ep.find( "@@@" ) == std::string::npos )
2349 {
2350 size_t dotPos = ep.find( '.' );
2351 PIN_CONNECTION conn;
2352 conn.reference = ep.substr( 0, dotPos );
2353 conn.pin_number = ep.substr( dotPos + 1 );
2355
2356 bool found = false;
2357
2358 for( const auto& existing : signal.connections )
2359 {
2360 if( existing.reference == conn.reference && existing.pin_number == conn.pin_number )
2361 {
2362 found = true;
2363 break;
2364 }
2365 }
2366
2367 if( !found )
2368 signal.connections.push_back( conn );
2369 }
2370 }
2371 }
2372
2373 m_signals.push_back( std::move( signal ) );
2374 }
2375
2376 i++;
2377 continue;
2378 }
2379 else
2380 {
2381 return i - 1;
2382 }
2383 }
2384
2385 i++;
2386 }
2387
2388 return aLines.size() - 1;
2389}
2390
2391
2392size_t PADS_SCH_PARSER::parseSignalDef( const std::vector<std::string>& aLines, size_t aStartLine, SCH_SIGNAL& aSignal )
2393{
2394 if( aStartLine >= aLines.size() )
2395 return aStartLine;
2396
2397 // Header fields after the marker: net_name flags1 flags2
2398 const std::string& headerLine = aLines[aStartLine];
2399 std::string secName = extractSectionName( headerLine );
2400
2401 if( secName != "SIGNAL" )
2402 return aStartLine;
2403
2404 size_t afterMarker = headerLine.find( '*', 1 );
2405
2406 if( afterMarker == std::string::npos )
2407 return aStartLine;
2408
2409 std::string rest = headerLine.substr( afterMarker + 1 );
2410 std::istringstream iss( rest );
2411
2412 iss >> aSignal.name >> aSignal.flags1 >> aSignal.flags2;
2413
2414 size_t i = aStartLine + 1;
2415
2416 if( aSignal.flags2 == 1 && i < aLines.size() )
2417 {
2418 const std::string& funcLine = aLines[i];
2419
2420 if( funcLine.find( "\"FUNCTION\"" ) != std::string::npos || funcLine.find( "FUNCTION" ) == 0 )
2421 {
2422 size_t qStart = funcLine.find( '"' );
2423
2424 if( qStart != std::string::npos )
2425 {
2426 size_t qEnd = funcLine.find( '"', qStart + 1 );
2427
2428 if( qEnd != std::string::npos )
2429 {
2430 size_t afterQ = funcLine.find_first_not_of( " \t", qEnd + 1 );
2431
2432 if( afterQ != std::string::npos )
2433 aSignal.function = funcLine.substr( afterQ );
2434 }
2435 }
2436
2437 i++;
2438 }
2439 }
2440
2441 // Each wire is a header line "endpoint_a endpoint_b vertex_count flags" then one line per
2442 // vertex coordinate.
2443 while( i < aLines.size() )
2444 {
2445 const std::string& line = aLines[i];
2446
2447 if( line.empty() )
2448 {
2449 i++;
2450 continue;
2451 }
2452
2453 if( isSectionMarker( line ) )
2454 return i - 1;
2455
2456 WIRE_SEGMENT wire;
2457 std::istringstream wiss( line );
2458 wiss >> wire.endpoint_a >> wire.endpoint_b >> wire.vertex_count >> wire.flags;
2459
2460 if( wire.endpoint_a.empty() || wire.endpoint_b.empty() )
2461 {
2462 i++;
2463 continue;
2464 }
2465
2466 i++;
2467
2468 for( int v = 0; v < wire.vertex_count && i < aLines.size(); v++ )
2469 {
2470 const std::string& ptLine = aLines[i];
2471
2472 if( ptLine.empty() || isSectionMarker( ptLine ) )
2473 break;
2474
2475 POINT pt;
2476 std::istringstream piss( ptLine );
2477 piss >> pt.x >> pt.y;
2478 wire.vertices.push_back( pt );
2479 i++;
2480 }
2481
2482 if( !wire.vertices.empty() )
2483 {
2484 wire.start = wire.vertices.front();
2485 wire.end = wire.vertices.back();
2486 }
2487
2488 aSignal.wires.push_back( wire );
2489 }
2490
2491 return i > 0 ? i - 1 : aStartLine;
2492}
2493
2494
2495size_t PADS_SCH_PARSER::parseSectionNETNAMES( const std::vector<std::string>& aLines, size_t aStartLine )
2496{
2497 size_t i = aStartLine + 1;
2498
2499 while( i < aLines.size() )
2500 {
2501 const std::string& line = aLines[i];
2502
2503 if( line.empty() )
2504 {
2505 i++;
2506 continue;
2507 }
2508
2509 if( isSectionMarker( line ) )
2510 return i - 1;
2511
2512 // Fields: net_name anchor_ref x_offset y_offset rotation justification f3 f4 f5 f6 f7
2513 // height width_pct "font_name"
2514 NETNAME_LABEL label;
2515 std::istringstream iss( line );
2516
2517 iss >> label.net_name >> label.anchor_ref >> label.x_offset >> label.y_offset >> label.rotation
2518 >> label.justification >> label.f3 >> label.f4 >> label.f5 >> label.f6 >> label.f7 >> label.height
2519 >> label.width_pct;
2520
2521 std::string rest;
2522 std::getline( iss, rest );
2523 size_t qStart = rest.find( '"' );
2524
2525 if( qStart != std::string::npos )
2526 {
2527 size_t qEnd = rest.find( '"', qStart + 1 );
2528
2529 if( qEnd != std::string::npos )
2530 label.font_name = rest.substr( qStart + 1, qEnd - qStart - 1 );
2531 }
2532
2533 m_netNameLabels.push_back( label );
2534 i++;
2535 }
2536
2537 return aLines.size() - 1;
2538}
2539
2540
2541size_t PADS_SCH_PARSER::skipBraceDelimitedSection( const std::vector<std::string>& aLines, size_t aStartLine )
2542{
2543 size_t i = aStartLine + 1;
2544 int braceDepth = 0;
2545 bool foundFirstBrace = false;
2546
2547 while( i < aLines.size() )
2548 {
2549 const std::string& line = aLines[i];
2550
2551 for( char c : line )
2552 {
2553 if( c == '{' )
2554 {
2555 braceDepth++;
2556 foundFirstBrace = true;
2557 }
2558 else if( c == '}' )
2559 {
2560 braceDepth--;
2561 }
2562 }
2563
2564 if( foundFirstBrace && braceDepth <= 0 )
2565 return i;
2566
2567 // A section marker before any brace means this section was empty
2568 if( !foundFirstBrace && isSectionMarker( line ) )
2569 return i - 1;
2570
2571 i++;
2572 }
2573
2574 return aLines.size() - 1;
2575}
2576
2577
2578const SYMBOL_DEF* PADS_SCH_PARSER::GetSymbolDef( const std::string& aName ) const
2579{
2580 for( const auto& sym : m_symbolDefs )
2581 {
2582 if( sym.name == aName )
2583 return &sym;
2584 }
2585
2586 return nullptr;
2587}
2588
2589
2590const PART_PLACEMENT* PADS_SCH_PARSER::GetPartPlacement( const std::string& aReference ) const
2591{
2592 for( const auto& part : m_partPlacements )
2593 {
2594 if( part.reference == aReference )
2595 return &part;
2596 }
2597
2598 return nullptr;
2599}
2600
2601
2602const SCH_SIGNAL* PADS_SCH_PARSER::GetSignal( const std::string& aName ) const
2603{
2604 for( const auto& signal : m_signals )
2605 {
2606 if( signal.name == aName )
2607 return &signal;
2608 }
2609
2610 return nullptr;
2611}
2612
2613
2615{
2616 std::set<int> sheets = GetSheetNumbers();
2617
2618 if( sheets.empty() )
2619 return 1;
2620
2621 return *sheets.rbegin();
2622}
2623
2624
2626{
2627 std::set<int> sheets;
2628
2629 for( const auto& header : m_sheetHeaders )
2630 sheets.insert( header.sheet_num );
2631
2632 for( const auto& part : m_partPlacements )
2633 sheets.insert( part.sheet_number );
2634
2635 for( const auto& signal : m_signals )
2636 {
2637 for( const auto& wire : signal.wires )
2638 sheets.insert( wire.sheet_number );
2639
2640 for( const auto& conn : signal.connections )
2641 sheets.insert( conn.sheet_number );
2642 }
2643
2644 if( sheets.empty() )
2645 sheets.insert( 1 );
2646
2647 return sheets;
2648}
2649
2650
2651std::vector<SCH_SIGNAL> PADS_SCH_PARSER::GetSignalsOnSheet( int aSheetNumber ) const
2652{
2653 std::vector<SCH_SIGNAL> result;
2654
2655 for( const auto& signal : m_signals )
2656 {
2657 SCH_SIGNAL filteredSignal;
2658 filteredSignal.name = signal.name;
2659
2660 for( const auto& wire : signal.wires )
2661 {
2662 if( wire.sheet_number == aSheetNumber )
2663 filteredSignal.wires.push_back( wire );
2664 }
2665
2666 for( const auto& conn : signal.connections )
2667 {
2668 if( conn.sheet_number == aSheetNumber )
2669 filteredSignal.connections.push_back( conn );
2670 }
2671
2672 if( !filteredSignal.wires.empty() || !filteredSignal.connections.empty() )
2673 result.push_back( filteredSignal );
2674 }
2675
2676 return result;
2677}
2678
2679
2680std::vector<PART_PLACEMENT> PADS_SCH_PARSER::GetPartsOnSheet( int aSheetNumber ) const
2681{
2682 std::vector<PART_PLACEMENT> result;
2683
2684 for( const auto& part : m_partPlacements )
2685 {
2686 if( part.sheet_number == aSheetNumber )
2687 result.push_back( part );
2688 }
2689
2690 return result;
2691}
2692
2693
2694PIN_TYPE PADS_SCH_PARSER::parsePinType( const std::string& aTypeStr )
2695{
2696 std::string upper = aTypeStr;
2697 std::transform( upper.begin(), upper.end(), upper.begin(), ::toupper );
2698
2699 if( upper == "I" || upper == "IN" || upper == "INPUT" || upper == "L" )
2700 return PIN_TYPE::INPUT;
2701
2702 if( upper == "O" || upper == "OUT" || upper == "OUTPUT" || upper == "S" )
2703 return PIN_TYPE::OUTPUT;
2704
2705 if( upper == "B" || upper == "BI" || upper == "BIDIR" || upper == "BIDIRECTIONAL" )
2707
2708 if( upper == "T" || upper == "TRI" || upper == "TRISTATE" )
2709 return PIN_TYPE::TRISTATE;
2710
2711 if( upper == "OC" || upper == "OPENCOLLECTOR" )
2713
2714 if( upper == "OE" || upper == "OPENEMITTER" )
2716
2717 if( upper == "P" || upper == "PWR" || upper == "POWER" || upper == "G" )
2718 return PIN_TYPE::POWER;
2719
2720 if( upper == "PAS" || upper == "PASSIVE" )
2721 return PIN_TYPE::PASSIVE;
2722
2723 return PIN_TYPE::UNSPECIFIED;
2724}
2725
2726
2728{
2729 switch( std::toupper( aTypeChar ) )
2730 {
2731 case 'L': return PIN_TYPE::INPUT;
2732 case 'S': return PIN_TYPE::OUTPUT;
2733 case 'B': return PIN_TYPE::BIDIRECTIONAL;
2734 case 'P': return PIN_TYPE::POWER;
2735 case 'G': return PIN_TYPE::POWER;
2736 case 'U': return PIN_TYPE::UNSPECIFIED;
2737 default: return PIN_TYPE::UNSPECIFIED;
2738 }
2739}
2740
2741} // namespace PADS_SCH
const char * name
size_t skipBraceDelimitedSection(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseSectionBUSSES(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseSectionPARTTYPE(const std::vector< std::string > &aLines, size_t aStartLine)
bool isSectionMarker(const std::string &aLine) const
bool Parse(const std::string &aFileName)
size_t parseSymbolDef(const std::vector< std::string > &aLines, size_t aStartLine, SYMBOL_DEF &aSymbol)
size_t parseSectionSCH(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseSectionCAEDECAL(const std::vector< std::string > &aLines, size_t aStartLine)
std::vector< OFF_PAGE_CONNECTOR > m_offPageConnectors
size_t parseSectionTIEDOTS(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parsePartPlacement(const std::vector< std::string > &aLines, size_t aStartLine, PART_PLACEMENT &aPart)
size_t parseSectionSHT(const std::vector< std::string > &aLines, size_t aStartLine)
std::vector< SCH_SIGNAL > m_signals
size_t parseSectionOFFPAGEREFS(const std::vector< std::string > &aLines, size_t aStartLine)
std::string extractSectionName(const std::string &aLine) const
size_t parseSectionCONNECTION(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseSectionLINES(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseGraphicPrimitive(const std::vector< std::string > &aLines, size_t aStartLine, SYMBOL_GRAPHIC &aGraphic)
static PIN_TYPE ParsePinTypeChar(char aTypeChar)
bool parseHeader(const std::string &aLine)
std::vector< SHEET_HEADER > m_sheetHeaders
size_t parseSectionNETNAMES(const std::vector< std::string > &aLines, size_t aStartLine)
std::vector< SYMBOL_DEF > m_symbolDefs
std::vector< PART_PLACEMENT > m_partPlacements
PIN_TYPE parsePinType(const std::string &aTypeStr)
std::vector< SCH_SIGNAL > GetSignalsOnSheet(int aSheetNumber) const
std::set< int > GetSheetNumbers() const
std::vector< TEXT_ITEM > m_textItems
static bool CheckFileHeader(const std::string &aFileName)
size_t parseSectionTEXT(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseSignalDef(const std::vector< std::string > &aLines, size_t aStartLine, SCH_SIGNAL &aSignal)
std::vector< NETNAME_LABEL > m_netNameLabels
std::vector< PART_PLACEMENT > GetPartsOnSheet(int aSheetNumber) const
std::vector< LINES_ITEM > m_linesItems
const SCH_SIGNAL * GetSignal(const std::string &aName) const
std::vector< TIED_DOT > m_tiedDots
std::map< std::string, PARTTYPE_DEF > m_partTypes
std::vector< BUS_DEF > m_buses
const SYMBOL_DEF * GetSymbolDef(const std::string &aName) const
const PART_PLACEMENT * GetPartPlacement(const std::string &aReference) const
size_t parseSectionCAE(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseSectionFIELDS(const std::vector< std::string > &aLines, size_t aStartLine)
size_t parseSectionPART(const std::vector< std::string > &aLines, size_t aStartLine)
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
int ParseInt(const std::string &aStr, int aDefault, const std::string &aContext)
Parse integer from string with error context.
double ParseDouble(const std::string &aStr, double aDefault, const std::string &aContext)
Parse double from string with error context.
VECTOR2I padsSchArcMidpoint(const VECTOR2I &aStart, const VECTOR2I &aEnd, const VECTOR2I &aCenter)
Midpoint of the arc through aStart and aEnd about aCenter, on the minor-arc side (the perpendicular b...
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
std::vector< FAB_LAYER_COLOR > dummy
std::vector< POINT > path
std::vector< std::string > aliases
std::vector< std::string > decal_names
std::vector< PARTTYPE_PIN > pins
std::optional< ARC_DATA > arc
std::vector< TEXT_ITEM > texts
std::vector< SYMBOL_GRAPHIC > primitives
std::vector< GATE_DEF > gates
std::vector< SPECIAL_VARIANT > special_variants
std::vector< std::string > swap_lines
std::vector< SIGPIN > sigpins
std::map< std::string, std::string > attr_overrides
std::string symbol_name
The part type, which is also the power symbol's net name.
std::string decal_name
CAEDECAL the placed gate draws, empty when unresolved.
std::vector< PART_ATTRIBUTE > attributes
std::vector< PIN_OVERRIDE > pin_overrides
std::vector< PIN_CONNECTION > connections
std::vector< WIRE_SEGMENT > wires
std::vector< SYMBOL_GRAPHIC > graphics
std::vector< SYMBOL_PIN > pins
std::vector< CAEDECAL_ATTR > attrs
std::vector< SYMBOL_TEXT > texts
std::vector< GRAPHIC_POINT > points
Wire segment connecting two endpoints through coordinate vertices.
std::vector< POINT > vertices
static EDA_ORIENTATION orientation(double d)
KIBIS_PIN * pin
int radius
VECTOR2I end
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708