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.

222 lines
7.6 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 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 <altium2kicadpcb_plugin/altium_circuit_maker_plugin.h>
  27. #include <altium2kicadpcb_plugin/altium_circuit_studio_plugin.h>
  28. #include <altium2kicadpcb_plugin/altium_designer_plugin.h>
  29. #include <config.h>
  30. #include <eagle_plugin.h>
  31. #include <gpcb_plugin.h>
  32. #include <io_mgr.h>
  33. #include <kicad_plugin.h>
  34. #include <legacy_plugin.h>
  35. #include <pcad2kicadpcb_plugin/pcad_plugin.h>
  36. #if defined(BUILD_GITHUB_PLUGIN)
  37. #include <github/github_plugin.h>
  38. #endif
  39. #include <wildcards_and_files_ext.h>
  40. #define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
  41. #define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
  42. // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
  43. // and code size. Until then, use the simplest method:
  44. // This implementation is one of two which could be done.
  45. // The other one would cater to DLL/DSO's. But since it would be nearly
  46. // impossible to link a KICAD type DLL/DSO right now without pulling in all
  47. // ::Draw() functions, I forgo that option temporarily.
  48. // Some day it may be possible to have some built in AND some DLL/DSO
  49. // plugins coexisting.
  50. PLUGIN* IO_MGR::PluginFind( PCB_FILE_T aFileType )
  51. {
  52. // This implementation is subject to change, any magic is allowed here.
  53. // The public IO_MGR API is the only pertinent public information.
  54. return PLUGIN_REGISTRY::Instance()->Create( aFileType );
  55. }
  56. void IO_MGR::PluginRelease( PLUGIN* aPlugin )
  57. {
  58. // This function is a place holder for a future point in time where
  59. // the plugin is a DLL/DSO. It could do reference counting, and then
  60. // unload the DLL/DSO when count goes to zero.
  61. delete aPlugin;
  62. }
  63. const wxString IO_MGR::ShowType( PCB_FILE_T aType )
  64. {
  65. const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
  66. for( const auto& plugin : plugins )
  67. {
  68. if ( plugin.m_type == aType )
  69. {
  70. return plugin.m_name;
  71. }
  72. }
  73. return wxString::Format( _( "UNKNOWN (%d)" ), aType );
  74. }
  75. IO_MGR::PCB_FILE_T IO_MGR::EnumFromStr( const wxString& aType )
  76. {
  77. const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
  78. for( const auto& plugin : plugins )
  79. {
  80. if ( plugin.m_name == aType )
  81. {
  82. return plugin.m_type;
  83. }
  84. }
  85. return PCB_FILE_T( -1 );
  86. }
  87. const wxString IO_MGR::GetFileExtension( PCB_FILE_T aFileType )
  88. {
  89. wxString ext = wxEmptyString;
  90. PLUGIN* plugin = PluginFind( aFileType );
  91. if( plugin != NULL )
  92. {
  93. ext = plugin->GetFileExtension();
  94. PluginRelease( plugin );
  95. }
  96. return ext;
  97. }
  98. IO_MGR::PCB_FILE_T IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
  99. {
  100. PCB_FILE_T ret = KICAD_SEXP; // default guess, unless detected otherwise.
  101. wxFileName fn( aLibPath );
  102. if( fn.GetExt() == LegacyFootprintLibPathExtension )
  103. {
  104. ret = LEGACY;
  105. }
  106. else if( fn.GetExt() == GedaPcbFootprintLibFileExtension )
  107. {
  108. ret = GEDA_PCB;
  109. }
  110. else if( fn.GetExt() == EagleFootprintLibPathExtension )
  111. {
  112. ret = EAGLE;
  113. }
  114. // Test this one anyways, even though it's the default guess, to avoid
  115. // the wxURI instantiation below.
  116. // We default ret to KICAD above, because somebody might have
  117. // mistakenly put a pretty library into a directory other than
  118. // *.pretty/ with *.kicad_mod in there., and I don't want to return -1,
  119. // since we only claimed to be guessing.
  120. //
  121. // However libraries on GitHub have names ending by .pretty
  122. // so test also this is not a name starting by http (including https).
  123. else if( fn.GetExt() == KiCadFootprintLibPathExtension &&
  124. !aLibPath.StartsWith( wxT( "http" ) ) )
  125. {
  126. ret = KICAD_SEXP;
  127. }
  128. else
  129. {
  130. #if defined(BUILD_GITHUB_PLUGIN)
  131. // There is no extension for a remote repo, so test the server name.
  132. wxURI uri( aLibPath );
  133. if( uri.HasServer() && uri.GetServer() == wxT( "github.com" ) )
  134. {
  135. ret = GITHUB;
  136. }
  137. #endif
  138. }
  139. return ret;
  140. }
  141. BOARD* IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName,
  142. BOARD* aAppendToMe, const PROPERTIES* aProperties )
  143. {
  144. // release the PLUGIN even if an exception is thrown.
  145. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  146. if( (PLUGIN*) pi ) // test pi->plugin
  147. {
  148. return pi->Load( aFileName, aAppendToMe, aProperties ); // virtual
  149. }
  150. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  151. }
  152. void IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
  153. {
  154. // release the PLUGIN even if an exception is thrown.
  155. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  156. if( (PLUGIN*) pi ) // test pi->plugin
  157. {
  158. pi->Save( aFileName, aBoard, aProperties ); // virtual
  159. return;
  160. }
  161. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  162. }
  163. // These text strings are "truth" for identifying the plugins. If you change the spellings,
  164. // you will obsolete library tables, so don't do it. Additions are OK.
  165. static IO_MGR::REGISTER_PLUGIN registerEaglePlugin( IO_MGR::EAGLE, wxT("Eagle"), []() -> PLUGIN* { return new EAGLE_PLUGIN; } );
  166. static IO_MGR::REGISTER_PLUGIN registerKicadPlugin( IO_MGR::KICAD_SEXP, wxT("KiCad"), []() -> PLUGIN* { return new PCB_IO; } );
  167. static IO_MGR::REGISTER_PLUGIN registerPcadPlugin( IO_MGR::PCAD, wxT("P-Cad"), []() -> PLUGIN* { return new PCAD_PLUGIN; } );
  168. static IO_MGR::REGISTER_PLUGIN registerAltiumDesignerPlugin( IO_MGR::ALTIUM_DESIGNER,
  169. wxT( "Altium Designer" ), []() -> PLUGIN* { return new ALTIUM_DESIGNER_PLUGIN; } );
  170. static IO_MGR::REGISTER_PLUGIN registerAltiumCircuitStudioPlugin( IO_MGR::ALTIUM_CIRCUIT_STUDIO,
  171. wxT( "Altium Circuit Studio" ),
  172. []() -> PLUGIN* { return new ALTIUM_CIRCUIT_STUDIO_PLUGIN; } );
  173. static IO_MGR::REGISTER_PLUGIN registerAltiumCircuitMakerPlugin( IO_MGR::ALTIUM_CIRCUIT_MAKER,
  174. wxT( "Altium Circuit Maker" ),
  175. []() -> PLUGIN* { return new ALTIUM_CIRCUIT_MAKER_PLUGIN; } );
  176. #ifdef BUILD_GITHUB_PLUGIN
  177. static IO_MGR::REGISTER_PLUGIN registerGithubPlugin( IO_MGR::GITHUB, wxT("Github"), []() -> PLUGIN* { return new GITHUB_PLUGIN; } );
  178. #endif /* BUILD_GITHUB_PLUGIN */
  179. static IO_MGR::REGISTER_PLUGIN registerLegacyPlugin( IO_MGR::LEGACY, wxT("Legacy"), []() -> PLUGIN* { return new LEGACY_PLUGIN; } );
  180. static IO_MGR::REGISTER_PLUGIN registerGPCBPlugin( IO_MGR::GEDA_PCB, wxT("GEDA/Pcb"), []() -> PLUGIN* { return new GPCB_PLUGIN; } );