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