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.

220 lines
8.1 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/altium/solidworks_pcb_plugin.h>
  37. #include <plugins/cadstar/cadstar_pcb_archive_plugin.h>
  38. #include <plugins/fabmaster/fabmaster_plugin.h>
  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 != nullptr )
  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. else if( fn.GetExt() == AltiumFootprintLibPathExtension )
  115. {
  116. ret = ALTIUM_DESIGNER;
  117. }
  118. // Test this one anyways, even though it's the default guess, to avoid
  119. // the wxURI instantiation below.
  120. // We default ret to KICAD above, because somebody might have
  121. // mistakenly put a pretty library into a directory other than
  122. // *.pretty/ with *.kicad_mod in there., and I don't want to return -1,
  123. // since we only claimed to be guessing.
  124. //
  125. else if( fn.GetExt() == KiCadFootprintLibPathExtension )
  126. {
  127. ret = KICAD_SEXP;
  128. }
  129. return ret;
  130. }
  131. BOARD* IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aAppendToMe,
  132. const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
  133. PROGRESS_REPORTER* aProgressReporter )
  134. {
  135. // release the PLUGIN even if an exception is thrown.
  136. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  137. if( (PLUGIN*) pi ) // test pi->plugin
  138. {
  139. return pi->Load( aFileName, aAppendToMe, aProperties, aProject, aProgressReporter );
  140. }
  141. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  142. }
  143. void IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard,
  144. const STRING_UTF8_MAP* aProperties )
  145. {
  146. // release the PLUGIN even if an exception is thrown.
  147. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  148. if( (PLUGIN*) pi ) // test pi->plugin
  149. {
  150. pi->Save( aFileName, aBoard, aProperties ); // virtual
  151. return;
  152. }
  153. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  154. }
  155. // These text strings are "truth" for identifying the plugins. If you change the spellings,
  156. // you will obsolete library tables, so don't do it. Additions are OK.
  157. static IO_MGR::REGISTER_PLUGIN registerEaglePlugin( IO_MGR::EAGLE, wxT("Eagle"),
  158. []() -> PLUGIN* { return new EAGLE_PLUGIN; } );
  159. static IO_MGR::REGISTER_PLUGIN registerKicadPlugin( IO_MGR::KICAD_SEXP,
  160. wxT("KiCad"), []() -> PLUGIN* { return new PCB_PLUGIN; } );
  161. static IO_MGR::REGISTER_PLUGIN registerPcadPlugin( IO_MGR::PCAD, wxT("P-Cad"),
  162. []() -> PLUGIN* { return new PCAD_PLUGIN; } );
  163. static IO_MGR::REGISTER_PLUGIN registerFabmasterPlugin( IO_MGR::FABMASTER, wxT( "Fabmaster" ),
  164. []() -> PLUGIN* { return new FABMASTER_PLUGIN; } );
  165. static IO_MGR::REGISTER_PLUGIN registerAltiumDesignerPlugin( IO_MGR::ALTIUM_DESIGNER,
  166. wxT( "Altium Designer" ), []() -> PLUGIN* { return new ALTIUM_DESIGNER_PLUGIN; } );
  167. static IO_MGR::REGISTER_PLUGIN registerAltiumCircuitStudioPlugin( IO_MGR::ALTIUM_CIRCUIT_STUDIO,
  168. wxT( "Altium Circuit Studio" ),
  169. []() -> PLUGIN* { return new ALTIUM_CIRCUIT_STUDIO_PLUGIN; } );
  170. static IO_MGR::REGISTER_PLUGIN registerAltiumCircuitMakerPlugin( IO_MGR::ALTIUM_CIRCUIT_MAKER,
  171. wxT( "Solidworks PCB" ),
  172. []() -> PLUGIN* { return new SOLIDWORKS_PCB_PLUGIN; } );
  173. static IO_MGR::REGISTER_PLUGIN registerSolidworksPCBPlugin( IO_MGR::SOLIDWORKS_PCB,
  174. wxT( "Altium Circuit Maker" ),
  175. []() -> PLUGIN* { return new ALTIUM_CIRCUIT_MAKER_PLUGIN; } );
  176. static IO_MGR::REGISTER_PLUGIN registerCadstarArchivePlugin( IO_MGR::CADSTAR_PCB_ARCHIVE,
  177. wxT( "CADSTAR PCB Archive" ), []() -> PLUGIN* { return new CADSTAR_PCB_ARCHIVE_PLUGIN; } );
  178. static IO_MGR::REGISTER_PLUGIN registerLegacyPlugin( IO_MGR::LEGACY, wxT("Legacy"),
  179. []() -> PLUGIN* { return new LEGACY_PLUGIN; } );
  180. static IO_MGR::REGISTER_PLUGIN registerGPCBPlugin( IO_MGR::GEDA_PCB, wxT("GEDA/Pcb"),
  181. []() -> PLUGIN* { return new GPCB_PLUGIN; } );