KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_kicad_legacy_lib_cache.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 The 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 2
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 <magic_enum.hpp>
21#include <wx/log.h>
22#include <wx/tokenzr.h>
23
24#include <common.h>
25#include <kiplatform/io.h>
26#include <lib_symbol.h>
27#include <sch_shape.h>
28#include <sch_pin.h>
29#include <sch_text.h>
30#include <macros.h>
31#include <richio.h>
32#include <string_utils.h>
33#include <template_fieldnames.h>
34#include <trace_helpers.h>
36
39
40
41#define LIB_VERSION_MAJOR 2
42#define LIB_VERSION_MINOR 4
43
44#define LIB_VERSION( major, minor ) ( major * 100 + minor )
45
47#define LIBFILE_IDENT "EESchema-LIBRARY Version"
48
50#define DOCFILE_IDENT "EESchema-DOCLIB Version 2.0"
51
58#define USE_OLD_DOC_FILE_FORMAT( major, minor ) \
59 ( LIB_VERSION( major, minor ) <= LIB_VERSION( 2, 4 ) )
60
61
62const int fill_tab[3] = { 'N', 'F', 'f' };
63
64
65SCH_IO_KICAD_LEGACY_LIB_CACHE::SCH_IO_KICAD_LEGACY_LIB_CACHE( const wxString& aFullPathAndFileName ) :
66 SCH_IO_LIB_CACHE( aFullPathAndFileName )
67{
68 m_versionMajor = -1;
69 m_versionMinor = -1;
70}
71
72
74{
75 if( !m_libFileName.FileExists() )
76 {
77 THROW_IO_ERRORF( _( "Library file '%s' not found." ), m_libFileName.GetFullPath() );
78 }
79
80 wxCHECK_RET( m_libFileName.IsAbsolute(),
81 wxString::Format( "Cannot use relative file paths in legacy plugin to open library '%s'.",
82 m_libFileName.GetFullPath() ) );
83
84 wxLogTrace( traceSchLegacyPlugin, "Loading legacy symbol file '%s'", m_libFileName.GetFullPath() );
85
86 FILE_LINE_READER reader( m_libFileName.GetFullPath() );
87
88 if( !reader.ReadLine() )
89 THROW_IO_ERROR( _( "Unexpected end of file." ) );
90
91 const char* line = reader.Line();
92
93 if( !strCompare( "EESchema-LIBRARY Version", line, &line ) )
94 {
95 // Old .sym files (which are libraries with only one symbol, used to store and reuse shapes)
96 // EESchema-LIB Version x.x SYMBOL. They are valid files.
97 if( !strCompare( "EESchema-LIB Version", line, &line ) )
98 SCH_PARSE_ERROR( "file is not a valid symbol or symbol library file", reader, line );
99 }
100
101 m_versionMajor = parseInt( reader, line, &line );
102
103 if( *line == '/' )
104 {
105 // Some old libraries use a version syntax like
106 // EESchema-LIBRARY Version 2/10/2006-18:49:15
107 // use 2.3 version numer to read the file
108 m_versionMajor = 2;
109 m_versionMinor = 3;
110 }
111 else
112 {
113 if( *line != '.' )
114 SCH_PARSE_ERROR( "invalid file version formatting in header", reader, line );
115
116 line++;
117
118 m_versionMinor = parseInt( reader, line, &line );
119 }
120
122 SCH_PARSE_ERROR( "invalid file version in header", reader, line );
123
124 // Check if this is a symbol library which is the same as a symbol library but without
125 // any alias, documentation, footprint filters, etc.
126 if( strCompare( "SYMBOL", line, &line ) )
127 {
128 // Symbol files add date and time stamp info to the header.
130
132 }
133 else
134 {
136 }
137
138 while( reader.ReadLine() )
139 {
140 line = reader.Line();
141
142 if( *line == '#' || isspace( *line ) ) // Skip comments and blank lines.
143 continue;
144
145 // Headers where only supported in older library file formats.
146 if( m_libType == SCH_LIB_TYPE::LT_EESCHEMA && strCompare( "$HEADER", line ) )
147 loadHeader( reader );
148
149 if( strCompare( "DEF", line ) )
150 {
151 // Read one DEF/ENDDEF symbol entry from library:
153
154 m_symbols[ symbol->GetName() ] = symbol;
155 }
156 }
157
159
160 // Remember the file modification time of library file when the
161 // cache snapshot was made, so that in a networked environment we will
162 // reload the cache as needed.
164
166 loadDocs();
167}
168
169
171{
172 const char* line;
173 wxString text;
174 wxString aliasName;
175 wxFileName fn = m_libFileName;
176 LIB_SYMBOL* symbol = nullptr;;
177
179
180 // Not all libraries will have a document file.
181 if( !fn.FileExists() )
182 return;
183
184 if( !fn.IsFileReadable() )
185 THROW_IO_ERRORF( _( "Insufficient permissions to read library '%s'." ), fn.GetFullPath() );
186
187 FILE_LINE_READER reader( fn.GetFullPath() );
188
189 line = reader.ReadLine();
190
191 if( !line )
192 THROW_IO_ERROR( _( "symbol document library file is empty" ) );
193
194 if( !strCompare( DOCFILE_IDENT, line, &line ) )
195 SCH_PARSE_ERROR( "invalid document library file version formatting in header", reader, line );
196
197 while( reader.ReadLine() )
198 {
199 line = reader.Line();
200
201 if( *line == '#' ) // Comment line.
202 continue;
203
204 if( !strCompare( "$CMP", line, &line ) != 0 )
205 SCH_PARSE_ERROR( "$CMP command expected", reader, line );
206
207 aliasName = wxString::FromUTF8( line );
208 aliasName.Trim();
209
210 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aliasName );
211
212 if( it == m_symbols.end() )
213 {
214 wxLogTrace( traceSchLegacyPlugin, "Symbol '%s' not found in library:\n\n"
215 "'%s'\n\nat line %d offset %d",
216 aliasName,
217 fn.GetFullPath(),
218 reader.LineNumber(),
219 (int) (line - reader.Line() ) );
220 }
221 else
222 {
223 symbol = it->second;
224 }
225
226 // Read the current alias associated doc.
227 // if the alias does not exist, just skip the description
228 // (Can happen if a .dcm is not synchronized with the corresponding .lib file)
229 while( reader.ReadLine() )
230 {
231 line = reader.Line();
232
233 if( !line )
234 SCH_PARSE_ERROR( "unexpected end of file", reader, line );
235
236 if( strCompare( "$ENDCMP", line, &line ) )
237 break;
238
239 text = From_UTF8( line + 2 );
240 // Remove spaces at eol, and eol chars:
241 text = text.Trim();
242
243 switch( line[0] )
244 {
245 case 'D':
246 if( symbol )
247 symbol->SetDescription( text );
248 break;
249
250 case 'K':
251 if( symbol )
252 symbol->SetKeyWords( text );
253 break;
254
255 case 'F':
256 if( symbol )
258 break;
259
260 case 0:
261 case '\n':
262 case '\r':
263 case '#':
264 // Empty line or commment
265 break;
266
267 default:
268 SCH_PARSE_ERROR( "expected token in symbol definition", reader, line );
269 }
270 }
271 }
272}
273
274
276{
277 const char* line = aReader.Line();
278
279 wxASSERT( strCompare( "$HEADER", line, &line ) );
280
281 while( aReader.ReadLine() )
282 {
283 line = (char*) aReader;
284
285 // The time stamp saved in old library files is not used or saved in the latest
286 // library file version.
287 if( strCompare( "TimeStamp", line, &line ) )
288 continue;
289 else if( strCompare( "$ENDHEADER", line, &line ) )
290 return;
291 }
292
293 SCH_PARSE_ERROR( "$ENDHEADER not found", aReader, line );
294}
295
296
298 int aMinorVersion, LIB_SYMBOL_MAP* aMap )
299{
300 const char* line = aReader.Line();
301
302 while( *line == '#' )
303 aReader.ReadLine();
304
305 if( !strCompare( "DEF", line, &line ) )
306 SCH_PARSE_ERROR( "invalid symbol definition", aReader, line );
307
308 long num;
309 size_t pos = 4; // "DEF" plus the first space.
310 wxString utf8Line = wxString::FromUTF8( line );
311 wxStringTokenizer tokens( utf8Line, " \t\r\n" );
312
313 if( tokens.CountTokens() < 8 )
314 SCH_PARSE_ERROR( "invalid symbol definition", aReader, line );
315
316 // Read DEF line:
317 std::unique_ptr<LIB_SYMBOL> symbol = std::make_unique<LIB_SYMBOL>( wxEmptyString );
318
319 wxString name, prefix, tmp;
320
321 name = tokens.GetNextToken();
322
323 // This fixes a dubious decision to escape LIB_ID characters. Escaped LIB_IDs broke rescue
324 // library look up. Legacy LIB_IDs should not be escaped.
325 if( name != UnescapeString( name ) )
327
328 pos += name.size() + 1;
329
330 prefix = tokens.GetNextToken();
331 pos += prefix.size() + 1;
332
333 tmp = tokens.GetNextToken();
334 pos += tmp.size() + 1; // NumOfPins, unused.
335
336 tmp = tokens.GetNextToken(); // Pin name offset.
337
338 if( !tmp.ToLong( &num ) )
339 {
340 THROW_PARSE_ERROR( "invalid pin offset", aReader.GetSource(), aReader.Line(),
341 aReader.LineNumber(), pos );
342 }
343
344 pos += tmp.size() + 1;
345 symbol->SetPinNameOffset( schIUScale.MilsToIU( (int)num ) );
346
347 tmp = tokens.GetNextToken(); // Show pin numbers.
348
349 if( !( tmp == "Y" || tmp == "N") )
350 {
351 THROW_PARSE_ERROR( "expected Y or N", aReader.GetSource(), aReader.Line(),
352 aReader.LineNumber(), pos );
353 }
354
355 pos += tmp.size() + 1;
356 symbol->SetShowPinNumbers( ( tmp == "N" ) ? false : true );
357
358 tmp = tokens.GetNextToken(); // Show pin names.
359
360 if( !( tmp == "Y" || tmp == "N") )
361 {
362 THROW_PARSE_ERROR( "expected Y or N", aReader.GetSource(), aReader.Line(),
363 aReader.LineNumber(), pos );
364 }
365
366 pos += tmp.size() + 1;
367 symbol->SetShowPinNames( ( tmp == "N" ) ? false : true );
368
369 tmp = tokens.GetNextToken(); // Number of units.
370
371 if( !tmp.ToLong( &num ) )
372 THROW_PARSE_ERROR( "invalid unit count", aReader.GetSource(), aReader.Line(), aReader.LineNumber(), pos );
373
374 pos += tmp.size() + 1;
375 symbol->SetUnitCount( (int)num, true );
376
377 // Ensure m_unitCount is >= 1. Could be read as 0 in old libraries.
378 if( symbol->GetUnitCount() < 1 )
379 symbol->SetUnitCount( 1, true );
380
381 // Copy symbol name and prefix.
382
383 // The root alias is added to the alias list by SetName() which is called by SetText().
384 if( name.IsEmpty() )
385 {
386 symbol->SetName( "~" );
387 }
388 else if( name[0] != '~' )
389 {
390 symbol->SetName( name );
391 }
392 else
393 {
394 symbol->SetName( name.Right( name.Length() - 1 ) );
395 symbol->GetValueField().SetVisible( false );
396 }
397
398 // Don't set the library alias, this is determined by the symbol library table.
399 symbol->SetLibId( LIB_ID( wxEmptyString, symbol->GetName() ) );
400
401 SCH_FIELD& reference = symbol->GetReferenceField();
402
403 if( prefix == "~" )
404 {
405 reference.Empty();
406 reference.SetVisible( false );
407 }
408 else
409 {
410 reference.SetText( prefix );
411 }
412
413 // In version 2.2 and earlier, this parameter was a '0' which was just a place holder.
414 // The was no concept of interchangeable multiple unit symbols.
415 if( LIB_VERSION( aMajorVersion, aMinorVersion ) > 0
416 && LIB_VERSION( aMajorVersion, aMinorVersion ) <= LIB_VERSION( 2, 2 ) )
417 {
418 // Nothing needs to be set since the default setting for symbols with multiple
419 // units were never interchangeable. Just parse the 0 an move on.
420 tmp = tokens.GetNextToken();
421 pos += tmp.size() + 1;
422 }
423 else
424 {
425 tmp = tokens.GetNextToken();
426
427 if( tmp == "L" )
428 symbol->LockUnits( true );
429 else if( tmp == "F" || tmp == "0" )
430 symbol->LockUnits( false );
431 else
432 THROW_PARSE_ERROR( "expected L, F, or 0", aReader.GetSource(), aReader.Line(),
433 aReader.LineNumber(), pos );
434
435 pos += tmp.size() + 1;
436 }
437
438 // There is the optional power symbol flag.
439 if( tokens.HasMoreTokens() )
440 {
441 tmp = tokens.GetNextToken();
442
443 if( tmp == "P" )
444 symbol->SetGlobalPower();
445 else if( tmp == "N" )
446 symbol->SetNormal();
447 else
448 THROW_PARSE_ERROR( "expected P or N", aReader.GetSource(), aReader.Line(),
449 aReader.LineNumber(), pos );
450 }
451
452 line = aReader.ReadLine();
453
454 // Read lines until "ENDDEF" is found.
455 while( line )
456 {
457 if( *line == '#' ) // Comment
458 ;
459 else if( strCompare( "Ti", line, &line ) ) // Modification date is ignored.
460 continue;
461 else if( strCompare( "ALIAS", line, &line ) ) // Aliases
462 loadAliases( symbol, aReader, aMap );
463 else if( *line == 'F' ) // Fields
464 loadField( symbol, aReader );
465 else if( strCompare( "DRAW", line, &line ) ) // Drawing objects.
466 loadDrawEntries( symbol, aReader, aMajorVersion, aMinorVersion );
467 else if( strCompare( "$FPLIST", line, &line ) ) // Footprint filter list
468 loadFootprintFilters( symbol, aReader );
469 else if( strCompare( "ENDDEF", line, &line ) ) // End of symbol description
470 {
471 symbol->SetHasDeMorganBodyStyles( symbol->HasLegacyAlternateBodyStyle() );
472 return symbol.release();
473 }
474
475 line = aReader.ReadLine();
476 }
477
478 SCH_PARSE_ERROR( "missing ENDDEF", aReader, line );
479}
480
481
482void SCH_IO_KICAD_LEGACY_LIB_CACHE::loadAliases( std::unique_ptr<LIB_SYMBOL>& aSymbol,
483 LINE_READER& aReader,
484 LIB_SYMBOL_MAP* aMap )
485{
486 wxString newAliasName;
487 const char* line = aReader.Line();
488
489 wxCHECK_RET( strCompare( "ALIAS", line, &line ), "Invalid ALIAS section" );
490
491 wxString utf8Line = wxString::FromUTF8( line );
492 wxStringTokenizer tokens( utf8Line, " \t\r\n" );
493
494 // Parse the ALIAS list.
495 while( tokens.HasMoreTokens() )
496 {
497 newAliasName = tokens.GetNextToken();
498
499 if( aMap )
500 {
501 LIB_SYMBOL* newSymbol = new LIB_SYMBOL( newAliasName );
502
503 // Inherit the parent mandatory field attributes.
504 for( FIELD_T fieldId : MANDATORY_FIELDS )
505 {
506 SCH_FIELD* field = newSymbol->GetField( fieldId );
507 SCH_FIELD* parentField = aSymbol->GetField( fieldId );
508
509 *field = *parentField;
510
511 if( fieldId == FIELD_T::VALUE )
512 field->SetText( newAliasName );
513
514 field->SetParent( newSymbol );
515 }
516
517 newSymbol->SetParent( aSymbol.get() );
518 newSymbol->SetHasDeMorganBodyStyles( newSymbol->HasLegacyAlternateBodyStyle() );
519
520 // This will prevent duplicate aliases.
521 (*aMap)[ newSymbol->GetName() ] = newSymbol;
522 }
523 }
524}
525
526
527void SCH_IO_KICAD_LEGACY_LIB_CACHE::loadField( std::unique_ptr<LIB_SYMBOL>& aSymbol,
528 LINE_READER& aReader )
529{
530 const char* line = aReader.Line();
531
532 wxCHECK_RET( *line == 'F', "Invalid field line" );
533
534 wxString text;
535 int legacy_field_id;
536
537 if( sscanf( line + 1, "%d", &legacy_field_id ) != 1 || legacy_field_id < 0 )
538 SCH_PARSE_ERROR( "invalid field ID", aReader, line + 1 );
539
541 SCH_FIELD* field;
542
543 // Map fixed legacy IDs
544 switch( legacy_field_id )
545 {
546 case 0: id = FIELD_T::REFERENCE; break;
547 case 1: id = FIELD_T::VALUE; break;
548 case 2: id = FIELD_T::FOOTPRINT; break;
549 case 3: id = FIELD_T::DATASHEET; break;
550 }
551
552 if( id != FIELD_T::USER )
553 {
554 field = aSymbol->GetField( id );
555 }
556 else
557 {
558 field = new SCH_FIELD( aSymbol.get(), id );
559 aSymbol->AddDrawItem( field, false );
560 }
561
562 // Skip to the first double quote.
563 while( *line != '"' && *line != 0 )
564 line++;
565
566 if( *line == 0 )
567 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, line );
568
569 parseQuotedString( text, aReader, line, &line, true );
570
571 // The value field needs to be "special" escaped. The other fields are
572 // escaped normally and don't need special handling
573 if( id == FIELD_T::VALUE )
575
576 // Doctor the *.lib file field which has a "~" in blank fields. New saves will
577 // not save like this.
578 if( text.size() == 1 && text[0] == '~' )
579 field->SetText( wxEmptyString );
580 else
582
583 VECTOR2I pos;
584
585 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
586 pos.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
587 field->SetPosition( pos );
588
589 VECTOR2I textSize;
590
591 textSize.x = textSize.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
592 field->SetTextSize( textSize );
593
594 char textOrient = parseChar( aReader, line, &line );
595
596 if( textOrient == 'H' )
598 else if( textOrient == 'V' )
600 else
601 SCH_PARSE_ERROR( "invalid field text orientation parameter", aReader, line );
602
603 char textVisible = parseChar( aReader, line, &line );
604
605 if( textVisible == 'V' )
606 field->SetVisible( true );
607 else if ( textVisible == 'I' )
608 field->SetVisible( false );
609 else
610 SCH_PARSE_ERROR( "invalid field text visibility parameter", aReader, line );
611
612 // It may be technically correct to use the library version to determine if the field text
613 // attributes are present. If anyone knows if that is valid and what version that would be,
614 // please change this to test the library version rather than an EOL or the quoted string
615 // of the field name.
616 if( *line != 0 && *line != '"' )
617 {
618 char textHJustify = parseChar( aReader, line, &line );
619
620 if( textHJustify == 'C' )
622 else if( textHJustify == 'L' )
624 else if( textHJustify == 'R' )
626 else
627 SCH_PARSE_ERROR( "invalid field text horizontal justification", aReader, line );
628
629 wxString attributes;
630
631 parseUnquotedString( attributes, aReader, line, &line );
632
633 size_t attrSize = attributes.size();
634
635 if( !(attrSize == 3 || attrSize == 1 ) )
636 SCH_PARSE_ERROR( "invalid field text attributes size", aReader, line );
637
638 switch( (wxChar) attributes[0] )
639 {
640 case 'C': field->SetVertJustify( GR_TEXT_V_ALIGN_CENTER ); break;
641 case 'B': field->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM ); break;
642 case 'T': field->SetVertJustify( GR_TEXT_V_ALIGN_TOP ); break;
643 default: SCH_PARSE_ERROR( "invalid field text vertical justification", aReader, line );
644 }
645
646 if( attrSize == 3 )
647 {
648 wxChar attr_1 = attributes[1];
649 wxChar attr_2 = attributes[2];
650
651 if( attr_1 == 'I' ) // Italic
652 field->SetItalicFlag( true );
653 else if( attr_1 != 'N' ) // No italics is default, check for error.
654 SCH_PARSE_ERROR( "invalid field text italic parameter", aReader, line );
655
656 if ( attr_2 == 'B' ) // Bold
657 field->SetBoldFlag( true );
658 else if( attr_2 != 'N' ) // No bold is default, check for error.
659 SCH_PARSE_ERROR( "invalid field text bold parameter", aReader, line );
660 }
661 }
662
663 // Fields in RAM must always have names.
664 if( field->IsMandatory() )
665 {
666 // Fields in RAM must always have names, because we are trying to get
667 // less dependent on field ids and more dependent on names.
668 // Plus assumptions are made in the field editors.
669 field->SetName( GetCanonicalFieldName( field->GetId() ) );
670
671 // Ensure the VALUE field = the symbol name (can be not the case
672 // with malformed libraries: edited by hand, or converted from other tools)
673 if( id == FIELD_T::VALUE )
674 field->SetText( aSymbol->GetName() );
675 }
676 else
677 {
678 wxString fieldName = wxEmptyString;
679 parseQuotedString( fieldName, aReader, line, &line, true ); // Optional.
680
681 if( fieldName.IsEmpty() )
682 return;
683
684 wxString candidateFieldName = fieldName;
685 int suffix = 0;
686
687 //Deduplicate field name
688 while( aSymbol->GetField( candidateFieldName ) != nullptr )
689 candidateFieldName = wxString::Format( "%s_%d", fieldName, ++suffix );
690
691 field->SetName( candidateFieldName );
692 }
693}
694
695
696void SCH_IO_KICAD_LEGACY_LIB_CACHE::loadDrawEntries( std::unique_ptr<LIB_SYMBOL>& aSymbol,
697 LINE_READER& aReader,
698 int aMajorVersion,
699 int aMinorVersion )
700{
701 const char* line = aReader.Line();
702
703 wxCHECK_RET( strCompare( "DRAW", line, &line ), "Invalid DRAW section" );
704
705 line = aReader.ReadLine();
706
707 while( line )
708 {
709 if( strCompare( "ENDDRAW", line, &line ) )
710 {
711 aSymbol->GetDrawItems().sort();
712 return;
713 }
714
715 switch( line[0] )
716 {
717 case 'A': // Arc
718 aSymbol->AddDrawItem( loadArc( aReader ), false );
719 break;
720
721 case 'C': // Circle
722 aSymbol->AddDrawItem( loadCircle( aReader ), false );
723 break;
724
725 case 'T': // Text
726 aSymbol->AddDrawItem( loadText( aReader, aMajorVersion, aMinorVersion ), false );
727 break;
728
729 case 'S': // Square
730 aSymbol->AddDrawItem( loadRect( aReader ), false );
731 break;
732
733 case 'X': // Pin Description
734 aSymbol->AddDrawItem( loadPin( aSymbol, aReader ), false );
735 break;
736
737 case 'P': // Polyline
738 aSymbol->AddDrawItem( loadPolyLine( aReader ), false );
739 break;
740
741 case 'B': // Bezier Curves
742 aSymbol->AddDrawItem( loadBezier( aReader ), false );
743 break;
744
745 case '#': // Comment
746 case '\n': // Empty line
747 case '\r':
748 case 0:
749 break;
750
751 default:
752 SCH_PARSE_ERROR( "undefined DRAW entry", aReader, line );
753 }
754
755 line = aReader.ReadLine();
756 }
757
758 SCH_PARSE_ERROR( "File ended prematurely loading symbol draw element.", aReader, line );
759}
760
761
763 const char** aOutput )
764{
765 switch ( parseChar( aReader, aLine, aOutput ) )
766 {
767 case 'F': return FILL_T::FILLED_SHAPE;
768 case 'f': return FILL_T::FILLED_WITH_BG_BODYCOLOR;
769 case 'N': return FILL_T::NO_FILL;
770 default: break;
771 }
772
773 SCH_PARSE_ERROR( "invalid fill type, expected f, F, or N", aReader, aLine );
774}
775
776
781static bool MapAnglesV6( int* aAngle1, int* aAngle2 )
782{
783 auto DECIDEG2RAD = []( double deg ) -> double
784 {
785 return deg * M_PI / 1800.0;
786 };
787
788 int angle, delta;
789 double x, y;
790 bool swap = false;
791
792 delta = *aAngle2 - *aAngle1;
793
794 if( delta >= 1800 )
795 {
796 *aAngle1 -= 1;
797 *aAngle2 += 1;
798 }
799
800 x = cos( DECIDEG2RAD( *aAngle1 ) );
801 y = -sin( DECIDEG2RAD( *aAngle1 ) );
802 *aAngle1 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
803
804 x = cos( DECIDEG2RAD( *aAngle2 ) );
805 y = -sin( DECIDEG2RAD( *aAngle2 ) );
806 *aAngle2 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
807
808 NORMALIZE_ANGLE_POS( *aAngle1 );
809 NORMALIZE_ANGLE_POS( *aAngle2 );
810
811 if( *aAngle2 < *aAngle1 )
812 *aAngle2 += 3600;
813
814 if( *aAngle2 - *aAngle1 > 1800 ) // Need to swap the two angles
815 {
816 angle = ( *aAngle1 );
817 *aAngle1 = ( *aAngle2 );
818 *aAngle2 = angle;
819
820 NORMALIZE_ANGLE_POS( *aAngle1 );
821 NORMALIZE_ANGLE_POS( *aAngle2 );
822
823 if( *aAngle2 < *aAngle1 )
824 *aAngle2 += 3600;
825
826 swap = true;
827 }
828
829 if( delta >= 1800 )
830 {
831 *aAngle1 += 1;
832 *aAngle2 -= 1;
833 }
834
835 return swap;
836}
837
838
840{
841 const char* line = aReader.Line();
842
843 wxCHECK_MSG( strCompare( "A", line, &line ), nullptr, "Invalid arc definition" );
844
846
848
849 center.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
850 center.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
851
852 arc->SetPosition( center );
853
854 int radius = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
855 int angle1 = parseInt( aReader, line, &line );
856 int angle2 = parseInt( aReader, line, &line );
857
858 NORMALIZE_ANGLE_POS( angle1 );
859 NORMALIZE_ANGLE_POS( angle2 );
860
861 arc->SetUnit( parseInt( aReader, line, &line ) );
862 arc->SetBodyStyle( parseInt( aReader, line, &line ) );
863
864 STROKE_PARAMS stroke( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ),
866
867 arc->SetStroke( stroke );
868
869 // Old libraries (version <= 2.2) do not have always this FILL MODE param when fill mode
870 // is no fill (default mode).
871 if( *line != 0 )
872 arc->SetFillMode( parseFillMode( aReader, line, &line ) );
873
874 // Actual Coordinates of arc ends are read from file
875 if( *line != 0 )
876 {
877 VECTOR2I arcStart, arcEnd;
878
879 arcStart.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
880 arcStart.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
881 arcEnd.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
882 arcEnd.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
883
884 arc->SetStart( arcStart );
885 arc->SetEnd( arcEnd );
886 }
887 else
888 {
889 // Actual Coordinates of arc ends are not read from file
890 // (old library), calculate them
891 VECTOR2I arcStart( radius, 0 );
892 VECTOR2I arcEnd( radius, 0 );
893
895 arcStart += arc->GetCenter();
896 arc->SetStart( arcStart );
898 arcEnd += arc->GetCenter();
899 arc->SetEnd( arcEnd );
900 }
901
909 if( MapAnglesV6( &angle1, &angle2 ) )
910 {
911 VECTOR2I temp = arc->GetEnd();
912 arc->SetEnd( arc->GetStart() );
913 arc->SetStart( temp );
914 }
915
916 // Re-run SetArcGeometry so the internal representation matches what the s-expression
917 // loader produces. Without this, the arc center stored internally after a legacy load
918 // differs from the center recalculated by SetArcGeometry during s-expression load,
919 // causing false ERC "symbol mismatch" warnings after save and reopen.
920 arc->SetArcGeometry( arc->GetStart(), arc->GetArcMid(), arc->GetEnd() );
921
922 return arc;
923}
924
925
927{
928 const char* line = aReader.Line();
929
930 wxCHECK_MSG( strCompare( "C", line, &line ), nullptr, "Invalid circle definition" );
931
933
935
936 center.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
937 center.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
938
939 int radius = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
940
941 circle->SetStart( center );
942 circle->SetEnd( VECTOR2I( center.x + radius, center.y ) );
943 circle->SetUnit( parseInt( aReader, line, &line ) );
944 circle->SetBodyStyle( parseInt( aReader, line, &line ) );
945
946 STROKE_PARAMS stroke( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ),
948
949 circle->SetStroke( stroke );
950
951 if( *line != 0 )
952 circle->SetFillMode( parseFillMode( aReader, line, &line ) );
953
954 return circle;
955}
956
957
959 int aMajorVersion, int aMinorVersion )
960{
961 const char* line = aReader.Line();
962
963 wxCHECK_MSG( strCompare( "T", line, &line ), nullptr, "Invalid SCH_TEXT definition" );
964
965 double angleInTenths;
967 VECTOR2I size;
968 wxString str;
969 bool visible;
970 int unit;
971 int bodyStyle;
972
973 angleInTenths = parseInt( aReader, line, &line );
974
975 center.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
976 center.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
977 size.x = size.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
978 visible = !parseInt( aReader, line, &line );
979 unit = parseInt( aReader, line, &line );
980 bodyStyle = parseInt( aReader, line, &line );
981
982 // If quoted string loading fails, load as not quoted string.
983 if( *line == '"' )
984 {
985 parseQuotedString( str, aReader, line, &line );
986
987 str = ConvertToNewOverbarNotation( str );
988 }
989 else
990 {
991 parseUnquotedString( str, aReader, line, &line );
992
993 // In old libs, "spaces" are replaced by '~' in unquoted strings:
994 str.Replace( "~", " " );
995 }
996
997 if( !str.IsEmpty() )
998 {
999 // convert two apostrophes back to double quote
1000 str.Replace( "''", "\"" );
1001 }
1002
1003 SCH_ITEM* sch_item = nullptr;
1004 EDA_TEXT* eda_text = nullptr;
1005
1006 if( !visible )
1007 {
1008 SCH_FIELD* field = new SCH_FIELD( nullptr, FIELD_T::USER );
1009 sch_item = field;
1010 eda_text = field;
1011 }
1012 else
1013 {
1014 SCH_TEXT* sch_text = new SCH_TEXT( center, str, LAYER_DEVICE );
1015 sch_item = sch_text;
1016 eda_text = sch_text;
1017 }
1018
1019 eda_text->SetTextAngle( EDA_ANGLE( angleInTenths, TENTHS_OF_A_DEGREE_T ) );
1020 eda_text->SetTextSize( size );
1021 eda_text->SetTextPos( center );
1022 eda_text->SetVisible( visible );
1023 sch_item->SetUnit( unit );
1024 sch_item->SetBodyStyle( bodyStyle );
1025
1026 // Here things are murky and not well defined. At some point it appears the format
1027 // was changed to add text properties. However rather than add the token to the end of
1028 // the text definition, it was added after the string and no mention if the file
1029 // verion was bumped or not so this code make break on very old symbol libraries.
1030 //
1031 // Update: apparently even in the latest version this can be different so added a test
1032 // for end of line before checking for the text properties.
1033 if( LIB_VERSION( aMajorVersion, aMinorVersion ) > 0
1034 && LIB_VERSION( aMajorVersion, aMinorVersion ) > LIB_VERSION( 2, 0 )
1035 && !is_eol( *line ) )
1036 {
1037 if( strCompare( "Italic", line, &line ) )
1038 eda_text->SetItalicFlag( true );
1039 else if( !strCompare( "Normal", line, &line ) )
1040 SCH_PARSE_ERROR( "invalid eda_text stype, expected 'Normal' or 'Italic'", aReader, line );
1041
1042 if( parseInt( aReader, line, &line ) > 0 )
1043 eda_text->SetBoldFlag( true );
1044
1045 // Some old libaries version > 2.0 do not have these options for eda_text justification:
1046 if( !is_eol( *line ) )
1047 {
1048 switch( parseChar( aReader, line, &line ) )
1049 {
1050 case 'L': eda_text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT ); break;
1051 case 'C': eda_text->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER ); break;
1052 case 'R': eda_text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT ); break;
1053 default: SCH_PARSE_ERROR( "invalid horizontal eda_text justication; expected L, C, or R",
1054 aReader, line );
1055 }
1056
1057 switch( parseChar( aReader, line, &line ) )
1058 {
1059 case 'T': eda_text->SetVertJustify( GR_TEXT_V_ALIGN_TOP ); break;
1060 case 'C': eda_text->SetVertJustify( GR_TEXT_V_ALIGN_CENTER ); break;
1061 case 'B': eda_text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM ); break;
1062 default: SCH_PARSE_ERROR( "invalid vertical eda_text justication; expected T, C, or B",
1063 aReader, line );
1064 }
1065 }
1066 }
1067
1068 return sch_item;
1069}
1070
1071
1073{
1074 const char* line = aReader.Line();
1075
1076 wxCHECK_MSG( strCompare( "S", line, &line ), nullptr, "Invalid rectangle definition" );
1077
1079
1080 VECTOR2I pos;
1081
1082 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1083 pos.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1084 rectangle->SetPosition( pos );
1085
1086 VECTOR2I end;
1087
1088 end.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1089 end.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1090 rectangle->SetEnd( end );
1091
1092 rectangle->SetUnit( parseInt( aReader, line, &line ) );
1093 rectangle->SetBodyStyle( parseInt( aReader, line, &line ) );
1094
1095 STROKE_PARAMS stroke( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ),
1097
1098 rectangle->SetStroke( stroke );
1099
1100
1101 if( *line != 0 )
1102 rectangle->SetFillMode( parseFillMode( aReader, line, &line ) );
1103
1104 return rectangle;
1105}
1106
1107
1108SCH_PIN* SCH_IO_KICAD_LEGACY_LIB_CACHE::loadPin( std::unique_ptr<LIB_SYMBOL>& aSymbol,
1109 LINE_READER& aReader )
1110{
1111 const char* line = aReader.Line();
1112
1113 wxCHECK_MSG( strCompare( "X", line, &line ), nullptr, "Invalid SCH_PIN definition" );
1114
1115 wxString name;
1116 wxString number;
1117
1118 size_t pos = 2; // "X" plus ' ' space character.
1119 wxString tmp;
1120 wxString utf8Line = wxString::FromUTF8( line );
1121 wxStringTokenizer tokens( utf8Line, " \t\r\n" );
1122
1123 if( tokens.CountTokens() < 11 )
1124 SCH_PARSE_ERROR( "invalid pin definition", aReader, line );
1125
1126 tmp = tokens.GetNextToken();
1127 name = tmp;
1128 pos += tmp.size() + 1;
1129
1130 tmp = tokens.GetNextToken();
1131 number = tmp ;
1132 pos += tmp.size() + 1;
1133
1134 long num;
1135 VECTOR2I position;
1136
1137 tmp = tokens.GetNextToken();
1138
1139 if( !tmp.ToLong( &num ) )
1140 {
1141 THROW_PARSE_ERROR( "invalid pin X coordinate", aReader.GetSource(), aReader.Line(),
1142 aReader.LineNumber(), pos );
1143 }
1144
1145 pos += tmp.size() + 1;
1146 position.x = schIUScale.MilsToIU( (int) num );
1147
1148 tmp = tokens.GetNextToken();
1149
1150 if( !tmp.ToLong( &num ) )
1151 {
1152 THROW_PARSE_ERROR( "invalid pin Y coordinate", aReader.GetSource(), aReader.Line(),
1153 aReader.LineNumber(), pos );
1154 }
1155
1156 pos += tmp.size() + 1;
1157 position.y = -schIUScale.MilsToIU( (int) num );
1158
1159 tmp = tokens.GetNextToken();
1160
1161 if( !tmp.ToLong( &num ) )
1162 {
1163 THROW_PARSE_ERROR( "invalid pin length", aReader.GetSource(), aReader.Line(),
1164 aReader.LineNumber(), pos );
1165 }
1166
1167 pos += tmp.size() + 1;
1168 int length = schIUScale.MilsToIU( (int) num );
1169
1170
1171 tmp = tokens.GetNextToken();
1172
1173 if( tmp.size() > 1 )
1174 {
1175 THROW_PARSE_ERROR( "invalid pin orientation", aReader.GetSource(), aReader.Line(),
1176 aReader.LineNumber(), pos );
1177 }
1178
1179 pos += tmp.size() + 1;
1180
1181 PIN_ORIENTATION orientation;
1182
1183 switch( static_cast<char>( tmp[0] ) )
1184 {
1185 case 'U': orientation = PIN_ORIENTATION::PIN_UP; break;
1186 case 'D': orientation = PIN_ORIENTATION::PIN_DOWN; break;
1187 case 'L': orientation = PIN_ORIENTATION::PIN_LEFT; break;
1188 case 'R': /* fall-through */
1189 default: orientation = PIN_ORIENTATION::PIN_RIGHT; break;
1190 }
1191
1192 tmp = tokens.GetNextToken();
1193
1194 if( !tmp.ToLong( &num ) )
1195 {
1196 THROW_PARSE_ERROR( "invalid pin number text size", aReader.GetSource(), aReader.Line(),
1197 aReader.LineNumber(), pos );
1198 }
1199
1200 pos += tmp.size() + 1;
1201 int numberTextSize = schIUScale.MilsToIU( (int) num );
1202
1203 tmp = tokens.GetNextToken();
1204
1205 if( !tmp.ToLong( &num ) )
1206 {
1207 THROW_PARSE_ERROR( "invalid pin name text size", aReader.GetSource(), aReader.Line(),
1208 aReader.LineNumber(), pos );
1209 }
1210
1211 pos += tmp.size() + 1;
1212 int nameTextSize = schIUScale.MilsToIU( (int) num );
1213
1214 tmp = tokens.GetNextToken();
1215
1216 if( !tmp.ToLong( &num ) )
1217 {
1218 THROW_PARSE_ERROR( "invalid pin unit", aReader.GetSource(), aReader.Line(),
1219 aReader.LineNumber(), pos );
1220 }
1221
1222 pos += tmp.size() + 1;
1223 int unit = (int) num;
1224
1225 tmp = tokens.GetNextToken();
1226
1227 if( !tmp.ToLong( &num ) )
1228 {
1229 THROW_PARSE_ERROR( "invalid pin body style", aReader.GetSource(), aReader.Line(),
1230 aReader.LineNumber(), pos );
1231 }
1232
1233 pos += tmp.size() + 1;
1234 int bodyStyle = (int) num;
1235
1236 tmp = tokens.GetNextToken();
1237
1238 if( tmp.size() != 1 )
1239 {
1240 THROW_PARSE_ERROR( "invalid pin type", aReader.GetSource(), aReader.Line(),
1241 aReader.LineNumber(), pos );
1242 }
1243
1244 pos += tmp.size() + 1;
1245 char type = tmp[0];
1246 ELECTRICAL_PINTYPE pinType;
1247
1248 switch( type )
1249 {
1250 case 'I': pinType = ELECTRICAL_PINTYPE::PT_INPUT; break;
1251 case 'O': pinType = ELECTRICAL_PINTYPE::PT_OUTPUT; break;
1252 case 'B': pinType = ELECTRICAL_PINTYPE::PT_BIDI; break;
1253 case 'T': pinType = ELECTRICAL_PINTYPE::PT_TRISTATE; break;
1254 case 'P': pinType = ELECTRICAL_PINTYPE::PT_PASSIVE; break;
1255 case 'U': pinType = ELECTRICAL_PINTYPE::PT_UNSPECIFIED; break;
1256 case 'W': pinType = ELECTRICAL_PINTYPE::PT_POWER_IN; break;
1257 case 'w': pinType = ELECTRICAL_PINTYPE::PT_POWER_OUT; break;
1258 case 'C': pinType = ELECTRICAL_PINTYPE::PT_OPENCOLLECTOR; break;
1259 case 'E': pinType = ELECTRICAL_PINTYPE::PT_OPENEMITTER; break;
1260 case 'N': pinType = ELECTRICAL_PINTYPE::PT_NC; break;
1261 default:
1262 THROW_PARSE_ERROR( "unknown pin type", aReader.GetSource(), aReader.Line(),
1263 aReader.LineNumber(), pos );
1264 }
1265
1266 SCH_PIN* pin = new SCH_PIN( aSymbol.get(),
1269 orientation,
1270 pinType,
1271 length,
1272 nameTextSize,
1273 numberTextSize,
1274 bodyStyle,
1275 position,
1276 unit );
1277
1278 // Optional
1279 if( tokens.HasMoreTokens() ) /* Special Symbol defined */
1280 {
1281 tmp = tokens.GetNextToken();
1282
1283 enum
1284 {
1285 INVERTED = 1 << 0,
1286 CLOCK = 1 << 1,
1287 LOWLEVEL_IN = 1 << 2,
1288 LOWLEVEL_OUT = 1 << 3,
1289 FALLING_EDGE = 1 << 4,
1290 NONLOGIC = 1 << 5
1291 };
1292
1293 int flags = 0;
1294
1295 for( int j = (int) tmp.size(); j > 0; )
1296 {
1297 switch( tmp[--j].GetValue() )
1298 {
1299 case '~': break;
1300 case 'N': pin->SetVisible( false ); break;
1301 case 'I': flags |= INVERTED; break;
1302 case 'C': flags |= CLOCK; break;
1303 case 'L': flags |= LOWLEVEL_IN; break;
1304 case 'V': flags |= LOWLEVEL_OUT; break;
1305 case 'F': flags |= FALLING_EDGE; break;
1306 case 'X': flags |= NONLOGIC; break;
1307 default: THROW_PARSE_ERROR( "invalid pin attribut", aReader.GetSource(),
1308 aReader.Line(), aReader.LineNumber(), pos );
1309 }
1310
1311 pos += 1;
1312 }
1313
1314 switch( flags )
1315 {
1316 case 0: pin->SetShape( GRAPHIC_PINSHAPE::LINE ); break;
1317 case INVERTED: pin->SetShape( GRAPHIC_PINSHAPE::INVERTED ); break;
1318 case CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::CLOCK ); break;
1319 case INVERTED | CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::INVERTED_CLOCK ); break;
1320 case LOWLEVEL_IN: pin->SetShape( GRAPHIC_PINSHAPE::INPUT_LOW ); break;
1321 case LOWLEVEL_IN | CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::CLOCK_LOW ); break;
1322 case LOWLEVEL_OUT: pin->SetShape( GRAPHIC_PINSHAPE::OUTPUT_LOW ); break;
1323 case FALLING_EDGE: pin->SetShape( GRAPHIC_PINSHAPE::FALLING_EDGE_CLOCK ); break;
1324 case NONLOGIC: pin->SetShape( GRAPHIC_PINSHAPE::NONLOGIC ); break;
1325 default: SCH_PARSE_ERROR( "pin attributes do not define a valid pin shape", aReader, line );
1326 }
1327 }
1328
1329 return pin;
1330}
1331
1332
1334{
1335 const char* line = aReader.Line();
1336
1337 wxCHECK_MSG( strCompare( "P", line, &line ), nullptr, "Invalid poly definition" );
1338
1339 SCH_SHAPE* polyLine = new SCH_SHAPE( SHAPE_T::POLY, LAYER_DEVICE );
1340
1341 int points = parseInt( aReader, line, &line );
1342 polyLine->SetUnit( parseInt( aReader, line, &line ) );
1343 polyLine->SetBodyStyle( parseInt( aReader, line, &line ) );
1344
1345 STROKE_PARAMS stroke( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ), LINE_STYLE::SOLID );
1346
1347 polyLine->SetStroke( stroke );
1348
1349 VECTOR2I pt;
1350
1351 for( int i = 0; i < points; i++ )
1352 {
1353 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1354 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1355 polyLine->AddPoint( pt );
1356 }
1357
1358 if( *line != 0 )
1359 polyLine->SetFillMode( parseFillMode( aReader, line, &line ) );
1360
1361 return polyLine;
1362}
1363
1364
1366{
1367 const char* line = aReader.Line();
1368
1369 wxCHECK_MSG( strCompare( "B", line, &line ), nullptr, "Invalid Bezier definition" );
1370
1371 int points = parseInt( aReader, line, &line );
1372
1373 wxCHECK_MSG( points == 4, NULL, "Invalid Bezier curve definition" );
1374
1376
1377 bezier->SetUnit( parseInt( aReader, line, &line ) );
1378 bezier->SetBodyStyle( parseInt( aReader, line, &line ) );
1379
1380 STROKE_PARAMS stroke ( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ), LINE_STYLE::SOLID );
1381
1382 bezier->SetStroke( stroke );
1383
1384 VECTOR2I pt;
1385
1386 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1387 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1388 bezier->SetStart( pt );
1389
1390 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1391 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1392 bezier->SetBezierC1( pt );
1393
1394 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1395 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1396 bezier->SetBezierC2( pt );
1397
1398 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1399 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1400 bezier->SetEnd( pt );
1401
1403
1404 if( *line != 0 )
1405 bezier->SetFillMode( parseFillMode( aReader, line, &line ) );
1406
1407 return bezier;
1408}
1409
1410
1411void SCH_IO_KICAD_LEGACY_LIB_CACHE::loadFootprintFilters( std::unique_ptr<LIB_SYMBOL>& aSymbol,
1412 LINE_READER& aReader )
1413{
1414 const char* line = aReader.Line();
1415
1416 wxCHECK_RET( strCompare( "$FPLIST", line, &line ), "Invalid footprint filter list" );
1417
1418 line = aReader.ReadLine();
1419
1420 wxArrayString footprintFilters;
1421
1422 while( line )
1423 {
1424 if( strCompare( "$ENDFPLIST", line, &line ) )
1425 {
1426 aSymbol->SetFPFilters( footprintFilters );
1427 return;
1428 }
1429
1430 wxString footprint;
1431
1432 parseUnquotedString( footprint, aReader, line, &line );
1433 footprintFilters.Add( footprint );
1434 line = aReader.ReadLine();
1435 }
1436
1437 SCH_PARSE_ERROR( "File ended prematurely while loading footprint filters.", aReader, line );
1438}
1439
1440
1441void SCH_IO_KICAD_LEGACY_LIB_CACHE::Save( const std::optional<bool>& aOpt )
1442{
1443 wxCHECK( aOpt, /* void */ );
1444
1445 bool doSaveDocFile = *aOpt;
1446
1447 if( !m_isModified )
1448 return;
1449
1450 // Write through symlinks, don't replace them
1451 wxFileName fn = GetRealFile();
1452
1453 auto formatter = std::make_unique<FILE_OUTPUTFORMATTER>( fn.GetFullPath() );
1454 formatter->Print( 0, "%s %d.%d\n", LIBFILE_IDENT, LIB_VERSION_MAJOR, LIB_VERSION_MINOR );
1455 formatter->Print( 0, "#encoding utf-8\n");
1456
1457 for( LIB_SYMBOL_MAP::iterator it = m_symbols.begin(); it != m_symbols.end(); it++ )
1458 {
1459 if( !it->second->IsRoot() )
1460 continue;
1461
1462 SaveSymbol( it->second, *formatter.get(), &m_symbols );
1463 }
1464
1465 formatter->Print( 0, "#\n#End Library\n" );
1466 formatter->Finish();
1467 formatter.reset();
1468
1469 m_fileModTime = KIPLATFORM::IO::TimestampDir( fn.GetPath(), fn.GetFullName() );
1470 m_isModified = false;
1471
1472 if( doSaveDocFile )
1473 saveDocFile();
1474}
1475
1476
1478 LIB_SYMBOL_MAP* aMap )
1479{
1480 /*
1481 * NB:
1482 * Some of the rescue code still uses the legacy format as an intermediary, so we have
1483 * to keep this code.
1484 */
1485
1486 wxCHECK_RET( aSymbol && aSymbol->IsRoot(), "Invalid LIB_SYMBOL pointer." );
1487
1488 // LIB_ALIAS objects are deprecated but we still need to gather up the derived symbols
1489 // and save their names for the old file format.
1490 wxArrayString aliasNames;
1491
1492 if( aMap )
1493 {
1494 for( auto& entry : *aMap )
1495 {
1496 LIB_SYMBOL* symbol = entry.second;
1497
1498 if( symbol->IsDerived() && symbol->GetParent().lock() == aSymbol->SharedPtr() )
1499 aliasNames.Add( symbol->GetName() );
1500 }
1501 }
1502
1503 SCH_FIELD& value = aSymbol->GetValueField();
1504
1505 // First line: it s a comment (symbol name for readers)
1506 aFormatter.Print( 0, "#\n# %s\n#\n", TO_UTF8( value.GetText() ) );
1507
1508 // Save data
1509 aFormatter.Print( 0, "DEF" );
1510 aFormatter.Print( 0, " %s", TO_UTF8( value.GetText() ) );
1511
1512 SCH_FIELD& reference = aSymbol->GetReferenceField();
1513
1514 if( !reference.GetText().IsEmpty() )
1515 aFormatter.Print( 0, " %s", TO_UTF8( reference.GetText() ) );
1516 else
1517 aFormatter.Print( 0, " ~" );
1518
1519 aFormatter.Print( 0, " %d %d %c %c %d %c %c\n",
1520 0, schIUScale.IUToMils( aSymbol->GetPinNameOffset() ),
1521 aSymbol->GetShowPinNumbers() ? 'Y' : 'N',
1522 aSymbol->GetShowPinNames() ? 'Y' : 'N',
1523 aSymbol->GetUnitCount(), aSymbol->UnitsLocked() ? 'L' : 'F',
1524 aSymbol->IsGlobalPower() ? 'P' : 'N' );
1525
1526 timestamp_t dateModified = aSymbol->GetLastModDate();
1527
1528 if( dateModified != 0 )
1529 {
1530 int sec = dateModified & 63;
1531 int min = ( dateModified >> 6 ) & 63;
1532 int hour = ( dateModified >> 12 ) & 31;
1533 int day = ( dateModified >> 17 ) & 31;
1534 int mon = ( dateModified >> 22 ) & 15;
1535 int year = ( dateModified >> 26 ) + 1990;
1536
1537 aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
1538 }
1539
1540 std::vector<SCH_FIELD*> orderedFields;
1541 aSymbol->GetFields( orderedFields );
1542
1543 // NB: FieldIDs in legacy libraries must be consecutive, and include user fields
1544 int legacy_field_id = 0;
1545
1546 for( SCH_FIELD* field : orderedFields )
1547 saveField( field, legacy_field_id++, aFormatter );
1548
1549 // Save the alias list: a line starting by "ALIAS".
1550 if( !aliasNames.IsEmpty() )
1551 {
1552 aFormatter.Print( 0, "ALIAS" );
1553
1554 for( unsigned i = 0; i < aliasNames.GetCount(); i++ )
1555 aFormatter.Print( 0, " %s", TO_UTF8( aliasNames[i] ) );
1556
1557 aFormatter.Print( 0, "\n" );
1558 }
1559
1560 wxArrayString footprints = aSymbol->GetFPFilters();
1561
1562 // Write the footprint filter list
1563 if( footprints.GetCount() != 0 )
1564 {
1565 aFormatter.Print( 0, "$FPLIST\n" );
1566
1567 for( unsigned i = 0; i < footprints.GetCount(); i++ )
1568 aFormatter.Print( 0, " %s\n", TO_UTF8( footprints[i] ) );
1569
1570 aFormatter.Print( 0, "$ENDFPLIST\n" );
1571 }
1572
1573 // Save graphics items (including pins)
1574 if( !aSymbol->GetDrawItems().empty() )
1575 {
1576 // Sort the draw items in order to editing a file editing by hand.
1577 aSymbol->GetDrawItems().sort();
1578
1579 aFormatter.Print( 0, "DRAW\n" );
1580
1581 for( SCH_ITEM& item : aSymbol->GetDrawItems() )
1582 {
1583 switch( item.Type() )
1584 {
1585 case SCH_PIN_T:
1586 savePin( static_cast<SCH_PIN*>( &item ), aFormatter );
1587
1588 break;
1589 case SCH_TEXT_T:
1590 saveText( static_cast<SCH_TEXT*>( &item ), aFormatter );
1591 break;
1592
1593 case SCH_SHAPE_T:
1594 {
1595 SCH_SHAPE& shape = static_cast<SCH_SHAPE&>( item );
1596
1597 switch( shape.GetShape() )
1598 {
1599 case SHAPE_T::ARC: saveArc( &shape, aFormatter ); break;
1600 case SHAPE_T::BEZIER: saveBezier( &shape, aFormatter ); break;
1601 case SHAPE_T::CIRCLE: saveCircle( &shape, aFormatter ); break;
1602 case SHAPE_T::POLY: savePolyLine( &shape, aFormatter ); break;
1603 case SHAPE_T::RECTANGLE: saveRectangle( &shape, aFormatter ); break;
1604 default: break;
1605 }
1606
1607 break;
1608 }
1609
1610 default:
1611 break;
1612 }
1613 }
1614
1615 aFormatter.Print( 0, "ENDDRAW\n" );
1616 }
1617
1618 aFormatter.Print( 0, "ENDDEF\n" );
1619}
1620
1621
1623{
1624 wxCHECK_RET( aArc && aArc->GetShape() == SHAPE_T::ARC, "Invalid ARC object." );
1625
1626 EDA_ANGLE startAngle, endAngle;
1627
1628 aArc->CalcArcAngles( endAngle, startAngle );
1629 startAngle.Normalize180();
1630 endAngle.Normalize180();
1631
1632 aFormatter.Print( 0, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n",
1633 schIUScale.IUToMils( aArc->GetPosition().x ),
1634 schIUScale.IUToMils( -aArc->GetPosition().y ),
1635 schIUScale.IUToMils( aArc->GetRadius() ),
1636 startAngle.AsTenthsOfADegree(),
1637 endAngle.AsTenthsOfADegree(),
1638 aArc->GetUnit(),
1639 aArc->GetBodyStyle(),
1640 schIUScale.IUToMils( aArc->GetWidth() ),
1641 fill_tab[ static_cast<int>( aArc->GetFillMode() ) - 1 ],
1642 schIUScale.IUToMils( aArc->GetStart().x ),
1643 schIUScale.IUToMils( -aArc->GetStart().y ),
1644 schIUScale.IUToMils( aArc->GetEnd().x ),
1645 schIUScale.IUToMils( -aArc->GetEnd().y ) );
1646}
1647
1648
1650{
1651 wxCHECK_RET( aBezier && aBezier->GetShape() == SHAPE_T::BEZIER, "Invalid BEZIER object." );
1652
1653 aFormatter.Print( 0, "B 4 %d %d %d",
1654 aBezier->GetUnit(),
1655 aBezier->GetBodyStyle(),
1656 schIUScale.IUToMils( aBezier->GetWidth() ) );
1657
1658 aFormatter.Print( 0, " %d %d %d %d %d %d %d %d",
1659 schIUScale.IUToMils( aBezier->GetStart().x ),
1660 schIUScale.IUToMils( -aBezier->GetStart().y ),
1661 schIUScale.IUToMils( aBezier->GetBezierC1().x ),
1662 schIUScale.IUToMils( -aBezier->GetBezierC1().y ),
1663 schIUScale.IUToMils( aBezier->GetBezierC2().x ),
1664 schIUScale.IUToMils( -aBezier->GetBezierC2().y ),
1665 schIUScale.IUToMils( aBezier->GetEnd().x ),
1666 schIUScale.IUToMils( -aBezier->GetEnd().y ) );
1667
1668 aFormatter.Print( 0, " %c\n", fill_tab[ static_cast<int>( aBezier->GetFillMode() ) - 1 ] );
1669}
1670
1671
1673{
1674 wxCHECK_RET( aCircle && aCircle->GetShape() == SHAPE_T::CIRCLE, "Invalid CIRCLE object." );
1675
1676 aFormatter.Print( 0, "C %d %d %d %d %d %d %c\n",
1677 schIUScale.IUToMils( aCircle->GetPosition().x ),
1678 schIUScale.IUToMils( -aCircle->GetPosition().y ),
1679 schIUScale.IUToMils( aCircle->GetRadius() ),
1680 aCircle->GetUnit(),
1681 aCircle->GetBodyStyle(),
1682 schIUScale.IUToMils( aCircle->GetWidth() ),
1683 fill_tab[ static_cast<int>( aCircle->GetFillMode() ) - 1 ] );
1684}
1685
1686
1687void SCH_IO_KICAD_LEGACY_LIB_CACHE::saveField( const SCH_FIELD* aField, int aLegacyFieldIdx,
1688 OUTPUTFORMATTER& aFormatter )
1689{
1690 wxCHECK_RET( aField && aField->Type() == SCH_FIELD_T, "Invalid SCH_FIELD object." );
1691
1692 int hjustify, vjustify;
1693 wxString text = aField->GetText();
1694
1695 hjustify = 'C';
1696
1697 if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT )
1698 hjustify = 'L';
1699 else if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
1700 hjustify = 'R';
1701
1702 vjustify = 'C';
1703
1704 if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_BOTTOM )
1705 vjustify = 'B';
1706 else if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_TOP )
1707 vjustify = 'T';
1708
1709 aFormatter.Print( 0, "F%d %s %d %d %d %c %c %c %c%c%c",
1710 aLegacyFieldIdx,
1711 EscapedUTF8( text ).c_str(), // wraps in quotes
1712 schIUScale.IUToMils( aField->GetTextPos().x ),
1713 schIUScale.IUToMils( -aField->GetTextPos().y ),
1714 schIUScale.IUToMils( aField->GetTextWidth() ),
1715 aField->GetTextAngle().IsHorizontal() ? 'H' : 'V',
1716 aField->IsVisible() ? 'V' : 'I',
1717 hjustify, vjustify,
1718 aField->IsItalic() ? 'I' : 'N',
1719 aField->IsBold() ? 'B' : 'N' );
1720
1721 // Translated names were stored in legacy files, so it's important not to save the
1722 // default names as they weren't yet canonical.
1723 if( !aField->IsMandatory()
1724 && !aField->GetName().IsEmpty()
1725 && aField->GetName() != GetUserFieldName( aLegacyFieldIdx, !DO_TRANSLATE ) )
1726 {
1727 aFormatter.Print( 0, " %s", EscapedUTF8( aField->GetName() ).c_str() );
1728 }
1729
1730 aFormatter.Print( 0, "\n" );
1731}
1732
1733
1735{
1736 wxCHECK_RET( aPin && aPin->Type() == SCH_PIN_T, "Invalid SCH_PIN object." );
1737
1738 int Etype;
1739
1740 switch( aPin->GetType() )
1741 {
1742 default:
1743 case ELECTRICAL_PINTYPE::PT_INPUT: Etype = 'I'; break;
1744 case ELECTRICAL_PINTYPE::PT_OUTPUT: Etype = 'O'; break;
1745 case ELECTRICAL_PINTYPE::PT_BIDI: Etype = 'B'; break;
1746 case ELECTRICAL_PINTYPE::PT_TRISTATE: Etype = 'T'; break;
1747 case ELECTRICAL_PINTYPE::PT_PASSIVE: Etype = 'P'; break;
1748 case ELECTRICAL_PINTYPE::PT_UNSPECIFIED: Etype = 'U'; break;
1749 case ELECTRICAL_PINTYPE::PT_POWER_IN: Etype = 'W'; break;
1750 case ELECTRICAL_PINTYPE::PT_POWER_OUT: Etype = 'w'; break;
1751 case ELECTRICAL_PINTYPE::PT_OPENCOLLECTOR: Etype = 'C'; break;
1752 case ELECTRICAL_PINTYPE::PT_OPENEMITTER: Etype = 'E'; break;
1753 case ELECTRICAL_PINTYPE::PT_NC: Etype = 'N'; break;
1754 }
1755
1756 if( !aPin->GetName().IsEmpty() )
1757 aFormatter.Print( 0, "X %s", TO_UTF8( aPin->GetName() ) );
1758 else
1759 aFormatter.Print( 0, "X ~" );
1760
1761 int pin_orient = 'L'; // Printed as a char in lib file
1762
1763 switch( aPin->GetOrientation() )
1764 {
1765 case PIN_ORIENTATION::PIN_RIGHT: pin_orient = 'R'; break;
1766 case PIN_ORIENTATION::PIN_LEFT: pin_orient = 'L'; break;
1767 case PIN_ORIENTATION::PIN_UP: pin_orient = 'U'; break;
1768 case PIN_ORIENTATION::PIN_DOWN: pin_orient = 'D'; break;
1769 case PIN_ORIENTATION::INHERIT: pin_orient = 'L'; break; // Should not happens
1770 }
1771
1772 aFormatter.Print( 0, " %s %d %d %d %c %d %d %d %d %c",
1773 aPin->GetNumber().IsEmpty() ? "~" : TO_UTF8( aPin->GetNumber() ),
1774 schIUScale.IUToMils( aPin->GetPosition().x ),
1775 schIUScale.IUToMils( -aPin->GetPosition().y ),
1776 schIUScale.IUToMils( (int) aPin->GetLength() ),
1777 pin_orient,
1778 schIUScale.IUToMils( aPin->GetNumberTextSize() ),
1779 schIUScale.IUToMils( aPin->GetNameTextSize() ),
1780 aPin->GetUnit(),
1781 aPin->GetBodyStyle(),
1782 Etype );
1783
1784 if( aPin->GetShape() != GRAPHIC_PINSHAPE::LINE || !aPin->IsVisible() )
1785 aFormatter.Print( 0, " " );
1786
1787 if( !aPin->IsVisible() )
1788 aFormatter.Print( 0, "N" );
1789
1790 switch( aPin->GetShape() )
1791 {
1792 case GRAPHIC_PINSHAPE::LINE: break;
1793 case GRAPHIC_PINSHAPE::INVERTED: aFormatter.Print( 0, "I" ); break;
1794 case GRAPHIC_PINSHAPE::CLOCK: aFormatter.Print( 0, "C" ); break;
1795 case GRAPHIC_PINSHAPE::INVERTED_CLOCK: aFormatter.Print( 0, "IC" ); break;
1796 case GRAPHIC_PINSHAPE::INPUT_LOW: aFormatter.Print( 0, "L" ); break;
1797 case GRAPHIC_PINSHAPE::CLOCK_LOW: aFormatter.Print( 0, "CL" ); break;
1798 case GRAPHIC_PINSHAPE::OUTPUT_LOW: aFormatter.Print( 0, "V" ); break;
1799 case GRAPHIC_PINSHAPE::FALLING_EDGE_CLOCK: aFormatter.Print( 0, "F" ); break;
1800 case GRAPHIC_PINSHAPE::NONLOGIC: aFormatter.Print( 0, "X" ); break;
1801 default: wxFAIL_MSG( "Invalid pin shape" );
1802 }
1803
1804 aFormatter.Print( 0, "\n" );
1805
1806 const_cast<SCH_PIN*>( aPin )->ClearFlags( IS_CHANGED );
1807}
1808
1809
1811 OUTPUTFORMATTER& aFormatter )
1812{
1813 wxCHECK_RET( aPolyLine && aPolyLine->GetShape() == SHAPE_T::POLY, "Invalid POLY object." );
1814
1815 aFormatter.Print( 0, "P %d %d %d %d",
1816 (int) aPolyLine->GetPolyShape().Outline( 0 ).GetPointCount(),
1817 aPolyLine->GetUnit(),
1818 aPolyLine->GetBodyStyle(),
1819 schIUScale.IUToMils( aPolyLine->GetWidth() ) );
1820
1821 for( const VECTOR2I& pt : aPolyLine->GetPolyShape().Outline( 0 ).CPoints() )
1822 aFormatter.Print( 0, " %d %d", schIUScale.IUToMils( pt.x ), -schIUScale.IUToMils( pt.y ) );
1823
1824 aFormatter.Print( 0, " %c\n", fill_tab[ static_cast<int>( aPolyLine->GetFillMode() ) - 1 ] );
1825}
1826
1827
1829 OUTPUTFORMATTER& aFormatter )
1830{
1831 wxCHECK_RET( aRectangle && aRectangle->GetShape() == SHAPE_T::RECTANGLE,
1832 "Invalid RECT object." );
1833
1834 aFormatter.Print( 0, "S %d %d %d %d %d %d %d %c\n",
1835 schIUScale.IUToMils( aRectangle->GetPosition().x ),
1836 schIUScale.IUToMils( -aRectangle->GetPosition().y ),
1837 schIUScale.IUToMils( aRectangle->GetEnd().x ),
1838 schIUScale.IUToMils( -aRectangle->GetEnd().y ),
1839 aRectangle->GetUnit(),
1840 aRectangle->GetBodyStyle(),
1841 schIUScale.IUToMils( aRectangle->GetWidth() ),
1842 fill_tab[ static_cast<int>( aRectangle->GetFillMode() ) - 1 ] );
1843}
1844
1845
1847{
1848 wxCHECK_RET( aText && aText->Type() == SCH_TEXT_T, "Invalid SCH_TEXT object." );
1849
1850 wxString text = aText->GetText();
1851
1852 if( text.Contains( wxT( " " ) ) || text.Contains( wxT( "~" ) ) || text.Contains( wxT( "\"" ) ) )
1853 {
1854 // convert double quote to similar-looking two apostrophes
1855 text.Replace( wxT( "\"" ), wxT( "''" ) );
1856 text = wxT( "\"" ) + text + wxT( "\"" );
1857 }
1858
1859 aFormatter.Print( 0, "T %d %d %d %d %d %d %d %s",
1861 schIUScale.IUToMils( aText->GetTextPos().x ),
1862 schIUScale.IUToMils( -aText->GetTextPos().y ),
1863 schIUScale.IUToMils( aText->GetTextWidth() ),
1864 !aText->IsVisible(),
1865 aText->GetUnit(),
1866 aText->GetBodyStyle(),
1867 TO_UTF8( text ) );
1868
1869 aFormatter.Print( 0, " %s %d", aText->IsItalic() ? "Italic" : "Normal", aText->IsBold() );
1870
1871 char hjustify = 'C';
1872
1873 if( aText->GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT )
1874 hjustify = 'L';
1875 else if( aText->GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
1876 hjustify = 'R';
1877
1878 char vjustify = 'C';
1879
1880 if( aText->GetVertJustify() == GR_TEXT_V_ALIGN_BOTTOM )
1881 vjustify = 'B';
1882 else if( aText->GetVertJustify() == GR_TEXT_V_ALIGN_TOP )
1883 vjustify = 'T';
1884
1885 aFormatter.Print( 0, " %c %c\n", hjustify, vjustify );
1886}
1887
1888
1890{
1891 /*
1892 * NB:
1893 * Some of the rescue code still uses the legacy format as an intermediary, so we have
1894 * to keep this code.
1895 */
1896
1897 wxFileName fileName = m_libFileName;
1898
1900 FILE_OUTPUTFORMATTER formatter( fileName.GetFullPath() );
1901
1902 formatter.Print( 0, "%s\n", DOCFILE_IDENT );
1903
1904 for( LIB_SYMBOL_MAP::iterator it = m_symbols.begin(); it != m_symbols.end(); ++it )
1905 {
1906 wxString description = it->second->GetDescription();
1907 wxString keyWords = it->second->GetKeyWords();
1908 wxString docFileName = it->second->GetDatasheetField().GetText();
1909
1910 if( description.IsEmpty() && keyWords.IsEmpty() && docFileName.IsEmpty() )
1911 continue;
1912
1913 formatter.Print( 0, "#\n$CMP %s\n", TO_UTF8( it->second->GetName() ) );
1914
1915 if( !description.IsEmpty() )
1916 formatter.Print( 0, "D %s\n", TO_UTF8( description ) );
1917
1918 if( !keyWords.IsEmpty() )
1919 formatter.Print( 0, "K %s\n", TO_UTF8( keyWords ) );
1920
1921 if( !docFileName.IsEmpty() )
1922 formatter.Print( 0, "F %s\n", TO_UTF8( docFileName ) );
1923
1924 formatter.Print( 0, "$ENDCMP\n" );
1925 }
1926
1927 formatter.Print( 0, "#\n#End Doc Library\n" );
1928 formatter.Finish();
1929}
1930
1931
1932void SCH_IO_KICAD_LEGACY_LIB_CACHE::DeleteSymbol( const wxString& aSymbolName )
1933{
1934 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aSymbolName );
1935
1936 if( it == m_symbols.end() )
1937 {
1938 THROW_IO_ERRORF( _( "library %s does not contain a symbol named %s" ),
1939 m_libFileName.GetFullName(), aSymbolName );
1940 }
1941
1942 LIB_SYMBOL* symbol = it->second;
1943
1944 if( symbol->IsRoot() )
1945 {
1946 LIB_SYMBOL* rootSymbol = symbol;
1947
1948 // Remove the root symbol and all its children.
1949 m_symbols.erase( it );
1950
1951 LIB_SYMBOL_MAP::iterator it1 = m_symbols.begin();
1952
1953 while( it1 != m_symbols.end() )
1954 {
1955 if( it1->second->IsDerived()
1956 && it1->second->GetParent().lock() == rootSymbol->SharedPtr() )
1957 {
1958 delete it1->second;
1959 it1 = m_symbols.erase( it1 );
1960 }
1961 else
1962 {
1963 it1++;
1964 }
1965 }
1966
1967 delete rootSymbol;
1968 }
1969 else
1970 {
1971 // Just remove the alias.
1972 m_symbols.erase( it );
1973 delete symbol;
1974 }
1975
1977 m_isModified = true;
1978}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
constexpr double ARC_LOW_DEF_MM
Definition base_units.h:127
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
int AsTenthsOfADegree() const
Definition eda_angle.h:118
bool IsHorizontal() const
Definition eda_angle.h:142
EDA_ANGLE Normalize180()
Definition eda_angle.h:268
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:89
virtual void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:244
const VECTOR2I & GetBezierC2() const
Definition eda_shape.h:283
FILL_T GetFillMode() const
Definition eda_shape.h:158
SHAPE_POLY_SET & GetPolyShape()
void CalcArcAngles(EDA_ANGLE &aStartAngle, EDA_ANGLE &aEndAngle) const
Calc arc start and end angles such that aStartAngle < aEndAngle.
int GetRadius() const
SHAPE_T GetShape() const
Definition eda_shape.h:185
virtual void SetBezierC2(const VECTOR2I &aPt)
Definition eda_shape.h:282
virtual void SetBezierC1(const VECTOR2I &aPt)
Definition eda_shape.h:279
void RebuildBezierToSegmentsPointsList(int aMaxError)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:240
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:190
void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the three controlling points for an arc.
const VECTOR2I & GetBezierC1() const
Definition eda_shape.h:280
virtual int GetWidth() const
Definition eda_shape.h:173
void SetFillMode(FILL_T aFill)
virtual void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:194
VECTOR2I GetArcMid() const
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:89
virtual VECTOR2I GetTextPos() const
Definition eda_text.h:294
bool IsItalic() const
Definition eda_text.h:190
virtual void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:532
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:110
virtual bool IsVisible() const
Definition eda_text.h:208
virtual void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:576
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:412
virtual int GetTextWidth() const
Definition eda_text.h:285
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:221
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition eda_text.cpp:373
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:381
void Empty()
Definition eda_text.cpp:617
virtual EDA_ANGLE GetTextAngle() const
Definition eda_text.h:168
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition eda_text.cpp:322
bool IsBold() const
Definition eda_text.h:205
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:224
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:294
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:404
A LINE_READER that reads from an open file.
Definition richio.h:154
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:204
Used for text file output.
Definition richio.h:470
bool Finish() override
Flushes the temp file to disk and atomically renames it over the final target path.
Definition richio.cpp:636
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
Define a library symbol object.
Definition lib_symbol.h:114
std::weak_ptr< LIB_SYMBOL > & GetParent()
Definition lib_symbol.h:145
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
bool UnitsLocked() const
Check whether symbol units are interchangeable.
Definition lib_symbol.h:366
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition lib_symbol.h:230
bool IsDerived() const
Definition lib_symbol.h:231
timestamp_t GetLastModDate() const
Definition lib_symbol.h:238
SCH_FIELD * GetField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName; return nullptr if not found.
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:809
void SetParent(LIB_SYMBOL *aParent=nullptr)
wxString GetName() const override
Definition lib_symbol.h:176
void SetDescription(const wxString &aDescription)
Gets the Description field text value *‍/.
void SetKeyWords(const wxString &aKeyWords)
SCH_FIELD & GetValueField()
Return reference to the value field.
Definition lib_symbol.h:428
wxArrayString GetFPFilters() const
Definition lib_symbol.h:242
bool HasLegacyAlternateBodyStyle() const
Before V10 we didn't store the number of body styles in a symbol – we just looked through all its dra...
std::shared_ptr< LIB_SYMBOL > SharedPtr() const
http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared.
Definition lib_symbol.h:123
void SetHasDeMorganBodyStyles(bool aFlag)
Definition lib_symbol.h:882
bool IsGlobalPower() const override
int GetUnitCount() const override
SCH_FIELD & GetReferenceField()
Return reference to the reference designator field.
Definition lib_symbol.h:432
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:62
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition richio.h:90
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition richio.h:116
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:98
bool empty(int aType=UNDEFINED_TYPE) const
An interface used to output 8 bit text in a convenient way.
Definition richio.h:291
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:418
bool IsMandatory() const
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:128
FIELD_T GetId() const
Definition sch_field.h:132
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
void SetPosition(const VECTOR2I &aPosition) override
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
SCH_IO_KICAD_LEGACY_LIB_CACHE(const wxString &aLibraryPath)
static void loadDrawEntries(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader, int aMajorVersion, int aMinorVersion)
void loadHeader(FILE_LINE_READER &aReader)
static SCH_SHAPE * loadBezier(LINE_READER &aReader)
static void saveField(const SCH_FIELD *aField, int aLegacyFieldIdx, OUTPUTFORMATTER &aFormatter)
static void saveArc(SCH_SHAPE *aArc, OUTPUTFORMATTER &aFormatter)
static void saveCircle(SCH_SHAPE *aCircle, OUTPUTFORMATTER &aFormatter)
static SCH_SHAPE * loadArc(LINE_READER &aReader)
static void loadField(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
static LIB_SYMBOL * LoadPart(LINE_READER &aReader, int aMajorVersion, int aMinorVersion, LIB_SYMBOL_MAP *aMap=nullptr)
static void savePolyLine(SCH_SHAPE *aPolyLine, OUTPUTFORMATTER &aFormatter)
static SCH_SHAPE * loadPolyLine(LINE_READER &aReader)
static void saveBezier(SCH_SHAPE *aBezier, OUTPUTFORMATTER &aFormatter)
static SCH_SHAPE * loadCircle(LINE_READER &aReader)
static SCH_ITEM * loadText(LINE_READER &aReader, int aMajorVersion, int aMinorVersion)
void Save(const std::optional< bool > &aOpt) override
Save the entire library to file m_libFileName;.
static SCH_SHAPE * loadRect(LINE_READER &aReader)
static void loadAliases(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader, LIB_SYMBOL_MAP *aMap=nullptr)
static void loadFootprintFilters(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
void DeleteSymbol(const wxString &aName) override
static void savePin(const SCH_PIN *aPin, OUTPUTFORMATTER &aFormatter)
static SCH_PIN * loadPin(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
static FILL_T parseFillMode(LINE_READER &aReader, const char *aLine, const char **aOutput)
static void saveRectangle(SCH_SHAPE *aRectangle, OUTPUTFORMATTER &aFormatter)
static void SaveSymbol(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter, LIB_SYMBOL_MAP *aMap=nullptr)
static void saveText(const SCH_TEXT *aText, OUTPUTFORMATTER &aFormatter)
SCH_LIB_TYPE m_libType
LIB_SYMBOL_MAP m_symbols
wxFileName GetRealFile() const
long long GetLibModificationTime()
SCH_IO_LIB_CACHE(const wxString &aLibraryPath)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
virtual void SetBodyStyle(int aBodyStyle)
Definition sch_item.h:241
int GetBodyStyle() const
Definition sch_item.h:242
int GetUnit() const
Definition sch_item.h:233
virtual void SetUnit(int aUnit)
Definition sch_item.h:232
int GetNumberTextSize() const
Definition sch_pin.cpp:850
int GetLength() const
Definition sch_pin.cpp:393
bool IsVisible() const
Definition sch_pin.cpp:485
const wxString & GetName() const
Definition sch_pin.cpp:499
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:358
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:350
int GetNameTextSize() const
Definition sch_pin.cpp:824
const wxString & GetNumber() const
Definition sch_pin.h:130
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:372
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:407
void SetPosition(const VECTOR2I &aPos) override
Definition sch_shape.h:85
void SetStroke(const STROKE_PARAMS &aStroke) override
Definition sch_shape.cpp:98
VECTOR2I GetCenter() const
Definition sch_shape.h:92
void AddPoint(const VECTOR2I &aPosition)
VECTOR2I GetPosition() const override
Definition sch_shape.h:84
virtual size_t GetPointCount() const override
const std::vector< VECTOR2I > & CPoints() const
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
Simple container to manage line stroke parameters.
int GetPinNameOffset() const
Definition symbol.h:159
virtual bool GetShowPinNames() const
Definition symbol.h:165
virtual bool GetShowPinNumbers() const
Definition symbol.h:171
The common library.
#define _(s)
@ TENTHS_OF_A_DEGREE_T
Definition eda_angle.h:30
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:408
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:407
#define IS_CHANGED
Item was edited, and modified.
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:47
FILL_T
Definition eda_shape.h:59
@ NO_FILL
Definition eda_shape.h:60
@ FILLED_WITH_BG_BODYCOLOR
Definition eda_shape.h:62
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:61
static const std::string LegacySymbolDocumentFileExtension
const wxChar *const traceSchLegacyPlugin
Flag to enable legacy schematic plugin debug output.
std::chrono::steady_clock CLOCK
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition kiid.h:43
@ LAYER_DEVICE
Definition layer_ids.h:472
This file contains miscellaneous commonly used macros and functions.
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
Computes a hash of modification times and sizes for files matching a pattern.
Definition unix/io.cpp:123
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:33
@ PT_NC
not connected (must be left open)
Definition pin_type.h:46
@ PT_OUTPUT
usual output
Definition pin_type.h:34
@ PT_TRISTATE
tri state bus pin
Definition pin_type.h:36
@ PT_BIDI
input or output (like port for a microprocessor)
Definition pin_type.h:35
@ PT_OPENEMITTER
pin type open emitter
Definition pin_type.h:45
@ PT_POWER_OUT
output of a regulator: intended to be connected to power input pins
Definition pin_type.h:43
@ PT_OPENCOLLECTOR
pin type open collector
Definition pin_type.h:44
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:42
@ PT_UNSPECIFIED
unknown electrical properties: creates always a warning when connected
Definition pin_type.h:41
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:39
PIN_ORIENTATION
The symbol library pin object orientations.
Definition pin_type.h:101
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:123
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:114
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:131
int parseInt(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parse an ASCII integer string with possible leading whitespace into an integer and updates the pointe...
bool strCompare(const char *aString, const char *aLine, const char **aOutput)
Compare aString to the string starting at aLine and advances the character point to the end of String...
void parseQuotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken, bool aCanBeEmpty)
Parse an quoted ASCII utf8 and updates the pointer at aOutput if it is not NULL.
void parseUnquotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken, bool aCanBeEmpty)
Parse an unquoted utf8 string and updates the pointer at aOutput if it is not NULL.
bool is_eol(char c)
char parseChar(LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken)
Parse a single ASCII character and updates the pointer at aOutput if it is not NULL.
#define SCH_PARSE_ERROR(text, reader, pos)
static bool MapAnglesV6(int *aAngle1, int *aAngle2)
This function based on version 6.0 is required for reading legacy arcs.
#define LIB_VERSION_MINOR
Legacy symbol library minor version.
const int fill_tab[3]
#define LIBFILE_IDENT
Legacy symbol library (.lib) file header.
#define USE_OLD_DOC_FILE_FORMAT(major, minor)
Library versions 2.4 and lower use the old separate library (.lib) and document (....
#define LIB_VERSION(major, minor)
#define DOCFILE_IDENT
Legacy symbol library document (.dcm) file header.
#define LIB_VERSION_MAJOR
Legacy symbol library major version.
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
wxString UnescapeString(const wxString &aSource)
wxString From_UTF8(const char *cstring)
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
@ CTX_QUOTED_STR
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
wxString GetUserFieldName(int aFieldNdx, bool aTranslateForHI)
#define DO_TRANSLATE
#define MANDATORY_FIELDS
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
wxString GetCanonicalFieldName(FIELD_T aFieldType)
KIBIS_PIN * pin
VECTOR2I center
int radius
VECTOR2I end
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
int delta
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
#define M_PI
wxLogTrace helper definitions.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:225
void NORMALIZE_ANGLE_POS(T &Angle)
Definition trigo.h:183
double RAD2DECIDEG(double rad)
Definition trigo.h:166
@ SCH_FIELD_T
Definition typeinfo.h:147
@ SCH_SHAPE_T
Definition typeinfo.h:146
@ SCH_TEXT_T
Definition typeinfo.h:148
@ SCH_PIN_T
Definition typeinfo.h:150
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
Definition of file extensions used in Kicad.