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.

205 lines
6.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 CERN.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef KICAD_PLUGIN_H_
  24. #define KICAD_PLUGIN_H_
  25. #include <io_mgr.h>
  26. #include <string>
  27. #include <layers_id_colors_and_visibility.h>
  28. class BOARD;
  29. class BOARD_ITEM;
  30. class FP_CACHE;
  31. class PCB_PARSER;
  32. /// Current s-expression file format version. 2 was the last legacy format version.
  33. #define SEXPR_BOARD_FILE_VERSION 3
  34. #define CTL_STD_LAYER_NAMES (1 << 0) ///< Use English Standard layer names
  35. #define CTL_OMIT_NETS (1 << 1)
  36. #define CTL_OMIT_TSTAMPS (1 << 2)
  37. #define CTL_OMIT_INITIAL_COMMENTS (1 << 3) ///< omit MODULE initial comments
  38. // common combinations of the above:
  39. /// Format output for the clipboard instead of footprint library or BOARD
  40. #define CTL_FOR_CLIPBOARD (CTL_STD_LAYER_NAMES|CTL_OMIT_NETS)
  41. /// Format output for a footprint library instead of clipboard or BOARD
  42. #define CTL_FOR_LIBRARY (CTL_STD_LAYER_NAMES|CTL_OMIT_NETS|CTL_OMIT_TSTAMPS)
  43. /// The zero arg constructor when PCB_IO is used for PLUGIN::Load() and PLUGIN::Save()ing
  44. /// a BOARD file underneath IO_MGR.
  45. #define CTL_FOR_BOARD (CTL_OMIT_INITIAL_COMMENTS)
  46. /**
  47. * Class PCB_IO
  48. * is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
  49. *
  50. * @note This class is not thread safe, but it is re-entrant multiple times in sequence.
  51. */
  52. class PCB_IO : public PLUGIN
  53. {
  54. friend class FP_CACHE;
  55. public:
  56. //-----<PLUGIN API>---------------------------------------------------------
  57. const wxString& PluginName() const
  58. {
  59. static const wxString name = wxT( "KiCad" );
  60. return name;
  61. }
  62. const wxString& GetFileExtension() const
  63. {
  64. // Would have used wildcards_and_files_ext.cpp's KiCadPcbFileExtension,
  65. // but to be pure, a plugin should not assume that it will always be linked
  66. // with the core of the pcbnew code. (Might someday be a DLL/DSO.) Besides,
  67. // file extension policy should be controlled by the plugin.
  68. static const wxString extension = wxT( "kicad_pcb" );
  69. return extension;
  70. }
  71. void Save( const wxString& aFileName, BOARD* aBoard,
  72. PROPERTIES* aProperties = NULL ); // overload
  73. BOARD* Load( const wxString& aFileName, BOARD* aAppendToMe, PROPERTIES* aProperties = NULL );
  74. wxArrayString FootprintEnumerate( const wxString& aLibraryPath, PROPERTIES* aProperties = NULL);
  75. MODULE* FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
  76. PROPERTIES* aProperties = NULL );
  77. void FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint,
  78. PROPERTIES* aProperties = NULL );
  79. void FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName );
  80. void FootprintLibCreate( const wxString& aLibraryPath, PROPERTIES* aProperties = NULL);
  81. bool FootprintLibDelete( const wxString& aLibraryPath, PROPERTIES* aProperties = NULL );
  82. bool IsFootprintLibWritable( const wxString& aLibraryPath );
  83. //-----</PLUGIN API>--------------------------------------------------------
  84. PCB_IO();
  85. PCB_IO( int aControlFlags );
  86. ~PCB_IO();
  87. /**
  88. * Function Format
  89. * outputs \a aItem to \a aFormatter in s-expression format.
  90. *
  91. * @param aItem A pointer the an #BOARD_ITEM object to format.
  92. * @param aNestLevel The indentation nest level.
  93. * @throw IO_ERROR on write error.
  94. */
  95. void Format( BOARD_ITEM* aItem, int aNestLevel = 0 ) const
  96. throw( IO_ERROR );
  97. std::string GetStringOutput( bool doClear )
  98. {
  99. std::string ret = m_sf.GetString();
  100. if( doClear )
  101. m_sf.Clear();
  102. return ret;
  103. }
  104. void SetOutputFormatter( OUTPUTFORMATTER* aFormatter ) { m_out = aFormatter; }
  105. BOARD_ITEM* Parse( const wxString& aClipboardSourceInput )
  106. throw( PARSE_ERROR, IO_ERROR );
  107. protected:
  108. wxString m_error; ///< for throwing exceptions
  109. BOARD* m_board; ///< which BOARD, no ownership here
  110. PROPERTIES* m_props; ///< passed via Save() or Load(), no ownership, may be NULL.
  111. FP_CACHE* m_cache; ///< Footprint library cache.
  112. LINE_READER* m_reader; ///< no ownership here.
  113. wxString m_filename; ///< for saves only, name is in m_reader for loads
  114. int m_loading_format_version; ///< which #SEXPR_BOARD_FILE_VERSION should be Load()ed?
  115. STRING_FORMATTER m_sf;
  116. OUTPUTFORMATTER* m_out; ///< output any Format()s to this, no ownership
  117. int m_ctl;
  118. PCB_PARSER* m_parser;
  119. private:
  120. void format( BOARD* aBoard, int aNestLevel = 0 ) const
  121. throw( IO_ERROR );
  122. void format( DIMENSION* aDimension, int aNestLevel = 0 ) const
  123. throw( IO_ERROR );
  124. void format( EDGE_MODULE* aModuleDrawing, int aNestLevel = 0 ) const
  125. throw( IO_ERROR );
  126. void format( DRAWSEGMENT* aSegment, int aNestLevel = 0 ) const
  127. throw( IO_ERROR );
  128. void format( PCB_TARGET* aTarget, int aNestLevel = 0 ) const
  129. throw( IO_ERROR );
  130. void format( MODULE* aModule, int aNestLevel = 0 ) const
  131. throw( IO_ERROR );
  132. void format( D_PAD* aPad, int aNestLevel = 0 ) const
  133. throw( IO_ERROR );
  134. void format( TEXTE_PCB* aText, int aNestLevel = 0 ) const
  135. throw( IO_ERROR );
  136. void format( TEXTE_MODULE* aText, int aNestLevel = 0 ) const
  137. throw( IO_ERROR );
  138. void format( TRACK* aTrack, int aNestLevel = 0 ) const
  139. throw( IO_ERROR );
  140. void format( ZONE_CONTAINER* aZone, int aNestLevel = 0 ) const
  141. throw( IO_ERROR );
  142. void formatLayer( const BOARD_ITEM* aItem ) const;
  143. void formatLayers( LAYER_MSK aLayerMask, int aNestLevel = 0 ) const
  144. throw( IO_ERROR );
  145. /// we only cache one footprint library for now, this determines which one.
  146. void cacheLib( const wxString& aLibraryPath );
  147. void init( PROPERTIES* aProperties );
  148. };
  149. #endif // KICAD_PLUGIN_H_