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.

216 lines
7.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <wx/filename.h>
  25. #include <wx/uri.h>
  26. #include <config.h>
  27. #include <plugins/eagle/eagle_plugin.h>
  28. #include <plugins/geda/gpcb_plugin.h>
  29. #include <io_mgr.h>
  30. #include <plugins/kicad/pcb_plugin.h>
  31. #include <plugins/legacy/legacy_plugin.h>
  32. #include <plugins/pcad/pcad_plugin.h>
  33. #include <plugins/altium/altium_circuit_maker_plugin.h>
  34. #include <plugins/altium/altium_circuit_studio_plugin.h>
  35. #include <plugins/altium/altium_designer_plugin.h>
  36. #include <plugins/cadstar/cadstar_pcb_archive_plugin.h>
  37. #include <plugins/fabmaster/fabmaster_plugin.h>
  38. #include <wildcards_and_files_ext.h>
  39. #define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
  40. #define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
  41. // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
  42. // and code size. Until then, use the simplest method:
  43. // This implementation is one of two which could be done.
  44. // The other one would cater to DLL/DSO's. But since it would be nearly
  45. // impossible to link a KICAD type DLL/DSO right now without pulling in all
  46. // ::Draw() functions, I forgo that option temporarily.
  47. // Some day it may be possible to have some built in AND some DLL/DSO
  48. // plugins coexisting.
  49. PLUGIN* IO_MGR::PluginFind( PCB_FILE_T aFileType )
  50. {
  51. // This implementation is subject to change, any magic is allowed here.
  52. // The public IO_MGR API is the only pertinent public information.
  53. return PLUGIN_REGISTRY::Instance()->Create( aFileType );
  54. }
  55. void IO_MGR::PluginRelease( PLUGIN* aPlugin )
  56. {
  57. // This function is a place holder for a future point in time where
  58. // the plugin is a DLL/DSO. It could do reference counting, and then
  59. // unload the DLL/DSO when count goes to zero.
  60. delete aPlugin;
  61. }
  62. const wxString IO_MGR::ShowType( PCB_FILE_T aType )
  63. {
  64. const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
  65. for( const auto& plugin : plugins )
  66. {
  67. if ( plugin.m_type == aType )
  68. {
  69. return plugin.m_name;
  70. }
  71. }
  72. return wxString::Format( _( "UNKNOWN (%d)" ), aType );
  73. }
  74. IO_MGR::PCB_FILE_T IO_MGR::EnumFromStr( const wxString& aType )
  75. {
  76. const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
  77. for( const auto& plugin : plugins )
  78. {
  79. if ( plugin.m_name == aType )
  80. {
  81. return plugin.m_type;
  82. }
  83. }
  84. return PCB_FILE_T( -1 );
  85. }
  86. const wxString IO_MGR::GetFileExtension( PCB_FILE_T aFileType )
  87. {
  88. wxString ext = wxEmptyString;
  89. PLUGIN* plugin = PluginFind( aFileType );
  90. if( plugin != nullptr )
  91. {
  92. ext = plugin->GetFileExtension();
  93. PluginRelease( plugin );
  94. }
  95. return ext;
  96. }
  97. IO_MGR::PCB_FILE_T IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
  98. {
  99. PCB_FILE_T ret = KICAD_SEXP; // default guess, unless detected otherwise.
  100. wxFileName fn( aLibPath );
  101. if( fn.GetExt() == LegacyFootprintLibPathExtension )
  102. {
  103. ret = LEGACY;
  104. }
  105. else if( fn.GetExt() == GedaPcbFootprintLibFileExtension )
  106. {
  107. ret = GEDA_PCB;
  108. }
  109. else if( fn.GetExt() == EagleFootprintLibPathExtension )
  110. {
  111. ret = EAGLE;
  112. }
  113. else if( fn.GetExt() == AltiumFootprintLibPathExtension )
  114. {
  115. ret = ALTIUM_DESIGNER;
  116. }
  117. // Test this one anyways, even though it's the default guess, to avoid
  118. // the wxURI instantiation below.
  119. // We default ret to KICAD above, because somebody might have
  120. // mistakenly put a pretty library into a directory other than
  121. // *.pretty/ with *.kicad_mod in there., and I don't want to return -1,
  122. // since we only claimed to be guessing.
  123. //
  124. else if( fn.GetExt() == KiCadFootprintLibPathExtension )
  125. {
  126. ret = KICAD_SEXP;
  127. }
  128. return ret;
  129. }
  130. BOARD* IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aAppendToMe,
  131. const PROPERTIES* aProperties, PROJECT* aProject,
  132. PROGRESS_REPORTER* aProgressReporter )
  133. {
  134. // release the PLUGIN even if an exception is thrown.
  135. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  136. if( (PLUGIN*) pi ) // test pi->plugin
  137. {
  138. return pi->Load( aFileName, aAppendToMe, aProperties, aProject, aProgressReporter );
  139. }
  140. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  141. }
  142. void IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard,
  143. const PROPERTIES* aProperties )
  144. {
  145. // release the PLUGIN even if an exception is thrown.
  146. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  147. if( (PLUGIN*) pi ) // test pi->plugin
  148. {
  149. pi->Save( aFileName, aBoard, aProperties ); // virtual
  150. return;
  151. }
  152. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  153. }
  154. // These text strings are "truth" for identifying the plugins. If you change the spellings,
  155. // you will obsolete library tables, so don't do it. Additions are OK.
  156. static IO_MGR::REGISTER_PLUGIN registerEaglePlugin( IO_MGR::EAGLE, wxT("Eagle"),
  157. []() -> PLUGIN* { return new EAGLE_PLUGIN; } );
  158. static IO_MGR::REGISTER_PLUGIN registerKicadPlugin( IO_MGR::KICAD_SEXP,
  159. wxT("KiCad"), []() -> PLUGIN* { return new PCB_PLUGIN; } );
  160. static IO_MGR::REGISTER_PLUGIN registerPcadPlugin( IO_MGR::PCAD, wxT("P-Cad"),
  161. []() -> PLUGIN* { return new PCAD_PLUGIN; } );
  162. static IO_MGR::REGISTER_PLUGIN registerFabmasterPlugin( IO_MGR::FABMASTER, wxT( "Fabmaster" ),
  163. []() -> PLUGIN* { return new FABMASTER_PLUGIN; } );
  164. static IO_MGR::REGISTER_PLUGIN registerAltiumDesignerPlugin( IO_MGR::ALTIUM_DESIGNER,
  165. wxT( "Altium Designer" ), []() -> PLUGIN* { return new ALTIUM_DESIGNER_PLUGIN; } );
  166. static IO_MGR::REGISTER_PLUGIN registerAltiumCircuitStudioPlugin( IO_MGR::ALTIUM_CIRCUIT_STUDIO,
  167. wxT( "Altium Circuit Studio" ),
  168. []() -> PLUGIN* { return new ALTIUM_CIRCUIT_STUDIO_PLUGIN; } );
  169. static IO_MGR::REGISTER_PLUGIN registerAltiumCircuitMakerPlugin( IO_MGR::ALTIUM_CIRCUIT_MAKER,
  170. wxT( "Altium Circuit Maker" ),
  171. []() -> PLUGIN* { return new ALTIUM_CIRCUIT_MAKER_PLUGIN; } );
  172. static IO_MGR::REGISTER_PLUGIN registerCadstarArchivePlugin( IO_MGR::CADSTAR_PCB_ARCHIVE,
  173. wxT( "CADSTAR PCB Archive" ), []() -> PLUGIN* { return new CADSTAR_PCB_ARCHIVE_PLUGIN; } );
  174. static IO_MGR::REGISTER_PLUGIN registerLegacyPlugin( IO_MGR::LEGACY, wxT("Legacy"),
  175. []() -> PLUGIN* { return new LEGACY_PLUGIN; } );
  176. static IO_MGR::REGISTER_PLUGIN registerGPCBPlugin( IO_MGR::GEDA_PCB, wxT("GEDA/Pcb"),
  177. []() -> PLUGIN* { return new GPCB_PLUGIN; } );