KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_kicad_legacy_lib_cache.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <magic_enum.hpp>
21#include <wx/log.h>
22
23#include <lib_symbol.h>
24#include <sch_shape.h>
25#include <sch_pin.h>
26#include <sch_text.h>
27#include <macros.h>
28#include <richio.h>
29#include <string_utils.h>
30#include <template_fieldnames.h>
31#include <trace_helpers.h>
33
36
37
38#define LIB_VERSION_MAJOR 2
39#define LIB_VERSION_MINOR 4
40
41#define LIB_VERSION( major, minor ) ( major * 100 + minor )
42
44#define LIBFILE_IDENT "EESchema-LIBRARY Version"
45
47#define DOCFILE_IDENT "EESchema-DOCLIB Version 2.0"
48
55#define USE_OLD_DOC_FILE_FORMAT( major, minor ) \
56 ( LIB_VERSION( major, minor ) <= LIB_VERSION( 2, 4 ) )
57
58
59const int fill_tab[3] = { 'N', 'F', 'f' };
60
61
62SCH_IO_KICAD_LEGACY_LIB_CACHE::SCH_IO_KICAD_LEGACY_LIB_CACHE( const wxString& aFullPathAndFileName ) :
63 SCH_IO_LIB_CACHE( aFullPathAndFileName )
64{
65 m_versionMajor = -1;
66 m_versionMinor = -1;
67}
68
69
71{
72 if( !m_libFileName.FileExists() )
73 {
74 THROW_IO_ERROR( wxString::Format( _( "Library file '%s' not found." ),
75 m_libFileName.GetFullPath() ) );
76 }
77
78 wxCHECK_RET( m_libFileName.IsAbsolute(),
79 wxString::Format( "Cannot use relative file paths in legacy plugin to "
80 "open library '%s'.", m_libFileName.GetFullPath() ) );
81
82 wxLogTrace( traceSchLegacyPlugin, "Loading legacy symbol file '%s'",
83 m_libFileName.GetFullPath() );
84
85 FILE_LINE_READER reader( m_libFileName.GetFullPath() );
86
87 if( !reader.ReadLine() )
88 THROW_IO_ERROR( _( "Unexpected end of file." ) );
89
90 const char* line = reader.Line();
91
92 if( !strCompare( "EESchema-LIBRARY Version", line, &line ) )
93 {
94 // Old .sym files (which are libraries with only one symbol, used to store and reuse shapes)
95 // EESchema-LIB Version x.x SYMBOL. They are valid files.
96 if( !strCompare( "EESchema-LIB Version", line, &line ) )
97 SCH_PARSE_ERROR( "file is not a valid symbol or symbol library file", reader, line );
98 }
99
100 m_versionMajor = parseInt( reader, line, &line );
101
102 if( *line == '/' )
103 {
104 // Some old libraries use a version syntax like
105 // EESchema-LIBRARY Version 2/10/2006-18:49:15
106 // use 2.3 version numer to read the file
107 m_versionMajor = 2;
108 m_versionMinor = 3;
109 }
110 else
111 {
112 if( *line != '.' )
113 SCH_PARSE_ERROR( "invalid file version formatting in header", reader, line );
114
115 line++;
116
117 m_versionMinor = parseInt( reader, line, &line );
118 }
119
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 {
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 )
262 symbol->GetField( FIELD_T::DATASHEET )->SetText( text );
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, " \r\n\t" );
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 THROW_PARSE_ERROR( "expected Y or N", aReader.GetSource(), aReader.Line(),
356 aReader.LineNumber(), pos );
357
358 pos += tmp.size() + 1;
359 symbol->SetShowPinNumbers( ( tmp == "N" ) ? false : true );
360
361 tmp = tokens.GetNextToken(); // Show pin names.
362
363 if( !( tmp == "Y" || tmp == "N") )
364 {
365 THROW_PARSE_ERROR( "expected Y or N", aReader.GetSource(), aReader.Line(),
366 aReader.LineNumber(), pos );
367 }
368
369 pos += tmp.size() + 1;
370 symbol->SetShowPinNames( ( tmp == "N" ) ? false : true );
371
372 tmp = tokens.GetNextToken(); // Number of units.
373
374 if( !tmp.ToLong( &num ) )
375 {
376 THROW_PARSE_ERROR( "invalid unit count", aReader.GetSource(), aReader.Line(),
377 aReader.LineNumber(), pos );
378 }
379
380 pos += tmp.size() + 1;
381 symbol->SetUnitCount( (int)num );
382
383 // Ensure m_unitCount is >= 1. Could be read as 0 in old libraries.
384 if( symbol->GetUnitCount() < 1 )
385 symbol->SetUnitCount( 1 );
386
387 // Copy symbol name and prefix.
388
389 // The root alias is added to the alias list by SetName() which is called by SetText().
390 if( name.IsEmpty() )
391 {
392 symbol->SetName( "~" );
393 }
394 else if( name[0] != '~' )
395 {
396 symbol->SetName( name );
397 }
398 else
399 {
400 symbol->SetName( name.Right( name.Length() - 1 ) );
401 symbol->GetValueField().SetVisible( false );
402 }
403
404 // Don't set the library alias, this is determined by the symbol library table.
405 symbol->SetLibId( LIB_ID( wxEmptyString, symbol->GetName() ) );
406
407 SCH_FIELD& reference = symbol->GetReferenceField();
408
409 if( prefix == "~" )
410 {
411 reference.Empty();
412 reference.SetVisible( false );
413 }
414 else
415 {
416 reference.SetText( prefix );
417 }
418
419 // In version 2.2 and earlier, this parameter was a '0' which was just a place holder.
420 // The was no concept of interchangeable multiple unit symbols.
421 if( LIB_VERSION( aMajorVersion, aMinorVersion ) > 0
422 && LIB_VERSION( aMajorVersion, aMinorVersion ) <= LIB_VERSION( 2, 2 ) )
423 {
424 // Nothing needs to be set since the default setting for symbols with multiple
425 // units were never interchangeable. Just parse the 0 an move on.
426 tmp = tokens.GetNextToken();
427 pos += tmp.size() + 1;
428 }
429 else
430 {
431 tmp = tokens.GetNextToken();
432
433 if( tmp == "L" )
434 symbol->LockUnits( true );
435 else if( tmp == "F" || tmp == "0" )
436 symbol->LockUnits( false );
437 else
438 THROW_PARSE_ERROR( "expected L, F, or 0", aReader.GetSource(), aReader.Line(),
439 aReader.LineNumber(), pos );
440
441 pos += tmp.size() + 1;
442 }
443
444 // There is the optional power symbol flag.
445 if( tokens.HasMoreTokens() )
446 {
447 tmp = tokens.GetNextToken();
448
449 if( tmp == "P" )
450 symbol->SetGlobalPower();
451 else if( tmp == "N" )
452 symbol->SetNormal();
453 else
454 THROW_PARSE_ERROR( "expected P or N", aReader.GetSource(), aReader.Line(),
455 aReader.LineNumber(), pos );
456 }
457
458 line = aReader.ReadLine();
459
460 // Read lines until "ENDDEF" is found.
461 while( line )
462 {
463 if( *line == '#' ) // Comment
464 ;
465 else if( strCompare( "Ti", line, &line ) ) // Modification date is ignored.
466 continue;
467 else if( strCompare( "ALIAS", line, &line ) ) // Aliases
468 loadAliases( symbol, aReader, aMap );
469 else if( *line == 'F' ) // Fields
470 loadField( symbol, aReader );
471 else if( strCompare( "DRAW", line, &line ) ) // Drawing objects.
472 loadDrawEntries( symbol, aReader, aMajorVersion, aMinorVersion );
473 else if( strCompare( "$FPLIST", line, &line ) ) // Footprint filter list
474 loadFootprintFilters( symbol, aReader );
475 else if( strCompare( "ENDDEF", line, &line ) ) // End of symbol description
476 {
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, " \r\n\t" );
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
524 // This will prevent duplicate aliases.
525 (*aMap)[ newSymbol->GetName() ] = newSymbol;
526 }
527 }
528}
529
530
531void SCH_IO_KICAD_LEGACY_LIB_CACHE::loadField( std::unique_ptr<LIB_SYMBOL>& aSymbol,
532 LINE_READER& aReader )
533{
534 const char* line = aReader.Line();
535
536 wxCHECK_RET( *line == 'F', "Invalid field line" );
537
538 wxString text;
539 int legacy_field_id;
540
541 if( sscanf( line + 1, "%d", &legacy_field_id ) != 1 || legacy_field_id < 0 )
542 SCH_PARSE_ERROR( "invalid field ID", aReader, line + 1 );
543
544 FIELD_T id = FIELD_T::USER;
545 SCH_FIELD* field;
546
547 // Map fixed legacy IDs
548 switch( legacy_field_id )
549 {
550 case 0: id = FIELD_T::REFERENCE; break;
551 case 1: id = FIELD_T::VALUE; break;
552 case 2: id = FIELD_T::FOOTPRINT; break;
553 case 3: id = FIELD_T::DATASHEET; break;
554 }
555
556 if( id != FIELD_T::USER )
557 {
558 field = aSymbol->GetField( id );
559 }
560 else
561 {
562 field = new SCH_FIELD( aSymbol.get(), id );
563 aSymbol->AddDrawItem( field, false );
564 }
565
566 // Skip to the first double quote.
567 while( *line != '"' && *line != 0 )
568 line++;
569
570 if( *line == 0 )
571 SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, line );
572
573 parseQuotedString( text, aReader, line, &line, true );
574
575 // The value field needs to be "special" escaped. The other fields are
576 // escaped normally and don't need special handling
577 if( id == FIELD_T::VALUE )
579
580 // Doctor the *.lib file field which has a "~" in blank fields. New saves will
581 // not save like this.
582 if( text.size() == 1 && text[0] == '~' )
583 field->SetText( wxEmptyString );
584 else
586
587 VECTOR2I pos;
588
589 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
590 pos.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
591 field->SetPosition( pos );
592
593 VECTOR2I textSize;
594
595 textSize.x = textSize.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
596 field->SetTextSize( textSize );
597
598 char textOrient = parseChar( aReader, line, &line );
599
600 if( textOrient == 'H' )
602 else if( textOrient == 'V' )
604 else
605 SCH_PARSE_ERROR( "invalid field text orientation parameter", aReader, line );
606
607 char textVisible = parseChar( aReader, line, &line );
608
609 if( textVisible == 'V' )
610 field->SetVisible( true );
611 else if ( textVisible == 'I' )
612 field->SetVisible( false );
613 else
614 SCH_PARSE_ERROR( "invalid field text visibility parameter", aReader, line );
615
616 // It may be technically correct to use the library version to determine if the field text
617 // attributes are present. If anyone knows if that is valid and what version that would be,
618 // please change this to test the library version rather than an EOL or the quoted string
619 // of the field name.
620 if( *line != 0 && *line != '"' )
621 {
622 char textHJustify = parseChar( aReader, line, &line );
623
624 if( textHJustify == 'C' )
626 else if( textHJustify == 'L' )
628 else if( textHJustify == 'R' )
630 else
631 SCH_PARSE_ERROR( "invalid field text horizontal justification", aReader, line );
632
633 wxString attributes;
634
635 parseUnquotedString( attributes, aReader, line, &line );
636
637 size_t attrSize = attributes.size();
638
639 if( !(attrSize == 3 || attrSize == 1 ) )
640 SCH_PARSE_ERROR( "invalid field text attributes size", aReader, line );
641
642 switch( (wxChar) attributes[0] )
643 {
644 case 'C': field->SetVertJustify( GR_TEXT_V_ALIGN_CENTER ); break;
645 case 'B': field->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM ); break;
646 case 'T': field->SetVertJustify( GR_TEXT_V_ALIGN_TOP ); break;
647 default: SCH_PARSE_ERROR( "invalid field text vertical justification", aReader, line );
648 }
649
650 if( attrSize == 3 )
651 {
652 wxChar attr_1 = attributes[1];
653 wxChar attr_2 = attributes[2];
654
655 if( attr_1 == 'I' ) // Italic
656 field->SetItalicFlag( true );
657 else if( attr_1 != 'N' ) // No italics is default, check for error.
658 SCH_PARSE_ERROR( "invalid field text italic parameter", aReader, line );
659
660 if ( attr_2 == 'B' ) // Bold
661 field->SetBoldFlag( true );
662 else if( attr_2 != 'N' ) // No bold is default, check for error.
663 SCH_PARSE_ERROR( "invalid field text bold parameter", aReader, line );
664 }
665 }
666
667 // Fields in RAM must always have names.
668 if( field->IsMandatory() )
669 {
670 // Fields in RAM must always have names, because we are trying to get
671 // less dependent on field ids and more dependent on names.
672 // Plus assumptions are made in the field editors.
673 field->SetName( GetCanonicalFieldName( field->GetId() ) );
674
675 // Ensure the VALUE field = the symbol name (can be not the case
676 // with malformed libraries: edited by hand, or converted from other tools)
677 if( id == FIELD_T::VALUE )
678 field->SetText( aSymbol->GetName() );
679 }
680 else
681 {
682 wxString fieldName = wxEmptyString;
683 parseQuotedString( fieldName, aReader, line, &line, true ); // Optional.
684
685 if( fieldName.IsEmpty() )
686 return;
687
688 wxString candidateFieldName = fieldName;
689 int suffix = 0;
690
691 //Deduplicate field name
692 while( aSymbol->GetField( candidateFieldName ) != nullptr )
693 candidateFieldName = wxString::Format( "%s_%d", fieldName, ++suffix );
694
695 field->SetName( candidateFieldName );
696 }
697}
698
699
700void SCH_IO_KICAD_LEGACY_LIB_CACHE::loadDrawEntries( std::unique_ptr<LIB_SYMBOL>& aSymbol,
701 LINE_READER& aReader,
702 int aMajorVersion,
703 int aMinorVersion )
704{
705 const char* line = aReader.Line();
706
707 wxCHECK_RET( strCompare( "DRAW", line, &line ), "Invalid DRAW section" );
708
709 line = aReader.ReadLine();
710
711 while( line )
712 {
713 if( strCompare( "ENDDRAW", line, &line ) )
714 {
715 aSymbol->GetDrawItems().sort();
716 return;
717 }
718
719 switch( line[0] )
720 {
721 case 'A': // Arc
722 aSymbol->AddDrawItem( loadArc( aReader ), false );
723 break;
724
725 case 'C': // Circle
726 aSymbol->AddDrawItem( loadCircle( aReader ), false );
727 break;
728
729 case 'T': // Text
730 aSymbol->AddDrawItem( loadText( aReader, aMajorVersion, aMinorVersion ), false );
731 break;
732
733 case 'S': // Square
734 aSymbol->AddDrawItem( loadRect( aReader ), false );
735 break;
736
737 case 'X': // Pin Description
738 aSymbol->AddDrawItem( loadPin( aSymbol, aReader ), false );
739 break;
740
741 case 'P': // Polyline
742 aSymbol->AddDrawItem( loadPolyLine( aReader ), false );
743 break;
744
745 case 'B': // Bezier Curves
746 aSymbol->AddDrawItem( loadBezier( aReader ), false );
747 break;
748
749 case '#': // Comment
750 case '\n': // Empty line
751 case '\r':
752 case 0:
753 break;
754
755 default:
756 SCH_PARSE_ERROR( "undefined DRAW entry", aReader, line );
757 }
758
759 line = aReader.ReadLine();
760 }
761
762 SCH_PARSE_ERROR( "File ended prematurely loading symbol draw element.", aReader, line );
763}
764
765
767 const char** aOutput )
768{
769 switch ( parseChar( aReader, aLine, aOutput ) )
770 {
771 case 'F': return FILL_T::FILLED_SHAPE;
772 case 'f': return FILL_T::FILLED_WITH_BG_BODYCOLOR;
773 case 'N': return FILL_T::NO_FILL;
774 default: break;
775 }
776
777 SCH_PARSE_ERROR( "invalid fill type, expected f, F, or N", aReader, aLine );
778}
779
780
785static bool MapAnglesV6( int* aAngle1, int* aAngle2 )
786{
787 auto DECIDEG2RAD = []( double deg ) -> double
788 {
789 return deg * M_PI / 1800.0;
790 };
791
792 int angle, delta;
793 double x, y;
794 bool swap = false;
795
796 delta = *aAngle2 - *aAngle1;
797
798 if( delta >= 1800 )
799 {
800 *aAngle1 -= 1;
801 *aAngle2 += 1;
802 }
803
804 x = cos( DECIDEG2RAD( *aAngle1 ) );
805 y = -sin( DECIDEG2RAD( *aAngle1 ) );
806 *aAngle1 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
807
808 x = cos( DECIDEG2RAD( *aAngle2 ) );
809 y = -sin( DECIDEG2RAD( *aAngle2 ) );
810 *aAngle2 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
811
812 NORMALIZE_ANGLE_POS( *aAngle1 );
813 NORMALIZE_ANGLE_POS( *aAngle2 );
814
815 if( *aAngle2 < *aAngle1 )
816 *aAngle2 += 3600;
817
818 if( *aAngle2 - *aAngle1 > 1800 ) // Need to swap the two angles
819 {
820 angle = ( *aAngle1 );
821 *aAngle1 = ( *aAngle2 );
822 *aAngle2 = angle;
823
824 NORMALIZE_ANGLE_POS( *aAngle1 );
825 NORMALIZE_ANGLE_POS( *aAngle2 );
826
827 if( *aAngle2 < *aAngle1 )
828 *aAngle2 += 3600;
829
830 swap = true;
831 }
832
833 if( delta >= 1800 )
834 {
835 *aAngle1 += 1;
836 *aAngle2 -= 1;
837 }
838
839 return swap;
840}
841
842
844{
845 const char* line = aReader.Line();
846
847 wxCHECK_MSG( strCompare( "A", line, &line ), nullptr, "Invalid arc definition" );
848
849 SCH_SHAPE* arc = new SCH_SHAPE( SHAPE_T::ARC, LAYER_DEVICE );
850
852
853 center.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
854 center.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
855
856 arc->SetPosition( center );
857
858 int radius = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
859 int angle1 = parseInt( aReader, line, &line );
860 int angle2 = parseInt( aReader, line, &line );
861
862 NORMALIZE_ANGLE_POS( angle1 );
863 NORMALIZE_ANGLE_POS( angle2 );
864
865 arc->SetUnit( parseInt( aReader, line, &line ) );
866 arc->SetBodyStyle( parseInt( aReader, line, &line ) );
867
868 STROKE_PARAMS stroke( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ),
869 LINE_STYLE::SOLID );
870
871 arc->SetStroke( stroke );
872
873 // Old libraries (version <= 2.2) do not have always this FILL MODE param when fill mode
874 // is no fill (default mode).
875 if( *line != 0 )
876 arc->SetFillMode( parseFillMode( aReader, line, &line ) );
877
878 // Actual Coordinates of arc ends are read from file
879 if( *line != 0 )
880 {
881 VECTOR2I arcStart, arcEnd;
882
883 arcStart.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
884 arcStart.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
885 arcEnd.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
886 arcEnd.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
887
888 arc->SetStart( arcStart );
889 arc->SetEnd( arcEnd );
890 }
891 else
892 {
893 // Actual Coordinates of arc ends are not read from file
894 // (old library), calculate them
895 VECTOR2I arcStart( radius, 0 );
896 VECTOR2I arcEnd( radius, 0 );
897
898 RotatePoint( arcStart, EDA_ANGLE( angle1, EDA_ANGLE_T::TENTHS_OF_A_DEGREE_T ) );
899 arcStart += arc->GetCenter();
900 arc->SetStart( arcStart );
901 RotatePoint( arcEnd, EDA_ANGLE( angle2, EDA_ANGLE_T::TENTHS_OF_A_DEGREE_T ) );
902 arcEnd += arc->GetCenter();
903 arc->SetEnd( arcEnd );
904 }
905
913 if( MapAnglesV6( &angle1, &angle2 ) )
914 {
915 VECTOR2I temp = arc->GetEnd();
916 arc->SetEnd( arc->GetStart() );
917 arc->SetStart( temp );
918 }
919
920 return arc;
921}
922
923
925{
926 const char* line = aReader.Line();
927
928 wxCHECK_MSG( strCompare( "C", line, &line ), nullptr, "Invalid circle definition" );
929
930 SCH_SHAPE* circle = new SCH_SHAPE( SHAPE_T::CIRCLE, LAYER_DEVICE );
931
933
934 center.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
935 center.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
936
937 int radius = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
938
939 circle->SetStart( center );
940 circle->SetEnd( VECTOR2I( center.x + radius, center.y ) );
941 circle->SetUnit( parseInt( aReader, line, &line ) );
942 circle->SetBodyStyle( parseInt( aReader, line, &line ) );
943
944 STROKE_PARAMS stroke( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ),
945 LINE_STYLE::SOLID );
946
947 circle->SetStroke( stroke );
948
949 if( *line != 0 )
950 circle->SetFillMode( parseFillMode( aReader, line, &line ) );
951
952 return circle;
953}
954
955
957 int aMajorVersion, int aMinorVersion )
958{
959 const char* line = aReader.Line();
960
961 wxCHECK_MSG( strCompare( "T", line, &line ), nullptr, "Invalid SCH_TEXT definition" );
962
963 double angleInTenths;
965 VECTOR2I size;
966 wxString str;
967 bool visible;
968 int unit;
969 int bodyStyle;
970
971 angleInTenths = parseInt( aReader, line, &line );
972
973 center.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
974 center.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
975 size.x = size.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
976 visible = !parseInt( aReader, line, &line );
977 unit = parseInt( aReader, line, &line );
978 bodyStyle = parseInt( aReader, line, &line );
979
980 // If quoted string loading fails, load as not quoted string.
981 if( *line == '"' )
982 {
983 parseQuotedString( str, aReader, line, &line );
984
985 str = ConvertToNewOverbarNotation( str );
986 }
987 else
988 {
989 parseUnquotedString( str, aReader, line, &line );
990
991 // In old libs, "spaces" are replaced by '~' in unquoted strings:
992 str.Replace( "~", " " );
993 }
994
995 if( !str.IsEmpty() )
996 {
997 // convert two apostrophes back to double quote
998 str.Replace( "''", "\"" );
999 }
1000
1001 SCH_ITEM* sch_item = nullptr;
1002 EDA_TEXT* eda_text = nullptr;
1003
1004 if( !visible )
1005 {
1006 SCH_FIELD* field = new SCH_FIELD( nullptr, FIELD_T::USER );
1007 sch_item = field;
1008 eda_text = field;
1009 }
1010 else
1011 {
1012 SCH_TEXT* sch_text = new SCH_TEXT( center, str, LAYER_DEVICE );
1013 sch_item = sch_text;
1014 eda_text = sch_text;
1015 }
1016
1017 eda_text->SetTextAngle( EDA_ANGLE( angleInTenths, TENTHS_OF_A_DEGREE_T ) );
1018 eda_text->SetTextSize( size );
1019 eda_text->SetTextPos( center );
1020 eda_text->SetVisible( visible );
1021 sch_item->SetUnit( unit );
1022 sch_item->SetBodyStyle( bodyStyle );
1023
1024 // Here things are murky and not well defined. At some point it appears the format
1025 // was changed to add text properties. However rather than add the token to the end of
1026 // the text definition, it was added after the string and no mention if the file
1027 // verion was bumped or not so this code make break on very old symbol libraries.
1028 //
1029 // Update: apparently even in the latest version this can be different so added a test
1030 // for end of line before checking for the text properties.
1031 if( LIB_VERSION( aMajorVersion, aMinorVersion ) > 0
1032 && LIB_VERSION( aMajorVersion, aMinorVersion ) > LIB_VERSION( 2, 0 )
1033 && !is_eol( *line ) )
1034 {
1035 if( strCompare( "Italic", line, &line ) )
1036 eda_text->SetItalicFlag( true );
1037 else if( !strCompare( "Normal", line, &line ) )
1038 SCH_PARSE_ERROR( "invalid eda_text stype, expected 'Normal' or 'Italic'", aReader, line );
1039
1040 if( parseInt( aReader, line, &line ) > 0 )
1041 eda_text->SetBoldFlag( true );
1042
1043 // Some old libaries version > 2.0 do not have these options for eda_text justification:
1044 if( !is_eol( *line ) )
1045 {
1046 switch( parseChar( aReader, line, &line ) )
1047 {
1048 case 'L': eda_text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT ); break;
1049 case 'C': eda_text->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER ); break;
1050 case 'R': eda_text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT ); break;
1051 default: SCH_PARSE_ERROR( "invalid horizontal eda_text justication; expected L, C, or R",
1052 aReader, line );
1053 }
1054
1055 switch( parseChar( aReader, line, &line ) )
1056 {
1057 case 'T': eda_text->SetVertJustify( GR_TEXT_V_ALIGN_TOP ); break;
1058 case 'C': eda_text->SetVertJustify( GR_TEXT_V_ALIGN_CENTER ); break;
1059 case 'B': eda_text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM ); break;
1060 default: SCH_PARSE_ERROR( "invalid vertical eda_text justication; expected T, C, or B",
1061 aReader, line );
1062 }
1063 }
1064 }
1065
1066 return sch_item;
1067}
1068
1069
1071{
1072 const char* line = aReader.Line();
1073
1074 wxCHECK_MSG( strCompare( "S", line, &line ), nullptr, "Invalid rectangle definition" );
1075
1076 SCH_SHAPE* rectangle = new SCH_SHAPE( SHAPE_T::RECTANGLE, LAYER_DEVICE );
1077
1078 VECTOR2I pos;
1079
1080 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1081 pos.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1082 rectangle->SetPosition( pos );
1083
1084 VECTOR2I end;
1085
1086 end.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1087 end.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1088 rectangle->SetEnd( end );
1089
1090 rectangle->SetUnit( parseInt( aReader, line, &line ) );
1091 rectangle->SetBodyStyle( parseInt( aReader, line, &line ) );
1092
1093 STROKE_PARAMS stroke( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ),
1094 LINE_STYLE::SOLID );
1095
1096 rectangle->SetStroke( stroke );
1097
1098
1099 if( *line != 0 )
1100 rectangle->SetFillMode( parseFillMode( aReader, line, &line ) );
1101
1102 return rectangle;
1103}
1104
1105
1106SCH_PIN* SCH_IO_KICAD_LEGACY_LIB_CACHE::loadPin( std::unique_ptr<LIB_SYMBOL>& aSymbol,
1107 LINE_READER& aReader )
1108{
1109 const char* line = aReader.Line();
1110
1111 wxCHECK_MSG( strCompare( "X", line, &line ), nullptr, "Invalid SCH_PIN definition" );
1112
1113 wxString name;
1114 wxString number;
1115
1116 size_t pos = 2; // "X" plus ' ' space character.
1117 wxString tmp;
1118 wxString utf8Line = wxString::FromUTF8( line );
1119 wxStringTokenizer tokens( utf8Line, " \r\n\t" );
1120
1121 if( tokens.CountTokens() < 11 )
1122 SCH_PARSE_ERROR( "invalid pin definition", aReader, line );
1123
1124 tmp = tokens.GetNextToken();
1125 name = tmp;
1126 pos += tmp.size() + 1;
1127
1128 tmp = tokens.GetNextToken();
1129 number = tmp ;
1130 pos += tmp.size() + 1;
1131
1132 long num;
1133 VECTOR2I position;
1134
1135 tmp = tokens.GetNextToken();
1136
1137 if( !tmp.ToLong( &num ) )
1138 {
1139 THROW_PARSE_ERROR( "invalid pin X coordinate", aReader.GetSource(), aReader.Line(),
1140 aReader.LineNumber(), pos );
1141 }
1142
1143 pos += tmp.size() + 1;
1144 position.x = schIUScale.MilsToIU( (int) num );
1145
1146 tmp = tokens.GetNextToken();
1147
1148 if( !tmp.ToLong( &num ) )
1149 {
1150 THROW_PARSE_ERROR( "invalid pin Y coordinate", aReader.GetSource(), aReader.Line(),
1151 aReader.LineNumber(), pos );
1152 }
1153
1154 pos += tmp.size() + 1;
1155 position.y = -schIUScale.MilsToIU( (int) num );
1156
1157 tmp = tokens.GetNextToken();
1158
1159 if( !tmp.ToLong( &num ) )
1160 {
1161 THROW_PARSE_ERROR( "invalid pin length", aReader.GetSource(), aReader.Line(),
1162 aReader.LineNumber(), pos );
1163 }
1164
1165 pos += tmp.size() + 1;
1166 int length = schIUScale.MilsToIU( (int) num );
1167
1168
1169 tmp = tokens.GetNextToken();
1170
1171 if( tmp.size() > 1 )
1172 {
1173 THROW_PARSE_ERROR( "invalid pin orientation", aReader.GetSource(), aReader.Line(),
1174 aReader.LineNumber(), pos );
1175 }
1176
1177 pos += tmp.size() + 1;
1178
1179 PIN_ORIENTATION orientation;
1180
1181 switch( static_cast<char>( tmp[0] ) )
1182 {
1183 case 'U': orientation = PIN_ORIENTATION::PIN_UP; break;
1184 case 'D': orientation = PIN_ORIENTATION::PIN_DOWN; break;
1185 case 'L': orientation = PIN_ORIENTATION::PIN_LEFT; break;
1186 case 'R': /* fall-through */
1187 default: orientation = PIN_ORIENTATION::PIN_RIGHT; break;
1188 }
1189
1190 tmp = tokens.GetNextToken();
1191
1192 if( !tmp.ToLong( &num ) )
1193 {
1194 THROW_PARSE_ERROR( "invalid pin number text size", aReader.GetSource(), aReader.Line(),
1195 aReader.LineNumber(), pos );
1196 }
1197
1198 pos += tmp.size() + 1;
1199 int numberTextSize = schIUScale.MilsToIU( (int) num );
1200
1201 tmp = tokens.GetNextToken();
1202
1203 if( !tmp.ToLong( &num ) )
1204 {
1205 THROW_PARSE_ERROR( "invalid pin name text size", aReader.GetSource(), aReader.Line(),
1206 aReader.LineNumber(), pos );
1207 }
1208
1209 pos += tmp.size() + 1;
1210 int nameTextSize = schIUScale.MilsToIU( (int) num );
1211
1212 tmp = tokens.GetNextToken();
1213
1214 if( !tmp.ToLong( &num ) )
1215 {
1216 THROW_PARSE_ERROR( "invalid pin unit", aReader.GetSource(), aReader.Line(),
1217 aReader.LineNumber(), pos );
1218 }
1219
1220 pos += tmp.size() + 1;
1221 int unit = (int) num;
1222
1223 tmp = tokens.GetNextToken();
1224
1225 if( !tmp.ToLong( &num ) )
1226 {
1227 THROW_PARSE_ERROR( "invalid pin alternate body type", aReader.GetSource(), aReader.Line(),
1228 aReader.LineNumber(), pos );
1229 }
1230
1231 pos += tmp.size() + 1;
1232 int convert = (int) num;
1233
1234 tmp = tokens.GetNextToken();
1235
1236 if( tmp.size() != 1 )
1237 {
1238 THROW_PARSE_ERROR( "invalid pin type", aReader.GetSource(), aReader.Line(),
1239 aReader.LineNumber(), pos );
1240 }
1241
1242 pos += tmp.size() + 1;
1243 char type = tmp[0];
1244 ELECTRICAL_PINTYPE pinType;
1245
1246 switch( type )
1247 {
1248 case 'I': pinType = ELECTRICAL_PINTYPE::PT_INPUT; break;
1249 case 'O': pinType = ELECTRICAL_PINTYPE::PT_OUTPUT; break;
1250 case 'B': pinType = ELECTRICAL_PINTYPE::PT_BIDI; break;
1251 case 'T': pinType = ELECTRICAL_PINTYPE::PT_TRISTATE; break;
1252 case 'P': pinType = ELECTRICAL_PINTYPE::PT_PASSIVE; break;
1253 case 'U': pinType = ELECTRICAL_PINTYPE::PT_UNSPECIFIED; break;
1254 case 'W': pinType = ELECTRICAL_PINTYPE::PT_POWER_IN; break;
1255 case 'w': pinType = ELECTRICAL_PINTYPE::PT_POWER_OUT; break;
1256 case 'C': pinType = ELECTRICAL_PINTYPE::PT_OPENCOLLECTOR; break;
1257 case 'E': pinType = ELECTRICAL_PINTYPE::PT_OPENEMITTER; break;
1258 case 'N': pinType = ELECTRICAL_PINTYPE::PT_NC; break;
1259 default:
1260 THROW_PARSE_ERROR( "unknown pin type", aReader.GetSource(), aReader.Line(),
1261 aReader.LineNumber(), pos );
1262 }
1263
1264 SCH_PIN* pin = new SCH_PIN( aSymbol.get(),
1267 orientation,
1268 pinType,
1269 length,
1270 nameTextSize,
1271 numberTextSize,
1272 convert,
1273 position,
1274 unit );
1275
1276 // Optional
1277 if( tokens.HasMoreTokens() ) /* Special Symbol defined */
1278 {
1279 tmp = tokens.GetNextToken();
1280
1281 enum
1282 {
1283 INVERTED = 1 << 0,
1284 CLOCK = 1 << 1,
1285 LOWLEVEL_IN = 1 << 2,
1286 LOWLEVEL_OUT = 1 << 3,
1287 FALLING_EDGE = 1 << 4,
1288 NONLOGIC = 1 << 5
1289 };
1290
1291 int flags = 0;
1292
1293 for( int j = (int) tmp.size(); j > 0; )
1294 {
1295 switch( tmp[--j].GetValue() )
1296 {
1297 case '~': break;
1298 case 'N': pin->SetVisible( false ); break;
1299 case 'I': flags |= INVERTED; break;
1300 case 'C': flags |= CLOCK; break;
1301 case 'L': flags |= LOWLEVEL_IN; break;
1302 case 'V': flags |= LOWLEVEL_OUT; break;
1303 case 'F': flags |= FALLING_EDGE; break;
1304 case 'X': flags |= NONLOGIC; break;
1305 default: THROW_PARSE_ERROR( "invalid pin attribut", aReader.GetSource(),
1306 aReader.Line(), aReader.LineNumber(), pos );
1307 }
1308
1309 pos += 1;
1310 }
1311
1312 switch( flags )
1313 {
1314 case 0: pin->SetShape( GRAPHIC_PINSHAPE::LINE ); break;
1315 case INVERTED: pin->SetShape( GRAPHIC_PINSHAPE::INVERTED ); break;
1316 case CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::CLOCK ); break;
1317 case INVERTED | CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::INVERTED_CLOCK ); break;
1318 case LOWLEVEL_IN: pin->SetShape( GRAPHIC_PINSHAPE::INPUT_LOW ); break;
1319 case LOWLEVEL_IN | CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::CLOCK_LOW ); break;
1320 case LOWLEVEL_OUT: pin->SetShape( GRAPHIC_PINSHAPE::OUTPUT_LOW ); break;
1321 case FALLING_EDGE: pin->SetShape( GRAPHIC_PINSHAPE::FALLING_EDGE_CLOCK ); break;
1322 case NONLOGIC: pin->SetShape( GRAPHIC_PINSHAPE::NONLOGIC ); break;
1323 default:
1324 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 ) ),
1345 LINE_STYLE::SOLID );
1346
1347 polyLine->SetStroke( stroke );
1348
1349 VECTOR2I pt;
1350
1351 for( int i = 0; i < points; i++ )
1352 {
1353 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1354 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1355 polyLine->AddPoint( pt );
1356 }
1357
1358 if( *line != 0 )
1359 polyLine->SetFillMode( parseFillMode( aReader, line, &line ) );
1360
1361 return polyLine;
1362}
1363
1364
1366{
1367 const char* line = aReader.Line();
1368
1369 wxCHECK_MSG( strCompare( "B", line, &line ), nullptr, "Invalid Bezier definition" );
1370
1371 int points = parseInt( aReader, line, &line );
1372
1373 wxCHECK_MSG( points == 4, NULL, "Invalid Bezier curve definition" );
1374
1375 SCH_SHAPE* bezier = new SCH_SHAPE( SHAPE_T::BEZIER, LAYER_DEVICE );
1376
1377 bezier->SetUnit( parseInt( aReader, line, &line ) );
1378 bezier->SetBodyStyle( parseInt( aReader, line, &line ) );
1379
1380 STROKE_PARAMS stroke ( schIUScale.MilsToIU( parseInt( aReader, line, &line ) ),
1381 LINE_STYLE::SOLID );
1382
1383 bezier->SetStroke( stroke );
1384
1385 VECTOR2I pt;
1386
1387 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1388 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1389 bezier->SetStart( pt );
1390
1391 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1392 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1393 bezier->SetBezierC1( pt );
1394
1395 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1396 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1397 bezier->SetBezierC2( pt );
1398
1399 pt.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1400 pt.y = -schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1401 bezier->SetEnd( pt );
1402
1403 bezier->RebuildBezierToSegmentsPointsList( bezier->GetWidth() / 2 );
1404
1405 if( *line != 0 )
1406 bezier->SetFillMode( parseFillMode( aReader, line, &line ) );
1407
1408 return bezier;
1409}
1410
1411
1412void SCH_IO_KICAD_LEGACY_LIB_CACHE::loadFootprintFilters( std::unique_ptr<LIB_SYMBOL>& aSymbol,
1413 LINE_READER& aReader )
1414{
1415 const char* line = aReader.Line();
1416
1417 wxCHECK_RET( strCompare( "$FPLIST", line, &line ), "Invalid footprint filter list" );
1418
1419 line = aReader.ReadLine();
1420
1421 wxArrayString footprintFilters;
1422
1423 while( line )
1424 {
1425 if( strCompare( "$ENDFPLIST", line, &line ) )
1426 {
1427 aSymbol->SetFPFilters( footprintFilters );
1428 return;
1429 }
1430
1431 wxString footprint;
1432
1433 parseUnquotedString( footprint, aReader, line, &line );
1434 footprintFilters.Add( footprint );
1435 line = aReader.ReadLine();
1436 }
1437
1438 SCH_PARSE_ERROR( "File ended prematurely while loading footprint filters.", aReader, line );
1439}
1440
1441
1442void SCH_IO_KICAD_LEGACY_LIB_CACHE::Save( const std::optional<bool>& aOpt )
1443{
1444 wxCHECK( aOpt, /* void */ );
1445
1446 bool doSaveDocFile = *aOpt;
1447
1448 if( !m_isModified )
1449 return;
1450
1451 // Write through symlinks, don't replace them
1452 wxFileName fn = GetRealFile();
1453
1454 auto formatter = std::make_unique<FILE_OUTPUTFORMATTER>( fn.GetFullPath() );
1455 formatter->Print( 0, "%s %d.%d\n", LIBFILE_IDENT, LIB_VERSION_MAJOR, LIB_VERSION_MINOR );
1456 formatter->Print( 0, "#encoding utf-8\n");
1457
1458 for( LIB_SYMBOL_MAP::iterator it = m_symbols.begin(); it != m_symbols.end(); it++ )
1459 {
1460 if( !it->second->IsRoot() )
1461 continue;
1462
1463 SaveSymbol( it->second, *formatter.get(), &m_symbols );
1464 }
1465
1466 formatter->Print( 0, "#\n#End Library\n" );
1467 formatter.reset();
1468
1469 m_fileModTime = fn.GetModificationTime();
1470 m_isModified = false;
1471
1472 if( doSaveDocFile )
1473 saveDocFile();
1474}
1475
1476
1478 LIB_SYMBOL_MAP* aMap )
1479{
1480 /*
1481 * NB:
1482 * Some of the rescue code still uses the legacy format as an intermediary, so we have
1483 * to keep this code.
1484 */
1485
1486 wxCHECK_RET( aSymbol && aSymbol->IsRoot(), "Invalid LIB_SYMBOL pointer." );
1487
1488 // LIB_ALIAS objects are deprecated but we still need to gather up the derived symbols
1489 // and save their names for the old file format.
1490 wxArrayString aliasNames;
1491
1492 if( aMap )
1493 {
1494 for( auto& entry : *aMap )
1495 {
1496 LIB_SYMBOL* symbol = entry.second;
1497
1498 if( symbol->IsDerived() && symbol->GetParent().lock() == aSymbol->SharedPtr() )
1499 aliasNames.Add( symbol->GetName() );
1500 }
1501 }
1502
1503 SCH_FIELD& value = aSymbol->GetValueField();
1504
1505 // First line: it s a comment (symbol name for readers)
1506 aFormatter.Print( 0, "#\n# %s\n#\n", TO_UTF8( value.GetText() ) );
1507
1508 // Save data
1509 aFormatter.Print( 0, "DEF" );
1510 aFormatter.Print( 0, " %s", TO_UTF8( value.GetText() ) );
1511
1512 SCH_FIELD& reference = aSymbol->GetReferenceField();
1513
1514 if( !reference.GetText().IsEmpty() )
1515 aFormatter.Print( 0, " %s", TO_UTF8( reference.GetText() ) );
1516 else
1517 aFormatter.Print( 0, " ~" );
1518
1519 aFormatter.Print( 0, " %d %d %c %c %d %c %c\n",
1520 0, schIUScale.IUToMils( aSymbol->GetPinNameOffset() ),
1521 aSymbol->GetShowPinNumbers() ? 'Y' : 'N',
1522 aSymbol->GetShowPinNames() ? 'Y' : 'N',
1523 aSymbol->GetUnitCount(), aSymbol->UnitsLocked() ? 'L' : 'F',
1524 aSymbol->IsGlobalPower() ? 'P' : 'N' );
1525
1526 timestamp_t dateModified = aSymbol->GetLastModDate();
1527
1528 if( dateModified != 0 )
1529 {
1530 int sec = dateModified & 63;
1531 int min = ( dateModified >> 6 ) & 63;
1532 int hour = ( dateModified >> 12 ) & 31;
1533 int day = ( dateModified >> 17 ) & 31;
1534 int mon = ( dateModified >> 22 ) & 15;
1535 int year = ( dateModified >> 26 ) + 1990;
1536
1537 aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
1538 }
1539
1540 std::vector<SCH_FIELD*> orderedFields;
1541 aSymbol->GetFields( orderedFields );
1542
1543 // NB: FieldIDs in legacy libraries must be consecutive, and include user fields
1544 int legacy_field_id = 0;
1545
1546 for( SCH_FIELD* field : orderedFields )
1547 saveField( field, legacy_field_id++, aFormatter );
1548
1549 // Save the alias list: a line starting by "ALIAS".
1550 if( !aliasNames.IsEmpty() )
1551 {
1552 aFormatter.Print( 0, "ALIAS" );
1553
1554 for( unsigned i = 0; i < aliasNames.GetCount(); i++ )
1555 aFormatter.Print( 0, " %s", TO_UTF8( aliasNames[i] ) );
1556
1557 aFormatter.Print( 0, "\n" );
1558 }
1559
1560 wxArrayString footprints = aSymbol->GetFPFilters();
1561
1562 // Write the footprint filter list
1563 if( footprints.GetCount() != 0 )
1564 {
1565 aFormatter.Print( 0, "$FPLIST\n" );
1566
1567 for( unsigned i = 0; i < footprints.GetCount(); i++ )
1568 aFormatter.Print( 0, " %s\n", TO_UTF8( footprints[i] ) );
1569
1570 aFormatter.Print( 0, "$ENDFPLIST\n" );
1571 }
1572
1573 // Save graphics items (including pins)
1574 if( !aSymbol->GetDrawItems().empty() )
1575 {
1576 // Sort the draw items in order to editing a file editing by hand.
1577 aSymbol->GetDrawItems().sort();
1578
1579 aFormatter.Print( 0, "DRAW\n" );
1580
1581 for( SCH_ITEM& item : aSymbol->GetDrawItems() )
1582 {
1583 switch( item.Type() )
1584 {
1585 case SCH_PIN_T:
1586 savePin( static_cast<SCH_PIN*>( &item ), aFormatter );
1587
1588 break;
1589 case SCH_TEXT_T:
1590 saveText( static_cast<SCH_TEXT*>( &item ), aFormatter );
1591 break;
1592
1593 case SCH_SHAPE_T:
1594 {
1595 SCH_SHAPE& shape = static_cast<SCH_SHAPE&>( item );
1596
1597 switch( shape.GetShape() )
1598 {
1599 case SHAPE_T::ARC: saveArc( &shape, aFormatter ); break;
1600 case SHAPE_T::BEZIER: saveBezier( &shape, aFormatter ); break;
1601 case SHAPE_T::CIRCLE: saveCircle( &shape, aFormatter ); break;
1602 case SHAPE_T::POLY: savePolyLine( &shape, aFormatter ); break;
1603 case SHAPE_T::RECTANGLE: saveRectangle( &shape, aFormatter ); break;
1604 default: break;
1605 }
1606
1607 break;
1608 }
1609
1610 default:
1611 break;
1612 }
1613 }
1614
1615 aFormatter.Print( 0, "ENDDRAW\n" );
1616 }
1617
1618 aFormatter.Print( 0, "ENDDEF\n" );
1619}
1620
1621
1623{
1624 wxCHECK_RET( aArc && aArc->GetShape() == SHAPE_T::ARC, "Invalid ARC object." );
1625
1626 EDA_ANGLE startAngle, endAngle;
1627
1628 aArc->CalcArcAngles( endAngle, startAngle );
1629 startAngle.Normalize180();
1630 endAngle.Normalize180();
1631
1632 aFormatter.Print( 0, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n",
1633 schIUScale.IUToMils( aArc->GetPosition().x ),
1634 schIUScale.IUToMils( -aArc->GetPosition().y ),
1635 schIUScale.IUToMils( aArc->GetRadius() ),
1636 startAngle.AsTenthsOfADegree(),
1637 endAngle.AsTenthsOfADegree(),
1638 aArc->GetUnit(),
1639 aArc->GetBodyStyle(),
1640 schIUScale.IUToMils( aArc->GetWidth() ),
1641 fill_tab[ static_cast<int>( aArc->GetFillMode() ) - 1 ],
1642 schIUScale.IUToMils( aArc->GetStart().x ),
1643 schIUScale.IUToMils( -aArc->GetStart().y ),
1644 schIUScale.IUToMils( aArc->GetEnd().x ),
1645 schIUScale.IUToMils( -aArc->GetEnd().y ) );
1646}
1647
1648
1650{
1651 wxCHECK_RET( aBezier && aBezier->GetShape() == SHAPE_T::BEZIER, "Invalid BEZIER object." );
1652
1653 aFormatter.Print( 0, "B 4 %d %d %d",
1654 aBezier->GetUnit(),
1655 aBezier->GetBodyStyle(),
1656 schIUScale.IUToMils( aBezier->GetWidth() ) );
1657
1658 aFormatter.Print( 0, " %d %d %d %d %d %d %d %d",
1659 schIUScale.IUToMils( aBezier->GetStart().x ),
1660 schIUScale.IUToMils( -aBezier->GetStart().y ),
1661 schIUScale.IUToMils( aBezier->GetBezierC1().x ),
1662 schIUScale.IUToMils( -aBezier->GetBezierC1().y ),
1663 schIUScale.IUToMils( aBezier->GetBezierC2().x ),
1664 schIUScale.IUToMils( -aBezier->GetBezierC2().y ),
1665 schIUScale.IUToMils( aBezier->GetEnd().x ),
1666 schIUScale.IUToMils( -aBezier->GetEnd().y ) );
1667
1668 aFormatter.Print( 0, " %c\n", fill_tab[ static_cast<int>( aBezier->GetFillMode() ) - 1 ] );
1669}
1670
1671
1673{
1674 wxCHECK_RET( aCircle && aCircle->GetShape() == SHAPE_T::CIRCLE, "Invalid CIRCLE object." );
1675
1676 aFormatter.Print( 0, "C %d %d %d %d %d %d %c\n",
1677 schIUScale.IUToMils( aCircle->GetPosition().x ),
1678 schIUScale.IUToMils( -aCircle->GetPosition().y ),
1679 schIUScale.IUToMils( aCircle->GetRadius() ),
1680 aCircle->GetUnit(),
1681 aCircle->GetBodyStyle(),
1682 schIUScale.IUToMils( aCircle->GetWidth() ),
1683 fill_tab[ static_cast<int>( aCircle->GetFillMode() ) - 1 ] );
1684}
1685
1686
1687void SCH_IO_KICAD_LEGACY_LIB_CACHE::saveField( const SCH_FIELD* aField, int aLegacyFieldIdx,
1688 OUTPUTFORMATTER& aFormatter )
1689{
1690 wxCHECK_RET( aField && aField->Type() == SCH_FIELD_T, "Invalid SCH_FIELD object." );
1691
1692 int hjustify, vjustify;
1693 wxString text = aField->GetText();
1694
1695 hjustify = 'C';
1696
1697 if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT )
1698 hjustify = 'L';
1699 else if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
1700 hjustify = 'R';
1701
1702 vjustify = 'C';
1703
1704 if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_BOTTOM )
1705 vjustify = 'B';
1706 else if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_TOP )
1707 vjustify = 'T';
1708
1709 aFormatter.Print( 0, "F%d %s %d %d %d %c %c %c %c%c%c",
1710 aLegacyFieldIdx,
1711 EscapedUTF8( text ).c_str(), // wraps in quotes
1712 schIUScale.IUToMils( aField->GetTextPos().x ),
1713 schIUScale.IUToMils( -aField->GetTextPos().y ),
1714 schIUScale.IUToMils( aField->GetTextWidth() ),
1715 aField->GetTextAngle().IsHorizontal() ? 'H' : 'V',
1716 aField->IsVisible() ? 'V' : 'I',
1717 hjustify, vjustify,
1718 aField->IsItalic() ? 'I' : 'N',
1719 aField->IsBold() ? 'B' : 'N' );
1720
1721 // Translated names were stored in legacy files, so it's important not to save the
1722 // default names as they weren't yet canonical.
1723 if( !aField->IsMandatory()
1724 && !aField->GetName().IsEmpty()
1725 && aField->GetName() != GetUserFieldName( aLegacyFieldIdx, !DO_TRANSLATE ) )
1726 {
1727 aFormatter.Print( 0, " %s", EscapedUTF8( aField->GetName() ).c_str() );
1728 }
1729
1730 aFormatter.Print( 0, "\n" );
1731}
1732
1733
1735{
1736 wxCHECK_RET( aPin && aPin->Type() == SCH_PIN_T, "Invalid SCH_PIN object." );
1737
1738 int Etype;
1739
1740 switch( aPin->GetType() )
1741 {
1742 default:
1743 case ELECTRICAL_PINTYPE::PT_INPUT: Etype = 'I'; break;
1744 case ELECTRICAL_PINTYPE::PT_OUTPUT: Etype = 'O'; break;
1745 case ELECTRICAL_PINTYPE::PT_BIDI: Etype = 'B'; break;
1746 case ELECTRICAL_PINTYPE::PT_TRISTATE: Etype = 'T'; break;
1747 case ELECTRICAL_PINTYPE::PT_PASSIVE: Etype = 'P'; break;
1748 case ELECTRICAL_PINTYPE::PT_UNSPECIFIED: Etype = 'U'; break;
1749 case ELECTRICAL_PINTYPE::PT_POWER_IN: Etype = 'W'; break;
1750 case ELECTRICAL_PINTYPE::PT_POWER_OUT: Etype = 'w'; break;
1751 case ELECTRICAL_PINTYPE::PT_OPENCOLLECTOR: Etype = 'C'; break;
1752 case ELECTRICAL_PINTYPE::PT_OPENEMITTER: Etype = 'E'; break;
1753 case ELECTRICAL_PINTYPE::PT_NC: Etype = 'N'; break;
1754 }
1755
1756 if( !aPin->GetName().IsEmpty() )
1757 aFormatter.Print( 0, "X %s", TO_UTF8( aPin->GetName() ) );
1758 else
1759 aFormatter.Print( 0, "X ~" );
1760
1761 int pin_orient = 'L'; // Printed as a char in lib file
1762
1763 switch( aPin->GetOrientation() )
1764 {
1765 case PIN_ORIENTATION::PIN_RIGHT: pin_orient = 'R'; break;
1766 case PIN_ORIENTATION::PIN_LEFT: pin_orient = 'L'; break;
1767 case PIN_ORIENTATION::PIN_UP: pin_orient = 'U'; break;
1768 case PIN_ORIENTATION::PIN_DOWN: pin_orient = 'D'; break;
1769 case PIN_ORIENTATION::INHERIT: pin_orient = 'L'; break; // Should not happens
1770 }
1771
1772 aFormatter.Print( 0, " %s %d %d %d %c %d %d %d %d %c",
1773 aPin->GetNumber().IsEmpty() ? "~" : TO_UTF8( aPin->GetNumber() ),
1774 schIUScale.IUToMils( aPin->GetPosition().x ),
1775 schIUScale.IUToMils( -aPin->GetPosition().y ),
1776 schIUScale.IUToMils( (int) aPin->GetLength() ),
1777 pin_orient,
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->IsDerived()
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:62
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:112
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:141
EDA_ANGLE Normalize180()
Definition: eda_angle.h:263
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:108
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:111
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:258
void SetBezierC2(const VECTOR2I &aPt)
Definition: eda_shape.h:257
FILL_T GetFillMode() const
Definition: eda_shape.h:142
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:337
void CalcArcAngles(EDA_ANGLE &aStartAngle, EDA_ANGLE &aEndAngle) const
Calc arc start and end angles such that aStartAngle < aEndAngle.
Definition: eda_shape.cpp:997
int GetRadius() const
Definition: eda_shape.cpp:1013
SHAPE_T GetShape() const
Definition: eda_shape.h:168
void RebuildBezierToSegmentsPointsList(int aMaxError)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:911
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:215
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:177
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:173
void SetEnd(const VECTOR2I &aEnd)
Definition: eda_shape.h:219
void SetBezierC1(const VECTOR2I &aPt)
Definition: eda_shape.h:254
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:255
virtual int GetWidth() const
Definition: eda_shape.h:156
void SetFillMode(FILL_T aFill)
Definition: eda_shape.cpp:547
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:79
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:270
bool IsItalic() const
Definition: eda_text.h:166
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:144
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:533
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:97
virtual bool IsVisible() const
Definition: eda_text.h:184
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:578
int GetTextWidth() const
Definition: eda_text.h:261
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:417
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:197
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition: eda_text.cpp:378
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:386
void Empty()
Definition: eda_text.cpp:615
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:327
bool IsBold() const
Definition: eda_text.h:181
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:200
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:299
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:409
A LINE_READER that reads from an open file.
Definition: richio.h:185
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:249
Used for text file output.
Definition: richio.h:491
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
Define a library symbol object.
Definition: lib_symbol.h:85
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
bool UnitsLocked() const
Check whether symbol units are interchangeable.
Definition: lib_symbol.h:289
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:206
bool IsDerived() const
Definition: lib_symbol.h:207
timestamp_t GetLastModDate() const
Definition: lib_symbol.h:214
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:519
void SetParent(LIB_SYMBOL *aParent=nullptr)
Definition: lib_symbol.cpp:336
wxString GetName() const override
Definition: lib_symbol.h:149
void SetDescription(const wxString &aDescription)
Gets the Description field text value *‍/.
Definition: lib_symbol.h:164
void SetKeyWords(const wxString &aKeyWords)
Definition: lib_symbol.h:181
SCH_FIELD & GetValueField()
Return reference to the value field.
Definition: lib_symbol.h:334
wxArrayString GetFPFilters() const
Definition: lib_symbol.h:218
LIB_SYMBOL_SPTR SharedPtr() const
http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared.
Definition: lib_symbol.h:96
bool IsGlobalPower() const override
Definition: lib_symbol.cpp:454
int GetUnitCount() const override
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:118
SCH_FIELD & GetReferenceField()
Return reference to the reference designator field.
Definition: lib_symbol.h:338
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:463
bool IsMandatory() const
Definition: sch_field.cpp:1361
FIELD_T GetId() const
Definition: sch_field.h:115
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1109
void SetPosition(const VECTOR2I &aPosition) override
Definition: sch_field.cpp:1319
void SetName(const wxString &aName)
Definition: sch_field.cpp:1085
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1095
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)
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:167
virtual void SetBodyStyle(int aBodyStyle)
Definition: sch_item.h:239
int GetBodyStyle() const
Definition: sch_item.h:240
int GetUnit() const
Definition: sch_item.h:236
virtual void SetUnit(int aUnit)
Definition: sch_item.h:235
int GetNumberTextSize() const
Definition: sch_pin.cpp:601
int GetLength() const
Definition: sch_pin.cpp:291
bool IsVisible() const
Definition: sch_pin.cpp:383
const wxString & GetName() const
Definition: sch_pin.cpp:397
PIN_ORIENTATION GetOrientation() const
Definition: sch_pin.cpp:256
VECTOR2I GetPosition() const override
Definition: sch_pin.cpp:248
int GetNameTextSize() const
Definition: sch_pin.cpp:577
const wxString & GetNumber() const
Definition: sch_pin.h:123
GRAPHIC_PINSHAPE GetShape() const
Definition: sch_pin.cpp:270
ELECTRICAL_PINTYPE GetType() const
Definition: sch_pin.cpp:305
void SetPosition(const VECTOR2I &aPos) override
Definition: sch_shape.h:92
void SetStroke(const STROKE_PARAMS &aStroke) override
Definition: sch_shape.cpp:71
VECTOR2I GetCenter() const
Definition: sch_shape.h:94
void AddPoint(const VECTOR2I &aPosition)
Definition: sch_shape.cpp:413
VECTOR2I GetPosition() const override
Definition: sch_shape.h:91
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:94
int GetPinNameOffset() const
Definition: symbol.h:153
virtual bool GetShowPinNames() const
Definition: symbol.h:159
virtual bool GetShowPinNumbers() const
Definition: symbol.h:165
#define _(s)
@ TENTHS_OF_A_DEGREE_T
Definition: eda_angle.h:30
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition: eda_angle.h:401
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition: eda_angle.h:400
#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:455
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:80
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:429
@ CTX_QUOTED_STR
Definition: string_utils.h:57
constexpr int IUToMils(int iu) const
Definition: base_units.h:101
constexpr int MilsToIU(int mils) const
Definition: base_units.h:95
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...
wxString GetCanonicalFieldName(FIELD_T aFieldType)
VECTOR2I center
int radius
VECTOR2I end
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
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:695
Definition of file extensions used in Kicad.