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.

1137 lines
34 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
18 years ago
18 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  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. /**
  24. * @file librairi.cpp
  25. * @brief Manage module (footprint) libraries.
  26. */
  27. #include <wx/ffile.h>
  28. #include <wx/stdpaths.h>
  29. #include <fctsys.h>
  30. #include <pgm_base.h>
  31. #include <kiface_i.h>
  32. #include <class_drawpanel.h>
  33. #include <confirm.h>
  34. #include <kicad_string.h>
  35. #include <gestfich.h>
  36. #include <pcb_edit_frame.h>
  37. #include <dialog_helpers.h>
  38. #include <filter_reader.h>
  39. #include <macros.h>
  40. #include <fp_lib_table.h>
  41. #include <validators.h>
  42. #include <dialog_text_entry.h>
  43. #include <tool/tool_manager.h>
  44. #include <tools/pcb_actions.h>
  45. #include <class_board.h>
  46. #include <class_module.h>
  47. #include <board_commit.h>
  48. #include <pcbnew.h>
  49. #include <footprint_edit_frame.h>
  50. #include <wildcards_and_files_ext.h>
  51. #include <kicad_plugin.h>
  52. #include <legacy_plugin.h>
  53. #include <env_paths.h>
  54. #include "footprint_viewer_frame.h"
  55. // unique, "file local" translations:
  56. #define FMT_OK_DELETE _( "OK to delete footprint \"%s\" in library \"%s\"" )
  57. #define FMT_IMPORT_MODULE _( "Import Footprint" )
  58. #define FMT_FILE_NOT_FOUND _( "File \"%s\" not found" )
  59. #define FMT_NOT_MODULE _( "Not a footprint file" )
  60. #define FMT_MOD_NOT_FOUND _( "Unable to find or load footprint \"%s\" from lib path \"%s\"" )
  61. #define FMT_LIB_READ_ONLY _( "Library \"%s\" is read only, not writable" )
  62. #define FMT_EXPORT_MODULE _( "Export Footprint" )
  63. #define FMT_SAVE_MODULE _( "Save Footprint" )
  64. #define FMT_MOD_REF _( "Enter footprint name:" )
  65. #define FMT_EXPORTED _( "Footprint exported to file \"%s\"" )
  66. #define FMT_MOD_DELETED _( "Footprint \"%s\" deleted from library \"%s\"" )
  67. #define FMT_MOD_CREATE _( "New Footprint" )
  68. #define FMT_NO_REF_ABORTED _( "No footprint name defined." )
  69. #define FMT_SELECT_LIB _( "Select Library" )
  70. static const wxString INFO_LEGACY_LIB_WARN_EDIT(
  71. _( "Writing/modifying legacy libraries (.mod files) is not allowed\n"\
  72. "Please save the current library to the new .pretty format\n"\
  73. "and update your footprint lib table\n"\
  74. "to save your footprint (a .kicad_mod file) in the .pretty library folder" ) );
  75. static const wxString INFO_LEGACY_LIB_WARN_DELETE(
  76. _( "Modifying legacy libraries (.mod files) is not allowed\n"\
  77. "Please save the current library under the new .pretty format\n"\
  78. "and update your footprint lib table\n"\
  79. "before deleting a footprint" ) );
  80. #define EXPORT_IMPORT_LASTPATH_KEY wxT( "import_last_path" )
  81. /**
  82. * Prompt the user for a module file to open.
  83. * @param aParent - parent window for the dialog
  84. * @param aLastPath - last opened path
  85. */
  86. static wxFileName getFootprintFilenameFromUser( wxWindow* aParent, const wxString& aLastPath )
  87. {
  88. static int lastFilterIndex = 0; // To store the last choice during a session.
  89. wxString wildCard;
  90. wildCard << KiCadFootprintLibFileWildcard() << wxChar( '|' )
  91. << ModLegacyExportFileWildcard() << wxChar( '|' )
  92. << GedaPcbFootprintLibFileWildcard() << wxChar( '|' )
  93. << AllFilesWildcard();
  94. wxFileDialog dlg( aParent, FMT_IMPORT_MODULE, aLastPath, wxEmptyString, wildCard,
  95. wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  96. dlg.SetFilterIndex( lastFilterIndex );
  97. if( dlg.ShowModal() == wxID_CANCEL )
  98. return wxFileName();
  99. lastFilterIndex = dlg.GetFilterIndex();
  100. return wxFileName( dlg.GetPath() );
  101. }
  102. /**
  103. * Read a file to detect the type.
  104. * @param aFile - open file to be read. File pointer will be closed.
  105. * @param aFileName - file name to be read
  106. * @param aName - wxString to receive the module name iff type is LEGACY
  107. */
  108. static IO_MGR::PCB_FILE_T detect_file_type( FILE* aFile, const wxFileName& aFileName,
  109. wxString* aName )
  110. {
  111. FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
  112. WHITESPACE_FILTER_READER reader( freader );
  113. IO_MGR::PCB_FILE_T file_type;
  114. wxASSERT( aName );
  115. reader.ReadLine();
  116. char* line = reader.Line();
  117. if( !strncasecmp( line, "(module", strlen( "(module" ) ) )
  118. {
  119. file_type = IO_MGR::KICAD_SEXP;
  120. *aName = aFileName.GetName();
  121. }
  122. else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
  123. {
  124. file_type = IO_MGR::LEGACY;
  125. while( reader.ReadLine() )
  126. {
  127. if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
  128. {
  129. *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
  130. break;
  131. }
  132. }
  133. }
  134. else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
  135. {
  136. file_type = IO_MGR::GEDA_PCB;
  137. *aName = aFileName.GetName();
  138. }
  139. else
  140. {
  141. file_type = IO_MGR::FILE_TYPE_NONE;
  142. }
  143. return file_type;
  144. }
  145. /**
  146. * Parse a footprint using a PLUGIN.
  147. * @param aFileName - file name to parse
  148. * @param aFileType - type of the file
  149. * @param aName - name of the footprint
  150. */
  151. static MODULE* parse_module_with_plugin(
  152. const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
  153. const wxString& aName )
  154. {
  155. wxString path;
  156. switch( aFileType )
  157. {
  158. case IO_MGR::GEDA_PCB:
  159. path = aFileName.GetPath();
  160. break;
  161. case IO_MGR::LEGACY:
  162. path = aFileName.GetFullPath();
  163. break;
  164. default:
  165. wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
  166. }
  167. PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
  168. return pi->FootprintLoad( path, aName );
  169. }
  170. /**
  171. * Parse a KICAD footprint.
  172. * @param aFileName - file name to parse
  173. */
  174. static MODULE* parse_module_kicad( const wxFileName& aFileName )
  175. {
  176. wxString fcontents;
  177. PCB_IO pcb_io;
  178. wxFFile f( aFileName.GetFullPath() );
  179. if( !f.IsOpened() )
  180. return NULL;
  181. f.ReadAll( &fcontents );
  182. return dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
  183. }
  184. /**
  185. * Try to load a footprint, returning NULL if the file couldn't be accessed.
  186. * @param aFileName - file name to load
  187. * @param aFileType - type of the file to load
  188. * @param aName - footprint name
  189. */
  190. MODULE* try_load_footprint( const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
  191. const wxString& aName )
  192. {
  193. MODULE* module;
  194. switch( aFileType )
  195. {
  196. case IO_MGR::GEDA_PCB:
  197. case IO_MGR::LEGACY:
  198. module = parse_module_with_plugin( aFileName, aFileType, aName );
  199. break;
  200. case IO_MGR::KICAD_SEXP:
  201. module = parse_module_kicad( aFileName );
  202. break;
  203. default:
  204. wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
  205. module = NULL;
  206. }
  207. return module;
  208. }
  209. MODULE* FOOTPRINT_EDIT_FRAME::Import_Module( const wxString& aName )
  210. {
  211. wxString lastOpenedPathForLoading = m_mruPath;
  212. wxConfigBase* cfg = Kiface().KifaceSettings();
  213. if( cfg )
  214. cfg->Read( EXPORT_IMPORT_LASTPATH_KEY, &lastOpenedPathForLoading );
  215. wxFileName fn;
  216. if( aName != wxT("") )
  217. fn = aName;
  218. else
  219. fn = getFootprintFilenameFromUser( this, lastOpenedPathForLoading );
  220. if( !fn.IsOk() )
  221. return NULL;
  222. FILE* fp = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
  223. if( !fp )
  224. {
  225. wxString msg = wxString::Format( FMT_FILE_NOT_FOUND, GetChars( fn.GetFullPath() ) );
  226. DisplayError( this, msg );
  227. return NULL;
  228. }
  229. if( cfg ) // Save file path
  230. {
  231. lastOpenedPathForLoading = fn.GetPath();
  232. cfg->Write( EXPORT_IMPORT_LASTPATH_KEY, lastOpenedPathForLoading );
  233. }
  234. wxString moduleName;
  235. IO_MGR::PCB_FILE_T fileType = detect_file_type( fp, fn.GetFullPath(), &moduleName );
  236. if( fileType == IO_MGR::FILE_TYPE_NONE )
  237. {
  238. DisplayError( this, FMT_NOT_MODULE );
  239. return NULL;
  240. }
  241. MODULE* module = NULL;
  242. try
  243. {
  244. module = try_load_footprint( fn, fileType, moduleName );
  245. if( !module )
  246. {
  247. wxString msg = wxString::Format(
  248. FMT_MOD_NOT_FOUND, GetChars( moduleName ), GetChars( fn.GetFullPath() ) );
  249. DisplayError( this, msg );
  250. return NULL;
  251. }
  252. }
  253. catch( const IO_ERROR& ioe )
  254. {
  255. DisplayError( this, ioe.What() );
  256. // if the footprint is not loaded, exit.
  257. // However, even if an error happens, it can be loaded, because in KICAD and GPCB format,
  258. // a fp library is a set of separate files, and the error(s) are not necessary when
  259. // reading the selected file
  260. if( !module )
  261. return NULL;
  262. }
  263. module->SetFPID( LIB_ID( wxEmptyString, moduleName ) );
  264. // Insert footprint in list
  265. AddModuleToBoard( module );
  266. // Display info :
  267. SetMsgPanel( module );
  268. PlaceModule( module, NULL );
  269. if( IsGalCanvasActive() )
  270. module->SetPosition( wxPoint( 0, 0 ) );
  271. GetBoard()->m_Status_Pcb = 0;
  272. GetBoard()->BuildListOfNets();
  273. updateView();
  274. return module;
  275. }
  276. void FOOTPRINT_EDIT_FRAME::Export_Module( MODULE* aModule )
  277. {
  278. wxFileName fn;
  279. wxConfigBase* cfg = Kiface().KifaceSettings();
  280. if( !aModule )
  281. return;
  282. fn.SetName( aModule->GetFPID().GetLibItemName() );
  283. wxString wildcard = KiCadFootprintLibFileWildcard();
  284. fn.SetExt( KiCadFootprintFileExtension );
  285. if( cfg )
  286. {
  287. wxString path;
  288. cfg->Read( EXPORT_IMPORT_LASTPATH_KEY, &path, m_mruPath );
  289. fn.SetPath( path );
  290. }
  291. wxFileDialog dlg( this, FMT_EXPORT_MODULE, fn.GetPath(), fn.GetFullName(),
  292. wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  293. if( dlg.ShowModal() == wxID_CANCEL )
  294. return;
  295. fn = dlg.GetPath();
  296. if( cfg ) // Save file path
  297. {
  298. cfg->Write( EXPORT_IMPORT_LASTPATH_KEY, fn.GetPath() );
  299. }
  300. try
  301. {
  302. // Export as *.kicad_pcb format, using a strategy which is specifically chosen
  303. // as an example on how it could also be used to send it to the system clipboard.
  304. PCB_IO pcb_io( CTL_FOR_LIBRARY );
  305. /* This module should *already* be "normalized" in a way such that
  306. orientation is zero, etc., since it came from module editor.
  307. module->SetTimeStamp( 0 );
  308. module->SetParent( 0 );
  309. module->SetOrientation( 0 );
  310. */
  311. pcb_io.Format( aModule );
  312. FILE* fp = wxFopen( dlg.GetPath(), wxT( "wt" ) );
  313. if( fp == NULL )
  314. {
  315. wxMessageBox( wxString::Format(
  316. _( "Unable to create or write file \"%s\"" ),
  317. GetChars( dlg.GetPath() ) ) );
  318. return;
  319. }
  320. fprintf( fp, "%s", pcb_io.GetStringOutput( false ).c_str() );
  321. fclose( fp );
  322. }
  323. catch( const IO_ERROR& ioe )
  324. {
  325. DisplayError( this, ioe.What() );
  326. return;
  327. }
  328. wxString msg = wxString::Format( FMT_EXPORTED, GetChars( dlg.GetPath() ) );
  329. DisplayInfoMessage( this, msg );
  330. }
  331. wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary(const wxString& aLibName )
  332. {
  333. // Kicad cannot write legacy format libraries, only .pretty new format
  334. // because the legacy format cannot handle current features.
  335. // The footprint library is actually a directory
  336. wxString initialPath = wxPathOnly( Prj().GetProjectFullName() );
  337. wxFileName fn;
  338. bool doAdd = false;
  339. if( aLibName.IsEmpty() )
  340. {
  341. fn = initialPath;
  342. if( !LibraryFileBrowser( false, fn,
  343. KiCadFootprintLibPathWildcard(), KiCadFootprintLibPathExtension) )
  344. {
  345. return wxEmptyString;
  346. }
  347. doAdd = true;
  348. }
  349. else
  350. {
  351. fn = aLibName;
  352. if( !fn.IsAbsolute() )
  353. {
  354. fn.SetName( aLibName );
  355. fn.MakeAbsolute( initialPath );
  356. }
  357. // Enforce the .pretty extension:
  358. fn.SetExt( KiCadFootprintLibPathExtension );
  359. }
  360. // We can save fp libs only using IO_MGR::KICAD_SEXP format (.pretty libraries)
  361. IO_MGR::PCB_FILE_T piType = IO_MGR::KICAD_SEXP;
  362. wxString libPath = fn.GetFullPath();
  363. try
  364. {
  365. PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
  366. bool writable = false;
  367. bool exists = false;
  368. try
  369. {
  370. writable = pi->IsFootprintLibWritable( libPath );
  371. exists = true; // no exception was thrown, lib must exist.
  372. }
  373. catch( const IO_ERROR& )
  374. { }
  375. if( exists )
  376. {
  377. if( !writable )
  378. {
  379. wxString msg = wxString::Format( FMT_LIB_READ_ONLY, libPath );
  380. DisplayError( this, msg );
  381. return wxEmptyString;
  382. }
  383. else
  384. {
  385. wxString msg = wxString::Format( _( "Library %s already exists." ), libPath );
  386. KIDIALOG dlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
  387. dlg.SetOKLabel( _( "Overwrite" ) );
  388. dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
  389. if( dlg.ShowModal() == wxID_CANCEL )
  390. return wxEmptyString;
  391. pi->FootprintLibDelete( libPath );
  392. }
  393. }
  394. pi->FootprintLibCreate( libPath );
  395. }
  396. catch( const IO_ERROR& ioe )
  397. {
  398. DisplayError( this, ioe.What() );
  399. return wxEmptyString;
  400. }
  401. if( doAdd )
  402. AddLibrary( libPath );
  403. return libPath;
  404. }
  405. bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename )
  406. {
  407. wxFileName fn( aFilename );
  408. if( aFilename.IsEmpty() )
  409. {
  410. if( !LibraryFileBrowser( true, fn,
  411. KiCadFootprintLibPathWildcard(), KiCadFootprintLibPathExtension,
  412. true ) )
  413. {
  414. return false;
  415. }
  416. }
  417. wxString libPath = fn.GetFullPath();
  418. wxString libName = fn.GetName();
  419. if( libName.IsEmpty() )
  420. return false;
  421. bool saveInGlobalTable = false;
  422. bool saveInProjectTable = false;
  423. wxArrayString libTableNames;
  424. libTableNames.Add( _( "Global" ) );
  425. libTableNames.Add( _( "Project" ) );
  426. switch( SelectSingleOption( this, _( "Select Library Table" ),
  427. _( "Choose the Library Table to add the library to:" ),
  428. libTableNames ) )
  429. {
  430. case 0: saveInGlobalTable = true; break;
  431. case 1: saveInProjectTable = true; break;
  432. default: return false;
  433. }
  434. wxString type = IO_MGR::ShowType( IO_MGR::GuessPluginTypeFromLibPath( libPath ) );
  435. // try to use path normalized to an environmental variable or project path
  436. wxString normalizedPath = NormalizePath( libPath, &Pgm().GetLocalEnvVariables(), &Prj() );
  437. if( normalizedPath.IsEmpty() )
  438. normalizedPath = libPath;
  439. try
  440. {
  441. if( saveInGlobalTable )
  442. {
  443. auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString );
  444. GFootprintTable.InsertRow( row );
  445. GFootprintTable.Save( FP_LIB_TABLE::GetGlobalTableFileName() );
  446. }
  447. else if( saveInProjectTable )
  448. {
  449. auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString );
  450. Prj().PcbFootprintLibs()->InsertRow( row );
  451. Prj().PcbFootprintLibs()->Save( Prj().FootprintLibTblName() );
  452. }
  453. }
  454. catch( const IO_ERROR& ioe )
  455. {
  456. DisplayError( this, ioe.What() );
  457. return false;
  458. }
  459. auto editor = (FOOTPRINT_EDIT_FRAME*) Kiway().Player( FRAME_PCB_MODULE_EDITOR, false );
  460. if( editor )
  461. editor->SyncLibraryTree( true );
  462. auto viewer = (FOOTPRINT_VIEWER_FRAME*) Kiway().Player( FRAME_PCB_MODULE_VIEWER, false );
  463. if( viewer )
  464. viewer->ReCreateLibraryList();
  465. return true;
  466. }
  467. bool FOOTPRINT_EDIT_FRAME::DeleteModuleFromLibrary( const LIB_ID& aFPID, bool aConfirm )
  468. {
  469. if( !aFPID.IsValid() )
  470. return false;
  471. wxString nickname = aFPID.GetLibNickname();
  472. wxString fpname = aFPID.GetLibItemName();
  473. // Legacy libraries are readable, but modifying legacy format is not allowed
  474. // So prompt the user if he try to delete a footprint from a legacy lib
  475. wxString libfullname = Prj().PcbFootprintLibs()->FindRow( nickname )->GetFullURI();
  476. if( IO_MGR::GuessPluginTypeFromLibPath( libfullname ) == IO_MGR::LEGACY )
  477. {
  478. DisplayInfoMessage( this, INFO_LEGACY_LIB_WARN_DELETE );
  479. return false;
  480. }
  481. if( !Prj().PcbFootprintLibs()->IsFootprintLibWritable( nickname ) )
  482. {
  483. wxString msg = wxString::Format( _( "Library \"%s\" is read only" ), nickname );
  484. DisplayError( this, msg );
  485. return false;
  486. }
  487. // Confirmation
  488. wxString msg = wxString::Format( FMT_OK_DELETE, fpname.GetData(), nickname.GetData() );
  489. if( aConfirm && !IsOK( this, msg ) )
  490. return false;
  491. try
  492. {
  493. Prj().PcbFootprintLibs()->FootprintDelete( nickname, fpname );
  494. }
  495. catch( const IO_ERROR& ioe )
  496. {
  497. DisplayError( this, ioe.What() );
  498. return false;
  499. }
  500. msg.Printf( FMT_MOD_DELETED, fpname.GetData(), nickname.GetData() );
  501. SetStatusText( msg );
  502. return true;
  503. }
  504. void PCB_EDIT_FRAME::ArchiveModulesOnBoard( bool aStoreInNewLib, const wxString& aLibName,
  505. wxString* aLibPath )
  506. {
  507. if( GetBoard()->m_Modules == NULL )
  508. {
  509. DisplayInfoMessage( this, _( "No footprints to archive!" ) );
  510. return;
  511. }
  512. wxString footprintName;
  513. if( !aStoreInNewLib )
  514. {
  515. // The footprints are saved in an existing .pretty library in the fp lib table
  516. PROJECT& prj = Prj();
  517. wxString last_nickname = prj.GetRString( PROJECT::PCB_LIB_NICKNAME );
  518. wxString nickname = SelectLibrary( last_nickname );
  519. if( !nickname ) // Aborted
  520. return;
  521. prj.SetRString( PROJECT::PCB_LIB_NICKNAME, nickname );
  522. try
  523. {
  524. FP_LIB_TABLE* tbl = prj.PcbFootprintLibs();
  525. for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
  526. {
  527. if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
  528. tbl->FootprintSave( nickname, curr_fp, false );
  529. }
  530. }
  531. catch( const IO_ERROR& ioe )
  532. {
  533. DisplayError( this, ioe.What() );
  534. }
  535. }
  536. else
  537. {
  538. // The footprints are saved in a new .pretty library.
  539. // If this library already exists, all previous footprints will be deleted
  540. wxString libPath = CreateNewLibrary( aLibName );
  541. if( libPath.IsEmpty() ) // Aborted
  542. return;
  543. if( aLibPath ) *aLibPath = libPath;
  544. IO_MGR::PCB_FILE_T piType = IO_MGR::KICAD_SEXP;
  545. PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
  546. for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
  547. {
  548. try
  549. {
  550. if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
  551. pi->FootprintSave( libPath, curr_fp );
  552. }
  553. catch( const IO_ERROR& ioe )
  554. {
  555. DisplayError( this, ioe.What() );
  556. }
  557. }
  558. }
  559. }
  560. bool FOOTPRINT_EDIT_FRAME::SaveFootprint( MODULE* aModule )
  561. {
  562. wxString libraryName = aModule->GetFPID().GetLibNickname();
  563. wxString footprintName = aModule->GetFPID().GetLibItemName();
  564. bool nameChanged = m_footprintNameWhenLoaded != footprintName;
  565. if( aModule->GetLink() )
  566. {
  567. if( SaveFootprintToBoard( false ) )
  568. {
  569. m_footprintNameWhenLoaded = footprintName;
  570. return true;
  571. }
  572. else
  573. return false;
  574. }
  575. else if( libraryName.IsEmpty() || footprintName.IsEmpty() )
  576. {
  577. if( SaveFootprintAs( aModule ) )
  578. {
  579. m_footprintNameWhenLoaded = footprintName;
  580. SyncLibraryTree( true );
  581. return true;
  582. }
  583. else
  584. return false;
  585. }
  586. FP_LIB_TABLE* tbl = Prj().PcbFootprintLibs();
  587. // Legacy libraries are readable, but modifying legacy format is not allowed
  588. // So prompt the user if he try to add/replace a footprint in a legacy lib
  589. wxString libfullname = tbl->FindRow( libraryName )->GetFullURI();
  590. if( IO_MGR::GuessPluginTypeFromLibPath( libfullname ) == IO_MGR::LEGACY )
  591. {
  592. DisplayInfoMessage( this, INFO_LEGACY_LIB_WARN_EDIT );
  593. return false;
  594. }
  595. if( nameChanged )
  596. {
  597. LIB_ID oldFPID( libraryName, m_footprintNameWhenLoaded );
  598. DeleteModuleFromLibrary( oldFPID, false );
  599. }
  600. if( !saveFootprintInLibrary( aModule, libraryName ) )
  601. return false;
  602. if( nameChanged )
  603. {
  604. m_footprintNameWhenLoaded = footprintName;
  605. SyncLibraryTree( true );
  606. }
  607. return true;
  608. }
  609. bool FOOTPRINT_EDIT_FRAME::saveFootprintInLibrary( MODULE* aModule, const wxString& aLibraryName )
  610. {
  611. try
  612. {
  613. aModule->SetFPID( LIB_ID( wxEmptyString, aModule->GetFPID().GetLibItemName() ) );
  614. Prj().PcbFootprintLibs()->FootprintSave( aLibraryName, aModule );
  615. aModule->SetFPID( LIB_ID( aLibraryName, aModule->GetFPID().GetLibItemName() ) );
  616. return true;
  617. }
  618. catch( const IO_ERROR& ioe )
  619. {
  620. DisplayError( this, ioe.What() );
  621. aModule->SetFPID( LIB_ID( aLibraryName, aModule->GetFPID().GetLibItemName() ) );
  622. return false;
  623. }
  624. }
  625. bool FOOTPRINT_EDIT_FRAME::SaveFootprintToBoard( bool aAddNew )
  626. {
  627. // update module in the current board,
  628. // not just add it to the board with total disregard for the netlist...
  629. PCB_EDIT_FRAME* pcbframe = (PCB_EDIT_FRAME*) Kiway().Player( FRAME_PCB, false );
  630. if( pcbframe == NULL ) // happens when the board editor is not active (or closed)
  631. {
  632. DisplayErrorMessage( this, _("No board currently open." ) );
  633. return false;
  634. }
  635. BOARD* mainpcb = pcbframe->GetBoard();
  636. MODULE* source_module = NULL;
  637. MODULE* module_in_edit = GetBoard()->m_Modules;
  638. // Search the old module (source) if exists
  639. // Because this source could be deleted when editing the main board...
  640. if( module_in_edit->GetLink() ) // this is not a new module ...
  641. {
  642. source_module = mainpcb->m_Modules;
  643. for( ; source_module != NULL; source_module = source_module->Next() )
  644. {
  645. if( module_in_edit->GetLink() == source_module->GetTimeStamp() )
  646. break;
  647. }
  648. }
  649. if( !aAddNew && source_module == NULL ) // source not found
  650. {
  651. DisplayError( this, _( "Unable to find the footprint on the main board.\nCannot save." ) );
  652. return false;
  653. }
  654. if( aAddNew && source_module != NULL )
  655. {
  656. DisplayError( this, _( "Footprint already exists on board." ) );
  657. return false;
  658. }
  659. m_toolManager->RunAction( PCB_ACTIONS::selectionClear, true );
  660. pcbframe->GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
  661. BOARD_COMMIT commit( pcbframe );
  662. // Create the "new" module
  663. MODULE* newmodule = new MODULE( *module_in_edit );
  664. newmodule->SetParent( mainpcb );
  665. newmodule->SetLink( 0 );
  666. if( source_module ) // this is an update command
  667. {
  668. // In the main board,
  669. // the new module replace the old module (pos, orient, ref, value
  670. // and connexions are kept)
  671. // and the source_module (old module) is deleted
  672. pcbframe->Exchange_Module( source_module, newmodule, commit );
  673. newmodule->SetTimeStamp( module_in_edit->GetLink() );
  674. commit.Push( wxT( "Update module" ) );
  675. }
  676. else // This is an insert command
  677. {
  678. wxPoint cursor_pos = pcbframe->GetCrossHairPosition();
  679. commit.Add( newmodule );
  680. pcbframe->SetCrossHairPosition( wxPoint( 0, 0 ) );
  681. pcbframe->PlaceModule( newmodule, NULL );
  682. newmodule->SetPosition( wxPoint( 0, 0 ) );
  683. pcbframe->SetCrossHairPosition( cursor_pos );
  684. newmodule->SetTimeStamp( GetNewTimeStamp() );
  685. commit.Push( wxT( "Insert module" ) );
  686. }
  687. newmodule->ClearFlags();
  688. pcbframe->SetCurItem( NULL );
  689. // @todo LEGACY should be unnecessary
  690. mainpcb->m_Status_Pcb = 0;
  691. return true;
  692. }
  693. bool FOOTPRINT_EDIT_FRAME::SaveFootprintAs( MODULE* aModule )
  694. {
  695. if( aModule == NULL )
  696. return false;
  697. FP_LIB_TABLE* tbl = Prj().PcbFootprintLibs();
  698. SetMsgPanel( aModule );
  699. wxString libraryName = aModule->GetFPID().GetLibNickname();
  700. wxString footprintName = aModule->GetFPID().GetLibItemName();
  701. bool updateValue = ( aModule->GetValue() == footprintName );
  702. wxArrayString headers;
  703. std::vector<wxArrayString> itemsToDisplay;
  704. std::vector<wxString> nicknames = tbl->GetLogicalLibs();
  705. headers.Add( _( "Nickname" ) );
  706. headers.Add( _( "Description" ) );
  707. for( unsigned i = 0; i < nicknames.size(); i++ )
  708. {
  709. wxArrayString item;
  710. item.Add( nicknames[i] );
  711. item.Add( tbl->GetDescription( nicknames[i] ) );
  712. itemsToDisplay.push_back( item );
  713. }
  714. EDA_LIST_DIALOG dlg( this, FMT_SAVE_MODULE, headers, itemsToDisplay, libraryName,
  715. nullptr, nullptr, /* sort */ false, /* show headers */ false );
  716. dlg.SetListLabel( _( "Save in library:" ) );
  717. dlg.SetOKLabel( _( "Save" ) );
  718. wxBoxSizer* bNameSizer = new wxBoxSizer( wxHORIZONTAL );
  719. wxStaticText* label = new wxStaticText( &dlg, wxID_ANY, _( "Name:" ),
  720. wxDefaultPosition, wxDefaultSize, 0 );
  721. bNameSizer->Add( label, 0, wxALIGN_CENTER_VERTICAL|wxTOP|wxBOTTOM|wxLEFT, 5 );
  722. wxTextCtrl* nameTextCtrl = new wxTextCtrl( &dlg, wxID_ANY, footprintName,
  723. wxDefaultPosition, wxDefaultSize, 0 );
  724. bNameSizer->Add( nameTextCtrl, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
  725. wxTextValidator nameValidator( wxFILTER_EXCLUDE_CHAR_LIST );
  726. nameValidator.SetCharExcludes( MODULE::StringLibNameInvalidChars( false ) );
  727. nameTextCtrl->SetValidator( nameValidator );
  728. wxSizer* mainSizer = dlg.GetSizer();
  729. mainSizer->Prepend( bNameSizer, 0, wxEXPAND|wxTOP|wxLEFT|wxRIGHT, 5 );
  730. // Move nameTextCtrl to the head of the tab-order
  731. if( dlg.GetChildren().DeleteObject( nameTextCtrl ) )
  732. dlg.GetChildren().Insert( nameTextCtrl );
  733. dlg.SetInitialFocus( nameTextCtrl );
  734. dlg.Layout();
  735. mainSizer->Fit( &dlg );
  736. if( dlg.ShowModal() != wxID_OK )
  737. return false; // canceled by user
  738. libraryName = dlg.GetTextSelection();
  739. if( libraryName.IsEmpty() )
  740. {
  741. DisplayError( NULL, _( "No library specified. Footprint could not be saved." ) );
  742. return false;
  743. }
  744. footprintName = nameTextCtrl->GetValue();
  745. footprintName.Trim( true );
  746. footprintName.Trim( false );
  747. if( footprintName.IsEmpty() )
  748. {
  749. DisplayError( NULL, _( "No footprint name specified. Footprint could not be saved." ) );
  750. return false;
  751. }
  752. aModule->SetFPID( LIB_ID( libraryName, footprintName ) );
  753. if( updateValue )
  754. aModule->SetValue( footprintName );
  755. // Legacy libraries are readable, but modifying legacy format is not allowed
  756. // So prompt the user if he try to add/replace a footprint in a legacy lib
  757. wxString libfullname = Prj().PcbFootprintLibs()->FindRow( libraryName )->GetFullURI();
  758. IO_MGR::PCB_FILE_T piType = IO_MGR::GuessPluginTypeFromLibPath( libfullname );
  759. if( piType == IO_MGR::LEGACY )
  760. {
  761. DisplayInfoMessage( this, INFO_LEGACY_LIB_WARN_EDIT );
  762. return false;
  763. }
  764. bool module_exists = tbl->FootprintExists( libraryName, footprintName );
  765. if( module_exists )
  766. {
  767. wxString msg = wxString::Format( _( "Footprint %s already exists in %s." ),
  768. footprintName,
  769. libraryName );
  770. KIDIALOG chkdlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
  771. chkdlg.SetOKLabel( _( "Overwrite" ) );
  772. if( chkdlg.ShowModal() == wxID_CANCEL )
  773. return false;
  774. }
  775. if( !saveFootprintInLibrary( aModule, libraryName ) )
  776. return false;
  777. // Once saved-as a board footprint is no longer a board footprint
  778. aModule->SetLink( 0 );
  779. wxString fmt = module_exists ? _( "Component \"%s\" replaced in \"%s\"" ) :
  780. _( "Component \"%s\" added in \"%s\"" );
  781. wxString msg = wxString::Format( fmt, footprintName.GetData(), libraryName.GetData() );
  782. SetStatusText( msg );
  783. updateTitle();
  784. return true;
  785. }
  786. bool FOOTPRINT_EDIT_FRAME::RevertFootprint()
  787. {
  788. if( GetScreen()->IsModify() && m_revertModule )
  789. {
  790. wxString msg = wxString::Format( _( "Revert \"%s\" to last version saved?" ),
  791. GetChars( GetLoadedFPID().GetLibItemName() ) );
  792. if( ConfirmRevertDialog( this, msg ) )
  793. {
  794. Clear_Pcb( false );
  795. AddModuleToBoard( (MODULE*) m_revertModule->Clone() );
  796. Zoom_Automatique( false );
  797. Update3DView();
  798. GetScreen()->ClearUndoRedoList();
  799. GetScreen()->ClrModify();
  800. updateView();
  801. m_canvas->Refresh();
  802. return true;
  803. }
  804. }
  805. return false;
  806. }
  807. MODULE* PCB_BASE_FRAME::CreateNewModule( const wxString& aModuleName )
  808. {
  809. // Creates a new footprint at position 0,0 which contains the minimal items:
  810. // the reference and the value.
  811. // Value : initialized to the footprint name.
  812. // put on fab layer (front side)
  813. // Reference : initialized to a default value (REF**).
  814. // put on silkscreen layer (front side)
  815. wxString moduleName = aModuleName;
  816. // Ask for the new module name
  817. if( moduleName.IsEmpty() )
  818. {
  819. WX_TEXT_ENTRY_DIALOG dlg( this, FMT_MOD_REF, FMT_MOD_CREATE, moduleName );
  820. dlg.SetTextValidator( FILE_NAME_CHAR_VALIDATOR( &moduleName ) );
  821. if( dlg.ShowModal() != wxID_OK )
  822. return NULL; //Aborted by user
  823. }
  824. moduleName.Trim( true );
  825. moduleName.Trim( false );
  826. if( moduleName.IsEmpty() )
  827. {
  828. DisplayInfoMessage( this, FMT_NO_REF_ABORTED );
  829. return NULL;
  830. }
  831. // Creates the new module and add it to the head of the linked list of modules
  832. MODULE* module = new MODULE( GetBoard() );
  833. // Update parameters: timestamp ...
  834. module->SetLastEditTime();
  835. // Update its name in lib
  836. module->SetFPID( LIB_ID( wxEmptyString, moduleName ) );
  837. wxPoint default_pos;
  838. BOARD_DESIGN_SETTINGS& settings = GetDesignSettings();
  839. // Update reference:
  840. if( settings.m_RefDefaultText.IsEmpty() )
  841. module->SetReference( moduleName );
  842. else
  843. module->SetReference( settings.m_RefDefaultText );
  844. PCB_LAYER_ID layer = ToLAYER_ID( settings.m_RefDefaultlayer );
  845. module->Reference().SetThickness( settings.GetTextThickness( layer ) );
  846. module->Reference().SetTextSize( settings.GetTextSize( layer ) );
  847. module->Reference().SetItalic( settings.GetTextItalic( layer ) );
  848. module->Reference().SetKeepUpright( settings.GetTextUpright( layer ) );
  849. default_pos.y = GetDesignSettings().GetTextSize( layer ).y / 2;
  850. module->Reference().SetPosition( default_pos );
  851. module->Reference().SetLayer( layer );
  852. module->Reference().SetVisible( settings.m_RefDefaultVisibility );
  853. // Set the value field to a default value
  854. if( settings.m_ValueDefaultText.IsEmpty() )
  855. module->SetValue( moduleName );
  856. else
  857. module->SetValue( settings.m_ValueDefaultText );
  858. layer = ToLAYER_ID( settings.m_ValueDefaultlayer );
  859. module->Value().SetThickness( settings.GetTextThickness( layer ) );
  860. module->Value().SetTextSize( settings.GetTextSize( layer ) );
  861. module->Value().SetItalic( settings.GetTextItalic( layer ) );
  862. module->Value().SetKeepUpright( settings.GetTextUpright( layer ) );
  863. default_pos.y = -default_pos.y;
  864. module->Value().SetPosition( default_pos );
  865. module->Value().SetLayer( layer );
  866. module->Value().SetVisible( settings.m_ValueDefaultVisibility );
  867. SetMsgPanel( module );
  868. return module;
  869. }
  870. wxString PCB_BASE_FRAME::SelectLibrary( const wxString& aNicknameExisting )
  871. {
  872. wxArrayString headers;
  873. headers.Add( _( "Nickname" ) );
  874. headers.Add( _( "Description" ) );
  875. FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
  876. std::vector< wxArrayString > itemsToDisplay;
  877. std::vector< wxString > nicknames = fptbl->GetLogicalLibs();
  878. for( unsigned i = 0; i < nicknames.size(); i++ )
  879. {
  880. wxArrayString item;
  881. item.Add( nicknames[i] );
  882. item.Add( fptbl->GetDescription( nicknames[i] ) );
  883. itemsToDisplay.push_back( item );
  884. }
  885. EDA_LIST_DIALOG dlg( this, FMT_SELECT_LIB, headers, itemsToDisplay, aNicknameExisting );
  886. if( dlg.ShowModal() != wxID_OK )
  887. return wxEmptyString;
  888. wxString nickname = dlg.GetTextSelection();
  889. wxLogDebug( wxT( "Chose footprint library \"%s\"." ), GetChars( nickname ) );
  890. return nickname;
  891. }