You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.4 KiB

  1. #ifndef _SCH_LEGACY_PLUGIN_H_
  2. #define _SCH_LEGACY_PLUGIN_H_
  3. /*
  4. * This program source code file is part of KiCad, a free EDA CAD application.
  5. *
  6. * Copyright (C) 2016 CERN
  7. * Copyright (C) 2016-2019 KiCad Developers, see change_log.txt for contributors.
  8. *
  9. * @author Wayne Stambaugh <stambaughw@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <memory>
  25. #include <sch_io_mgr.h>
  26. #include <stack>
  27. #include <general.h> // for EESCHEMA_VERSION definition
  28. class KIWAY;
  29. class LINE_READER;
  30. class SCH_SCREEN;
  31. class SCH_SHEET;
  32. class SCH_BITMAP;
  33. class SCH_JUNCTION;
  34. class SCH_NO_CONNECT;
  35. class SCH_LINE;
  36. class SCH_BUS_ENTRY_BASE;
  37. class SCH_TEXT;
  38. class SCH_COMPONENT;
  39. class SCH_FIELD;
  40. class PROPERTIES;
  41. class SELECTION;
  42. class SCH_LEGACY_PLUGIN_CACHE;
  43. class LIB_PART;
  44. class PART_LIB;
  45. class BUS_ALIAS;
  46. /**
  47. * A #SCH_PLUGIN derivation for loading schematic files created before the new s-expression
  48. * file format.
  49. *
  50. * The legacy parser and formatter attempt to be compatible with the legacy file format.
  51. * The original parser was very forgiving in that it would parse only part of a keyword.
  52. * So "$C", "$Co", and "$Com" could be used for "$Comp" and the old parser would allow
  53. * this. This parser is not that forgiving and sticks to the legacy file format document.
  54. *
  55. * As with all SCH_PLUGINs there is no UI dependencies i.e. windowing calls allowed.
  56. */
  57. class SCH_LEGACY_PLUGIN : public SCH_PLUGIN
  58. {
  59. public:
  60. SCH_LEGACY_PLUGIN();
  61. virtual ~SCH_LEGACY_PLUGIN();
  62. const wxString GetName() const override
  63. {
  64. return wxT( "Eeschema-Legacy" );
  65. }
  66. const wxString GetFileExtension() const override
  67. {
  68. return wxT( "sch" );
  69. }
  70. const wxString GetLibraryFileExtension() const override
  71. {
  72. return wxT( "lib" );
  73. }
  74. /**
  75. * The property used internally by the plugin to enable cache buffering which prevents
  76. * the library file from being written every time the cache is changed. This is useful
  77. * when writing the schematic cache library file or saving a library to a new file name.
  78. */
  79. static const char* PropBuffering;
  80. /**
  81. * The property used internally by the plugin to disable writing the library
  82. * documentation (.dcm) file when saving the library cache.
  83. */
  84. static const char* PropNoDocFile;
  85. int GetModifyHash() const override;
  86. SCH_SHEET* Load( const wxString& aFileName, SCHEMATIC* aSchematic,
  87. SCH_SHEET* aAppendToMe = nullptr,
  88. const PROPERTIES* aProperties = nullptr ) override;
  89. void LoadContent( LINE_READER& aReader, SCH_SCREEN* aScreen,
  90. int version = EESCHEMA_VERSION );
  91. void Save( const wxString& aFileName, SCH_SHEET* aScreen, SCHEMATIC* aSchematic,
  92. const PROPERTIES* aProperties = nullptr ) override;
  93. void Format( SCH_SHEET* aSheet );
  94. void Format( SELECTION* aSelection, OUTPUTFORMATTER* aFormatter );
  95. void EnumerateSymbolLib( wxArrayString& aSymbolNameList,
  96. const wxString& aLibraryPath,
  97. const PROPERTIES* aProperties = nullptr ) override;
  98. void EnumerateSymbolLib( std::vector<LIB_PART*>& aSymbolList,
  99. const wxString& aLibraryPath,
  100. const PROPERTIES* aProperties = nullptr ) override;
  101. LIB_PART* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
  102. const PROPERTIES* aProperties = nullptr ) override;
  103. void SaveSymbol( const wxString& aLibraryPath, const LIB_PART* aSymbol,
  104. const PROPERTIES* aProperties = nullptr ) override;
  105. void DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
  106. const PROPERTIES* aProperties = nullptr ) override;
  107. void CreateSymbolLib( const wxString& aLibraryPath,
  108. const PROPERTIES* aProperties = nullptr ) override;
  109. bool DeleteSymbolLib( const wxString& aLibraryPath,
  110. const PROPERTIES* aProperties = nullptr ) override;
  111. void SaveLibrary( const wxString& aLibraryPath,
  112. const PROPERTIES* aProperties = nullptr ) override;
  113. bool CheckHeader( const wxString& aFileName ) override;
  114. bool IsSymbolLibWritable( const wxString& aLibraryPath ) override;
  115. const wxString& GetError() const override { return m_error; }
  116. static LIB_PART* ParsePart( LINE_READER& aReader, int majorVersion = 0, int minorVersion = 0 );
  117. static void FormatPart( LIB_PART* aPart, OUTPUTFORMATTER& aFormatter );
  118. private:
  119. void loadHierarchy( SCH_SHEET* aSheet );
  120. void loadHeader( LINE_READER& aReader, SCH_SCREEN* aScreen );
  121. void loadPageSettings( LINE_READER& aReader, SCH_SCREEN* aScreen );
  122. void loadFile( const wxString& aFileName, SCH_SCREEN* aScreen );
  123. SCH_SHEET* loadSheet( LINE_READER& aReader );
  124. SCH_BITMAP* loadBitmap( LINE_READER& aReader );
  125. SCH_JUNCTION* loadJunction( LINE_READER& aReader );
  126. SCH_NO_CONNECT* loadNoConnect( LINE_READER& aReader );
  127. SCH_LINE* loadWire( LINE_READER& aReader );
  128. SCH_BUS_ENTRY_BASE* loadBusEntry( LINE_READER& aReader );
  129. SCH_TEXT* loadText( LINE_READER& aReader );
  130. SCH_COMPONENT* loadComponent( LINE_READER& aReader );
  131. std::shared_ptr<BUS_ALIAS> loadBusAlias( LINE_READER& aReader, SCH_SCREEN* aScreen );
  132. void saveComponent( SCH_COMPONENT* aComponent );
  133. void saveField( SCH_FIELD* aField );
  134. void saveBitmap( SCH_BITMAP* aBitmap );
  135. void saveSheet( SCH_SHEET* aSheet );
  136. void saveJunction( SCH_JUNCTION* aJunction );
  137. void saveNoConnect( SCH_NO_CONNECT* aNoConnect );
  138. void saveBusEntry( SCH_BUS_ENTRY_BASE* aBusEntry );
  139. void saveLine( SCH_LINE* aLine );
  140. void saveText( SCH_TEXT* aText );
  141. void saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias );
  142. void cacheLib( const wxString& aLibraryFileName );
  143. bool writeDocFile( const PROPERTIES* aProperties );
  144. bool isBuffering( const PROPERTIES* aProperties );
  145. protected:
  146. int m_version; ///< Version of file being loaded.
  147. /** For throwing exceptions or errors on partial schematic loads. */
  148. wxString m_error;
  149. wxString m_path; ///< Root project path for loading child sheets.
  150. std::stack<wxString> m_currentPath;///< Stack to maintain nested sheet paths
  151. const PROPERTIES* m_props; ///< Passed via Save() or Load(), no ownership, may be nullptr.
  152. SCH_SHEET* m_rootSheet; ///< The root sheet of the schematic being loaded..
  153. OUTPUTFORMATTER* m_out; ///< The output formatter for saving SCH_SCREEN objects.
  154. SCH_LEGACY_PLUGIN_CACHE* m_cache;
  155. SCHEMATIC* m_schematic; ///< Passed to Load(), the schematic object being loaded
  156. /// initialize PLUGIN like a constructor would.
  157. void init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties = nullptr );
  158. };
  159. #endif // _SCH_LEGACY_PLUGIN_H_