KiCad PCB EDA Suite
sch_legacy_plugin.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) 2016 CERN
5  * Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Wayne Stambaugh <stambaughw@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <algorithm>
24 #include <boost/algorithm/string/join.hpp>
25 #include <cctype>
26 #include <mutex>
27 #include <set>
28 
29 #include <wx/mstream.h>
30 #include <wx/filename.h>
31 #include <wx/log.h>
32 #include <wx/textfile.h>
33 #include <wx/tokenzr.h>
34 #include <wx_filename.h> // For ::ResolvePossibleSymlinks()
35 
36 #include <kiway.h>
37 #include <string_utils.h>
38 #include <locale_io.h>
39 #include <richio.h>
40 #include <trace_helpers.h>
41 #include <trigo.h>
42 #include <progress_reporter.h>
43 #include <general.h>
44 #include <sch_bitmap.h>
45 #include <sch_bus_entry.h>
46 #include <sch_symbol.h>
47 #include <sch_junction.h>
48 #include <sch_line.h>
49 #include <sch_marker.h>
50 #include <sch_no_connect.h>
51 #include <sch_text.h>
52 #include <sch_sheet.h>
53 #include <sch_sheet_pin.h>
54 #include <bus_alias.h>
56 #include <sch_screen.h>
57 #include <schematic.h>
58 #include <symbol_library.h>
59 #include <lib_arc.h>
60 #include <lib_bezier.h>
61 #include <lib_circle.h>
62 #include <lib_field.h>
63 #include <lib_pin.h>
64 #include <lib_polyline.h>
65 #include <lib_rectangle.h>
66 #include <lib_text.h>
67 #include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
68 #include <tool/selection.h>
69 
70 
71 #define Mils2Iu( x ) Mils2iu( x )
72 
73 
74 // Must be the first line of symbol library document (.dcm) files.
75 #define DOCFILE_IDENT "EESchema-DOCLIB Version 2.0"
76 
77 #define SCH_PARSE_ERROR( text, reader, pos ) \
78  THROW_PARSE_ERROR( text, reader.GetSource(), reader.Line(), \
79  reader.LineNumber(), pos - reader.Line() )
80 
81 
82 // Token delimiters.
83 const char* delims = " \t\r\n";
84 
85 // Tokens to read/save graphic lines style
86 #define T_STYLE "style"
87 #define T_COLOR "rgb" // cannot be modified (used by wxWidgets)
88 #define T_COLORA "rgba" // cannot be modified (used by wxWidgets)
89 #define T_WIDTH "width"
90 
91 
92 static bool is_eol( char c )
93 {
94  // The default file eol character used internally by KiCad.
95  // |
96  // | Possible eol if someone edited the file by hand on certain platforms.
97  // | |
98  // | | May have gone past eol with strtok().
99  // | | |
100  if( c == '\n' || c == '\r' || c == 0 )
101  return true;
102 
103  return false;
104 }
105 
106 
116 static bool strCompare( const char* aString, const char* aLine, const char** aOutput = nullptr )
117 {
118  size_t len = strlen( aString );
119  bool retv = ( strncasecmp( aLine, aString, len ) == 0 ) &&
120  ( isspace( aLine[ len ] ) || aLine[ len ] == 0 );
121 
122  if( retv && aOutput )
123  {
124  const char* tmp = aLine;
125 
126  // Move past the end of the token.
127  tmp += len;
128 
129  // Move to the beginning of the next token.
130  while( *tmp && isspace( *tmp ) )
131  tmp++;
132 
133  *aOutput = tmp;
134  }
135 
136  return retv;
137 }
138 
139 
153 static int parseInt( LINE_READER& aReader, const char* aLine, const char** aOutput = nullptr )
154 {
155  if( !*aLine )
156  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
157 
158  // Clear errno before calling strtol() in case some other crt call set it.
159  errno = 0;
160 
161  long retv = strtol( aLine, (char**) aOutput, 10 );
162 
163  // Make sure no error occurred when calling strtol().
164  if( errno == ERANGE )
165  SCH_PARSE_ERROR( "invalid integer value", aReader, aLine );
166 
167  // strtol does not strip off whitespace before the next token.
168  if( aOutput )
169  {
170  const char* next = *aOutput;
171 
172  while( *next && isspace( *next ) )
173  next++;
174 
175  *aOutput = next;
176  }
177 
178  return (int) retv;
179 }
180 
181 
195 static uint32_t parseHex( LINE_READER& aReader, const char* aLine, const char** aOutput = nullptr )
196 {
197  if( !*aLine )
198  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
199 
200  // Due to some issues between some files created by a 64 bits version and those
201  // created by a 32 bits version, we use here a temporary at least 64 bits storage:
202  unsigned long long retv;
203 
204  // Clear errno before calling strtoull() in case some other crt call set it.
205  errno = 0;
206  retv = strtoull( aLine, (char**) aOutput, 16 );
207 
208  // Make sure no error occurred when calling strtoull().
209  if( errno == ERANGE )
210  SCH_PARSE_ERROR( "invalid hexadecimal number", aReader, aLine );
211 
212  // Strip off whitespace before the next token.
213  if( aOutput )
214  {
215  const char* next = *aOutput;
216 
217  while( *next && isspace( *next ) )
218  next++;
219 
220  *aOutput = next;
221  }
222 
223  return (uint32_t)retv;
224 }
225 
226 
240 static double parseDouble( LINE_READER& aReader, const char* aLine,
241  const char** aOutput = nullptr )
242 {
243  if( !*aLine )
244  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aLine );
245 
246  // Clear errno before calling strtod() in case some other crt call set it.
247  errno = 0;
248 
249  double retv = strtod( aLine, (char**) aOutput );
250 
251  // Make sure no error occurred when calling strtod().
252  if( errno == ERANGE )
253  SCH_PARSE_ERROR( "invalid floating point number", aReader, aLine );
254 
255  // strtod does not strip off whitespace before the next token.
256  if( aOutput )
257  {
258  const char* next = *aOutput;
259 
260  while( *next && isspace( *next ) )
261  next++;
262 
263  *aOutput = next;
264  }
265 
266  return retv;
267 }
268 
269 
281 static char parseChar( LINE_READER& aReader, const char* aCurrentToken,
282  const char** aNextToken = nullptr )
283 {
284  while( *aCurrentToken && isspace( *aCurrentToken ) )
285  aCurrentToken++;
286 
287  if( !*aCurrentToken )
288  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
289 
290  if( !isspace( *( aCurrentToken + 1 ) ) )
291  SCH_PARSE_ERROR( "expected single character token", aReader, aCurrentToken );
292 
293  if( aNextToken )
294  {
295  const char* next = aCurrentToken + 2;
296 
297  while( *next && isspace( *next ) )
298  next++;
299 
300  *aNextToken = next;
301  }
302 
303  return *aCurrentToken;
304 }
305 
306 
321 static void parseUnquotedString( wxString& aString, LINE_READER& aReader,
322  const char* aCurrentToken, const char** aNextToken = nullptr,
323  bool aCanBeEmpty = false )
324 {
325  if( !*aCurrentToken )
326  {
327  if( aCanBeEmpty )
328  return;
329  else
330  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
331  }
332 
333  const char* tmp = aCurrentToken;
334 
335  while( *tmp && isspace( *tmp ) )
336  tmp++;
337 
338  if( !*tmp )
339  {
340  if( aCanBeEmpty )
341  return;
342  else
343  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
344  }
345 
346  std::string utf8;
347 
348  while( *tmp && !isspace( *tmp ) )
349  utf8 += *tmp++;
350 
351  aString = FROM_UTF8( utf8.c_str() );
352 
353  if( aString.IsEmpty() && !aCanBeEmpty )
354  SCH_PARSE_ERROR( _( "expected unquoted string" ), aReader, aCurrentToken );
355 
356  if( aNextToken )
357  {
358  const char* next = tmp;
359 
360  while( *next && isspace( *next ) )
361  next++;
362 
363  *aNextToken = next;
364  }
365 }
366 
367 
383 static void parseQuotedString( wxString& aString, LINE_READER& aReader,
384  const char* aCurrentToken, const char** aNextToken = nullptr,
385  bool aCanBeEmpty = false )
386 {
387  if( !*aCurrentToken )
388  {
389  if( aCanBeEmpty )
390  return;
391  else
392  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
393  }
394 
395  const char* tmp = aCurrentToken;
396 
397  while( *tmp && isspace( *tmp ) )
398  tmp++;
399 
400  if( !*tmp )
401  {
402  if( aCanBeEmpty )
403  return;
404  else
405  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
406  }
407 
408  // Verify opening quote.
409  if( *tmp != '"' )
410  SCH_PARSE_ERROR( "expecting opening quote", aReader, aCurrentToken );
411 
412  tmp++;
413 
414  std::string utf8; // utf8 without escapes and quotes.
415 
416  // Fetch everything up to closing quote.
417  while( *tmp )
418  {
419  if( *tmp == '\\' )
420  {
421  tmp++;
422 
423  if( !*tmp )
424  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, aCurrentToken );
425 
426  // Do not copy the escape byte if it is followed by \ or "
427  if( *tmp != '"' && *tmp != '\\' )
428  utf8 += '\\';
429 
430  utf8 += *tmp;
431  }
432  else if( *tmp == '"' ) // Closing double quote.
433  {
434  break;
435  }
436  else
437  {
438  utf8 += *tmp;
439  }
440 
441  tmp++;
442  }
443 
444  aString = FROM_UTF8( utf8.c_str() );
445 
446  if( aString.IsEmpty() && !aCanBeEmpty )
447  SCH_PARSE_ERROR( "expected quoted string", aReader, aCurrentToken );
448 
449  if( *tmp && *tmp != '"' )
450  SCH_PARSE_ERROR( "no closing quote for string found", aReader, tmp );
451 
452  // Move past the closing quote.
453  tmp++;
454 
455  if( aNextToken )
456  {
457  const char* next = tmp;
458 
459  while( *next == ' ' )
460  next++;
461 
462  *aNextToken = next;
463  }
464 }
465 
466 
473 {
474  static int s_modHash; // Keep track of the modification status of the library.
475  wxString m_fileName; // Absolute path and file name.
476  wxFileName m_libFileName; // Absolute path and file name is required here.
477  wxDateTime m_fileModTime;
478  LIB_SYMBOL_MAP m_symbols; // Map of names of #LIB_SYMBOL pointers.
483  SCH_LIB_TYPE m_libType; // Is this cache a symbol or symbol library.
484 
485  void loadHeader( FILE_LINE_READER& aReader );
486  static void loadAliases( std::unique_ptr<LIB_SYMBOL>& aSymbol, LINE_READER& aReader,
487  LIB_SYMBOL_MAP* aMap = nullptr );
488  static void loadField( std::unique_ptr<LIB_SYMBOL>& aSymbol, LINE_READER& aReader );
489  static void loadDrawEntries( std::unique_ptr<LIB_SYMBOL>& aSymbol,
490  LINE_READER& aReader,
491  int aMajorVersion, int aMinorVersion );
492  static void loadFootprintFilters( std::unique_ptr<LIB_SYMBOL>& aSymbol,
493  LINE_READER& aReader );
494  void loadDocs();
495  static LIB_ARC* loadArc( std::unique_ptr<LIB_SYMBOL>& aSymbol, LINE_READER& aReader );
496  static LIB_CIRCLE* loadCircle( std::unique_ptr<LIB_SYMBOL>& aSymbol, LINE_READER& aReader );
497  static LIB_TEXT* loadText( std::unique_ptr<LIB_SYMBOL>& aSymbol, LINE_READER& aReader,
498  int aMajorVersion, int aMinorVersion );
499  static LIB_RECTANGLE* loadRectangle( std::unique_ptr<LIB_SYMBOL>& aSymbol,
500  LINE_READER& aReader );
501  static LIB_PIN* loadPin( std::unique_ptr<LIB_SYMBOL>& aSymbol, LINE_READER& aReader );
502  static LIB_POLYLINE* loadPolyLine( std::unique_ptr<LIB_SYMBOL>& aSymbol,
503  LINE_READER& aReader );
504  static LIB_BEZIER* loadBezier( std::unique_ptr<LIB_SYMBOL>& aSymbol, LINE_READER& aReader );
505 
506  static FILL_TYPE parseFillMode( LINE_READER& aReader, const char* aLine,
507  const char** aOutput );
508  LIB_SYMBOL* removeSymbol( LIB_SYMBOL* aAlias );
509 
510  void saveDocFile();
511  static void saveArc( LIB_ARC* aArc, OUTPUTFORMATTER& aFormatter );
512  static void saveBezier( LIB_BEZIER* aBezier, OUTPUTFORMATTER& aFormatter );
513  static void saveCircle( LIB_CIRCLE* aCircle, OUTPUTFORMATTER& aFormatter );
514  static void saveField( const LIB_FIELD* aField, OUTPUTFORMATTER& aFormatter );
515  static void savePin( const LIB_PIN* aPin, OUTPUTFORMATTER& aFormatter );
516  static void savePolyLine( LIB_POLYLINE* aPolyLine, OUTPUTFORMATTER& aFormatter );
517  static void saveRectangle( LIB_RECTANGLE* aRectangle, OUTPUTFORMATTER& aFormatter );
518  static void saveText( const LIB_TEXT* aText, OUTPUTFORMATTER& aFormatter );
519 
521 
522  static std::mutex s_modHashMutex;
523 
524 public:
525  SCH_LEGACY_PLUGIN_CACHE( const wxString& aLibraryPath );
527 
528  static void IncrementModifyHash()
529  {
530  std::lock_guard<std::mutex> mut( SCH_LEGACY_PLUGIN_CACHE::s_modHashMutex );
532  }
533 
534  static int GetModifyHash()
535  {
536  std::lock_guard<std::mutex> mut( SCH_LEGACY_PLUGIN_CACHE::s_modHashMutex );
538  }
539 
540  // Most all functions in this class throw IO_ERROR exceptions. There are no
541  // error codes nor user interface calls from here, nor in any SCH_PLUGIN objects.
542  // Catch these exceptions higher up please.
543 
545  void Save( bool aSaveDocFile = true );
546 
547  void Load();
548 
549  void AddSymbol( const LIB_SYMBOL* aSymbol );
550 
551  void DeleteSymbol( const wxString& aName );
552 
553  // If m_libFileName is a symlink follow it to the real source file
554  wxFileName GetRealFile() const;
555 
556  wxDateTime GetLibModificationTime();
557 
558  bool IsFile( const wxString& aFullPathAndFileName ) const;
559 
560  bool IsFileChanged() const;
561 
562  void SetModified( bool aModified = true ) { m_isModified = aModified; }
563 
564  wxString GetLogicalName() const { return m_libFileName.GetName(); }
565 
566  void SetFileName( const wxString& aFileName ) { m_libFileName = aFileName; }
567 
568  wxString GetFileName() const { return m_libFileName.GetFullPath(); }
569 
570  static LIB_SYMBOL* LoadPart( LINE_READER& aReader, int aMajorVersion, int aMinorVersion,
571  LIB_SYMBOL_MAP* aMap = nullptr );
572  static void SaveSymbol( LIB_SYMBOL* aSymbol, OUTPUTFORMATTER& aFormatter,
573  LIB_SYMBOL_MAP* aMap = nullptr );
574 };
575 
576 
578  m_progressReporter( nullptr ),
579  m_lineReader( nullptr ),
580  m_lastProgressLine( 0 ),
581  m_lineCount( 0 )
582 {
583  init( nullptr );
584 }
585 
586 
588 {
589  delete m_cache;
590 }
591 
592 
593 void SCH_LEGACY_PLUGIN::init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties )
594 {
595  m_version = 0;
596  m_rootSheet = nullptr;
597  m_schematic = aSchematic;
598  m_cache = nullptr;
599  m_out = nullptr;
600 }
601 
602 
604 {
605  const unsigned PROGRESS_DELTA = 250;
606 
607  if( m_progressReporter )
608  {
609  unsigned curLine = m_lineReader->LineNumber();
610 
611  if( curLine > m_lastProgressLine + PROGRESS_DELTA )
612  {
613  m_progressReporter->SetCurrentProgress( ( (double) curLine )
614  / std::max( 1U, m_lineCount ) );
615 
617  THROW_IO_ERROR( ( "Open cancelled by user." ) );
618 
619  m_lastProgressLine = curLine;
620  }
621  }
622 }
623 
624 
625 SCH_SHEET* SCH_LEGACY_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSchematic,
626  SCH_SHEET* aAppendToMe, const PROPERTIES* aProperties )
627 {
628  wxASSERT( !aFileName || aSchematic != nullptr );
629 
630  LOCALE_IO toggle; // toggles on, then off, the C locale.
631  SCH_SHEET* sheet;
632 
633  wxFileName fn = aFileName;
634 
635  // Unfortunately child sheet file names the legacy schematic file format are not fully
636  // qualified and are always appended to the project path. The aFileName attribute must
637  // always be an absolute path so the project path can be used for load child sheet files.
638  wxASSERT( fn.IsAbsolute() );
639 
640  if( aAppendToMe )
641  {
642  wxLogTrace( traceSchLegacyPlugin, "Append \"%s\" to sheet \"%s\".",
643  aFileName, aAppendToMe->GetFileName() );
644 
645  wxFileName normedFn = aAppendToMe->GetFileName();
646 
647  if( !normedFn.IsAbsolute() )
648  {
649  if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
650  m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
651  }
652 
653  if( m_path.IsEmpty() )
654  m_path = aSchematic->Prj().GetProjectPath();
655 
656  wxLogTrace( traceSchLegacyPlugin, "m_Normalized append path \"%s\".", m_path );
657  }
658  else
659  {
660  m_path = aSchematic->Prj().GetProjectPath();
661  }
662 
663  m_currentPath.push( m_path );
664  init( aSchematic, aProperties );
665 
666  if( aAppendToMe == nullptr )
667  {
668  // Clean up any allocated memory if an exception occurs loading the schematic.
669  std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( aSchematic );
670  newSheet->SetFileName( aFileName );
671  m_rootSheet = newSheet.get();
672  loadHierarchy( newSheet.get() );
673 
674  // If we got here, the schematic loaded successfully.
675  sheet = newSheet.release();
676  m_rootSheet = nullptr; // Quiet Coverity warning.
677  }
678  else
679  {
680  wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
681  m_rootSheet = &aSchematic->Root();
682  sheet = aAppendToMe;
683  loadHierarchy( sheet );
684  }
685 
686  wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
687 
688  return sheet;
689 }
690 
691 
692 // Everything below this comment is recursive. Modify with care.
693 
695 {
696  SCH_SCREEN* screen = nullptr;
697 
698  if( !aSheet->GetScreen() )
699  {
700  // SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
701  // stores the file name and extension. Add the project path to the file name and
702  // extension to compare when calling SCH_SHEET::SearchHierarchy().
703  wxFileName fileName = aSheet->GetFileName();
704  fileName.SetExt( "sch" );
705 
706  if( !fileName.IsAbsolute() )
707  fileName.MakeAbsolute( m_currentPath.top() );
708 
709  // Save the current path so that it gets restored when descending and ascending the
710  // sheet hierarchy which allows for sheet schematic files to be nested in folders
711  // relative to the last path a schematic was loaded from.
712  wxLogTrace( traceSchLegacyPlugin, "Saving path '%s'", m_currentPath.top() );
713  m_currentPath.push( fileName.GetPath() );
714  wxLogTrace( traceSchLegacyPlugin, "Current path '%s'", m_currentPath.top() );
715  wxLogTrace( traceSchLegacyPlugin, "Loading '%s'", fileName.GetFullPath() );
716 
717  m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen );
718 
719  if( screen )
720  {
721  aSheet->SetScreen( screen );
722  screen->SetParent( m_schematic );
723  // Do not need to load the sub-sheets - this has already been done.
724  }
725  else
726  {
727  aSheet->SetScreen( new SCH_SCREEN( m_schematic ) );
728  aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
729 
730  try
731  {
732  loadFile( fileName.GetFullPath(), aSheet->GetScreen() );
733  }
734  catch( const IO_ERROR& ioe )
735  {
736  // If there is a problem loading the root sheet, there is no recovery.
737  if( aSheet == m_rootSheet )
738  throw( ioe );
739 
740  // For all subsheets, queue up the error message for the caller.
741  if( !m_error.IsEmpty() )
742  m_error += "\n";
743 
744  m_error += ioe.What();
745  }
746 
747  for( auto aItem : aSheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
748  {
749  wxCHECK2( aItem->Type() == SCH_SHEET_T, continue );
750  auto sheet = static_cast<SCH_SHEET*>( aItem );
751 
752  // Set the parent to aSheet. This effectively creates a method to find
753  // the root sheet from any sheet so a pointer to the root sheet does not
754  // need to be stored globally. Note: this is not the same as a hierarchy.
755  // Complex hierarchies can have multiple copies of a sheet. This only
756  // provides a simple tree to find the root sheet.
757  sheet->SetParent( aSheet );
758 
759  // Recursion starts here.
760  loadHierarchy( sheet );
761  }
762  }
763 
764  m_currentPath.pop();
765  wxLogTrace( traceSchLegacyPlugin, "Restoring path \"%s\"", m_currentPath.top() );
766  }
767 }
768 
769 
770 void SCH_LEGACY_PLUGIN::loadFile( const wxString& aFileName, SCH_SCREEN* aScreen )
771 {
772  FILE_LINE_READER reader( aFileName );
773 
774  if( m_progressReporter )
775  {
776  m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
777 
779  THROW_IO_ERROR( ( "Open cancelled by user." ) );
780 
781  m_lineReader = &reader;
782  m_lineCount = 0;
783 
784  while( reader.ReadLine() )
785  m_lineCount++;
786 
787  reader.Rewind();
788  }
789 
790  loadHeader( reader, aScreen );
791 
792  LoadContent( reader, aScreen, m_version );
793 
794  // Unfortunately schematic files prior to version 2 are not terminated with $EndSCHEMATC
795  // so checking for its existance will fail so just exit here and take our chances. :(
796  if( m_version > 1 )
797  {
798  char* line = reader.Line();
799 
800  while( *line == ' ' )
801  line++;
802 
803  if( !strCompare( "$EndSCHEMATC", line ) )
804  THROW_IO_ERROR( "'$EndSCHEMATC' not found" );
805  }
806 }
807 
808 
809 void SCH_LEGACY_PLUGIN::LoadContent( LINE_READER& aReader, SCH_SCREEN* aScreen, int version )
810 {
811  m_version = version;
812 
813  // We cannot safely load content without a set root level.
814  wxCHECK_RET( m_rootSheet,
815  "Cannot call SCH_LEGACY_PLUGIN::LoadContent() without setting root sheet." );
816 
817  while( aReader.ReadLine() )
818  {
819  checkpoint();
820 
821  char* line = aReader.Line();
822 
823  while( *line == ' ' )
824  line++;
825 
826  // Either an object will be loaded properly or the file load will fail and raise
827  // an exception.
828  if( strCompare( "$Descr", line ) )
829  loadPageSettings( aReader, aScreen );
830  else if( strCompare( "$Comp", line ) )
831  aScreen->Append( loadSymbol( aReader ) );
832  else if( strCompare( "$Sheet", line ) )
833  aScreen->Append( loadSheet( aReader ) );
834  else if( strCompare( "$Bitmap", line ) )
835  aScreen->Append( loadBitmap( aReader ) );
836  else if( strCompare( "Connection", line ) )
837  aScreen->Append( loadJunction( aReader ) );
838  else if( strCompare( "NoConn", line ) )
839  aScreen->Append( loadNoConnect( aReader ) );
840  else if( strCompare( "Wire", line ) )
841  aScreen->Append( loadWire( aReader ) );
842  else if( strCompare( "Entry", line ) )
843  aScreen->Append( loadBusEntry( aReader ) );
844  else if( strCompare( "Text", line ) )
845  aScreen->Append( loadText( aReader ) );
846  else if( strCompare( "BusAlias", line ) )
847  aScreen->AddBusAlias( loadBusAlias( aReader, aScreen ) );
848  else if( strCompare( "$EndSCHEMATC", line ) )
849  return;
850  else
851  SCH_PARSE_ERROR( "unrecognized token", aReader, line );
852  }
853 }
854 
855 
857 {
858  const char* line = aReader.ReadLine();
859 
860  if( !line || !strCompare( "Eeschema Schematic File Version", line, &line ) )
861  {
862  m_error.Printf( _( "'%s' does not appear to be an Eeschema file." ),
863  aScreen->GetFileName() );
865  }
866 
867  // get the file version here.
868  m_version = parseInt( aReader, line, &line );
869 
870  // The next lines are the lib list section, and are mainly comments, like:
871  // LIBS:power
872  // the lib list is not used, but is in schematic file just in case.
873  // It is usually not empty, but we accept empty list.
874  // If empty, there is a legacy section, not used
875  // EELAYER i j
876  // and the last line is
877  // EELAYER END
878  // Skip all lines until the end of header "EELAYER END" is found
879  while( aReader.ReadLine() )
880  {
881  checkpoint();
882 
883  line = aReader.Line();
884 
885  while( *line == ' ' )
886  line++;
887 
888  if( strCompare( "EELAYER END", line ) )
889  return;
890  }
891 
892  THROW_IO_ERROR( _( "Missing 'EELAYER END'" ) );
893 }
894 
895 
897 {
898  wxASSERT( aScreen != nullptr );
899 
900  wxString buf;
901  const char* line = aReader.Line();
902 
903  PAGE_INFO pageInfo;
904  TITLE_BLOCK tb;
905 
906  wxCHECK_RET( strCompare( "$Descr", line, &line ), "Invalid sheet description" );
907 
908  parseUnquotedString( buf, aReader, line, &line );
909 
910  if( !pageInfo.SetType( buf ) )
911  SCH_PARSE_ERROR( "invalid page size", aReader, line );
912 
913  int pagew = parseInt( aReader, line, &line );
914  int pageh = parseInt( aReader, line, &line );
915 
916  if( buf == PAGE_INFO::Custom )
917  {
918  pageInfo.SetWidthMils( pagew );
919  pageInfo.SetHeightMils( pageh );
920  }
921  else
922  {
923  wxString orientation;
924 
925  // Non custom size, set portrait if its present. Can be empty string which defaults
926  // to landscape.
927  parseUnquotedString( orientation, aReader, line, &line, true );
928 
929  if( orientation == "portrait" )
930  pageInfo.SetPortrait( true );
931  }
932 
933  aScreen->SetPageSettings( pageInfo );
934 
935  while( line != nullptr )
936  {
937  buf.clear();
938 
939  if( !aReader.ReadLine() )
940  SCH_PARSE_ERROR( _( "unexpected end of file" ), aReader, line );
941 
942  line = aReader.Line();
943 
944  if( strCompare( "Sheet", line, &line ) )
945  {
946  aScreen->SetVirtualPageNumber( parseInt( aReader, line, &line ) );
947  aScreen->SetPageCount( parseInt( aReader, line, &line ) );
948  }
949  else if( strCompare( "Title", line, &line ) )
950  {
951  parseQuotedString( buf, aReader, line, &line, true );
952  tb.SetTitle( buf );
953  }
954  else if( strCompare( "Date", line, &line ) )
955  {
956  parseQuotedString( buf, aReader, line, &line, true );
957  tb.SetDate( buf );
958  }
959  else if( strCompare( "Rev", line, &line ) )
960  {
961  parseQuotedString( buf, aReader, line, &line, true );
962  tb.SetRevision( buf );
963  }
964  else if( strCompare( "Comp", line, &line ) )
965  {
966  parseQuotedString( buf, aReader, line, &line, true );
967  tb.SetCompany( buf );
968  }
969  else if( strCompare( "Comment1", line, &line ) )
970  {
971  parseQuotedString( buf, aReader, line, &line, true );
972  tb.SetComment( 0, buf );
973  }
974  else if( strCompare( "Comment2", line, &line ) )
975  {
976  parseQuotedString( buf, aReader, line, &line, true );
977  tb.SetComment( 1, buf );
978  }
979  else if( strCompare( "Comment3", line, &line ) )
980  {
981  parseQuotedString( buf, aReader, line, &line, true );
982  tb.SetComment( 2, buf );
983  }
984  else if( strCompare( "Comment4", line, &line ) )
985  {
986  parseQuotedString( buf, aReader, line, &line, true );
987  tb.SetComment( 3, buf );
988  }
989  else if( strCompare( "Comment5", line, &line ) )
990  {
991  parseQuotedString( buf, aReader, line, &line, true );
992  tb.SetComment( 4, buf );
993  }
994  else if( strCompare( "Comment6", line, &line ) )
995  {
996  parseQuotedString( buf, aReader, line, &line, true );
997  tb.SetComment( 5, buf );
998  }
999  else if( strCompare( "Comment7", line, &line ) )
1000  {
1001  parseQuotedString( buf, aReader, line, &line, true );
1002  tb.SetComment( 6, buf );
1003  }
1004  else if( strCompare( "Comment8", line, &line ) )
1005  {
1006  parseQuotedString( buf, aReader, line, &line, true );
1007  tb.SetComment( 7, buf );
1008  }
1009  else if( strCompare( "Comment9", line, &line ) )
1010  {
1011  parseQuotedString( buf, aReader, line, &line, true );
1012  tb.SetComment( 8, buf );
1013  }
1014  else if( strCompare( "$EndDescr", line ) )
1015  {
1016  aScreen->SetTitleBlock( tb );
1017  return;
1018  }
1019  }
1020 
1021  SCH_PARSE_ERROR( "missing 'EndDescr'", aReader, line );
1022 }
1023 
1024 
1026 {
1027  std::unique_ptr<SCH_SHEET> sheet = std::make_unique<SCH_SHEET>();
1028 
1029  const char* line = aReader.ReadLine();
1030 
1031  while( line != nullptr )
1032  {
1033  if( strCompare( "S", line, &line ) ) // Sheet dimensions.
1034  {
1035  wxPoint position;
1036 
1037  position.x = Mils2Iu( parseInt( aReader, line, &line ) );
1038  position.y = Mils2Iu( parseInt( aReader, line, &line ) );
1039  sheet->SetPosition( position );
1040 
1041  wxSize size;
1042 
1043  size.SetWidth( Mils2Iu( parseInt( aReader, line, &line ) ) );
1044  size.SetHeight( Mils2Iu( parseInt( aReader, line, &line ) ) );
1045  sheet->SetSize( size );
1046  }
1047  else if( strCompare( "U", line, &line ) ) // Sheet UUID.
1048  {
1049  wxString text;
1050  parseUnquotedString( text, aReader, line );
1051 
1052  if( text != "00000000" )
1053  const_cast<KIID&>( sheet->m_Uuid ) = KIID( text );
1054  }
1055  else if( *line == 'F' ) // Sheet field.
1056  {
1057  line++;
1058 
1059  wxString text;
1060  int size;
1061  int fieldId = parseInt( aReader, line, &line );
1062 
1063  if( fieldId == 0 || fieldId == 1 ) // Sheet name and file name.
1064  {
1065  parseQuotedString( text, aReader, line, &line );
1066  size = Mils2Iu( parseInt( aReader, line, &line ) );
1067 
1068  SCH_FIELD& field = sheet->GetFields()[ fieldId ];
1069  field.SetText( text );
1070  field.SetTextSize( wxSize( size, size ) );
1071  }
1072  else // Sheet pin.
1073  {
1074  // Use a unique_ptr so that we clean up in the case of a throw
1075  std::unique_ptr<SCH_SHEET_PIN> sheetPin = std::make_unique<SCH_SHEET_PIN>( sheet.get() );
1076 
1077  sheetPin->SetNumber( fieldId );
1078 
1079  // Can be empty fields.
1080  parseQuotedString( text, aReader, line, &line, true );
1081 
1082  sheetPin->SetText( ConvertToNewOverbarNotation( text ) );
1083 
1084  if( line == nullptr )
1085  THROW_IO_ERROR( _( "unexpected end of line" ) );
1086 
1087  switch( parseChar( aReader, line, &line ) )
1088  {
1089  case 'I': sheetPin->SetShape( PINSHEETLABEL_SHAPE::PS_INPUT ); break;
1090  case 'O': sheetPin->SetShape( PINSHEETLABEL_SHAPE::PS_OUTPUT ); break;
1091  case 'B': sheetPin->SetShape( PINSHEETLABEL_SHAPE::PS_BIDI ); break;
1092  case 'T': sheetPin->SetShape( PINSHEETLABEL_SHAPE::PS_TRISTATE ); break;
1093  case 'U': sheetPin->SetShape( PINSHEETLABEL_SHAPE::PS_UNSPECIFIED ); break;
1094  default: SCH_PARSE_ERROR( "invalid sheet pin type", aReader, line );
1095  }
1096 
1097  switch( parseChar( aReader, line, &line ) )
1098  {
1099  case 'R': sheetPin->SetEdge( SHEET_SIDE::RIGHT ); break;
1100  case 'T': sheetPin->SetEdge( SHEET_SIDE::TOP ); break;
1101  case 'B': sheetPin->SetEdge( SHEET_SIDE::BOTTOM ); break;
1102  case 'L': sheetPin->SetEdge( SHEET_SIDE::LEFT ); break;
1103  default:
1104  SCH_PARSE_ERROR( "invalid sheet pin side", aReader, line );
1105  }
1106 
1107  wxPoint position;
1108 
1109  position.x = Mils2Iu( parseInt( aReader, line, &line ) );
1110  position.y = Mils2Iu( parseInt( aReader, line, &line ) );
1111  sheetPin->SetPosition( position );
1112 
1113  size = Mils2Iu( parseInt( aReader, line, &line ) );
1114 
1115  sheetPin->SetTextSize( wxSize( size, size ) );
1116 
1117  sheet->AddPin( sheetPin.release() );
1118  }
1119  }
1120  else if( strCompare( "$EndSheet", line ) )
1121  {
1122  sheet->AutoplaceFields( /* aScreen */ nullptr, /* aManual */ false );
1123  return sheet.release();
1124  }
1125 
1126  line = aReader.ReadLine();
1127  }
1128 
1129  SCH_PARSE_ERROR( "missing '$EndSheet`", aReader, line );
1130 
1131  return nullptr; // Prevents compiler warning. Should never get here.
1132 }
1133 
1134 
1136 {
1137  std::unique_ptr<SCH_BITMAP> bitmap = std::make_unique<SCH_BITMAP>();
1138 
1139  const char* line = aReader.Line();
1140 
1141  wxCHECK( strCompare( "$Bitmap", line, &line ), nullptr );
1142 
1143  line = aReader.ReadLine();
1144 
1145  while( line != nullptr )
1146  {
1147  if( strCompare( "Pos", line, &line ) )
1148  {
1149  wxPoint position;
1150 
1151  position.x = Mils2Iu( parseInt( aReader, line, &line ) );
1152  position.y = Mils2Iu( parseInt( aReader, line, &line ) );
1153  bitmap->SetPosition( position );
1154  }
1155  else if( strCompare( "Scale", line, &line ) )
1156  {
1157  auto scalefactor = parseDouble( aReader, line, &line );
1158 
1159  // Prevent scalefactor values that cannot be displayed.
1160  // In the case of a bad value, we accept that the image might be mis-scaled
1161  // rather than removing the full image. Users can then edit the scale factor in
1162  // Eeschema to the appropriate value
1163  if( !std::isnormal( scalefactor ) )
1164  scalefactor = 1.0;
1165 
1166  bitmap->GetImage()->SetScale( scalefactor );
1167  }
1168  else if( strCompare( "Data", line, &line ) )
1169  {
1170  wxMemoryOutputStream stream;
1171 
1172  while( line )
1173  {
1174  if( !aReader.ReadLine() )
1175  SCH_PARSE_ERROR( _( "Unexpected end of file" ), aReader, line );
1176 
1177  line = aReader.Line();
1178 
1179  if( strCompare( "EndData", line ) )
1180  {
1181  // all the PNG date is read.
1182  // We expect here m_image and m_bitmap are void
1183  wxImage* image = new wxImage();
1184  wxMemoryInputStream istream( stream );
1185  image->LoadFile( istream, wxBITMAP_TYPE_PNG );
1186  bitmap->GetImage()->SetImage( image );
1187  bitmap->GetImage()->SetBitmap( new wxBitmap( *image ) );
1188  break;
1189  }
1190 
1191  // Read PNG data, stored in hexadecimal,
1192  // each byte = 2 hexadecimal digits and a space between 2 bytes
1193  // and put it in memory stream buffer
1194  int len = strlen( line );
1195 
1196  for( ; len > 0 && !isspace( *line ); len -= 3, line += 3 )
1197  {
1198  int value = 0;
1199 
1200  if( sscanf( line, "%X", &value ) == 1 )
1201  stream.PutC( (char) value );
1202  else
1203  THROW_IO_ERROR( "invalid PNG data" );
1204  }
1205  }
1206 
1207  if( line == nullptr )
1208  THROW_IO_ERROR( _( "unexpected end of file" ) );
1209  }
1210  else if( strCompare( "$EndBitmap", line ) )
1211  return bitmap.release();
1212 
1213  line = aReader.ReadLine();
1214  }
1215 
1216  THROW_IO_ERROR( _( "unexpected end of file" ) );
1217 }
1218 
1219 
1221 {
1222  std::unique_ptr<SCH_JUNCTION> junction = std::make_unique<SCH_JUNCTION>();
1223 
1224  const char* line = aReader.Line();
1225 
1226  wxCHECK( strCompare( "Connection", line, &line ), nullptr );
1227 
1228  wxString name;
1229 
1230  parseUnquotedString( name, aReader, line, &line );
1231 
1232  wxPoint position;
1233 
1234  position.x = Mils2Iu( parseInt( aReader, line, &line ) );
1235  position.y = Mils2Iu( parseInt( aReader, line, &line ) );
1236  junction->SetPosition( position );
1237 
1238  return junction.release();
1239 }
1240 
1241 
1243 {
1244  std::unique_ptr<SCH_NO_CONNECT> no_connect = std::make_unique<SCH_NO_CONNECT>();
1245 
1246  const char* line = aReader.Line();
1247 
1248  wxCHECK( strCompare( "NoConn", line, &line ), nullptr );
1249 
1250  wxString name;
1251 
1252  parseUnquotedString( name, aReader, line, &line );
1253 
1254  wxPoint position;
1255 
1256  position.x = Mils2Iu( parseInt( aReader, line, &line ) );
1257  position.y = Mils2Iu( parseInt( aReader, line, &line ) );
1258  no_connect->SetPosition( position );
1259 
1260  return no_connect.release();
1261 }
1262 
1263 
1265 {
1266  std::unique_ptr<SCH_LINE> wire = std::make_unique<SCH_LINE>();
1267 
1268  const char* line = aReader.Line();
1269 
1270  wxCHECK( strCompare( "Wire", line, &line ), nullptr );
1271 
1272  if( strCompare( "Wire", line, &line ) )
1273  wire->SetLayer( LAYER_WIRE );
1274  else if( strCompare( "Bus", line, &line ) )
1275  wire->SetLayer( LAYER_BUS );
1276  else if( strCompare( "Notes", line, &line ) )
1277  wire->SetLayer( LAYER_NOTES );
1278  else
1279  SCH_PARSE_ERROR( "invalid line type", aReader, line );
1280 
1281  if( !strCompare( "Line", line, &line ) )
1282  SCH_PARSE_ERROR( "invalid wire definition", aReader, line );
1283 
1284  // Since Sept 15, 2017, a line style is alloved (width, style, color)
1285  // Only non default values are stored
1286  while( !is_eol( *line ) )
1287  {
1288  wxString buf;
1289 
1290  parseUnquotedString( buf, aReader, line, &line );
1291 
1292  if( buf == ")" )
1293  continue;
1294 
1295  else if( buf == T_WIDTH )
1296  {
1297  int size = Mils2Iu( parseInt( aReader, line, &line ) );
1298  wire->SetLineWidth( size );
1299  }
1300  else if( buf == T_STYLE )
1301  {
1302  parseUnquotedString( buf, aReader, line, &line );
1304  wire->SetLineStyle( style );
1305  }
1306  else // should be the color parameter.
1307  {
1308  // The color param is something like rgb(150, 40, 191)
1309  // and because there is no space between ( and 150
1310  // the first param is inside buf.
1311  // So break keyword and the first param into 2 separate strings.
1312  wxString prm, keyword;
1313  keyword = buf.BeforeLast( '(', &prm );
1314 
1315  if( ( keyword == T_COLOR ) || ( keyword == T_COLORA ) )
1316  {
1317  long color[4] = { 0 };
1318 
1319  int ii = 0;
1320 
1321  if( !prm.IsEmpty() )
1322  {
1323  prm.ToLong( &color[ii] );
1324  ii++;
1325  }
1326 
1327  int prm_count = ( keyword == T_COLORA ) ? 4 : 3;
1328  // fix opacity to 1.0 or 255, when not exists in file
1329  color[3] = 255;
1330 
1331  for(; ii < prm_count && !is_eol( *line ); ii++ )
1332  {
1333  color[ii] = parseInt( aReader, line, &line );
1334 
1335  // Skip the separator between values
1336  if( *line == ',' || *line == ' ')
1337  line++;
1338  }
1339 
1340  wire->SetLineColor( color[0]/255.0, color[1]/255.0, color[2]/255.0,color[3]/255.0 );
1341  }
1342  }
1343  }
1344 
1345  // Read the segment en points coordinates:
1346  line = aReader.ReadLine();
1347 
1348  wxPoint begin, end;
1349 
1350  begin.x = Mils2Iu( parseInt( aReader, line, &line ) );
1351  begin.y = Mils2Iu( parseInt( aReader, line, &line ) );
1352  end.x = Mils2Iu( parseInt( aReader, line, &line ) );
1353  end.y = Mils2Iu( parseInt( aReader, line, &line ) );
1354 
1355  wire->SetStartPoint( begin );
1356  wire->SetEndPoint( end );
1357 
1358  return wire.release();
1359 }
1360 
1361 
1363 {
1364  const char* line = aReader.Line();
1365 
1366  wxCHECK( strCompare( "Entry", line, &line ), nullptr );
1367 
1368  std::unique_ptr<SCH_BUS_ENTRY_BASE> busEntry;
1369 
1370  if( strCompare( "Wire", line, &line ) )
1371  {
1372  busEntry = std::make_unique<SCH_BUS_WIRE_ENTRY>();
1373 
1374  if( !strCompare( "Line", line, &line ) )
1375  SCH_PARSE_ERROR( "invalid bus entry definition expected 'Line'", aReader, line );
1376  }
1377  else if( strCompare( "Bus", line, &line ) )
1378  {
1379  busEntry = std::make_unique<SCH_BUS_BUS_ENTRY>();
1380 
1381  if( !strCompare( "Bus", line, &line ) )
1382  SCH_PARSE_ERROR( "invalid bus entry definition expected 'Bus'", aReader, line );
1383  }
1384  else
1385  SCH_PARSE_ERROR( "invalid bus entry type", aReader, line );
1386 
1387  line = aReader.ReadLine();
1388 
1389  wxPoint pos;
1390  wxSize size;
1391 
1392  pos.x = Mils2Iu( parseInt( aReader, line, &line ) );
1393  pos.y = Mils2Iu( parseInt( aReader, line, &line ) );
1394  size.x = Mils2Iu( parseInt( aReader, line, &line ) );
1395  size.y = Mils2Iu( parseInt( aReader, line, &line ) );
1396 
1397  size.x -= pos.x;
1398  size.y -= pos.y;
1399 
1400  busEntry->SetPosition( pos );
1401  busEntry->SetSize( size );
1402 
1403  return busEntry.release();
1404 }
1405 
1406 // clang-format off
1407 const std::map<PINSHEETLABEL_SHAPE, const char*> sheetLabelNames
1409  { PINSHEETLABEL_SHAPE::PS_INPUT, "Input" },
1410  { PINSHEETLABEL_SHAPE::PS_OUTPUT, "Output" },
1411  { PINSHEETLABEL_SHAPE::PS_BIDI, "BiDi" },
1412  { PINSHEETLABEL_SHAPE::PS_TRISTATE, "3State" },
1414 };
1415 // clang-format on
1416 
1417 
1419 {
1420  const char* line = aReader.Line();
1421 
1422  wxCHECK( strCompare( "Text", line, &line ), nullptr );
1423 
1424  std::unique_ptr<SCH_TEXT> text;
1425 
1426  if( strCompare( "Notes", line, &line ) )
1427  text.reset( new SCH_TEXT );
1428  else if( strCompare( "Label", line, &line ) )
1429  text.reset( new SCH_LABEL );
1430  else if( strCompare( "HLabel", line, &line ) )
1431  text.reset( new SCH_HIERLABEL );
1432  else if( strCompare( "GLabel", line, &line ) )
1433  {
1434  // Prior to version 2, the SCH_GLOBALLABEL object did not exist.
1435  if( m_version == 1 )
1436  text = std::make_unique<SCH_HIERLABEL>();
1437  else
1438  text = std::make_unique<SCH_GLOBALLABEL>();
1439  }
1440  else
1441  SCH_PARSE_ERROR( "unknown Text type", aReader, line );
1442 
1443  // Parse the parameters common to all text objects.
1444  wxPoint position;
1445 
1446  position.x = Mils2Iu( parseInt( aReader, line, &line ) );
1447  position.y = Mils2Iu( parseInt( aReader, line, &line ) );
1448  text->SetPosition( position );
1449 
1450  int spinStyle = parseInt( aReader, line, &line );
1451 
1452  // Sadly we store the orientation of hierarchical and global labels using a different
1453  // int encoding than that for local labels:
1454  // Global Local
1455  // Left justified 0 2
1456  // Up 1 3
1457  // Right justified 2 0
1458  // Down 3 1
1459  // So we must flip it as the enum is setup with the "global" numbering
1460  if( text->Type() != SCH_GLOBAL_LABEL_T && text->Type() != SCH_HIER_LABEL_T )
1461  {
1462  if( spinStyle == 0 )
1463  spinStyle = 2;
1464  else if( spinStyle == 2 )
1465  spinStyle = 0;
1466  }
1467 
1468  text->SetLabelSpinStyle( (LABEL_SPIN_STYLE::SPIN) spinStyle );
1469 
1470  int size = Mils2Iu( parseInt( aReader, line, &line ) );
1471 
1472  text->SetTextSize( wxSize( size, size ) );
1473 
1474  // Parse the global and hierarchical label type.
1475  if( text->Type() == SCH_HIER_LABEL_T || text->Type() == SCH_GLOBAL_LABEL_T )
1476  {
1477  auto resultIt = std::find_if( sheetLabelNames.begin(), sheetLabelNames.end(),
1478  [ &line ]( const auto& it )
1479  {
1480  return strCompare( it.second, line, &line );
1481  } );
1482 
1483  if( resultIt != sheetLabelNames.end() )
1484  text->SetShape( resultIt->first );
1485  else
1486  SCH_PARSE_ERROR( "invalid label type", aReader, line );
1487  }
1488 
1489  int penWidth = 0;
1490 
1491  // The following tokens do not exist in version 1 schematic files,
1492  // and not always in version 2 for HLabels and GLabels
1493  if( m_version > 1 )
1494  {
1495  if( m_version > 2 || *line >= ' ' )
1496  {
1497  if( strCompare( "Italic", line, &line ) )
1498  text->SetItalic( true );
1499  else if( !strCompare( "~", line, &line ) )
1500  SCH_PARSE_ERROR( _( "expected 'Italics' or '~'" ), aReader, line );
1501  }
1502 
1503  // The penWidth token does not exist in older versions of the schematic file format
1504  // so calling parseInt will be made only if the EOL is not reached.
1505  if( *line >= ' ' )
1506  penWidth = parseInt( aReader, line, &line );
1507  }
1508 
1509  text->SetBold( penWidth != 0 );
1510  text->SetTextThickness( penWidth != 0 ? GetPenSizeForBold( size ) : 0 );
1511 
1512  // Read the text string for the text.
1513  char* tmp = aReader.ReadLine();
1514 
1515  tmp = strtok( tmp, "\r\n" );
1516  wxString val = FROM_UTF8( tmp );
1517 
1518  for( ; ; )
1519  {
1520  int i = val.find( wxT( "\\n" ) );
1521 
1522  if( i == wxNOT_FOUND )
1523  break;
1524 
1525  val.erase( i, 2 );
1526  val.insert( i, wxT( "\n" ) );
1527  }
1528 
1529  text->SetText( ConvertToNewOverbarNotation( val ) );
1530 
1531  return text.release();
1532 }
1533 
1534 
1536 {
1537  const char* line = aReader.Line();
1538 
1539  wxCHECK( strCompare( "$Comp", line, &line ), nullptr );
1540 
1541  std::unique_ptr<SCH_SYMBOL> symbol = std::make_unique<SCH_SYMBOL>();
1542 
1543  line = aReader.ReadLine();
1544 
1545  while( line != nullptr )
1546  {
1547  if( strCompare( "L", line, &line ) )
1548  {
1549  wxString libName;
1550  size_t pos = 2; // "X" plus ' ' space character.
1551  wxString utf8Line = wxString::FromUTF8( line );
1552  wxStringTokenizer tokens( utf8Line, " \r\n\t" );
1553 
1554  if( tokens.CountTokens() < 2 )
1555  THROW_PARSE_ERROR( "invalid symbol library definition", aReader.GetSource(),
1556  aReader.Line(), aReader.LineNumber(), pos );
1557 
1558  libName = tokens.GetNextToken();
1559  libName.Replace( "~", " " );
1560 
1561  LIB_ID libId;
1562 
1563  // Prior to schematic version 4, library IDs did not have a library nickname so
1564  // parsing the symbol name with LIB_ID::Parse() would break symbol library links
1565  // that contained '/' and ':' characters.
1566  if( m_version > 3 )
1567  libId.Parse( libName, true );
1568  else
1569  libId.SetLibItemName( libName );
1570 
1571  symbol->SetLibId( libId );
1572 
1573  wxString refDesignator = tokens.GetNextToken();
1574 
1575  refDesignator.Replace( "~", " " );
1576 
1577  wxString prefix = refDesignator;
1578 
1579  while( prefix.Length() )
1580  {
1581  if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
1582  break;
1583 
1584  prefix.RemoveLast();
1585  }
1586 
1587  // Avoid a prefix containing trailing/leading spaces
1588  prefix.Trim( true );
1589  prefix.Trim( false );
1590 
1591  if( prefix.IsEmpty() )
1592  symbol->SetPrefix( wxString( "U" ) );
1593  else
1594  symbol->SetPrefix( prefix );
1595  }
1596  else if( strCompare( "U", line, &line ) )
1597  {
1598  // This fixes a potentially buggy files caused by unit being set to zero which
1599  // causes netlist issues. See https://bugs.launchpad.net/kicad/+bug/1677282.
1600  int unit = parseInt( aReader, line, &line );
1601 
1602  if( unit == 0 )
1603  {
1604  unit = 1;
1605 
1606  // Set the file as modified so the user can be warned.
1607  if( m_rootSheet->GetScreen() )
1609  }
1610 
1611  symbol->SetUnit( unit );
1612 
1613  // Same can also happen with the convert parameter
1614  int convert = parseInt( aReader, line, &line );
1615 
1616  if( convert == 0 )
1617  {
1618  convert = 1;
1619 
1620  // Set the file as modified so the user can be warned.
1621  if( m_rootSheet->GetScreen() )
1623  }
1624 
1625  symbol->SetConvert( convert );
1626 
1627  wxString text;
1628  parseUnquotedString( text, aReader, line, &line );
1629 
1630  if( text != "00000000" )
1631  const_cast<KIID&>( symbol->m_Uuid ) = KIID( text );
1632  }
1633  else if( strCompare( "P", line, &line ) )
1634  {
1635  wxPoint pos;
1636 
1637  pos.x = Mils2Iu( parseInt( aReader, line, &line ) );
1638  pos.y = Mils2Iu( parseInt( aReader, line, &line ) );
1639  symbol->SetPosition( pos );
1640  }
1641  else if( strCompare( "AR", line, &line ) )
1642  {
1643  const char* strCompare = "Path=";
1644  int len = strlen( strCompare );
1645 
1646  if( strncasecmp( strCompare, line, len ) != 0 )
1647  SCH_PARSE_ERROR( "missing 'Path=' token", aReader, line );
1648 
1649  line += len;
1650  wxString pathStr, reference, unit;
1651 
1652  parseQuotedString( pathStr, aReader, line, &line );
1653 
1654  // Note: AR path excludes root sheet, but includes symbol. Normalize to
1655  // internal format by shifting everything down one and adding the root sheet.
1656  KIID_PATH path( pathStr );
1657 
1658  if( path.size() > 0 )
1659  {
1660  for( size_t i = path.size() - 1; i > 0; --i )
1661  path[i] = path[i-1];
1662 
1663  path[0] = m_rootSheet->m_Uuid;
1664  }
1665  else
1666  path.push_back( m_rootSheet->m_Uuid );
1667 
1668  strCompare = "Ref=";
1669  len = strlen( strCompare );
1670 
1671  if( strncasecmp( strCompare, line, len ) != 0 )
1672  SCH_PARSE_ERROR( "missing 'Ref=' token", aReader, line );
1673 
1674  line+= len;
1675  parseQuotedString( reference, aReader, line, &line );
1676 
1677  strCompare = "Part=";
1678  len = strlen( strCompare );
1679 
1680  if( strncasecmp( strCompare, line, len ) != 0 )
1681  SCH_PARSE_ERROR( "missing 'Part=' token", aReader, line );
1682 
1683  line+= len;
1684  parseQuotedString( unit, aReader, line, &line );
1685 
1686  long tmp;
1687 
1688  if( !unit.ToLong( &tmp, 10 ) )
1689  SCH_PARSE_ERROR( "expected integer value", aReader, line );
1690 
1691  if( tmp < 0 || tmp > MAX_UNIT_COUNT_PER_PACKAGE )
1692  SCH_PARSE_ERROR( "unit value out of range", aReader, line );
1693 
1694  symbol->AddHierarchicalReference( path, reference, (int)tmp );
1695  symbol->GetField( REFERENCE_FIELD )->SetText( reference );
1696 
1697  }
1698  else if( strCompare( "F", line, &line ) )
1699  {
1700  int index = parseInt( aReader, line, &line );
1701 
1702  wxString text, name;
1703 
1704  parseQuotedString( text, aReader, line, &line, true );
1705 
1706  char orientation = parseChar( aReader, line, &line );
1707  wxPoint pos;
1708  pos.x = Mils2Iu( parseInt( aReader, line, &line ) );
1709  pos.y = Mils2Iu( parseInt( aReader, line, &line ) );
1710  int size = Mils2Iu( parseInt( aReader, line, &line ) );
1711  int attributes = parseHex( aReader, line, &line );
1712 
1713  if( index >= symbol->GetFieldCount() )
1714  {
1715  // The first MANDATOR_FIELDS _must_ be constructed within the SCH_SYMBOL
1716  // constructor. This assert is simply here to guard against a change in that
1717  // constructor.
1718  wxASSERT( symbol->GetFieldCount() >= MANDATORY_FIELDS );
1719 
1720  // Ignore the _supplied_ fieldNdx. It is not important anymore if within the
1721  // user defined fields region (i.e. >= MANDATORY_FIELDS).
1722  // We freely renumber the index to fit the next available field slot.
1723  index = symbol->GetFieldCount(); // new has this index after insertion
1724 
1725  SCH_FIELD field( wxPoint( 0, 0 ), index, symbol.get(), name );
1726  symbol->AddField( field );
1727  }
1728 
1729  SCH_FIELD& field = symbol->GetFields()[index];
1730 
1731  // Prior to version 2 of the schematic file format, none of the following existed.
1732  if( m_version > 1 )
1733  {
1734  wxString textAttrs;
1735  char hjustify = parseChar( aReader, line, &line );
1736 
1737  parseUnquotedString( textAttrs, aReader, line, &line );
1738 
1739  // The name of the field is optional.
1740  parseQuotedString( name, aReader, line, &line, true );
1741 
1742  if( hjustify == 'L' )
1744  else if( hjustify == 'R' )
1746  else if( hjustify != 'C' )
1747  SCH_PARSE_ERROR( "symbol field text horizontal justification must be "
1748  "L, R, or C", aReader, line );
1749 
1750  // We are guaranteed to have a least one character here for older file formats
1751  // otherwise an exception would have been raised..
1752  if( textAttrs[0] == 'T' )
1754  else if( textAttrs[0] == 'B' )
1756  else if( textAttrs[0] != 'C' )
1757  SCH_PARSE_ERROR( "symbol field text vertical justification must be "
1758  "B, T, or C", aReader, line );
1759 
1760  // Newer file formats include the bold and italics text attribute.
1761  if( textAttrs.Length() > 1 )
1762  {
1763  if( textAttrs.Length() != 3 )
1764  SCH_PARSE_ERROR( _( "symbol field text attributes must be 3 characters wide" ),
1765  aReader, line );
1766 
1767  if( textAttrs[1] == 'I' )
1768  field.SetItalic( true );
1769  else if( textAttrs[1] != 'N' )
1770  SCH_PARSE_ERROR( "symbol field text italics indicator must be I or N",
1771  aReader, line );
1772 
1773  if( textAttrs[2] == 'B' )
1774  field.SetBold( true );
1775  else if( textAttrs[2] != 'N' )
1776  SCH_PARSE_ERROR( "symbol field text bold indicator must be B or N",
1777  aReader, line );
1778  }
1779  }
1780 
1781  field.SetText( text );
1782  field.SetTextPos( pos );
1783  field.SetVisible( !attributes );
1784  field.SetTextSize( wxSize( size, size ) );
1785 
1786  if( orientation == 'H' )
1787  field.SetTextAngle( TEXT_ANGLE_HORIZ );
1788  else if( orientation == 'V' )
1789  field.SetTextAngle( TEXT_ANGLE_VERT );
1790  else
1791  SCH_PARSE_ERROR( "symbol field orientation must be H or V", aReader, line );
1792 
1793  if( name.IsEmpty() )
1795 
1796  field.SetName( name );
1797  }
1798  else if( strCompare( "$EndComp", line ) )
1799  {
1800  // Ensure all flags (some are set by previous initializations) are reset:
1801  symbol->ClearFlags();
1802  return symbol.release();
1803  }
1804  else
1805  {
1806  // There are two lines that begin with a tab or spaces that includes a line with the
1807  // redundant position information and the transform matrix settings.
1808 
1809  // Parse the redundant position information just the same to check for formatting
1810  // errors.
1811  parseInt( aReader, line, &line ); // Always 1.
1812  parseInt( aReader, line, &line ); // The X coordinate.
1813  parseInt( aReader, line, &line ); // The Y coordinate.
1814 
1815  line = aReader.ReadLine();
1816 
1817  TRANSFORM transform;
1818 
1819  transform.x1 = parseInt( aReader, line, &line );
1820 
1821  if( transform.x1 < -1 || transform.x1 > 1 )
1822  SCH_PARSE_ERROR( "invalid symbol X1 transform value", aReader, line );
1823 
1824  transform.y1 = parseInt( aReader, line, &line );
1825 
1826  if( transform.y1 < -1 || transform.y1 > 1 )
1827  SCH_PARSE_ERROR( "invalid symbol Y1 transform value", aReader, line );
1828 
1829  transform.x2 = parseInt( aReader, line, &line );
1830 
1831  if( transform.x2 < -1 || transform.x2 > 1 )
1832  SCH_PARSE_ERROR( "invalid symbol X2 transform value", aReader, line );
1833 
1834  transform.y2 = parseInt( aReader, line, &line );
1835 
1836  if( transform.y2 < -1 || transform.y2 > 1 )
1837  SCH_PARSE_ERROR( "invalid symbol Y2 transform value", aReader, line );
1838 
1839  symbol->SetTransform( transform );
1840  }
1841 
1842  line = aReader.ReadLine();
1843  }
1844 
1845  SCH_PARSE_ERROR( "invalid symbol line", aReader, line );
1846 
1847  return nullptr; // Prevents compiler warning. Should never get here.
1848 }
1849 
1850 
1851 std::shared_ptr<BUS_ALIAS> SCH_LEGACY_PLUGIN::loadBusAlias( LINE_READER& aReader,
1852  SCH_SCREEN* aScreen )
1853 {
1854  auto busAlias = std::make_shared<BUS_ALIAS>( aScreen );
1855  const char* line = aReader.Line();
1856 
1857  wxCHECK( strCompare( "BusAlias", line, &line ), nullptr );
1858 
1859  wxString buf;
1860  parseUnquotedString( buf, aReader, line, &line );
1861  busAlias->SetName( buf );
1862 
1863  while( *line != '\0' )
1864  {
1865  buf.clear();
1866  parseUnquotedString( buf, aReader, line, &line, true );
1867  if( buf.Len() > 0 )
1868  {
1869  busAlias->AddMember( buf );
1870  }
1871  }
1872 
1873  return busAlias;
1874 }
1875 
1876 
1877 void SCH_LEGACY_PLUGIN::Save( const wxString& aFileName, SCH_SHEET* aSheet, SCHEMATIC* aSchematic,
1878  const PROPERTIES* aProperties )
1879 {
1880  wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
1881  wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
1882 
1883  LOCALE_IO toggle; // toggles on, then off, the C locale, to write floating point values.
1884 
1885  init( aSchematic, aProperties );
1886 
1887  wxFileName fn = aFileName;
1888 
1889  // File names should be absolute. Don't assume everything relative to the project path
1890  // works properly.
1891  wxASSERT( fn.IsAbsolute() );
1892 
1893  FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
1894 
1895  m_out = &formatter; // no ownership
1896 
1897  Format( aSheet );
1898 }
1899 
1900 
1902 {
1903  wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
1904  wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
1905 
1906  SCH_SCREEN* screen = aSheet->GetScreen();
1907 
1908  wxCHECK( screen, /* void */ );
1909 
1910  // Write the header
1911  m_out->Print( 0, "%s %s %d\n", "EESchema", SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION );
1912 
1913  // This section is not used, but written for file compatibility
1914  m_out->Print( 0, "EELAYER %d %d\n", SCH_LAYER_ID_COUNT, 0 );
1915  m_out->Print( 0, "EELAYER END\n" );
1916 
1917  /* Write page info, ScreenNumber and NumberOfScreen; not very meaningful for
1918  * SheetNumber and Sheet Count in a complex hierarchy, but useful in
1919  * simple hierarchy and flat hierarchy. Used also to search the root
1920  * sheet ( ScreenNumber = 1 ) within the files
1921  */
1922  const TITLE_BLOCK& tb = screen->GetTitleBlock();
1923  const PAGE_INFO& page = screen->GetPageSettings();
1924 
1925  m_out->Print( 0, "$Descr %s %d %d%s\n", TO_UTF8( page.GetType() ),
1926  page.GetWidthMils(),
1927  page.GetHeightMils(),
1928  !page.IsCustom() && page.IsPortrait() ? " portrait" : "" );
1929  m_out->Print( 0, "encoding utf-8\n" );
1930  m_out->Print( 0, "Sheet %d %d\n", screen->GetVirtualPageNumber(), screen->GetPageCount() );
1931  m_out->Print( 0, "Title %s\n", EscapedUTF8( tb.GetTitle() ).c_str() );
1932  m_out->Print( 0, "Date %s\n", EscapedUTF8( tb.GetDate() ).c_str() );
1933  m_out->Print( 0, "Rev %s\n", EscapedUTF8( tb.GetRevision() ).c_str() );
1934  m_out->Print( 0, "Comp %s\n", EscapedUTF8( tb.GetCompany() ).c_str() );
1935  m_out->Print( 0, "Comment1 %s\n", EscapedUTF8( tb.GetComment( 0 ) ).c_str() );
1936  m_out->Print( 0, "Comment2 %s\n", EscapedUTF8( tb.GetComment( 1 ) ).c_str() );
1937  m_out->Print( 0, "Comment3 %s\n", EscapedUTF8( tb.GetComment( 2 ) ).c_str() );
1938  m_out->Print( 0, "Comment4 %s\n", EscapedUTF8( tb.GetComment( 3 ) ).c_str() );
1939  m_out->Print( 0, "Comment5 %s\n", EscapedUTF8( tb.GetComment( 4 ) ).c_str() );
1940  m_out->Print( 0, "Comment6 %s\n", EscapedUTF8( tb.GetComment( 5 ) ).c_str() );
1941  m_out->Print( 0, "Comment7 %s\n", EscapedUTF8( tb.GetComment( 6 ) ).c_str() );
1942  m_out->Print( 0, "Comment8 %s\n", EscapedUTF8( tb.GetComment( 7 ) ).c_str() );
1943  m_out->Print( 0, "Comment9 %s\n", EscapedUTF8( tb.GetComment( 8 ) ).c_str() );
1944  m_out->Print( 0, "$EndDescr\n" );
1945 
1946  for( const auto& alias : screen->GetBusAliases() )
1947  {
1948  saveBusAlias( alias );
1949  }
1950 
1951  // Enforce item ordering
1952  auto cmp = []( const SCH_ITEM* a, const SCH_ITEM* b ) { return *a < *b; };
1953  std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
1954 
1955  for( auto item : screen->Items() )
1956  save_map.insert( item );
1957 
1958 
1959  for( auto& item : save_map )
1960  {
1961  switch( item->Type() )
1962  {
1963  case SCH_SYMBOL_T:
1964  saveSymbol( static_cast<SCH_SYMBOL*>( item ) );
1965  break;
1966  case SCH_BITMAP_T:
1967  saveBitmap( static_cast<SCH_BITMAP*>( item ) );
1968  break;
1969  case SCH_SHEET_T:
1970  saveSheet( static_cast<SCH_SHEET*>( item ) );
1971  break;
1972  case SCH_JUNCTION_T:
1973  saveJunction( static_cast<SCH_JUNCTION*>( item ) );
1974  break;
1975  case SCH_NO_CONNECT_T:
1976  saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ) );
1977  break;
1978  case SCH_BUS_WIRE_ENTRY_T:
1979  case SCH_BUS_BUS_ENTRY_T:
1980  saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ) );
1981  break;
1982  case SCH_LINE_T:
1983  saveLine( static_cast<SCH_LINE*>( item ) );
1984  break;
1985  case SCH_TEXT_T:
1986  case SCH_LABEL_T:
1987  case SCH_GLOBAL_LABEL_T:
1988  case SCH_HIER_LABEL_T:
1989  saveText( static_cast<SCH_TEXT*>( item ) );
1990  break;
1991  default:
1992  wxASSERT( "Unexpected schematic object type in SCH_LEGACY_PLUGIN::Format()" );
1993  }
1994  }
1995 
1996  m_out->Print( 0, "$EndSCHEMATC\n" );
1997 }
1998 
1999 
2000 void SCH_LEGACY_PLUGIN::Format( SELECTION* aSelection, OUTPUTFORMATTER* aFormatter )
2001 {
2002  m_out = aFormatter;
2003 
2004  for( unsigned i = 0; i < aSelection->GetSize(); ++i )
2005  {
2006  SCH_ITEM* item = (SCH_ITEM*) aSelection->GetItem( i );
2007 
2008  switch( item->Type() )
2009  {
2010  case SCH_SYMBOL_T:
2011  saveSymbol( static_cast< SCH_SYMBOL* >( item ) );
2012  break;
2013  case SCH_BITMAP_T:
2014  saveBitmap( static_cast< SCH_BITMAP* >( item ) );
2015  break;
2016  case SCH_SHEET_T:
2017  saveSheet( static_cast< SCH_SHEET* >( item ) );
2018  break;
2019  case SCH_JUNCTION_T:
2020  saveJunction( static_cast< SCH_JUNCTION* >( item ) );
2021  break;
2022  case SCH_NO_CONNECT_T:
2023  saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ) );
2024  break;
2025  case SCH_BUS_WIRE_ENTRY_T:
2026  case SCH_BUS_BUS_ENTRY_T:
2027  saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ) );
2028  break;
2029  case SCH_LINE_T:
2030  saveLine( static_cast< SCH_LINE* >( item ) );
2031  break;
2032  case SCH_TEXT_T:
2033  case SCH_LABEL_T:
2034  case SCH_GLOBAL_LABEL_T:
2035  case SCH_HIER_LABEL_T:
2036  saveText( static_cast< SCH_TEXT* >( item ) );
2037  break;
2038  default:
2039  wxASSERT( "Unexpected schematic object type in SCH_LEGACY_PLUGIN::Format()" );
2040  }
2041  }
2042 }
2043 
2044 
2046 {
2047  std::string name1;
2048  std::string name2;
2049 
2050  static wxString delimiters( wxT( " " ) );
2051 
2052  // This is redundant with the AR entries below, but it makes the files backwards-compatible.
2053  if( aSymbol->GetInstanceReferences().size() > 0 )
2054  {
2055  const SYMBOL_INSTANCE_REFERENCE& instance = aSymbol->GetInstanceReferences()[0];
2056  name1 = toUTFTildaText( instance.m_Reference );
2057  }
2058  else
2059  {
2060  if( aSymbol->GetField( REFERENCE_FIELD )->GetText().IsEmpty() )
2061  name1 = toUTFTildaText( aSymbol->GetPrefix() );
2062  else
2063  name1 = toUTFTildaText( aSymbol->GetField( REFERENCE_FIELD )->GetText() );
2064  }
2065 
2066  wxString symbol_name = aSymbol->GetLibId().Format();
2067 
2068  if( symbol_name.size() )
2069  {
2070  name2 = toUTFTildaText( symbol_name );
2071  }
2072  else
2073  {
2074  name2 = "_NONAME_";
2075  }
2076 
2077  m_out->Print( 0, "$Comp\n" );
2078  m_out->Print( 0, "L %s %s\n", name2.c_str(), name1.c_str() );
2079 
2080  // Generate unit number, conversion and timestamp
2081  m_out->Print( 0, "U %d %d %8.8X\n",
2082  aSymbol->GetUnit(),
2083  aSymbol->GetConvert(),
2084  aSymbol->m_Uuid.AsLegacyTimestamp() );
2085 
2086  // Save the position
2087  m_out->Print( 0, "P %d %d\n",
2088  Iu2Mils( aSymbol->GetPosition().x ),
2089  Iu2Mils( aSymbol->GetPosition().y ) );
2090 
2091  /* If this is a complex hierarchy; save hierarchical references.
2092  * but for simple hierarchies it is not necessary.
2093  * the reference inf is already saved
2094  * this is useful for old Eeschema version compatibility
2095  */
2096  if( aSymbol->GetInstanceReferences().size() > 1 )
2097  {
2098  for( const SYMBOL_INSTANCE_REFERENCE& instance : aSymbol->GetInstanceReferences() )
2099  {
2100  /*format:
2101  * AR Path="/140/2" Ref="C99" Part="1"
2102  * where 140 is the uid of the containing sheet and 2 is the timestamp of this symbol.
2103  * (timestamps are actually 8 hex chars)
2104  * Ref is the conventional symbol reference designator for this 'path'
2105  * Part is the conventional symbol unit selection for this 'path'
2106  */
2107  wxString path = "/";
2108 
2109  // Skip root sheet
2110  for( int i = 1; i < (int) instance.m_Path.size(); ++i )
2111  path += instance.m_Path[i].AsLegacyTimestampString() + "/";
2112 
2113  m_out->Print( 0, "AR Path=\"%s\" Ref=\"%s\" Part=\"%d\" \n",
2114  TO_UTF8( path + aSymbol->m_Uuid.AsLegacyTimestampString() ),
2115  TO_UTF8( instance.m_Reference ),
2116  instance.m_Unit );
2117  }
2118  }
2119 
2120  // update the ugly field id, which I would like to see go away someday soon.
2121  for( int i = 0; i < aSymbol->GetFieldCount(); ++i )
2122  aSymbol->GetFields()[i].SetId( i );
2123 
2124  // Fixed fields:
2125  // Save mandatory fields even if they are blank,
2126  // because the visibility, size and orientation are set from library editor.
2127  for( unsigned i = 0; i < MANDATORY_FIELDS; ++i )
2128  saveField( &aSymbol->GetFields()[i] );
2129 
2130  // User defined fields:
2131  // The *policy* about which user defined fields are symbol of a symbol is now
2132  // only in the dialog editors. No policy should be enforced here, simply
2133  // save all the user defined fields, they are present because a dialog editor
2134  // thought they should be. If you disagree, go fix the dialog editors.
2135  for( int i = MANDATORY_FIELDS; i < aSymbol->GetFieldCount(); ++i )
2136  saveField( &aSymbol->GetFields()[i] );
2137 
2138  // Unit number, position, box ( old standard )
2139  m_out->Print( 0, "\t%-4d %-4d %-4d\n", aSymbol->GetUnit(),
2140  Iu2Mils( aSymbol->GetPosition().x ),
2141  Iu2Mils( aSymbol->GetPosition().y ) );
2142 
2143  TRANSFORM transform = aSymbol->GetTransform();
2144 
2145  m_out->Print( 0, "\t%-4d %-4d %-4d %-4d\n",
2146  transform.x1, transform.y1, transform.x2, transform.y2 );
2147  m_out->Print( 0, "$EndComp\n" );
2148 }
2149 
2150 
2152 {
2153  char hjustify = 'C';
2154 
2155  if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
2156  hjustify = 'L';
2157  else if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
2158  hjustify = 'R';
2159 
2160  char vjustify = 'C';
2161 
2162  if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
2163  vjustify = 'B';
2164  else if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
2165  vjustify = 'T';
2166 
2167  m_out->Print( 0, "F %d %s %c %-3d %-3d %-3d %4.4X %c %c%c%c",
2168  aField->GetId(),
2169  EscapedUTF8( aField->GetText() ).c_str(), // wraps in quotes too
2170  aField->GetTextAngle() == TEXT_ANGLE_HORIZ ? 'H' : 'V',
2171  Iu2Mils( aField->GetLibPosition().x ),
2172  Iu2Mils( aField->GetLibPosition().y ),
2173  Iu2Mils( aField->GetTextWidth() ),
2174  !aField->IsVisible(),
2175  hjustify, vjustify,
2176  aField->IsItalic() ? 'I' : 'N',
2177  aField->IsBold() ? 'B' : 'N' );
2178 
2179  // Save field name, if the name is user definable
2180  if( aField->GetId() >= MANDATORY_FIELDS )
2181  m_out->Print( 0, " %s", EscapedUTF8( aField->GetName() ).c_str() );
2182 
2183  m_out->Print( 0, "\n" );
2184 }
2185 
2186 
2188 {
2189  wxCHECK_RET( aBitmap != nullptr, "SCH_BITMAP* is NULL" );
2190 
2191  const wxImage* image = aBitmap->GetImage()->GetImageData();
2192 
2193  wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
2194 
2195  m_out->Print( 0, "$Bitmap\n" );
2196  m_out->Print( 0, "Pos %-4d %-4d\n",
2197  Iu2Mils( aBitmap->GetPosition().x ),
2198  Iu2Mils( aBitmap->GetPosition().y ) );
2199  m_out->Print( 0, "Scale %f\n", aBitmap->GetImage()->GetScale() );
2200  m_out->Print( 0, "Data\n" );
2201 
2202  wxMemoryOutputStream stream;
2203 
2204  image->SaveFile( stream, wxBITMAP_TYPE_PNG );
2205 
2206  // Write binary data in hexadecimal form (ASCII)
2207  wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
2208  char* begin = (char*) buffer->GetBufferStart();
2209 
2210  for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
2211  {
2212  if( ii >= 32 )
2213  {
2214  ii = 0;
2215 
2216  m_out->Print( 0, "\n" );
2217  }
2218 
2219  m_out->Print( 0, "%2.2X ", *begin & 0xFF );
2220  }
2221 
2222  m_out->Print( 0, "\nEndData\n" );
2223  m_out->Print( 0, "$EndBitmap\n" );
2224 }
2225 
2226 
2228 {
2229  wxCHECK_RET( aSheet != nullptr, "SCH_SHEET* is NULL" );
2230 
2231  m_out->Print( 0, "$Sheet\n" );
2232  m_out->Print( 0, "S %-4d %-4d %-4d %-4d\n",
2233  Iu2Mils( aSheet->GetPosition().x ),
2234  Iu2Mils( aSheet->GetPosition().y ),
2235  Iu2Mils( aSheet->GetSize().x ),
2236  Iu2Mils( aSheet->GetSize().y ) );
2237 
2238  m_out->Print( 0, "U %8.8X\n", aSheet->m_Uuid.AsLegacyTimestamp() );
2239 
2240  SCH_FIELD& sheetName = aSheet->GetFields()[SHEETNAME];
2241  SCH_FIELD& fileName = aSheet->GetFields()[SHEETFILENAME];
2242 
2243  if( !sheetName.GetText().IsEmpty() )
2244  m_out->Print( 0, "F0 %s %d\n",
2245  EscapedUTF8( sheetName.GetText() ).c_str(),
2246  Iu2Mils( sheetName.GetTextSize().x ) );
2247 
2248  if( !fileName.GetText().IsEmpty() )
2249  m_out->Print( 0, "F1 %s %d\n",
2250  EscapedUTF8( fileName.GetText() ).c_str(),
2251  Iu2Mils( fileName.GetTextSize().x ) );
2252 
2253  for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
2254  {
2255  int type, side;
2256 
2257  if( pin->GetText().IsEmpty() )
2258  break;
2259 
2260  switch( pin->GetEdge() )
2261  {
2262  default:
2263  case SHEET_SIDE::LEFT: side = 'L'; break;
2264  case SHEET_SIDE::RIGHT: side = 'R'; break;
2265  case SHEET_SIDE::TOP: side = 'T'; break;
2266  case SHEET_SIDE::BOTTOM: side = 'B'; break;
2267  }
2268 
2269  switch( pin->GetShape() )
2270  {
2271  default:
2272  case PINSHEETLABEL_SHAPE::PS_UNSPECIFIED: type = 'U'; break;
2273  case PINSHEETLABEL_SHAPE::PS_INPUT: type = 'I'; break;
2274  case PINSHEETLABEL_SHAPE::PS_OUTPUT: type = 'O'; break;
2275  case PINSHEETLABEL_SHAPE::PS_BIDI: type = 'B'; break;
2276  case PINSHEETLABEL_SHAPE::PS_TRISTATE: type = 'T'; break;
2277  }
2278 
2279  m_out->Print( 0, "F%d %s %c %c %-3d %-3d %-3d\n",
2280  pin->GetNumber(),
2281  EscapedUTF8( pin->GetText() ).c_str(), // supplies wrapping quotes
2282  type, side, Iu2Mils( pin->GetPosition().x ),
2283  Iu2Mils( pin->GetPosition().y ),
2284  Iu2Mils( pin->GetTextWidth() ) );
2285  }
2286 
2287  m_out->Print( 0, "$EndSheet\n" );
2288 }
2289 
2290 
2292 {
2293  wxCHECK_RET( aJunction != nullptr, "SCH_JUNCTION* is NULL" );
2294 
2295  m_out->Print( 0, "Connection ~ %-4d %-4d\n",
2296  Iu2Mils( aJunction->GetPosition().x ),
2297  Iu2Mils( aJunction->GetPosition().y ) );
2298 }
2299 
2300 
2302 {
2303  wxCHECK_RET( aNoConnect != nullptr, "SCH_NOCONNECT* is NULL" );
2304 
2305  m_out->Print( 0, "NoConn ~ %-4d %-4d\n",
2306  Iu2Mils( aNoConnect->GetPosition().x ),
2307  Iu2Mils( aNoConnect->GetPosition().y ) );
2308 }
2309 
2310 
2312 {
2313  wxCHECK_RET( aBusEntry != nullptr, "SCH_BUS_ENTRY_BASE* is NULL" );
2314 
2315  if( aBusEntry->GetLayer() == LAYER_WIRE )
2316  m_out->Print( 0, "Entry Wire Line\n\t%-4d %-4d %-4d %-4d\n",
2317  Iu2Mils( aBusEntry->GetPosition().x ),
2318  Iu2Mils( aBusEntry->GetPosition().y ),
2319  Iu2Mils( aBusEntry->GetEnd().x ), Iu2Mils( aBusEntry->GetEnd().y ) );
2320  else
2321  m_out->Print( 0, "Entry Bus Bus\n\t%-4d %-4d %-4d %-4d\n",
2322  Iu2Mils( aBusEntry->GetPosition().x ),
2323  Iu2Mils( aBusEntry->GetPosition().y ),
2324  Iu2Mils( aBusEntry->GetEnd().x ), Iu2Mils( aBusEntry->GetEnd().y ) );
2325 }
2326 
2327 
2329 {
2330  wxCHECK_RET( aLine != nullptr, "SCH_LINE* is NULL" );
2331 
2332  const char* layer = "Notes";
2333  const char* width = "Line";
2334 
2335  if( aLine->GetLayer() == LAYER_WIRE )
2336  layer = "Wire";
2337  else if( aLine->GetLayer() == LAYER_BUS )
2338  layer = "Bus";
2339 
2340  m_out->Print( 0, "Wire %s %s", layer, width );
2341 
2342  // Write line style (width, type, color) only for non default values
2343  if( aLine->IsGraphicLine() )
2344  {
2345  if( aLine->GetLineSize() != 0 )
2346  m_out->Print( 0, " %s %d", T_WIDTH, Iu2Mils( aLine->GetLineSize() ) );
2347 
2348  if( aLine->GetLineStyle() != aLine->GetDefaultStyle() )
2349  m_out->Print( 0, " %s %s", T_STYLE,
2351 
2352  if( aLine->GetLineColor() != COLOR4D::UNSPECIFIED )
2353  m_out->Print( 0, " %s",
2354  TO_UTF8( aLine->GetLineColor().ToColour().GetAsString( wxC2S_CSS_SYNTAX ) ) );
2355  }
2356 
2357  m_out->Print( 0, "\n" );
2358 
2359  m_out->Print( 0, "\t%-4d %-4d %-4d %-4d",
2360  Iu2Mils( aLine->GetStartPoint().x ), Iu2Mils( aLine->GetStartPoint().y ),
2361  Iu2Mils( aLine->GetEndPoint().x ), Iu2Mils( aLine->GetEndPoint().y ) );
2362 
2363  m_out->Print( 0, "\n");
2364 }
2365 
2366 
2368 {
2369  wxCHECK_RET( aText != nullptr, "SCH_TEXT* is NULL" );
2370 
2371  const char* italics = "~";
2372  const char* textType = "Notes";
2373 
2374  if( aText->IsItalic() )
2375  italics = "Italic";
2376 
2377  wxString text = aText->GetText();
2378 
2379  SCH_LAYER_ID layer = aText->GetLayer();
2380 
2381  if( layer == LAYER_NOTES || layer == LAYER_LOCLABEL )
2382  {
2383  if( layer == LAYER_NOTES )
2384  {
2385  // For compatibility reasons, the text must be saved in only one text line
2386  // so replace all EOLs with \\n
2387  text.Replace( wxT( "\n" ), wxT( "\\n" ) );
2388 
2389  // Here we should have no CR or LF character in line
2390  // This is not always the case if a multiline text was copied (using a copy/paste
2391  // function) from a text that uses E.O.L characters that differs from the current
2392  // EOL format. This is mainly the case under Linux using LF symbol when copying
2393  // a text from Windows (using CRLF symbol) so we must just remove the extra CR left
2394  // (or LF left under MacOSX)
2395  for( unsigned ii = 0; ii < text.Len(); )
2396  {
2397  if( text[ii] == 0x0A || text[ii] == 0x0D )
2398  text.erase( ii, 1 );
2399  else
2400  ii++;
2401  }
2402  }
2403  else
2404  {
2405  textType = "Label";
2406  }
2407 
2408  // Local labels must have their spin style inverted for left and right
2409  int spinStyle = static_cast<int>( aText->GetLabelSpinStyle() );
2410 
2411  if( spinStyle == 0 )
2412  spinStyle = 2;
2413  else if( spinStyle == 2 )
2414  spinStyle = 0;
2415 
2416  m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %d\n%s\n", textType,
2417  Iu2Mils( aText->GetPosition().x ), Iu2Mils( aText->GetPosition().y ),
2418  spinStyle,
2419  Iu2Mils( aText->GetTextWidth() ),
2420  italics, Iu2Mils( aText->GetTextThickness() ), TO_UTF8( text ) );
2421  }
2422  else if( layer == LAYER_GLOBLABEL || layer == LAYER_HIERLABEL )
2423  {
2424  textType = ( layer == LAYER_GLOBLABEL ) ? "GLabel" : "HLabel";
2425 
2426  auto shapeLabelIt = sheetLabelNames.find( aText->GetShape() );
2427  wxCHECK_RET( shapeLabelIt != sheetLabelNames.end(), "Shape not found in names list" );
2428 
2429  m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %s %d\n%s\n", textType,
2430  Iu2Mils( aText->GetPosition().x ), Iu2Mils( aText->GetPosition().y ),
2431  static_cast<int>( aText->GetLabelSpinStyle() ),
2432  Iu2Mils( aText->GetTextWidth() ),
2433  shapeLabelIt->second,
2434  italics,
2435  Iu2Mils( aText->GetTextThickness() ), TO_UTF8( text ) );
2436  }
2437 }
2438 
2439 
2440 void SCH_LEGACY_PLUGIN::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias )
2441 {
2442  wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
2443 
2444  wxString members = boost::algorithm::join( aAlias->Members(), " " );
2445 
2446  m_out->Print( 0, "BusAlias %s %s\n",
2447  TO_UTF8( aAlias->GetName() ), TO_UTF8( members ) );
2448 }
2449 
2450 
2451 int SCH_LEGACY_PLUGIN_CACHE::s_modHash = 1; // starts at 1 and goes up
2453 
2454 
2455 SCH_LEGACY_PLUGIN_CACHE::SCH_LEGACY_PLUGIN_CACHE( const wxString& aFullPathAndFileName ) :
2456  m_fileName( aFullPathAndFileName ),
2457  m_libFileName( aFullPathAndFileName ),
2458  m_isWritable( true ),
2459  m_isModified( false )
2460 {
2461  m_versionMajor = -1;
2462  m_versionMinor = -1;
2464 }
2465 
2466 
2468 {
2469  // When the cache is destroyed, all of the alias objects on the heap should be deleted.
2470  for( auto& symbol : m_symbols )
2471  delete symbol.second;
2472 
2473  m_symbols.clear();
2474 }
2475 
2476 
2477 // If m_libFileName is a symlink follow it to the real source file
2479 {
2480  wxFileName fn( m_libFileName );
2482  return fn;
2483 }
2484 
2485 
2487 {
2488  wxFileName fn = GetRealFile();
2489 
2490  // update the writable flag while we have a wxFileName, in a network this
2491  // is possibly quite dynamic anyway.
2492  m_isWritable = fn.IsFileWritable();
2493 
2494  return fn.GetModificationTime();
2495 }
2496 
2497 
2498 bool SCH_LEGACY_PLUGIN_CACHE::IsFile( const wxString& aFullPathAndFileName ) const
2499 {
2500  return m_fileName == aFullPathAndFileName;
2501 }
2502 
2503 
2505 {
2506  wxFileName fn = GetRealFile();
2507 
2508  if( m_fileModTime.IsValid() && fn.IsOk() && fn.FileExists() )
2509  return fn.GetModificationTime() != m_fileModTime;
2510 
2511  return false;
2512 }
2513 
2514 
2516 {
2517  wxCHECK_MSG( aSymbol != nullptr, nullptr, "NULL pointer cannot be removed from library." );
2518 
2519  LIB_SYMBOL* firstChild = nullptr;
2520  LIB_SYMBOL_MAP::iterator it = m_symbols.find( aSymbol->GetName() );
2521 
2522  if( it == m_symbols.end() )
2523  return nullptr;
2524 
2525  // If the entry pointer doesn't match the name it is mapped to in the library, we
2526  // have done something terribly wrong.
2527  wxCHECK_MSG( *it->second == aSymbol, nullptr,
2528  "Pointer mismatch while attempting to remove alias entry <" + aSymbol->GetName() +
2529  "> from library cache <" + m_libFileName.GetName() + ">." );
2530 
2531  // If the symbol is a root symbol used by other symbols find the first alias that uses
2532  // the root symbol and make it the new root.
2533  if( aSymbol->IsRoot() )
2534  {
2535  for( auto entry : m_symbols )
2536  {
2537  if( entry.second->IsAlias()
2538  && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
2539  {
2540  firstChild = entry.second;
2541  break;
2542  }
2543  }
2544 
2545  if( firstChild )
2546  {
2547  for( LIB_ITEM& drawItem : aSymbol->GetDrawItems() )
2548  {
2549  if( drawItem.Type() == LIB_FIELD_T )
2550  {
2551  LIB_FIELD& field = static_cast<LIB_FIELD&>( drawItem );
2552 
2553  if( firstChild->FindField( field.GetCanonicalName() ) )
2554  continue;
2555  }
2556 
2557  LIB_ITEM* newItem = (LIB_ITEM*) drawItem.Clone();
2558  drawItem.SetParent( firstChild );
2559  firstChild->AddDrawItem( newItem );
2560  }
2561 
2562  // Reparent the remaining aliases.
2563  for( auto entry : m_symbols )
2564  {
2565  if( entry.second->IsAlias()
2566  && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
2567  entry.second->SetParent( firstChild );
2568  }
2569  }
2570  }
2571 
2572  m_symbols.erase( it );
2573  delete aSymbol;
2574  m_isModified = true;
2576  return firstChild;
2577 }
2578 
2579 
2581 {
2582  // aSymbol is cloned in SYMBOL_LIB::AddSymbol(). The cache takes ownership of aSymbol.
2583  wxString name = aSymbol->GetName();
2584  LIB_SYMBOL_MAP::iterator it = m_symbols.find( name );
2585 
2586  if( it != m_symbols.end() )
2587  {
2588  removeSymbol( it->second );
2589  }
2590 
2591  m_symbols[ name ] = const_cast< LIB_SYMBOL* >( aSymbol );
2592  m_isModified = true;
2594 }
2595 
2596 
2598 {
2599  if( !m_libFileName.FileExists() )
2600  {
2601  THROW_IO_ERROR( wxString::Format( _( "Library file '%s' not found." ),
2602  m_libFileName.GetFullPath() ) );
2603  }
2604 
2605  wxCHECK_RET( m_libFileName.IsAbsolute(),
2606  wxString::Format( "Cannot use relative file paths in legacy plugin to "
2607  "open library '%s'.", m_libFileName.GetFullPath() ) );
2608 
2609  wxLogTrace( traceSchLegacyPlugin, "Loading legacy symbol file '%s'",
2610  m_libFileName.GetFullPath() );
2611 
2612  FILE_LINE_READER reader( m_libFileName.GetFullPath() );
2613 
2614  if( !reader.ReadLine() )
2615  THROW_IO_ERROR( _( "Unexpected end of file." ) );
2616 
2617  const char* line = reader.Line();
2618 
2619  if( !strCompare( "EESchema-LIBRARY Version", line, &line ) )
2620  {
2621  // Old .sym files (which are libraries with only one symbol, used to store and reuse shapes)
2622  // EESchema-LIB Version x.x SYMBOL. They are valid files.
2623  if( !strCompare( "EESchema-LIB Version", line, &line ) )
2624  SCH_PARSE_ERROR( "file is not a valid symbol or symbol library file", reader, line );
2625  }
2626 
2627  m_versionMajor = parseInt( reader, line, &line );
2628 
2629  if( *line != '.' )
2630  SCH_PARSE_ERROR( "invalid file version formatting in header", reader, line );
2631 
2632  line++;
2633 
2634  m_versionMinor = parseInt( reader, line, &line );
2635 
2636  if( m_versionMajor < 1 || m_versionMinor < 0 || m_versionMinor > 99 )
2637  SCH_PARSE_ERROR( "invalid file version in header", reader, line );
2638 
2639  // Check if this is a symbol library which is the same as a symbol library but without
2640  // any alias, documentation, footprint filters, etc.
2641  if( strCompare( "SYMBOL", line, &line ) )
2642  {
2643  // Symbol files add date and time stamp info to the header.
2645 
2647  }
2648  else
2649  {
2651  }
2652 
2653  while( reader.ReadLine() )
2654  {
2655  line = reader.Line();
2656 
2657  if( *line == '#' || isspace( *line ) ) // Skip comments and blank lines.
2658  continue;
2659 
2660  // Headers where only supported in older library file formats.
2661  if( m_libType == SCH_LIB_TYPE::LT_EESCHEMA && strCompare( "$HEADER", line ) )
2662  loadHeader( reader );
2663 
2664  if( strCompare( "DEF", line ) )
2665  {
2666  // Read one DEF/ENDDEF symbol entry from library:
2667  LIB_SYMBOL* symbol = LoadPart( reader, m_versionMajor, m_versionMinor, &m_symbols );
2668 
2669  m_symbols[ symbol->GetName() ] = symbol;
2670  }
2671  }
2672 
2674 
2675  // Remember the file modification time of library file when the
2676  // cache snapshot was made, so that in a networked environment we will
2677  // reload the cache as needed.
2679 
2681  loadDocs();
2682 }
2683 
2684 
2686 {
2687  const char* line;
2688  wxString text;
2689  wxString aliasName;
2690  wxFileName fn = m_libFileName;
2691  LIB_SYMBOL* symbol = nullptr;;
2692 
2693  fn.SetExt( DOC_EXT );
2694 
2695  // Not all libraries will have a document file.
2696  if( !fn.FileExists() )
2697  return;
2698 
2699  if( !fn.IsFileReadable() )
2700  {
2701  THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to read library '%s'." ),
2702  fn.GetFullPath() ) );
2703  }
2704 
2705  FILE_LINE_READER reader( fn.GetFullPath() );
2706 
2707  line = reader.ReadLine();
2708 
2709  if( !line )
2710  THROW_IO_ERROR( _( "symbol document library file is empty" ) );
2711 
2712  if( !strCompare( DOCFILE_IDENT, line, &line ) )
2713  SCH_PARSE_ERROR( "invalid document library file version formatting in header",
2714  reader, line );
2715 
2716  while( reader.ReadLine() )
2717  {
2718  line = reader.Line();
2719 
2720  if( *line == '#' ) // Comment line.
2721  continue;
2722 
2723  if( !strCompare( "$CMP", line, &line ) != 0 )
2724  SCH_PARSE_ERROR( "$CMP command expected", reader, line );
2725 
2726  aliasName = wxString::FromUTF8( line );
2727  aliasName.Trim();
2728  // aliasName = EscapeString( aliasName, CTX_LIBID );
2729 
2730  LIB_SYMBOL_MAP::iterator it = m_symbols.find( aliasName );
2731 
2732  if( it == m_symbols.end() )
2733  wxLogWarning( "Symbol '%s' not found in library:\n\n"
2734  "'%s'\n\nat line %d offset %d", aliasName, fn.GetFullPath(),
2735  reader.LineNumber(), (int) (line - reader.Line() ) );
2736  else
2737  symbol = it->second;
2738 
2739  // Read the current alias associated doc.
2740  // if the alias does not exist, just skip the description
2741  // (Can happen if a .dcm is not synchronized with the corresponding .lib file)
2742  while( reader.ReadLine() )
2743  {
2744  line = reader.Line();
2745 
2746  if( !line )
2747  SCH_PARSE_ERROR( "unexpected end of file", reader, line );
2748 
2749  if( strCompare( "$ENDCMP", line, &line ) )
2750  break;
2751 
2752  text = FROM_UTF8( line + 2 );
2753  // Remove spaces at eol, and eol chars:
2754  text = text.Trim();
2755 
2756  switch( line[0] )
2757  {
2758  case 'D':
2759  if( symbol )
2760  symbol->SetDescription( text );
2761  break;
2762 
2763  case 'K':
2764  if( symbol )
2765  symbol->SetKeyWords( text );
2766  break;
2767 
2768  case 'F':
2769  if( symbol )
2770  symbol->GetFieldById( DATASHEET_FIELD )->SetText( text );
2771  break;
2772 
2773  case 0:
2774  case '\n':
2775  case '\r':
2776  case '#':
2777  // Empty line or commment
2778  break;
2779 
2780  default:
2781  SCH_PARSE_ERROR( "expected token in symbol definition", reader, line );
2782  }
2783  }
2784  }
2785 }
2786 
2787 
2789 {
2790  const char* line = aReader.Line();
2791 
2792  wxASSERT( strCompare( "$HEADER", line, &line ) );
2793 
2794  while( aReader.ReadLine() )
2795  {
2796  line = (char*) aReader;
2797 
2798  // The time stamp saved in old library files is not used or saved in the latest
2799  // library file version.
2800  if( strCompare( "TimeStamp", line, &line ) )
2801  continue;
2802  else if( strCompare( "$ENDHEADER", line, &line ) )
2803  return;
2804  }
2805 
2806  SCH_PARSE_ERROR( "$ENDHEADER not found", aReader, line );
2807 }
2808 
2809 
2811  int aMinorVersion, LIB_SYMBOL_MAP* aMap )
2812 {
2813  const char* line = aReader.Line();
2814 
2815  while( *line == '#' )
2816  aReader.ReadLine();
2817 
2818  if( !strCompare( "DEF", line, &line ) )
2819  SCH_PARSE_ERROR( "invalid symbol definition", aReader, line );
2820 
2821  long num;
2822  size_t pos = 4; // "DEF" plus the first space.
2823  wxString utf8Line = wxString::FromUTF8( line );
2824  wxStringTokenizer tokens( utf8Line, " \r\n\t" );
2825 
2826  if( tokens.CountTokens() < 8 )
2827  SCH_PARSE_ERROR( "invalid symbol definition", aReader, line );
2828 
2829  // Read DEF line:
2830  std::unique_ptr<LIB_SYMBOL> symbol = std::make_unique<LIB_SYMBOL>( wxEmptyString );
2831 
2832  wxString name, prefix, tmp;
2833 
2834  name = tokens.GetNextToken();
2835  pos += name.size() + 1;
2836 
2837  prefix = tokens.GetNextToken();
2838  pos += prefix.size() + 1;
2839 
2840  tmp = tokens.GetNextToken();
2841  pos += tmp.size() + 1; // NumOfPins, unused.
2842 
2843  tmp = tokens.GetNextToken(); // Pin name offset.
2844 
2845  if( !tmp.ToLong( &num ) )
2846  THROW_PARSE_ERROR( "invalid pin offset", aReader.GetSource(), aReader.Line(),
2847  aReader.LineNumber(), pos );
2848 
2849  pos += tmp.size() + 1;
2850  symbol->SetPinNameOffset( Mils2Iu( (int)num ) );
2851 
2852  tmp = tokens.GetNextToken(); // Show pin numbers.
2853 
2854  if( !( tmp == "Y" || tmp == "N") )
2855  THROW_PARSE_ERROR( "expected Y or N", aReader.GetSource(), aReader.Line(),
2856  aReader.LineNumber(), pos );
2857 
2858  pos += tmp.size() + 1;
2859  symbol->SetShowPinNumbers( ( tmp == "N" ) ? false : true );
2860 
2861  tmp = tokens.GetNextToken(); // Show pin names.
2862 
2863  if( !( tmp == "Y" || tmp == "N") )
2864  THROW_PARSE_ERROR( "expected Y or N", aReader.GetSource(), aReader.Line(),
2865  aReader.LineNumber(), pos );
2866 
2867  pos += tmp.size() + 1;
2868  symbol->SetShowPinNames( ( tmp == "N" ) ? false : true );
2869 
2870  tmp = tokens.GetNextToken(); // Number of units.
2871 
2872  if( !tmp.ToLong( &num ) )
2873  THROW_PARSE_ERROR( "invalid unit count", aReader.GetSource(), aReader.Line(),
2874  aReader.LineNumber(), pos );
2875 
2876  pos += tmp.size() + 1;
2877  symbol->SetUnitCount( (int)num );
2878 
2879  // Ensure m_unitCount is >= 1. Could be read as 0 in old libraries.
2880  if( symbol->GetUnitCount() < 1 )
2881  symbol->SetUnitCount( 1 );
2882 
2883  // Copy symbol name and prefix.
2884 
2885  // The root alias is added to the alias list by SetName() which is called by SetText().
2886  if( name.IsEmpty() )
2887  {
2888  symbol->SetName( "~" );
2889  }
2890  else if( name[0] != '~' )
2891  {
2892  symbol->SetName( name );
2893  }
2894  else
2895  {
2896  symbol->SetName( name.Right( name.Length() - 1 ) );
2897  symbol->GetValueField().SetVisible( false );
2898  }
2899 
2900  // Don't set the library alias, this is determined by the symbol library table.
2901  symbol->SetLibId( LIB_ID( wxEmptyString, symbol->GetName() ) );
2902 
2903  LIB_FIELD& reference = symbol->GetReferenceField();
2904 
2905  if( prefix == "~" )
2906  {
2907  reference.Empty();
2908  reference.SetVisible( false );
2909  }
2910  else
2911  {
2912  reference.SetText( prefix );
2913  }
2914 
2915  // In version 2.2 and earlier, this parameter was a '0' which was just a place holder.
2916  // The was no concept of interchangeable multiple unit symbols.
2917  if( LIB_VERSION( aMajorVersion, aMinorVersion ) > 0
2918  && LIB_VERSION( aMajorVersion, aMinorVersion ) <= LIB_VERSION( 2, 2 ) )
2919  {
2920  // Nothing needs to be set since the default setting for symbols with multiple
2921  // units were never interchangeable. Just parse the 0 an move on.
2922  tmp = tokens.GetNextToken();
2923  pos += tmp.size() + 1;
2924  }
2925  else
2926  {
2927  tmp = tokens.GetNextToken();
2928 
2929  if( tmp == "L" )
2930  symbol->LockUnits( true );
2931  else if( tmp == "F" || tmp == "0" )
2932  symbol->LockUnits( false );
2933  else
2934  THROW_PARSE_ERROR( "expected L, F, or 0", aReader.GetSource(), aReader.Line(),
2935  aReader.LineNumber(), pos );
2936 
2937  pos += tmp.size() + 1;
2938  }
2939 
2940  // There is the optional power symbol flag.
2941  if( tokens.HasMoreTokens() )
2942  {
2943  tmp = tokens.GetNextToken();
2944 
2945  if( tmp == "P" )
2946  symbol->SetPower();
2947  else if( tmp == "N" )
2948  symbol->SetNormal();
2949  else
2950  THROW_PARSE_ERROR( "expected P or N", aReader.GetSource(), aReader.Line(),
2951  aReader.LineNumber(), pos );
2952  }
2953 
2954  line = aReader.ReadLine();
2955 
2956  // Read lines until "ENDDEF" is found.
2957  while( line )
2958  {
2959  if( *line == '#' ) // Comment
2960  ;
2961  else if( strCompare( "Ti", line, &line ) ) // Modification date is ignored.
2962  continue;
2963  else if( strCompare( "ALIAS", line, &line ) ) // Aliases
2964  loadAliases( symbol, aReader, aMap );
2965  else if( *line == 'F' ) // Fields
2966  loadField( symbol, aReader );
2967  else if( strCompare( "DRAW", line, &line ) ) // Drawing objects.
2968  loadDrawEntries( symbol, aReader, aMajorVersion, aMinorVersion );
2969  else if( strCompare( "$FPLIST", line, &line ) ) // Footprint filter list
2970  loadFootprintFilters( symbol, aReader );
2971  else if( strCompare( "ENDDEF", line, &line ) ) // End of symbol description
2972  {
2973  return symbol.release();
2974  }
2975 
2976  line = aReader.ReadLine();
2977  }
2978 
2979  SCH_PARSE_ERROR( "missing ENDDEF", aReader, line );
2980 }
2981 
2982 
2983 void SCH_LEGACY_PLUGIN_CACHE::loadAliases( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2984  LINE_READER& aReader,
2985  LIB_SYMBOL_MAP* aMap )
2986 {
2987  wxString newAliasName;
2988  const char* line = aReader.Line();
2989 
2990  wxCHECK_RET( strCompare( "ALIAS", line, &line ), "Invalid ALIAS section" );
2991 
2992  wxString utf8Line = wxString::FromUTF8( line );
2993  wxStringTokenizer tokens( utf8Line, " \r\n\t" );
2994 
2995  // Parse the ALIAS list.
2996  while( tokens.HasMoreTokens() )
2997  {
2998  newAliasName = tokens.GetNextToken();
2999 
3000  if( aMap )
3001  {
3002  LIB_SYMBOL* newSymbol = new LIB_SYMBOL( newAliasName );
3003 
3004  // Inherit the parent mandatory field attributes.
3005  for( int id = 0; id < MANDATORY_FIELDS; ++id )
3006  {
3007  LIB_FIELD* field = newSymbol->GetFieldById( id );
3008 
3009  // the MANDATORY_FIELDS are exactly that in RAM.
3010  wxASSERT( field );
3011 
3012  LIB_FIELD* parentField = aSymbol->GetFieldById( id );
3013 
3014  wxASSERT( parentField );
3015 
3016  *field = *parentField;
3017 
3018  if( id == VALUE_FIELD )
3019  field->SetText( newAliasName );
3020 
3021  field->SetParent( newSymbol );
3022  }
3023 
3024  newSymbol->SetParent( aSymbol.get() );
3025 
3026  // This will prevent duplicate aliases.
3027  (*aMap)[ newSymbol->GetName() ] = newSymbol;
3028  }
3029  }
3030 }
3031 
3032 
3033 void SCH_LEGACY_PLUGIN_CACHE::loadField( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3034  LINE_READER& aReader )
3035 {
3036  const char* line = aReader.Line();
3037 
3038  wxCHECK_RET( *line == 'F', "Invalid field line" );
3039 
3040  wxString text;
3041  int id;
3042 
3043  if( sscanf( line + 1, "%d", &id ) != 1 || id < 0 )
3044  SCH_PARSE_ERROR( "invalid field ID", aReader, line + 1 );
3045 
3046  LIB_FIELD* field;
3047 
3048  if( id >= 0 && id < MANDATORY_FIELDS )
3049  {
3050  field = aSymbol->GetFieldById( id );
3051 
3052  // this will fire only if somebody broke a constructor or editor.
3053  // MANDATORY_FIELDS are always present in ram resident symbols, no
3054  // exceptions, and they always have their names set, even fixed fields.
3055  wxASSERT( field );
3056  }
3057  else
3058  {
3059  field = new LIB_FIELD( aSymbol.get(), id );
3060  aSymbol->AddDrawItem( field, false );
3061  }
3062 
3063  // Skip to the first double quote.
3064  while( *line != '"' && *line != 0 )
3065  line++;
3066 
3067  if( *line == 0 )
3068  SCH_PARSE_ERROR( _( "unexpected end of line" ), aReader, line );
3069 
3070  parseQuotedString( text, aReader, line, &line, true );
3071 
3072  // Doctor the *.lib file field which has a "~" in blank fields. New saves will
3073  // not save like this.
3074  if( text.size() == 1 && text[0] == '~' )
3075  field->SetText( wxEmptyString );
3076  else
3078 
3079  wxPoint pos;
3080 
3081  pos.x = Mils2Iu( parseInt( aReader, line, &line ) );
3082  pos.y = Mils2Iu( parseInt( aReader, line, &line ) );
3083  field->SetPosition( pos );
3084 
3085  wxSize textSize;
3086 
3087  textSize.x = textSize.y = Mils2Iu( parseInt( aReader, line, &line ) );
3088  field->SetTextSize( textSize );
3089 
3090  char textOrient = parseChar( aReader, line, &line );
3091 
3092  if( textOrient == 'H' )
3093  field->SetTextAngle( TEXT_ANGLE_HORIZ );
3094  else if( textOrient == 'V' )
3095  field->SetTextAngle( TEXT_ANGLE_VERT );
3096  else
3097  SCH_PARSE_ERROR( "invalid field text orientation parameter", aReader, line );
3098 
3099  char textVisible = parseChar( aReader, line, &line );
3100 
3101  if( textVisible == 'V' )
3102  field->SetVisible( true );
3103  else if ( textVisible == 'I' )
3104  field->SetVisible( false );
3105  else
3106  SCH_PARSE_ERROR( "invalid field text visibility parameter", aReader, line );
3107 
3108  // It may be technically correct to use the library version to determine if the field text
3109  // attributes are present. If anyone knows if that is valid and what version that would be,
3110  // please change this to test the library version rather than an EOL or the quoted string
3111  // of the field name.
3112  if( *line != 0 && *line != '"' )
3113  {
3114  char textHJustify = parseChar( aReader, line, &line );
3115 
3116  if( textHJustify == 'C' )
3118  else if( textHJustify == 'L' )
3120  else if( textHJustify == 'R' )
3122  else
3123  SCH_PARSE_ERROR( "invalid field text horizontal justification", aReader, line );
3124 
3125  wxString attributes;
3126 
3127  parseUnquotedString( attributes, aReader, line, &line );
3128 
3129  size_t attrSize = attributes.size();
3130 
3131  if( !(attrSize == 3 || attrSize == 1 ) )
3132  SCH_PARSE_ERROR( "invalid field text attributes size", aReader, line );
3133 
3134  switch( (wxChar) attributes[0] )
3135  {
3136  case 'C': field->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER ); break;
3137  case 'B': field->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM ); break;
3138  case 'T': field->SetVertJustify( GR_TEXT_VJUSTIFY_TOP ); break;
3139  default: SCH_PARSE_ERROR( "invalid field text vertical justification", aReader, line );
3140  }
3141 
3142  if( attrSize == 3 )
3143  {
3144  wxChar attr_1 = attributes[1];
3145  wxChar attr_2 = attributes[2];
3146 
3147  if( attr_1 == 'I' ) // Italic
3148  field->SetItalic( true );
3149  else if( attr_1 != 'N' ) // No italics is default, check for error.
3150  SCH_PARSE_ERROR( "invalid field text italic parameter", aReader, line );
3151 
3152  if ( attr_2 == 'B' ) // Bold
3153  field->SetBold( true );
3154  else if( attr_2 != 'N' ) // No bold is default, check for error.
3155  SCH_PARSE_ERROR( "invalid field text bold parameter", aReader, line );
3156  }
3157  }
3158 
3159  // Fields in RAM must always have names.
3160  if( id >= 0 && id < MANDATORY_FIELDS )
3161  {
3162  // Fields in RAM must always have names, because we are trying to get
3163  // less dependent on field ids and more dependent on names.
3164  // Plus assumptions are made in the field editors.
3166 
3167  // Ensure the VALUE field = the symbol name (can be not the case
3168  // with malformed libraries: edited by hand, or converted from other tools)
3169  if( id == VALUE_FIELD )
3170  field->SetText( aSymbol->GetName() );
3171  }
3172  else
3173  {
3174  parseQuotedString( field->m_name, aReader, line, &line, true ); // Optional.
3175  }
3176 }
3177 
3178 
3179 void SCH_LEGACY_PLUGIN_CACHE::loadDrawEntries( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3180  LINE_READER& aReader,
3181  int aMajorVersion,
3182  int aMinorVersion )
3183 {
3184  const char* line = aReader.Line();
3185 
3186  wxCHECK_RET( strCompare( "DRAW", line, &line ), "Invalid DRAW section" );
3187 
3188  line = aReader.ReadLine();
3189 
3190  while( line )
3191  {
3192  if( strCompare( "ENDDRAW", line, &line ) )
3193  {
3194  aSymbol->GetDrawItems().sort();
3195  return;
3196  }
3197 
3198  switch( line[0] )
3199  {
3200  case 'A': // Arc
3201  aSymbol->AddDrawItem( loadArc( aSymbol, aReader ), false );
3202  break;
3203 
3204  case 'C': // Circle
3205  aSymbol->AddDrawItem( loadCircle( aSymbol, aReader ), false );
3206  break;
3207 
3208  case 'T': // Text
3209  aSymbol->AddDrawItem( loadText( aSymbol, aReader, aMajorVersion,
3210  aMinorVersion ), false );
3211  break;
3212 
3213  case 'S': // Square
3214  aSymbol->AddDrawItem( loadRectangle( aSymbol, aReader ), false );
3215  break;
3216 
3217  case 'X': // Pin Description
3218  aSymbol->AddDrawItem( loadPin( aSymbol, aReader ), false );
3219  break;
3220 
3221  case 'P': // Polyline
3222  aSymbol->AddDrawItem( loadPolyLine( aSymbol, aReader ), false );
3223  break;
3224 
3225  case 'B': // Bezier Curves
3226  aSymbol->AddDrawItem( loadBezier( aSymbol, aReader ), false );
3227  break;
3228 
3229  case '#': // Comment
3230  case '\n': // Empty line
3231  case '\r':
3232  case 0:
3233  break;
3234 
3235  default:
3236  SCH_PARSE_ERROR( "undefined DRAW entry", aReader, line );
3237  }
3238 
3239  line = aReader.ReadLine();
3240  }
3241 
3242  SCH_PARSE_ERROR( "File ended prematurely loading symbol draw element.", aReader, line );
3243 }
3244 
3245 
3247  const char** aOutput )
3248 {
3249  switch ( parseChar( aReader, aLine, aOutput ) )
3250  {
3251  case 'F': return FILL_TYPE::FILLED_SHAPE;
3252  case 'f': return FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
3253  case 'N': return FILL_TYPE::NO_FILL;
3254  default: SCH_PARSE_ERROR( "invalid fill type, expected f, F, or N", aReader, aLine );
3255  }
3256 
3257  // This will never be reached but quiets the compiler warnings
3258  return FILL_TYPE::NO_FILL;
3259 }
3260 
3261 
3262 LIB_ARC* SCH_LEGACY_PLUGIN_CACHE::loadArc( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3263  LINE_READER& aReader )
3264 {
3265  const char* line = aReader.Line();
3266 
3267  wxCHECK_MSG( strCompare( "A", line, &line ), nullptr, "Invalid LIB_ARC definition" );
3268 
3269  LIB_ARC* arc = new LIB_ARC( aSymbol.get() );
3270 
3271  wxPoint center;
3272 
3273  center.x = Mils2Iu( parseInt( aReader, line, &line ) );
3274  center.y = Mils2Iu( parseInt( aReader, line, &line ) );
3275 
3276  arc->SetPosition( center );
3277  arc->SetRadius( Mils2Iu( parseInt( aReader, line, &line ) ) );
3278 
3279  int angle1 = parseInt( aReader, line, &line );
3280  int angle2 = parseInt( aReader, line, &line );
3281 
3282  NORMALIZE_ANGLE_POS( angle1 );
3283  NORMALIZE_ANGLE_POS( angle2 );
3284  arc->SetFirstRadiusAngle( angle1 );
3285  arc->SetSecondRadiusAngle( angle2 );
3286 
3287  arc->SetUnit( parseInt( aReader, line, &line ) );
3288  arc->SetConvert( parseInt( aReader, line, &line ) );
3289  arc->SetWidth( Mils2Iu( parseInt( aReader, line, &line ) ) );
3290 
3291  // Old libraries (version <= 2.2) do not have always this FILL MODE param
3292  // when fill mode is no fill (default mode).
3293  if( *line != 0 )
3294  arc->SetFillMode( parseFillMode( aReader, line, &line ) );
3295 
3296  // Actual Coordinates of arc ends are read from file
3297  if( *line != 0 )
3298  {
3299  wxPoint arcStart, arcEnd;
3300 
3301  arcStart.x = Mils2Iu( parseInt( aReader, line, &line ) );
3302  arcStart.y = Mils2Iu( parseInt( aReader, line, &line ) );
3303  arcEnd.x = Mils2Iu( parseInt( aReader, line, &line ) );
3304  arcEnd.y = Mils2Iu( parseInt( aReader, line, &line ) );
3305 
3306  arc->SetStart( arcStart );
3307  arc->SetEnd( arcEnd );
3308  }
3309  else
3310  {
3311  // Actual Coordinates of arc ends are not read from file
3312  // (old library), calculate them
3313  wxPoint arcStart( arc->GetRadius(), 0 );
3314  wxPoint arcEnd( arc->GetRadius(), 0 );
3315 
3316  RotatePoint( &arcStart.x, &arcStart.y, -angle1 );
3317  arcStart += arc->GetPosition();
3318  arc->SetStart( arcStart );
3319  RotatePoint( &arcEnd.x, &arcEnd.y, -angle2 );
3320  arcEnd += arc->GetPosition();
3321  arc->SetEnd( arcEnd );
3322  }
3323 
3324  return arc;
3325 }
3326 
3327 
3328 LIB_CIRCLE* SCH_LEGACY_PLUGIN_CACHE::loadCircle( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3329  LINE_READER& aReader )
3330 {
3331  const char* line = aReader.Line();
3332 
3333  wxCHECK_MSG( strCompare( "C", line, &line ), nullptr, "Invalid LIB_CIRCLE definition" );
3334 
3335  LIB_CIRCLE* circle = new LIB_CIRCLE( aSymbol.get() );
3336 
3337  wxPoint center;
3338 
3339  center.x = Mils2Iu( parseInt( aReader, line, &line ) );
3340  center.y = Mils2Iu( parseInt( aReader, line, &line ) );
3341 
3342  circle->SetPosition( center );
3343  circle->SetRadius( Mils2Iu( parseInt( aReader, line, &line ) ) );
3344  circle->SetUnit( parseInt( aReader, line, &line ) );
3345  circle->SetConvert( parseInt( aReader, line, &line ) );
3346  circle->SetWidth( Mils2Iu( parseInt( aReader, line, &line ) ) );
3347 
3348  if( *line != 0 )
3349  circle->SetFillMode( parseFillMode( aReader, line, &line ) );
3350 
3351  return circle;
3352 }
3353 
3354 
3355 LIB_TEXT* SCH_LEGACY_PLUGIN_CACHE::loadText( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3356  LINE_READER& aReader,
3357  int aMajorVersion,
3358  int aMinorVersion )
3359 {
3360  const char* line = aReader.Line();
3361 
3362  wxCHECK_MSG( strCompare( "T", line, &line ), nullptr, "Invalid LIB_TEXT definition" );
3363 
3364  LIB_TEXT* text = new LIB_TEXT( aSymbol.get() );
3365 
3366  text->SetTextAngle( (double) parseInt( aReader, line, &line ) );
3367 
3368  wxPoint center;
3369 
3370  center.x = Mils2Iu( parseInt( aReader, line, &line ) );
3371  center.y = Mils2Iu( parseInt( aReader, line, &line ) );
3372  text->SetPosition( center );
3373 
3374  wxSize size;
3375 
3376  size.x = size.y = Mils2Iu( parseInt( aReader, line, &line ) );
3377  text->SetTextSize( size );
3378  text->SetVisible( !parseInt( aReader, line, &line ) );
3379  text->SetUnit( parseInt( aReader, line, &line ) );
3380  text->SetConvert( parseInt( aReader, line, &line ) );
3381 
3382  wxString str;
3383 
3384  // If quoted string loading fails, load as not quoted string.
3385  if( *line == '"' )
3386  {
3387  parseQuotedString( str, aReader, line, &line );
3388 
3389  str = ConvertToNewOverbarNotation( str );
3390  }
3391  else
3392  {
3393  parseUnquotedString( str, aReader, line, &line );
3394 
3395  // In old libs, "spaces" are replaced by '~' in unquoted strings:
3396  str.Replace( "~", " " );
3397  }
3398 
3399  if( !str.IsEmpty() )
3400  {
3401  // convert two apostrophes back to double quote
3402  str.Replace( "''", "\"" );
3403  }
3404 
3405  text->SetText( str );
3406 
3407  // Here things are murky and not well defined. At some point it appears the format
3408  // was changed to add text properties. However rather than add the token to the end of
3409  // the text definition, it was added after the string and no mention if the file
3410  // verion was bumped or not so this code make break on very old symbol libraries.
3411  //
3412  // Update: apparently even in the latest version this can be different so added a test
3413  // for end of line before checking for the text properties.
3414  if( LIB_VERSION( aMajorVersion, aMinorVersion ) > 0
3415  && LIB_VERSION( aMajorVersion, aMinorVersion ) > LIB_VERSION( 2, 0 ) && !is_eol( *line ) )
3416  {
3417  if( strCompare( "Italic", line, &line ) )
3418  text->SetItalic( true );
3419  else if( !strCompare( "Normal", line, &line ) )
3420  SCH_PARSE_ERROR( "invalid text stype, expected 'Normal' or 'Italic'", aReader, line );
3421 
3422  if( parseInt( aReader, line, &line ) > 0 )
3423  text->SetBold( true );
3424 
3425  // Some old libaries version > 2.0 do not have these options for text justification:
3426  if( !is_eol( *line ) )
3427  {
3428  switch( parseChar( aReader, line, &line ) )
3429  {
3430  case 'L': text->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT ); break;
3431  case 'C': text->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER ); break;
3432  case 'R': text->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT ); break;
3433  default: SCH_PARSE_ERROR( "invalid horizontal text justication; expected L, C, or R",
3434  aReader, line );
3435  }
3436 
3437  switch( parseChar( aReader, line, &line ) )
3438  {
3439  case 'T': text->SetVertJustify( GR_TEXT_VJUSTIFY_TOP ); break;
3440  case 'C': text->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER ); break;
3441  case 'B': text->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM ); break;
3442  default: SCH_PARSE_ERROR( "invalid vertical text justication; expected T, C, or B",
3443  aReader, line );
3444  }
3445  }
3446  }
3447 
3448  return text;
3449 }
3450 
3451 
3452 LIB_RECTANGLE* SCH_LEGACY_PLUGIN_CACHE::loadRectangle( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3453  LINE_READER& aReader )
3454 {
3455  const char* line = aReader.Line();
3456 
3457  wxCHECK_MSG( strCompare( "S", line, &line ), nullptr, "Invalid LIB_RECTANGLE definition" );
3458 
3459  LIB_RECTANGLE* rectangle = new LIB_RECTANGLE( aSymbol.get() );
3460 
3461  wxPoint pos;
3462 
3463  pos.x = Mils2Iu( parseInt( aReader, line, &line ) );
3464  pos.y = Mils2Iu( parseInt( aReader, line, &line ) );
3465  rectangle->SetPosition( pos );
3466 
3467  wxPoint end;
3468 
3469  end.x = Mils2Iu( parseInt( aReader, line, &line ) );
3470  end.y = Mils2Iu( parseInt( aReader, line, &line ) );
3471  rectangle->SetEnd( end );
3472 
3473  rectangle->SetUnit( parseInt( aReader, line, &line ) );
3474  rectangle->SetConvert( parseInt( aReader, line, &line ) );
3475  rectangle->SetWidth( Mils2Iu( parseInt( aReader, line, &line ) ) );
3476 
3477  if( *line != 0 )
3478  rectangle->SetFillMode( parseFillMode( aReader, line, &line ) );
3479 
3480  return rectangle;
3481 }
3482 
3483 
3484 LIB_PIN* SCH_LEGACY_PLUGIN_CACHE::loadPin( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3485  LINE_READER& aReader )
3486 {
3487  const char* line = aReader.Line();
3488 
3489  wxCHECK_MSG( strCompare( "X", line, &line ), nullptr, "Invalid LIB_PIN definition" );
3490 
3491  wxString name;
3492  wxString number;
3493 
3494  size_t pos = 2; // "X" plus ' ' space character.
3495  wxString tmp;
3496  wxString utf8Line = wxString::FromUTF8( line );
3497  wxStringTokenizer tokens( utf8Line, " \r\n\t" );
3498 
3499  if( tokens.CountTokens() < 11 )
3500  SCH_PARSE_ERROR( "invalid pin definition", aReader, line );
3501 
3502  tmp = tokens.GetNextToken();
3503  name = tmp;
3504  pos += tmp.size() + 1;
3505 
3506  tmp = tokens.GetNextToken();
3507  number = tmp ;
3508  pos += tmp.size() + 1;
3509 
3510  long num;
3511  wxPoint position;
3512 
3513  tmp = tokens.GetNextToken();
3514 
3515  if( !tmp.ToLong( &num ) )
3516  THROW_PARSE_ERROR( "invalid pin X coordinate", aReader.GetSource(), aReader.Line(),
3517  aReader.LineNumber(), pos );
3518 
3519  pos += tmp.size() + 1;
3520  position.x = Mils2Iu( (int) num );
3521 
3522  tmp = tokens.GetNextToken();
3523 
3524  if( !tmp.ToLong( &num ) )
3525  THROW_PARSE_ERROR( "invalid pin Y coordinate", aReader.GetSource(), aReader.Line(),
3526  aReader.LineNumber(), pos );
3527 
3528  pos += tmp.size() + 1;
3529  position.y = Mils2Iu( (int) num );
3530 
3531  tmp = tokens.GetNextToken();
3532 
3533  if( !tmp.ToLong( &num ) )
3534  THROW_PARSE_ERROR( "invalid pin length", aReader.GetSource(), aReader.Line(),
3535  aReader.LineNumber(), pos );
3536 
3537  pos += tmp.size() + 1;
3538  int length = Mils2Iu( (int) num );
3539 
3540 
3541  tmp = tokens.GetNextToken();
3542 
3543  if( tmp.size() > 1 )
3544  THROW_PARSE_ERROR( "invalid pin orientation", aReader.GetSource(), aReader.Line(),
3545  aReader.LineNumber(), pos );
3546 
3547  pos += tmp.size() + 1;
3548  int orientation = tmp[0];
3549 
3550  tmp = tokens.GetNextToken();
3551 
3552  if( !tmp.ToLong( &num ) )
3553  THROW_PARSE_ERROR( "invalid pin number text size", aReader.GetSource(), aReader.Line(),
3554  aReader.LineNumber(), pos );
3555 
3556  pos += tmp.size() + 1;
3557  int numberTextSize = Mils2Iu( (int) num );
3558 
3559  tmp = tokens.GetNextToken();
3560 
3561  if( !tmp.ToLong( &num ) )
3562  THROW_PARSE_ERROR( "invalid pin name text size", aReader.GetSource(), aReader.Line(),
3563  aReader.LineNumber(), pos );
3564 
3565  pos += tmp.size() + 1;
3566  int nameTextSize = Mils2Iu( (int) num );
3567 
3568  tmp = tokens.GetNextToken();
3569 
3570  if( !tmp.ToLong( &num ) )
3571  THROW_PARSE_ERROR( "invalid pin unit", aReader.GetSource(), aReader.Line(),
3572  aReader.LineNumber(), pos );
3573 
3574  pos += tmp.size() + 1;
3575  int unit = (int) num;
3576 
3577  tmp = tokens.GetNextToken();
3578 
3579  if( !tmp.ToLong( &num ) )
3580  THROW_PARSE_ERROR( "invalid pin alternate body type", aReader.GetSource(), aReader.Line(),
3581  aReader.LineNumber(), pos );
3582 
3583  pos += tmp.size() + 1;
3584  int convert = (int) num;
3585 
3586  tmp = tokens.GetNextToken();
3587 
3588  if( tmp.size() != 1 )
3589  THROW_PARSE_ERROR( "invalid pin type", aReader.GetSource(), aReader.Line(),
3590  aReader.LineNumber(), pos );
3591 
3592  pos += tmp.size() + 1;
3593  char type = tmp[0];
3594  ELECTRICAL_PINTYPE pinType;
3595 
3596  switch( type )
3597  {
3598  case 'I': pinType = ELECTRICAL_PINTYPE::PT_INPUT; break;
3599  case 'O': pinType = ELECTRICAL_PINTYPE::PT_OUTPUT; break;
3600  case 'B': pinType = ELECTRICAL_PINTYPE::PT_BIDI; break;
3601  case 'T': pinType = ELECTRICAL_PINTYPE::PT_TRISTATE; break;
3602  case 'P': pinType = ELECTRICAL_PINTYPE::PT_PASSIVE; break;
3603  case 'U': pinType = ELECTRICAL_PINTYPE::PT_UNSPECIFIED; break;
3604  case 'W': pinType = ELECTRICAL_PINTYPE::PT_POWER_IN; break;
3605  case 'w': pinType = ELECTRICAL_PINTYPE::PT_POWER_OUT; break;
3606  case 'C': pinType = ELECTRICAL_PINTYPE::PT_OPENCOLLECTOR; break;
3607  case 'E': pinType = ELECTRICAL_PINTYPE::PT_OPENEMITTER; break;
3608  case 'N': pinType = ELECTRICAL_PINTYPE::PT_NC; break;
3609  default:
3610  THROW_PARSE_ERROR( "unknown pin type", aReader.GetSource(), aReader.Line(),
3611  aReader.LineNumber(), pos );
3612  }
3613 
3614 
3615  LIB_PIN* pin = new LIB_PIN( aSymbol.get(),
3617  ConvertToNewOverbarNotation( number ),
3618  orientation,
3619  pinType,
3620  length,
3621  nameTextSize,
3622  numberTextSize,
3623  convert,
3624  position,
3625  unit );
3626 
3627  // Optional
3628  if( tokens.HasMoreTokens() ) /* Special Symbol defined */
3629  {
3630  tmp = tokens.GetNextToken();
3631 
3632  enum
3633  {
3634  INVERTED = 1 << 0,
3635  CLOCK = 1 << 1,
3636  LOWLEVEL_IN = 1 << 2,
3637  LOWLEVEL_OUT = 1 << 3,
3638  FALLING_EDGE = 1 << 4,
3639  NONLOGIC = 1 << 5
3640  };
3641 
3642  int flags = 0;
3643 
3644  for( int j = tmp.size(); j > 0; )
3645  {
3646  switch( tmp[--j].GetValue() )
3647  {
3648  case '~': break;
3649  case 'N': pin->SetVisible( false ); break;
3650  case 'I': flags |= INVERTED; break;
3651  case 'C': flags |= CLOCK; break;
3652  case 'L': flags |= LOWLEVEL_IN; break;
3653  case 'V': flags |= LOWLEVEL_OUT; break;
3654  case 'F': flags |= FALLING_EDGE; break;
3655  case 'X': flags |= NONLOGIC; break;
3656  default: THROW_PARSE_ERROR( "invalid pin attribut", aReader.GetSource(),
3657  aReader.Line(), aReader.LineNumber(), pos );
3658  }
3659 
3660  pos += 1;
3661  }
3662 
3663  switch( flags )
3664  {
3665  case 0: pin->SetShape( GRAPHIC_PINSHAPE::LINE ); break;
3666  case INVERTED: pin->SetShape( GRAPHIC_PINSHAPE::INVERTED ); break;
3667  case CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::CLOCK ); break;
3668  case INVERTED | CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::INVERTED_CLOCK ); break;
3669  case LOWLEVEL_IN: pin->SetShape( GRAPHIC_PINSHAPE::INPUT_LOW ); break;
3670  case LOWLEVEL_IN | CLOCK: pin->SetShape( GRAPHIC_PINSHAPE::CLOCK_LOW ); break;
3671  case LOWLEVEL_OUT: pin->SetShape( GRAPHIC_PINSHAPE::OUTPUT_LOW ); break;
3672  case FALLING_EDGE: pin->SetShape( GRAPHIC_PINSHAPE::FALLING_EDGE_CLOCK ); break;
3673  case NONLOGIC: pin->SetShape( GRAPHIC_PINSHAPE::NONLOGIC ); break;
3674  default:
3675  SCH_PARSE_ERROR( "pin attributes do not define a valid pin shape", aReader, line );
3676  }
3677  }
3678 
3679  return pin;
3680 }
3681 
3682 
3683 LIB_POLYLINE* SCH_LEGACY_PLUGIN_CACHE::loadPolyLine( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3684  LINE_READER& aReader )
3685 {
3686  const char* line = aReader.Line();
3687 
3688  wxCHECK_MSG( strCompare( "P", line, &line ), nullptr, "Invalid LIB_POLYLINE definition" );
3689 
3690  LIB_POLYLINE* polyLine = new LIB_POLYLINE( aSymbol.get() );
3691 
3692  int points = parseInt( aReader, line, &line );
3693  polyLine->SetUnit( parseInt( aReader, line, &line ) );
3694  polyLine->SetConvert( parseInt( aReader, line, &line ) );
3695  polyLine->SetWidth( Mils2Iu( parseInt( aReader, line, &line ) ) );
3696  polyLine->Reserve( points );
3697 
3698  wxPoint pt;
3699 
3700  for( int i = 0; i < points; i++ )
3701  {
3702  pt.x = Mils2Iu( parseInt( aReader, line, &line ) );
3703  pt.y = Mils2Iu( parseInt( aReader, line, &line ) );
3704  polyLine->AddPoint( pt );
3705  }
3706 
3707  if( *line != 0 )
3708  polyLine->SetFillMode( parseFillMode( aReader, line, &line ) );
3709 
3710  return polyLine;
3711 }
3712 
3713 
3714 LIB_BEZIER* SCH_LEGACY_PLUGIN_CACHE::loadBezier( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3715  LINE_READER& aReader )
3716 {
3717  const char* line = aReader.Line();
3718 
3719  wxCHECK_MSG( strCompare( "B", line, &line ), nullptr, "Invalid LIB_BEZIER definition" );
3720 
3721  LIB_BEZIER* bezier = new LIB_BEZIER( aSymbol.get() );
3722 
3723  int points = parseInt( aReader, line, &line );
3724  bezier->SetUnit( parseInt( aReader, line, &line ) );
3725  bezier->SetConvert( parseInt( aReader, line, &line ) );
3726  bezier->SetWidth( Mils2Iu( parseInt( aReader, line, &line ) ) );
3727 
3728  wxPoint pt;
3729  bezier->Reserve( points );
3730 
3731  for( int i = 0; i < points; i++ )
3732  {
3733  pt.x = Mils2Iu( parseInt( aReader, line, &line ) );
3734  pt.y = Mils2Iu( parseInt( aReader, line, &line ) );
3735  bezier->AddPoint( pt );
3736  }
3737 
3738  if( *line != 0 )
3739  bezier->SetFillMode( parseFillMode( aReader, line, &line ) );
3740 
3741  return bezier;
3742 }
3743 
3744 
3745 void SCH_LEGACY_PLUGIN_CACHE::loadFootprintFilters( std::unique_ptr<LIB_SYMBOL>& aSymbol,
3746  LINE_READER& aReader )
3747 {
3748  const char* line = aReader.Line();
3749 
3750  wxCHECK_RET( strCompare( "$FPLIST", line, &line ), "Invalid footprint filter list" );
3751 
3752  line = aReader.ReadLine();
3753 
3754  wxArrayString footprintFilters;
3755 
3756  while( line )
3757  {
3758  if( strCompare( "$ENDFPLIST", line, &line ) )
3759  {
3760  aSymbol->SetFPFilters( footprintFilters );
3761  return;
3762  }
3763 
3764  wxString footprint;
3765 
3766  parseUnquotedString( footprint, aReader, line, &line );
3767  footprintFilters.Add( footprint );
3768  line = aReader.ReadLine();
3769  }
3770 
3771  SCH_PARSE_ERROR( "File ended prematurely while loading footprint filters.", aReader, line );
3772 }
3773 
3774 
3775 void SCH_LEGACY_PLUGIN_CACHE::Save( bool aSaveDocFile )
3776 {
3777  if( !m_isModified )
3778  return;
3779 
3780  // Write through symlinks, don't replace them
3781  wxFileName fn = GetRealFile();
3782 
3783  auto formatter = std::make_unique<FILE_OUTPUTFORMATTER>( fn.GetFullPath() );
3784  formatter->Print( 0, "%s %d.%d\n", LIBFILE_IDENT, LIB_VERSION_MAJOR, LIB_VERSION_MINOR );
3785  formatter->Print( 0, "#encoding utf-8\n");
3786 
3787  for( LIB_SYMBOL_MAP::iterator it = m_symbols.begin(); it != m_symbols.end(); it++ )
3788  {
3789  if( !it->second->IsRoot() )
3790  continue;
3791 
3792  SaveSymbol( it->second, *formatter.get(), &m_symbols );
3793  }
3794 
3795  formatter->Print( 0, "#\n#End Library\n" );
3796  formatter.reset();
3797 
3798  m_fileModTime = fn.GetModificationTime();
3799  m_isModified = false;
3800 
3801  if( aSaveDocFile )
3802  saveDocFile();
3803 }
3804 
3805 
3807  LIB_SYMBOL_MAP* aMap )
3808 {
3809  wxCHECK_RET( aSymbol && aSymbol->IsRoot(), "Invalid LIB_SYMBOL pointer." );
3810 
3811  // LIB_ALIAS objects are deprecated but we still need to gather up the derived symbols
3812  // and save their names for the old file format.
3813  wxArrayString aliasNames;
3814 
3815  if( aMap )
3816  {
3817  for( auto entry : *aMap )
3818  {
3819  LIB_SYMBOL* symbol = entry.second;
3820 
3821  if( symbol->IsAlias() && symbol->GetParent().lock() == aSymbol->SharedPtr() )
3822  aliasNames.Add( symbol->GetName() );
3823  }
3824  }
3825 
3826  LIB_FIELD& value = aSymbol->GetValueField();
3827 
3828  // First line: it s a comment (symbol name for readers)
3829  aFormatter.Print( 0, "#\n# %s\n#\n", TO_UTF8( value.GetText() ) );
3830 
3831  // Save data
3832  aFormatter.Print( 0, "DEF" );
3833  aFormatter.Print( 0, " %s", TO_UTF8( value.GetText() ) );
3834 
3835  LIB_FIELD& reference = aSymbol->GetReferenceField();
3836 
3837  if( !reference.GetText().IsEmpty() )
3838  {
3839  aFormatter.Print( 0, " %s", TO_UTF8( reference.GetText() ) );
3840  }
3841  else
3842  {
3843  aFormatter.Print( 0, " ~" );
3844  }
3845 
3846  aFormatter.Print( 0, " %d %d %c %c %d %c %c\n",
3847  0, Iu2Mils( aSymbol->GetPinNameOffset() ),
3848  aSymbol->ShowPinNumbers() ? 'Y' : 'N',
3849  aSymbol->ShowPinNames() ? 'Y' : 'N',
3850  aSymbol->GetUnitCount(), aSymbol->UnitsLocked() ? 'L' : 'F',
3851  aSymbol->IsPower() ? 'P' : 'N' );
3852 
3853  timestamp_t dateModified = aSymbol->GetLastModDate();
3854 
3855  if( dateModified != 0 )
3856  {
3857  int sec = dateModified & 63;
3858  int min = ( dateModified >> 6 ) & 63;
3859  int hour = ( dateModified >> 12 ) & 31;
3860  int day = ( dateModified >> 17 ) & 31;
3861  int mon = ( dateModified >> 22 ) & 15;
3862  int year = ( dateModified >> 26 ) + 1990;
3863 
3864  aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
3865  }
3866 
3867  std::vector<LIB_FIELD*> fields;
3868  aSymbol->GetFields( fields );
3869 
3870  // Mandatory fields:
3871  // may have their own save policy so there is a separate loop for them.
3872  // Empty fields are saved, because the user may have set visibility,
3873  // size and orientation
3874  for( int i = 0; i < MANDATORY_FIELDS; ++i )
3875  saveField( fields[i], aFormatter );
3876 
3877  // User defined fields:
3878  // may have their own save policy so there is a separate loop for them.
3879  int fieldId = MANDATORY_FIELDS; // really wish this would go away.
3880 
3881  for( unsigned i = MANDATORY_FIELDS; i < fields.size(); ++i )
3882  {
3883  // There is no need to save empty fields, i.e. no reason to preserve field
3884  // names now that fields names come in dynamically through the template
3885  // fieldnames.
3886  if( !fields[i]->GetText().IsEmpty() )
3887  {
3888  fields[i]->SetId( fieldId++ );
3889  saveField( fields[i], aFormatter );
3890  }
3891  }
3892 
3893  // Save the alias list: a line starting by "ALIAS".
3894  if( !aliasNames.IsEmpty() )
3895  {
3896  aFormatter.Print( 0, "ALIAS" );
3897 
3898  for( unsigned i = 0; i < aliasNames.GetCount(); i++ )
3899  aFormatter.Print( 0, " %s", TO_UTF8( aliasNames[i] ) );
3900 
3901  aFormatter.Print( 0, "\n" );
3902  }
3903 
3904  wxArrayString footprints = aSymbol->GetFPFilters();
3905 
3906  // Write the footprint filter list
3907  if( footprints.GetCount() != 0 )
3908  {
3909  aFormatter.Print( 0, "$FPLIST\n" );
3910 
3911  for( unsigned i = 0; i < footprints.GetCount(); i++ )
3912  aFormatter.Print( 0, " %s\n", TO_UTF8( footprints[i] ) );
3913 
3914  aFormatter.Print( 0, "$ENDFPLIST\n" );
3915  }
3916 
3917  // Save graphics items (including pins)
3918  if( !aSymbol->GetDrawItems().empty() )
3919  {
3920  // Sort the draw items in order to editing a file editing by hand.
3921  aSymbol->GetDrawItems().sort();
3922 
3923  aFormatter.Print( 0, "DRAW\n" );
3924 
3925  for( LIB_ITEM& item : aSymbol->GetDrawItems() )
3926  {
3927  switch( item.Type() )
3928  {
3929  default:
3930  case LIB_FIELD_T: /* Fields have already been saved above. */ break;
3931  case LIB_ARC_T: saveArc( (LIB_ARC*) &item, aFormatter ); break;
3932  case LIB_BEZIER_T: saveBezier( (LIB_BEZIER*) &item, aFormatter ); break;
3933  case LIB_CIRCLE_T: saveCircle( ( LIB_CIRCLE* ) &item, aFormatter ); break;
3934  case LIB_PIN_T: savePin( (LIB_PIN* ) &item, aFormatter ); break;
3935  case LIB_POLYLINE_T: savePolyLine( ( LIB_POLYLINE* ) &item, aFormatter ); break;
3936  case LIB_RECTANGLE_T: saveRectangle( ( LIB_RECTANGLE* ) &item, aFormatter ); break;
3937  case LIB_TEXT_T: saveText( ( LIB_TEXT* ) &item, aFormatter ); break;
3938  }
3939  }
3940 
3941  aFormatter.Print( 0, "ENDDRAW\n" );
3942  }
3943 
3944  aFormatter.Print( 0, "ENDDEF\n" );
3945 }
3946 
3947 
3949 {
3950  wxCHECK_RET( aArc && aArc->Type() == LIB_ARC_T, "Invalid LIB_ARC object." );
3951 
3952  int x1 = aArc->GetFirstRadiusAngle();
3953 
3954  if( x1 > 1800 )
3955  x1 -= 3600;
3956 
3957  int x2 = aArc->GetSecondRadiusAngle();
3958 
3959  if( x2 > 1800 )
3960  x2 -= 3600;
3961 
3962  aFormatter.Print( 0, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n",
3963  Iu2Mils( aArc->GetPosition().x ), Iu2Mils( aArc->GetPosition().y ),
3964  Iu2Mils( aArc->GetRadius() ), x1, x2, aArc->GetUnit(), aArc->GetConvert(),
3965  Iu2Mils( aArc->GetWidth() ),
3966  fill_tab[ static_cast<int>( aArc->GetFillMode() ) ],
3967  Iu2Mils( aArc->GetStart().x ), Iu2Mils( aArc->GetStart().y ),
3968  Iu2Mils( aArc->GetEnd().x ), Iu2Mils( aArc->GetEnd().y ) );
3969 }
3970 
3971 
3973  OUTPUTFORMATTER& aFormatter )
3974 {
3975  wxCHECK_RET( aBezier && aBezier->Type() == LIB_BEZIER_T, "Invalid LIB_BEZIER object." );
3976 
3977  aFormatter.Print( 0, "B %u %d %d %d", (unsigned)aBezier->GetPoints().size(),
3978  aBezier->GetUnit(), aBezier->GetConvert(), Iu2Mils( aBezier->GetWidth() ) );
3979 
3980  for( const auto& pt : aBezier->GetPoints() )
3981  aFormatter.Print( 0, " %d %d", Iu2Mils( pt.x ), Iu2Mils( pt.y ) );
3982 
3983  aFormatter.Print( 0, " %c\n", fill_tab[static_cast<int>( aBezier->GetFillMode() )] );
3984 }
3985 
3986 
3988  OUTPUTFORMATTER& aFormatter )
3989 {
3990  wxCHECK_RET( aCircle && aCircle->Type() == LIB_CIRCLE_T, "Invalid LIB_CIRCLE object." );
3991 
3992  aFormatter.Print( 0, "C %d %d %d %d %d %d %c\n",
3993  Iu2Mils( aCircle->GetPosition().x ), Iu2Mils( aCircle->GetPosition().y ),
3994  Iu2Mils( aCircle->GetRadius() ), aCircle->GetUnit(), aCircle->GetConvert(),
3995  Iu2Mils( aCircle->GetWidth() ),
3996  fill_tab[static_cast<int>( aCircle->GetFillMode() )] );
3997 }
3998 
3999 
4001 {
4002  wxCHECK_RET( aField && aField->Type() == LIB_FIELD_T, "Invalid LIB_FIELD object." );
4003 
4004  int hjustify, vjustify;
4005  int id = aField->GetId();
4006  wxString text = aField->GetText();
4007 
4008  hjustify = 'C';
4009 
4010  if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
4011  hjustify = 'L';
4012  else if( aField->GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
4013  hjustify = 'R';
4014 
4015  vjustify = 'C';
4016 
4017  if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
4018  vjustify = 'B';
4019  else if( aField->GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
4020  vjustify = 'T';
4021 
4022  aFormatter.Print( 0, "F%d %s %d %d %d %c %c %c %c%c%c",
4023  id,
4024  EscapedUTF8( text ).c_str(), // wraps in quotes
4025  Iu2Mils( aField->GetTextPos().x ), Iu2Mils( aField->GetTextPos().y ),
4026  Iu2Mils( aField->GetTextWidth() ),
4027  aField->GetTextAngle() == 0 ? 'H' : 'V',
4028  aField->IsVisible() ? 'V' : 'I',
4029  hjustify, vjustify,
4030  aField->IsItalic() ? 'I' : 'N',
4031  aField->IsBold() ? 'B' : 'N' );
4032 
4033  /* Save field name, if necessary
4034  * Field name is saved only if it is not the default name.
4035  * Just because default name depends on the language and can change from
4036  * a country to another
4037  */
4038  wxString defName = TEMPLATE_FIELDNAME::GetDefaultFieldName( id );
4039 
4040  if( id >= MANDATORY_FIELDS && !aField->m_name.IsEmpty() && aField->m_name != defName )
4041  aFormatter.Print( 0, " %s", EscapedUTF8( aField->m_name ).c_str() );
4042 
4043  aFormatter.Print( 0, "\n" );
4044 }
4045 
4046 
4048 {
4049  wxCHECK_RET( aPin && aPin->Type() == LIB_PIN_T, "Invalid LIB_PIN object." );
4050 
4051  int Etype;
4052 
4053  switch( aPin->GetType() )
4054  {
4055  default:
4056  case ELECTRICAL_PINTYPE::PT_INPUT: Etype = 'I'; break;
4057  case ELECTRICAL_PINTYPE::PT_OUTPUT: Etype = 'O'; break;
4058  case ELECTRICAL_PINTYPE::PT_BIDI: Etype = 'B'; break;
4059  case ELECTRICAL_PINTYPE::PT_TRISTATE: Etype = 'T'; break;
4060  case ELECTRICAL_PINTYPE::PT_PASSIVE: Etype = 'P'; break;
4061  case ELECTRICAL_PINTYPE::PT_UNSPECIFIED: Etype = 'U'; break;
4062  case ELECTRICAL_PINTYPE::PT_POWER_IN: Etype = 'W'; break;
4063  case ELECTRICAL_PINTYPE::PT_POWER_OUT: Etype = 'w'; break;
4064  case ELECTRICAL_PINTYPE::PT_OPENCOLLECTOR: Etype = 'C'; break;
4065  case ELECTRICAL_PINTYPE::PT_OPENEMITTER: Etype = 'E'; break;
4066  case ELECTRICAL_PINTYPE::PT_NC: Etype = 'N'; break;
4067  }
4068 
4069  if( !aPin->GetName().IsEmpty() )
4070  aFormatter.Print( 0, "X %s", TO_UTF8( aPin->GetName() ) );
4071  else
4072  aFormatter.Print( 0, "X ~" );
4073 
4074  aFormatter.Print( 0, " %s %d %d %d %c %d %d %d %d %c",
4075  aPin->GetNumber().IsEmpty() ? "~" : TO_UTF8( aPin->GetNumber() ),
4076  Iu2Mils( aPin->GetPosition().x ), Iu2Mils( aPin->GetPosition().y ),
4077  Iu2Mils( (int) aPin->GetLength() ), (int) aPin->GetOrientation(),
4078  Iu2Mils( aPin->GetNumberTextSize() ), Iu2Mils( aPin->GetNameTextSize() ),
4079  aPin->GetUnit(), aPin->GetConvert(), Etype );
4080 
4081  if( aPin->GetShape() != GRAPHIC_PINSHAPE::LINE || !aPin->IsVisible() )
4082  aFormatter.Print( 0, " " );
4083 
4084  if( !aPin->IsVisible() )
4085  aFormatter.Print( 0, "N" );
4086 
4087  switch( aPin->GetShape() )
4088  {
4089  case GRAPHIC_PINSHAPE::LINE: break;
4090  case GRAPHIC_PINSHAPE::INVERTED: aFormatter.Print( 0, "I" ); break;
4091  case GRAPHIC_PINSHAPE::CLOCK: aFormatter.Print( 0, "C" ); break;
4092  case GRAPHIC_PINSHAPE::INVERTED_CLOCK: aFormatter.Print( 0, "IC" ); break;
4093  case GRAPHIC_PINSHAPE::INPUT_LOW: aFormatter.Print( 0, "L" ); break;
4094  case GRAPHIC_PINSHAPE::CLOCK_LOW: aFormatter.Print( 0, "CL" ); break;
4095  case GRAPHIC_PINSHAPE::OUTPUT_LOW: aFormatter.Print( 0, "V" ); break;
4096  case GRAPHIC_PINSHAPE::FALLING_EDGE_CLOCK: aFormatter.Print( 0, "F" ); break;
4097  case GRAPHIC_PINSHAPE::NONLOGIC: aFormatter.Print( 0, "X" ); break;
4098  default: wxFAIL_MSG( "Invalid pin shape" );
4099  }
4100 
4101  aFormatter.Print( 0, "\n" );
4102 
4103  const_cast<LIB_PIN*>( aPin )->ClearFlags( IS_CHANGED );
4104 }
4105 
4106 
4108  OUTPUTFORMATTER& aFormatter )
4109 {
4110  wxCHECK_RET( aPolyLine && aPolyLine->Type() == LIB_POLYLINE_T, "Invalid LIB_POLYLINE object." );
4111 
4112  int ccount = aPolyLine->GetCornerCount();
4113 
4114  aFormatter.Print( 0, "P %d %d %d %d", ccount, aPolyLine->GetUnit(), aPolyLine->GetConvert(),
4115  Iu2Mils( aPolyLine->GetWidth() ) );
4116 
4117  for( const auto& pt : aPolyLine->GetPolyPoints() )
4118  {
4119  aFormatter.Print( 0, " %d %d", Iu2Mils( pt.x ), Iu2Mils( pt.y ) );
4120  }
4121 
4122  aFormatter.Print( 0, " %c\n", fill_tab[static_cast<int>( aPolyLine->GetFillMode() )] );
4123 }
4124 
4125 
4127  OUTPUTFORMATTER& aFormatter )
4128 {
4129  wxCHECK_RET( aRectangle && aRectangle->Type() == LIB_RECTANGLE_T,
4130  "Invalid LIB_RECTANGLE object." );
4131 
4132  aFormatter.Print( 0, "S %d %d %d %d %d %d %d %c\n",
4133  Iu2Mils( aRectangle->GetPosition().x ),
4134  Iu2Mils( aRectangle->GetPosition().y ),
4135  Iu2Mils( aRectangle->GetEnd().x ), Iu2Mils( aRectangle->GetEnd().y ),
4136  aRectangle->GetUnit(), aRectangle->GetConvert(),
4137  Iu2Mils( aRectangle->GetWidth() ),
4138  fill_tab[static_cast<int>( aRectangle->GetFillMode() )] );
4139 }
4140 
4141 
4143 {
4144  wxCHECK_RET( aText && aText->Type() == LIB_TEXT_T, "Invalid LIB_TEXT object." );
4145 
4146  wxString text = aText->GetText();
4147 
4148  if( text.Contains( wxT( " " ) ) || text.Contains( wxT( "~" ) ) || text.Contains( wxT( "\"" ) ) )
4149  {
4150  // convert double quote to similar-looking two apostrophes
4151  text.Replace( wxT( "\"" ), wxT( "''" ) );
4152  text = wxT( "\"" ) + text + wxT( "\"" );
4153  }
4154 
4155  aFormatter.Print( 0, "T %g %d %d %d %d %d %d %s", aText->GetTextAngle(),
4156  Iu2Mils( aText->GetTextPos().x ), Iu2Mils( aText->GetTextPos().y ),
4157  Iu2Mils( aText->GetTextWidth() ), !aText->IsVisible(),
4158  aText->GetUnit(), aText->GetConvert(), TO_UTF8( text ) );
4159 
4160  aFormatter.Print( 0, " %s %d", aText->IsItalic() ? "Italic" : "Normal", aText->IsBold() );
4161 
4162  char hjustify = 'C';
4163 
4164  if( aText->GetHorizJustify() == GR_TEXT_HJUSTIFY_LEFT )
4165  hjustify = 'L';
4166  else if( aText->GetHorizJustify() == GR_TEXT_HJUSTIFY_RIGHT )
4167  hjustify = 'R';
4168 
4169  char vjustify = 'C';
4170 
4171  if( aText->GetVertJustify() == GR_TEXT_VJUSTIFY_BOTTOM )
4172  vjustify = 'B';
4173  else if( aText->GetVertJustify() == GR_TEXT_VJUSTIFY_TOP )
4174  vjustify = 'T';
4175 
4176  aFormatter.Print( 0, " %c %c\n", hjustify, vjustify );
4177 }
4178 
4179 
4181 {
4182  wxFileName fileName = m_libFileName;
4183 
4184  fileName.SetExt( DOC_EXT );
4185  FILE_OUTPUTFORMATTER formatter( fileName.GetFullPath() );
4186 
4187  formatter.Print( 0, "%s\n", DOCFILE_IDENT );
4188 
4189  for( LIB_SYMBOL_MAP::iterator it = m_symbols.begin(); it != m_symbols.end(); ++it )
4190  {
4191  wxString description = it->second->GetDescription();
4192  wxString keyWords = it->second->GetKeyWords();
4193  wxString docFileName = it->second->GetDatasheetField().GetText();
4194 
4195  if( description.IsEmpty() && keyWords.IsEmpty() && docFileName.IsEmpty() )
4196  continue;
4197 
4198  formatter.Print( 0, "#\n$CMP %s\n", TO_UTF8( it->second->GetName() ) );
4199 
4200  if( !description.IsEmpty() )
4201  formatter.Print( 0, "D %s\n", TO_UTF8( description ) );
4202 
4203  if( !keyWords.IsEmpty() )
4204  formatter.Print( 0, "K %s\n", TO_UTF8( keyWords ) );
4205 
4206  if( !docFileName.IsEmpty() )
4207  formatter.Print( 0, "F %s\n", TO_UTF8( docFileName ) );
4208 
4209  formatter.Print( 0, "$ENDCMP\n" );
4210  }
4211 
4212  formatter.Print( 0, "#\n#End Doc Library\n" );
4213 }
4214 
4215 
4216 void SCH_LEGACY_PLUGIN_CACHE::DeleteSymbol( const wxString& aSymbolName )
4217 {
4218  LIB_SYMBOL_MAP::iterator it = m_symbols.find( aSymbolName );
4219 
4220  if( it == m_symbols.end() )
4221  THROW_IO_ERROR( wxString::Format( _( "library %s does not contain a symbol named %s" ),
4222  m_libFileName.GetFullName(), aSymbolName ) );
4223 
4224  LIB_SYMBOL* symbol = it->second;
4225 
4226  if( symbol->IsRoot() )
4227  {
4228  LIB_SYMBOL* rootSymbol = symbol;
4229 
4230  // Remove the root symbol and all its children.
4231  m_symbols.erase( it );
4232 
4233  LIB_SYMBOL_MAP::iterator it1 = m_symbols.begin();
4234 
4235  while( it1 != m_symbols.end() )
4236  {
4237  if( it1->second->IsAlias()
4238  && it1->second->GetParent().lock() == rootSymbol->SharedPtr() )
4239  {
4240  delete it1->second;
4241  it1 = m_symbols.erase( it1 );
4242  }
4243  else
4244  {
4245  it1++;
4246  }
4247  }
4248 
4249  delete rootSymbol;
4250  }
4251  else
4252  {
4253  // Just remove the alias.
4254  m_symbols.erase( it );
4255  delete symbol;
4256  }
4257 
4259  m_isModified = true;
4260 }
4261 
4262 
4263 void SCH_LEGACY_PLUGIN::cacheLib( const wxString& aLibraryFileName, const PROPERTIES* aProperties )
4264 {
4265  if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
4266  {
4267  // a spectacular episode in memory management:
4268  delete m_cache;
4269  m_cache = new SCH_LEGACY_PLUGIN_CACHE( aLibraryFileName );
4270 
4271  // Because m_cache is rebuilt, increment SYMBOL_LIBS::s_modify_generation
4272  // to modify the hash value that indicate symbol to symbol links
4273  // must be updated.
4275 
4276  if( !isBuffering( aProperties ) )
4277  m_cache->Load();
4278  }
4279 }
4280 
4281 
4283 {
4284  std::string propName( SCH_LEGACY_PLUGIN::PropNoDocFile );
4285 
4286  if( aProperties && aProperties->find( propName ) != aProperties->end() )
4287  return false;
4288 
4289  return true;
4290 }
4291 
4292 
4293 bool SCH_LEGACY_PLUGIN::isBuffering( const PROPERTIES* aProperties )
4294 {
4295  return ( aProperties && aProperties->Exists( SCH_LEGACY_PLUGIN::PropBuffering ) );
4296 }
4297 
4298 
4300 {
4301  if( m_cache )
4303 
4304  // If the cache hasn't been loaded, it hasn't been modified.
4305  return 0;
4306 }
4307 
4308 
4309 void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
4310  const wxString& aLibraryPath,
4311  const PROPERTIES* aProperties )
4312 {
4313  LOCALE_IO toggle; // toggles on, then off, the C locale.
4314 
4315  bool powerSymbolsOnly = ( aProperties &&
4316  aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
4317 
4318  cacheLib( aLibraryPath, aProperties );
4319 
4320  const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
4321 
4322  for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
4323  {
4324  if( !powerSymbolsOnly || it->second->IsPower() )
4325  aSymbolNameList.Add( it->first );
4326  }
4327 }
4328 
4329 
4330 void SCH_LEGACY_PLUGIN::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
4331  const wxString& aLibraryPath,
4332  const PROPERTIES* aProperties )
4333 {
4334  LOCALE_IO toggle; // toggles on, then off, the C locale.
4335 
4336  bool powerSymbolsOnly = ( aProperties &&
4337  aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
4338 
4339  cacheLib( aLibraryPath, aProperties );
4340 
4341  const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
4342 
4343  for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
4344  {
4345  if( !powerSymbolsOnly || it->second->IsPower() )
4346  aSymbolList.push_back( it->second );
4347  }
4348 }
4349 
4350 
4351 LIB_SYMBOL* SCH_LEGACY_PLUGIN::LoadSymbol( const wxString& aLibraryPath,
4352  const wxString& aSymbolName,
4353  const PROPERTIES* aProperties )
4354 {
4355  LOCALE_IO toggle; // toggles on, then off, the C locale.
4356 
4357  cacheLib( aLibraryPath, aProperties );
4358 
4359  LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
4360 
4361  if( it == m_cache->m_symbols.end() )
4362  return nullptr;
4363 
4364  return it->second;
4365 }
4366 
4367 
4368 void SCH_LEGACY_PLUGIN::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
4369  const PROPERTIES* aProperties )
4370 {
4371  LOCALE_IO toggle; // toggles on, then off, the C locale.
4372 
4373  cacheLib( aLibraryPath, aProperties );
4374 
4375  m_cache->AddSymbol( aSymbol );
4376 
4377  if( !isBuffering( aProperties ) )
4378  m_cache->Save( writeDocFile( aProperties ) );
4379 }
4380 
4381 
4382 void SCH_LEGACY_PLUGIN::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
4383  const PROPERTIES* aProperties )
4384 {
4385  LOCALE_IO toggle; // toggles on, then off, the C locale.
4386 
4387  cacheLib( aLibraryPath, aProperties );
4388 
4389  m_cache->DeleteSymbol( aSymbolName );
4390 
4391  if( !isBuffering( aProperties ) )
4392  m_cache->Save( writeDocFile( aProperties ) );
4393 }
4394 
4395 
4396 void SCH_LEGACY_PLUGIN::CreateSymbolLib( const wxString& aLibraryPath,
4397  const PROPERTIES* aProperties )
4398 {
4399  if( wxFileExists( aLibraryPath ) )
4400  {
4401  THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' already exists." ),
4402  aLibraryPath.GetData() ) );
4403  }
4404 
4405  LOCALE_IO toggle;
4406 
4407  delete m_cache;
4408  m_cache = new SCH_LEGACY_PLUGIN_CACHE( aLibraryPath );
4409  m_cache->SetModified();
4410  m_cache->Save( writeDocFile( aProperties ) );
4411  m_cache->Load(); // update m_writable and m_mod_time
4412 }
4413 
4414 
4415 bool SCH_LEGACY_PLUGIN::DeleteSymbolLib( const wxString& aLibraryPath,
4416  const PROPERTIES* aProperties )
4417 {
4418  wxFileName fn = aLibraryPath;
4419 
4420  if( !fn.FileExists() )
4421  return false;
4422 
4423  // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
4424  // we don't want that. we want bare metal portability with no UI here.
4425  if( wxRemove( aLibraryPath ) )
4426  {
4427  THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' cannot be deleted." ),
4428  aLibraryPath.GetData() ) );
4429  }
4430 
4431  if( m_cache && m_cache->IsFile( aLibraryPath ) )
4432  {
4433  delete m_cache;
4434  m_cache = nullptr;
4435  }
4436 
4437  return true;
4438 }
4439 
4440 
4441 void SCH_LEGACY_PLUGIN::SaveLibrary( const wxString& aLibraryPath, const PROPERTIES* aProperties )
4442 {
4443  if( !m_cache )
4444  m_cache = new SCH_LEGACY_PLUGIN_CACHE( aLibraryPath );
4445 
4446  wxString oldFileName = m_cache->GetFileName();
4447 
4448  if( !m_cache->IsFile( aLibraryPath ) )
4449  {
4450  m_cache->SetFileName( aLibraryPath );
4451  }
4452 
4453  // This is a forced save.
4454  m_cache->SetModified();
4455  m_cache->Save( writeDocFile( aProperties ) );
4456  m_cache->SetFileName( oldFileName );
4457 }
4458 
4459 
4460 bool SCH_LEGACY_PLUGIN::CheckHeader( const wxString& aFileName )
4461 {
4462  // Open file and check first line
4463  wxTextFile tempFile;
4464 
4465  tempFile.Open( aFileName );
4466  wxString firstline;
4467  // read the first line
4468  firstline = tempFile.GetFirstLine();
4469  tempFile.Close();
4470 
4471  return firstline.StartsWith( "EESchema" );
4472 }
4473 
4474 
4475 bool SCH_LEGACY_PLUGIN::IsSymbolLibWritable( const wxString& aLibraryPath )
4476 {
4477  // Writing legacy symbol libraries is deprecated.
4478  return false;
4479 }
4480 
4481 
4483  int aMinorVersion )
4484 {
4485  return SCH_LEGACY_PLUGIN_CACHE::LoadPart( reader, aMajorVersion, aMinorVersion );
4486 }
4487 
4488 
4490 {
4491  SCH_LEGACY_PLUGIN_CACHE::SaveSymbol( symbol, formatter );
4492 }
4493 
4494 
4495 
4496 const char* SCH_LEGACY_PLUGIN::PropBuffering = "buffering";
4497 const char* SCH_LEGACY_PLUGIN::PropNoDocFile = "no_doc_file";
Field Reference of part, i.e. "IC21".
static LIB_TEXT * loadText(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader, int aMajorVersion, int aMinorVersion)
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
SCH_SHEET * loadSheet(LINE_READER &aReader)
CITER next(CITER it)
Definition: ptree.cpp:126
power input (GND, VCC for ICs). Must be connected to a power output.
#define TEXT_ANGLE_HORIZ
Frequent text rotations, used with {Set,Get}TextAngle(), in 0.1 degrees for now, hoping to migrate to...
Definition: eda_text.h:50
LIB_FIELD * FindField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName and returns it or NULL if not found.
Definition: lib_symbol.cpp:953
void LoadContent(LINE_READER &aReader, SCH_SCREEN *aScreen, int version=EESCHEMA_VERSION)
SCH_LIB_TYPE
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:216
static const char * GetLineStyleName(PLOT_DASH_TYPE aStyle)
Definition: sch_line.cpp:85
bool IsBold() const
Definition: eda_text.h:183
SCH_JUNCTION * loadJunction(LINE_READER &aReader)
#define IS_CHANGED
Item was edited, and modified.
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const PROPERTIES *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:49
void DeleteSymbol(const wxString &aName)
static LIB_SYMBOL * LoadPart(LINE_READER &aReader, int aMajorVersion, int aMinorVersion, LIB_SYMBOL_MAP *aMap=nullptr)
LIB_SYMBOL_REF & GetParent()
Definition: lib_symbol.h:124
int GetPinNameOffset() const
Definition: lib_symbol.h:561
COLOR4D GetLineColor() const
Returns COLOR4D::UNSPECIFIED if a custom color hasn't been set for this line.
Definition: sch_line.cpp:210
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
Symbol map used by symbol library object.
const wxString & GetFileName() const
Definition: sch_screen.h:145
LIB_SYMBOL_SPTR SharedPtr() const
Definition: lib_symbol.h:107
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
timestamp_t GetLastModDate() const
Definition: lib_symbol.h:179
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:214
SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const PROPERTIES *aProperties=nullptr) override
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
Definition: sch_sheet.cpp:635
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:117
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:199
bool IsGraphicLine() const
Return if the line is a graphic (non electrical line)
Definition: sch_line.cpp:922
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
static void saveCircle(LIB_CIRCLE *aCircle, OUTPUTFORMATTER &aFormatter)
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
void SetVirtualPageNumber(int aPageNumber)
Definition: base_screen.h:76
FILL_TYPE
The set of fill types used in plotting or drawing enclosed areas.
Definition: fill_type.h:28
int y2
Definition: transform.h:51
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition: richio.h:109
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
wxPoint GetStartPoint() const
Definition: sch_line.h:90
wxPoint GetEnd() const
Definition: lib_rectangle.h:82
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:675
int GetPenSizeForBold(int aTextSize)
Definition: gr_text.cpp:46
wxString m_error
For throwing exceptions or errors on partial schematic loads.
static bool strCompare(const char *aString, const char *aLine, const char **aOutput=nullptr)
Compare aString to the string starting at aLine and advances the character point to the end of String...
Holds all the data relating to one schematic.
Definition: schematic.h:59
wxPoint GetPosition() const override
void SetRevision(const wxString &aRevision)
Definition: title_block.h:81
wxString GetName() const override
Definition: lib_symbol.h:133
Define a symbol library graphical text item.
Definition: lib_text.h:39
void saveLine(SCH_LINE *aLine)
int GetOrientation() const
Definition: lib_pin.h:75
const wxString & GetComment(int aIdx) const
Definition: title_block.h:107
int GetHeightMils() const
Definition: page_info.h:133
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:78
static void parseUnquotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=nullptr, bool aCanBeEmpty=false)
Parse an unquoted utf8 string and updates the pointer at aOutput if it is not NULL.
SCH_LEGACY_PLUGIN_CACHE * m_cache
static void loadAliases(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader, LIB_SYMBOL_MAP *aMap=nullptr)
void SetItalic(bool isItalic)
Definition: eda_text.h:179
int color
Definition: DXF_plotter.cpp:57
Field object used in symbol libraries.
Definition: lib_field.h:59
void SetFillMode(FILL_TYPE aFillMode)
Definition: lib_item.h:277
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:156
const wxChar *const traceSchLegacyPlugin
Flag to enable legacy schematic plugin debug output.
bool IsAlias() const
Definition: lib_symbol.h:172
double GetScale() const
Definition: bitmap_base.h:79
int GetVirtualPageNumber() const
Definition: base_screen.h:75
pin for passive symbols: must be connected, and can be connected to any pin
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:108
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:711
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:246
wxImage * GetImageData()
Definition: bitmap_base.h:70
void AddSymbol(const LIB_SYMBOL *aSymbol)
bool ShowPinNumbers() const
Definition: lib_symbol.h:577
void sort()
Definition: multivector.h:247
void init(SCHEMATIC *aSchematic, const PROPERTIES *aProperties=nullptr)
initialize PLUGIN like a constructor would.
void SetFirstRadiusAngle(int aAngle)
Definition: lib_arc.h:86
bool SetType(const wxString &aStandardPageDescriptionName, bool aIsPortrait=false)
Set the name of the page type and also the sizes and margins commonly associated with that type name.
Definition: page_info.cpp:119
int x2
Definition: transform.h:50
static LIB_CIRCLE * loadCircle(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
wxString GetLogicalName() const
double GetTextAngle() const
Definition: eda_text.h:174
int GetId() const
Definition: sch_field.h:113
int GetPageCount() const
Definition: base_screen.h:72
wxPoint GetPosition() const override
Definition: lib_circle.h:66
static uint32_t parseHex(LINE_READER &aReader, const char *aLine, const char **aOutput=nullptr)
Parse an ASCII hex integer string with possible leading whitespace into a long integer and updates th...
unknown electrical properties: creates always a warning when connected
void saveText(SCH_TEXT *aText)
void SetDate(const wxString &aDate)
Set the date field, and defaults to the current time and date.
Definition: title_block.h:71
static LIB_BEZIER * loadBezier(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:150
SCH_LEGACY_PLUGIN_CACHE(const wxString &aLibraryPath)
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:309
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: sch_screen.h:133
int x1
Definition: transform.h:48
#define MAX_UNIT_COUNT_PER_PACKAGE
The maximum number of units per package.
Definition: eeschema_id.h:44
bool IsPower() const
Definition: lib_symbol.cpp:409
static const wxChar Custom[]
"User" defined page type
Definition: page_info.h:77
const wxString & GetType() const
Definition: page_info.h:94
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:237
int GetId() const
Definition: lib_field.h:115
void saveNoConnect(SCH_NO_CONNECT *aNoConnect)
static double parseDouble(LINE_READER &aReader, const char *aLine, const char **aOutput=nullptr)
Parses an ASCII point string with possible leading whitespace into a double precision floating point ...
LIB_FIELD & GetValueField()
Return reference to the value field.
Definition: lib_symbol.cpp:977
static void saveBezier(LIB_BEZIER *aBezier, OUTPUTFORMATTER &aFormatter)
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
bool IsCustom() const
Definition: page_info.cpp:180
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:290
wxPoint GetPosition() const override
Definition: sch_bitmap.h:136
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
#define LIB_VERSION_MINOR
static FILL_TYPE parseFillMode(LINE_READER &aReader, const char *aLine, const char **aOutput)
bool empty(int aType=UNDEFINED_TYPE) const
Definition: multivector.h:242
int GetTextThickness() const
Definition: eda_text.h:160
A name/value tuple with unique names and optional values.
Definition: properties.h:33
void loadHeader(FILE_LINE_READER &aReader)
int GetWidth() const override
Definition: lib_circle.h:78
std::string toUTFTildaText(const wxString &txt)
Convert a wxString to UTF8 and replace any control characters with a ~, where a control character is ...
Definition: sch_symbol.cpp:46
void SetWidth(int aWidth) override
Definition: lib_polyline.h:97
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:103
Define a library symbol object.
Definition: lib_symbol.h:96
std::chrono::steady_clock CLOCK
#define Mils2Iu(x)
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:40
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:132
TRANSFORM & GetTransform()
Definition: sch_symbol.h:231
#define T_WIDTH
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
#define SCH_LAYER_ID_COUNT
Definition: layer_ids.h:365
LIB_FIELD & GetReferenceField()
Return reference to the reference designator field.
Definition: lib_symbol.cpp:985
static LIB_RECTANGLE * loadRectangle(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
void saveSheet(SCH_SHEET *aSheet)
SCH_BUS_ENTRY_BASE * loadBusEntry(LINE_READER &aReader)
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:116
int GetWidth() const override
Definition: lib_polyline.h:96
unsigned GetCornerCount() const
Definition: lib_polyline.h:67
name of datasheet
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:83
The base class for drawable items used by schematic library symbols.
Definition: lib_item.h:61
void GetFields(std::vector< LIB_FIELD * > &aList)
Return a list of fields within this symbol.
Definition: lib_symbol.cpp:905
void SetWidth(int aWidth) override
Definition: lib_arc.h:81
A cache assistant for the symbol library portion of the SCH_PLUGIN API, and only for the SCH_LEGACY_P...
void saveField(SCH_FIELD *aField)
static void FormatPart(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter)
#define T_STYLE
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:42
const std::vector< SYMBOL_INSTANCE_REFERENCE > & GetInstanceReferences()
Definition: sch_symbol.h:123
void saveJunction(SCH_JUNCTION *aJunction)
int m_version
Version of file being loaded.
void loadFile(const wxString &aFileName, SCH_SCREEN *aScreen)
int GetWidth() const override
Definition: lib_bezier.h:82
bool IsItalic() const
Definition: eda_text.h:180
#define SCHEMATIC_HEAD_STRING
Definition: general.h:36
void saveSymbol(SCH_SYMBOL *aSymbol)
const std::vector< wxPoint > & GetPolyPoints() const
Definition: lib_polyline.h:54
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:222
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:88
void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const PROPERTIES *aProperties=nullptr) override
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
void Reserve(size_t aCount)
Definition: lib_bezier.h:53
wxString m_path
Root project path for loading child sheets.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
int y1
Definition: transform.h:49
static LIB_PIN * loadPin(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
int GetSecondRadiusAngle() const
Definition: lib_arc.h:90
virtual void SetVisible(bool aVisible)
Definition: eda_text.h:185
Definition: kiid.h:44
#define EESCHEMA_VERSION
Definition: general.h:35
FILL_TYPE GetFillMode() const
Definition: lib_item.h:278
int GetUnit() const
Definition: lib_item.h:272
PLOT_DASH_TYPE GetDefaultStyle() const
Definition: sch_line.cpp:224
#define DOC_EXT
A LINE_READER that reads from an open file.
Definition: richio.h:172
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
#define DOCFILE_IDENT
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
bool isBuffering(const PROPERTIES *aProperties)
void SetEnd(const wxPoint &aPoint)
Definition: lib_arc.h:96
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:198
Field Value of part, i.e. "3.3K".
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:114
void SetComment(int aIdx, const wxString &aComment)
Definition: title_block.h:101
static void loadDrawEntries(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader, int aMajorVersion, int aMinorVersion)
void SetWidth(int aWidth) override
Definition: lib_rectangle.h:79
void loadHierarchy(SCH_SHEET *aSheet)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
BITMAP_BASE * GetImage() const
Definition: sch_bitmap.h:54
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:323
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:135
const wxString & GetRevision() const
Definition: title_block.h:86
wxPoint GetPosition() const override
Definition: sch_junction.h:92
timestamp_t AsLegacyTimestamp() const
Definition: kiid.cpp:191
void Reserve(size_t aPointCount)
Definition: lib_polyline.h:51
const wxString & GetName() const
Definition: lib_pin.h:106
PLOT_DASH_TYPE GetLineStyle() const
Definition: sch_line.cpp:248
virtual bool IsVisible() const
Definition: eda_text.h:186
static void saveRectangle(LIB_RECTANGLE *aRectangle, OUTPUTFORMATTER &aFormatter)
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
const wxSize & GetTextSize() const
Definition: eda_text.h:238
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:202
const wxString & GetCompany() const
Definition: title_block.h:96
int GetUnitCount() const override
For items with units, return the number of units.
int GetFieldCount() const
Return the number of fields in this symbol.
Definition: sch_symbol.h:421
int GetWidth() const override
Definition: lib_rectangle.h:78
static bool is_eol(char c)
wxPoint GetStart() const
Definition: lib_arc.h:92
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:85
const int fill_tab[3]
Definition: lib_item.cpp:32
#define LIB_VERSION_MAJOR
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: lib_field.cpp:371
void SetDescription(const wxString &aDescription)
Definition: lib_symbol.h:140
void cacheLib(const wxString &aLibraryFileName, const PROPERTIES *aProperties)
static void savePolyLine(LIB_POLYLINE *aPolyLine, OUTPUTFORMATTER &aFormatter)
static void IncrementModifyGeneration()
void SetRadius(int aRadius)
Definition: lib_circle.h:81
void Empty()
Definition: eda_text.h:254
void SetCompany(const wxString &aCompany)
Definition: title_block.h:91
A simple container for schematic symbol instance information.
Definition for symbol library class.
const std::vector< wxPoint > & GetPoints() const
Definition: lib_bezier.h:64
void loadPageSettings(LINE_READER &aReader, SCH_SCREEN *aScreen)
void Save(const wxString &aFileName, SCH_SHEET *aScreen, SCHEMATIC *aSchematic, const PROPERTIES *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_PLUGIN implementation knows about,...
bool ShowPinNames() const
Definition: lib_symbol.h:569
#define _(s)
wxLogTrace helper definitions.
static int parseInt(LINE_READER &aReader, const char *aLine, const char **aOutput=nullptr)
Parse an ASCII integer string with possible leading whitespace into an integer and updates the pointe...
#define USE_OLD_DOC_FILE_FORMAT(major, minor)
#define T_COLOR
void AddDrawItem(LIB_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Definition: lib_symbol.cpp:653
void saveBitmap(SCH_BITMAP *aBitmap)
wxPoint GetLibPosition() const
Definition: sch_field.h:186
void SetParent(LIB_SYMBOL *aParent=nullptr)
Definition: lib_symbol.cpp:323
LABEL_SPIN_STYLE GetLabelSpinStyle() const
Definition: sch_text.h:159
static const char * PropNoDocFile
The property used internally by the plugin to disable writing the library documentation (....
static LIB_SYMBOL * ParsePart(LINE_READER &aReader, int majorVersion=0, int minorVersion=0)
bool IsVisible() const
Definition: lib_pin.h:97
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:65
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslate=true)
Return a default symbol field name for field aFieldNdx for all components.
int GetRadius() const
Definition: lib_circle.cpp:261
int GetNameTextSize() const
Definition: lib_pin.h:126
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:315
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:108
void SetModified(bool aModified=true)
void AddPoint(const wxPoint &aPoint)
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition: sch_screen.h:152
wxPoint GetPosition() const override
Definition: sch_text.h:239
wxPoint GetPosition() const override
Definition: sch_sheet.h:380
void SetTitle(const wxString &aTitle)
Definition: title_block.h:58
#define LIBFILE_IDENT
int GetConvert() const
Definition: lib_item.h:275
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition: lib_symbol.h:473
static PLOT_DASH_TYPE GetLineStyleByName(const wxString &aStyleName)
Definition: sch_line.cpp:95
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:40
void SetHeightMils(int aHeightInMils)
Definition: page_info.cpp:257
UTF8 Format() const
Definition: lib_id.cpp:116
void SaveLibrary(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
const char * delims
bool UnitsLocked() const
Check whether symbol units are interchangeable.
Definition: lib_symbol.h:241
void SetSecondRadiusAngle(int aAngle)
Definition: lib_arc.h:89
bool CheckHeader(const wxString &aFileName) override
Return true if the first line in aFileName begins with the expected header.
static void SaveSymbol(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter, LIB_SYMBOL_MAP *aMap=nullptr)
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:54
PLOT_DASH_TYPE
Dashed line types.
Definition: plotter.h:104
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:75
int GetRadius() const
Definition: lib_arc.h:84
const KIID m_Uuid
Definition: eda_item.h:475
void SetConvert(int aConvert)
Definition: lib_item.h:274
wxPoint GetPosition() const override
Definition: lib_pin.h:210
void Format(SCH_SHEET *aSheet)
virtual unsigned int GetSize() const override
Return the number of stored items.
Definition: selection.h:87
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:268
void SetFileName(const wxString &aFileName)
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:184
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void SetContentModified(bool aModified=true)
Definition: base_screen.h:59
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
SCH_BITMAP * loadBitmap(LINE_READER &aReader)
static void saveText(const LIB_TEXT *aText, OUTPUTFORMATTER &aFormatter)
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:32
static void loadField(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
static void savePin(const LIB_PIN *aPin, OUTPUTFORMATTER &aFormatter)
bool IsFile(const wxString &aFullPathAndFileName) const
const wxString & GetDate() const
Definition: title_block.h:76
const wxString & GetNumber() const
Definition: lib_pin.h:116
ELECTRICAL_PINTYPE GetType() const
Definition: lib_pin.h:84
static void saveField(const LIB_FIELD *aField, OUTPUTFORMATTER &aFormatter)
void SetUnit(int aUnit)
Definition: lib_item.h:271
std::unordered_set< std::shared_ptr< BUS_ALIAS > > GetBusAliases() const
Return a list of bus aliases defined in this screen.
Definition: sch_screen.h:454
int GetLineSize() const
Definition: sch_line.h:152
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:49
int GetWidth() const override
Definition: lib_arc.h:80
LIB_FIELD * GetFieldById(int aId) const
Return pointer to the requested field.
Definition: lib_symbol.cpp:939
SCH_SHEET & Root() const
Definition: schematic.h:92
int GetNumberTextSize() const
Definition: lib_pin.h:129
bool Exists(const std::string &aProperty) const
Definition: properties.h:41
int GetTextWidth() const
Definition: eda_text.h:241
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
#define SCH_PARSE_ERROR(text, reader, pos)
void SetEnd(const wxPoint &aEnd)
Definition: lib_rectangle.h:81
Schematic symbol object.
Definition: sch_symbol.h:78
wxString AsLegacyTimestampString() const
Definition: kiid.cpp:224
void saveBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
void AddBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
Add a bus alias definition (and transfers ownership of the pointer).
const char * name
Definition: DXF_plotter.cpp:56
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:37
OUTPUTFORMATTER * m_out
The formatter for saving SCH_SCREEN objects.
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:35
void Append(SCH_ITEM *aItem)
Definition: sch_screen.cpp:144
const std::map< PINSHEETLABEL_SHAPE, const char * > sheetLabelNames
virtual KIGFX::VIEW_ITEM * GetItem(unsigned int aIdx) const override
Definition: selection.cpp:52
int GetLength() const
Definition: lib_pin.h:81
bool IsPortrait() const
Definition: page_info.h:117
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:201
usual pin input: must be connected
wxSize GetSize() const
Definition: sch_sheet.h:105
LINE_READER * m_lineReader
for progress reporting
PROGRESS_REPORTER * m_progressReporter
optional; may be nullptr
void CreateSymbolLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Create a new empty symbol library at aLibraryPath.
wxString GetName(bool aUseDefaultName=true) const
Return the field name.
Definition: sch_field.cpp:544
unsigned m_lineCount
for progress reporting
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:110
void SetWidthMils(int aWidthInMils)
Definition: page_info.cpp:243
static char parseChar(LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=nullptr)
Parse a single ASCII character and updates the pointer at aOutput if it is not NULL.
void SetRadius(int aRadius)
Definition: lib_arc.h:83
wxPoint GetEnd() const
Definition: lib_arc.h:95
void SetName(const wxString &aName)
Definition: sch_field.h:111
wxString GetPrefix() const
Definition: sch_symbol.h:227
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const PROPERTIES *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
static void saveArc(LIB_ARC *aArc, OUTPUTFORMATTER &aFormatter)
wxPoint GetPosition() const override
Definition: sch_symbol.h:641
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
wxArrayString GetFPFilters() const
Definition: lib_symbol.h:183
Used for text file output.
Definition: richio.h:456
int GetModifyHash() const override
Return the modification hash from the library cache.
void SetWidth(int aWidth) override
Definition: lib_circle.h:79
SCH_NO_CONNECT * loadNoConnect(LINE_READER &aReader)
const wxPoint & GetTextPos() const
Definition: eda_text.h:247
void SetPosition(const wxPoint &aPosition) override
Definition: lib_item.h:225
int GetFirstRadiusAngle() const
Definition: lib_arc.h:87
void SetStart(const wxPoint &aPoint)
Definition: lib_arc.h:93
static void parseQuotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken=nullptr, bool aCanBeEmpty=false)
Parse an quoted ASCII utf8 and updates the pointer at aOutput if it is not NULL.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
const wxString & GetTitle() const
Definition: title_block.h:63
input or output (like port for a microprocessor)
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:171
void loadHeader(LINE_READER &aReader, SCH_SCREEN *aScreen)
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
static std::mutex s_modHashMutex
#define TEXT_ANGLE_VERT
Definition: eda_text.h:51
virtual void SetTextAngle(double aAngle)
Definition: eda_text.h:167
SCH_LINE * loadWire(LINE_READER &aReader)
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:108
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:426
wxString m_name
Name (not the field text value itself, that is .m_Text)
Definition: lib_field.h:213
int GetUnit() const
Definition: sch_symbol.h:195
not connected (must be left open)
SCH_TEXT * loadText(LINE_READER &aReader)
void SetWidth(int aWidth) override
Definition: lib_bezier.h:83
#define T_COLORA
LIB_SYMBOL * removeSymbol(LIB_SYMBOL *aAlias)
static LIB_POLYLINE * loadPolyLine(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
void SetBold(bool aBold)
Definition: eda_text.h:182
bool IsSymbolLibWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void SetPageCount(int aPageCount)
Definition: base_screen.cpp:63
void SetPortrait(bool aIsPortrait)
Rotate the paper page 90 degrees.
Definition: page_info.cpp:186
void Save(bool aSaveDocFile=true)
Save the entire library to file m_libFileName;.
int GetConvert() const
Definition: sch_symbol.h:223
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:193
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
SCH_SYMBOL * loadSymbol(LINE_READER &aReader)
output of a regulator: intended to be connected to power input pins
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry)
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
void AddPoint(const wxPoint &aPoint)
Definition: lib_bezier.h:54
static void loadFootprintFilters(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
static const char * PropPowerSymsOnly
void SetKeyWords(const wxString &aKeyWords)
Definition: lib_symbol.h:153
wxPoint GetPosition() const override
Definition: lib_rectangle.h:69
bool writeDocFile(const PROPERTIES *aProperties)
wxFileName GetRealFile() const
int GetWidthMils() const
Definition: page_info.h:130
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
PINSHEETLABEL_SHAPE GetShape() const
Definition: sch_text.h:161
static LIB_ARC * loadArc(std::unique_ptr< LIB_SYMBOL > &aSymbol, LINE_READER &aReader)
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
wxPoint GetEnd() const
std::shared_ptr< BUS_ALIAS > loadBusAlias(LINE_READER &aReader, SCH_SCREEN *aScreen)
Define a bezier curve graphic body item.
Definition: lib_bezier.h:34
const LIB_ID & GetLibId() const
Definition: sch_symbol.h:147
bool DeleteSymbolLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Delete an existing symbol library and returns true if successful, or if library does not exist return...
#define LIB_VERSION(major, minor)
wxPoint GetPosition() const override
Definition: lib_arc.h:71
wxPoint GetPosition() const override
virtual void SetCurrentProgress(double aProgress)=0
Set the progress value to aProgress (0..1).
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
wxPoint GetEndPoint() const
Definition: sch_line.h:93