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