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