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