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