KiCad PCB EDA Suite
librairi.cpp File Reference

Manage module (footprint) libraries. More...

#include <wx/ffile.h>
#include <wx/stdpaths.h>
#include <fctsys.h>
#include <pgm_base.h>
#include <kiface_i.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <wxPcbStruct.h>
#include <dialog_helpers.h>
#include <filter_reader.h>
#include <macros.h>
#include <fp_lib_table.h>
#include <validators.h>
#include <class_board.h>
#include <class_module.h>
#include <pcbnew.h>
#include <module_editor_frame.h>
#include <wildcards_and_files_ext.h>
#include <kicad_plugin.h>
#include <legacy_plugin.h>
#include <dialog_select_pretty_lib.h>

Go to the source code of this file.

Macros

#define FMT_OK_OVERWRITE   _( "Library '%s' exists, OK to replace ?" )
 
#define FMT_CREATE_LIB   _( "Create New Library Folder (the .pretty folder is the library)" )
 
#define FMT_OK_DELETE   _( "OK to delete footprint %s in library '%s'" )
 
#define FMT_IMPORT_MODULE   _( "Import Footprint" )
 
#define FMT_FILE_NOT_FOUND   _( "File '%s' not found" )
 
#define FMT_NOT_MODULE   _( "Not a footprint file" )
 
#define FMT_MOD_NOT_FOUND   _( "Unable to find or load footprint %s from lib path '%s'" )
 
#define FMT_BAD_PATH   _( "Unable to find or load footprint from path '%s'" )
 
#define FMT_BAD_PATHS   _( "The footprint library '%s' could not be found in any of the search paths." )
 
#define FMT_LIB_READ_ONLY   _( "Library '%s' is read only, not writable" )
 
#define FMT_EXPORT_MODULE   _( "Export Footprint" )
 
#define FMT_SAVE_MODULE   _( "Save Footprint" )
 
#define FMT_MOD_REF   _( "Enter footprint name:" )
 
#define FMT_EXPORTED   _( "Footprint exported to file '%s'" )
 
#define FMT_MOD_DELETED   _( "Footprint %s deleted from library '%s'" )
 
#define FMT_MOD_CREATE   _( "New Footprint" )
 
#define FMT_MOD_EXISTS   _( "Footprint %s already exists in library '%s'" )
 
#define FMT_NO_REF_ABORTED   _( "No footprint name defined." )
 
#define FMT_SELECT_LIB   _( "Select Library" )
 
#define EXPORT_IMPORT_LASTPATH_KEY   wxT( "import_last_path" )
 

Functions

static const wxString INFO_LEGACY_LIB_WARN_EDIT (_("Writing/modifying legacy libraries (.mod files) is not allowed\n""Please save the current library to the new .pretty format\n""and update your footprint lib table\n""to save your footprint (a .kicad_mod file) in the .pretty library folder"))
 
static const wxString INFO_LEGACY_LIB_WARN_DELETE (_("Modifying legacy libraries (.mod files) is not allowed\n""Please save the current library under the new .pretty format\n""and update your footprint lib table\n""before deleting a footprint"))
 
static const wxString ModLegacyExportFileWildcard (_("Legacy foot print export files (*.emp)|*.emp"))
 
static const wxString ModImportFileWildcard (_("GPcb foot print files (*)|*"))
 
static wxFileName prompt_for_module (wxWindow *aParent, const wxString &aLastPath)
 Prompt the user for a module file to open. More...
 
static IO_MGR::PCB_FILE_T detect_file_type (FILE *aFile, const wxFileName &aFileName, wxString *aName)
 Read a file to detect the type. More...
 
static MODULEparse_module_with_plugin (const wxFileName &aFileName, IO_MGR::PCB_FILE_T aFileType, const wxString &aName)
 Parse a footprint using a PLUGIN. More...
 
static MODULEparse_module_kicad (const wxFileName &aFileName)
 Parse a KICAD footprint. More...
 
MODULEtry_load_footprint (const wxFileName &aFileName, IO_MGR::PCB_FILE_T aFileType, const wxString &aName)
 Try to load a footprint, returning NULL if the file couldn't be accessed. More...
 

Detailed Description

Manage module (footprint) libraries.

Definition in file librairi.cpp.

Macro Definition Documentation

#define EXPORT_IMPORT_LASTPATH_KEY   wxT( "import_last_path" )
#define FMT_BAD_PATH   _( "Unable to find or load footprint from path '%s'" )

Definition at line 67 of file librairi.cpp.

#define FMT_BAD_PATHS   _( "The footprint library '%s' could not be found in any of the search paths." )

Definition at line 68 of file librairi.cpp.

#define FMT_CREATE_LIB   _( "Create New Library Folder (the .pretty folder is the library)" )

Definition at line 61 of file librairi.cpp.

#define FMT_EXPORT_MODULE   _( "Export Footprint" )

Definition at line 71 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::Export_Module().

#define FMT_EXPORTED   _( "Footprint exported to file '%s'" )

Definition at line 74 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::Export_Module().

#define FMT_FILE_NOT_FOUND   _( "File '%s' not found" )

Definition at line 64 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

#define FMT_IMPORT_MODULE   _( "Import Footprint" )

Definition at line 63 of file librairi.cpp.

Referenced by prompt_for_module().

#define FMT_LIB_READ_ONLY   _( "Library '%s' is read only, not writable" )

Definition at line 69 of file librairi.cpp.

Referenced by PCB_BASE_EDIT_FRAME::CreateNewLibrary().

#define FMT_MOD_CREATE   _( "New Footprint" )

Definition at line 76 of file librairi.cpp.

Referenced by PCB_BASE_FRAME::CreateNewModule().

#define FMT_MOD_DELETED   _( "Footprint %s deleted from library '%s'" )

Definition at line 75 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::DeleteModuleFromCurrentLibrary().

#define FMT_MOD_EXISTS   _( "Footprint %s already exists in library '%s'" )

Definition at line 78 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::SaveFootprintInLibrary().

#define FMT_MOD_NOT_FOUND   _( "Unable to find or load footprint %s from lib path '%s'" )

Definition at line 66 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

#define FMT_MOD_REF   _( "Enter footprint name:" )

Definition at line 73 of file librairi.cpp.

Referenced by PCB_BASE_FRAME::CreateNewModule().

#define FMT_NO_REF_ABORTED   _( "No footprint name defined." )

Definition at line 79 of file librairi.cpp.

Referenced by PCB_BASE_FRAME::CreateNewModule().

#define FMT_NOT_MODULE   _( "Not a footprint file" )

Definition at line 65 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

#define FMT_OK_DELETE   _( "OK to delete footprint %s in library '%s'" )

Definition at line 62 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::DeleteModuleFromCurrentLibrary().

#define FMT_OK_OVERWRITE   _( "Library '%s' exists, OK to replace ?" )

Definition at line 60 of file librairi.cpp.

Referenced by PCB_BASE_EDIT_FRAME::CreateNewLibrary().

#define FMT_SAVE_MODULE   _( "Save Footprint" )

Definition at line 72 of file librairi.cpp.

Referenced by FOOTPRINT_EDIT_FRAME::SaveFootprintInLibrary().

#define FMT_SELECT_LIB   _( "Select Library" )

Definition at line 80 of file librairi.cpp.

Referenced by PCB_BASE_FRAME::SelectLibrary().

Function Documentation

static IO_MGR::PCB_FILE_T detect_file_type ( FILE *  aFile,
const wxFileName &  aFileName,
wxString *  aName 
)
static

Read a file to detect the type.

Parameters
aFile- open file to be read. File pointer will be closed.
aFileName- file name to be read
aName- wxString to receive the module name iff type is LEGACY

Definition at line 136 of file librairi.cpp.

References IO_MGR::FILE_TYPE_NONE, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT, FROM_UTF8(), IO_MGR::GEDA_PCB, IO_MGR::KICAD, IO_MGR::LEGACY, and StrPurge().

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

137 {
138  FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
139  WHITESPACE_FILTER_READER reader( freader );
140  IO_MGR::PCB_FILE_T file_type;
141 
142  wxASSERT( aName );
143 
144  reader.ReadLine();
145  char* line = reader.Line();
146 
147  if( !strncasecmp( line, "(module", strlen( "(module" ) ) )
148  {
149  file_type = IO_MGR::KICAD;
150  *aName = aFileName.GetName();
151  }
152  else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
153  {
154  file_type = IO_MGR::LEGACY;
155  while( reader.ReadLine() )
156  {
157  if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
158  {
159  *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
160  break;
161  }
162  }
163  }
164  else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
165  {
166  file_type = IO_MGR::GEDA_PCB;
167  *aName = aFileName.GetName();
168  }
169  else
170  {
171  file_type = IO_MGR::FILE_TYPE_NONE;
172  }
173 
174  return file_type;
175 }
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
char * StrPurge(char *text)
Function StrPurge removes leading and training spaces, tabs and end of line chars in text return a po...
Definition: string.cpp:194
Class WHITESPACE_FILTER_READER reads lines of text from another LINE_READER, but only returns non-com...
Definition: filter_reader.h:71
Geda PCB file formats.
Definition: io_mgr.h:57
#define FOOTPRINT_LIBRARY_HEADER
Definition: legacy_plugin.h:38
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
S-expression Pcbnew file format.
Definition: io_mgr.h:54
#define FOOTPRINT_LIBRARY_HEADER_CNT
Definition: legacy_plugin.h:39
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:53
PCB_FILE_T
Enum PCB_FILE_T is a set of file types that the IO_MGR knows about, and for which there has been a pl...
Definition: io_mgr.h:51
static const wxString INFO_LEGACY_LIB_WARN_DELETE ( _("Modifying legacy libraries (.mod files) is not allowed\n""Please save the current library under the new .pretty format\n""and update your footprint lib table\n""before deleting a footprint")  )
static
static const wxString INFO_LEGACY_LIB_WARN_EDIT ( _("Writing/modifying legacy libraries (.mod files) is not allowed\n""Please save the current library to the new .pretty format\n""and update your footprint lib table\n""to save your footprint (a .kicad_mod file) in the .pretty library folder")  )
static
static const wxString ModImportFileWildcard ( _("GPcb foot print files (*)|*")  )
static

Referenced by prompt_for_module().

static const wxString ModLegacyExportFileWildcard ( _("Legacy foot print export files (*.emp)|*.emp")  )
static

Referenced by prompt_for_module().

static MODULE* parse_module_kicad ( const wxFileName &  aFileName)
static

Parse a KICAD footprint.

Parameters
aFileName- file name to parse

Definition at line 212 of file librairi.cpp.

References PCB_IO::Parse().

Referenced by try_load_footprint().

213 {
214  wxString fcontents;
215  PCB_IO pcb_io;
216  wxFFile f( aFileName.GetFullPath() );
217 
218  if( !f.IsOpened() )
219  return NULL;
220 
221  f.ReadAll( &fcontents );
222 
223  return dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
224 }
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
Class PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files...
static MODULE* parse_module_with_plugin ( const wxFileName &  aFileName,
IO_MGR::PCB_FILE_T  aFileType,
const wxString &  aName 
)
static

Parse a footprint using a PLUGIN.

Parameters
aFileName- file name to parse
aFileType- type of the file
aName- name of the footprint

Definition at line 184 of file librairi.cpp.

References PLUGIN::FootprintLoad(), IO_MGR::GEDA_PCB, IO_MGR::LEGACY, and IO_MGR::PluginFind().

Referenced by try_load_footprint().

187 {
188  wxString path;
189 
190  switch( aFileType )
191  {
192  case IO_MGR::GEDA_PCB:
193  path = aFileName.GetPath();
194  break;
195  case IO_MGR::LEGACY:
196  path = aFileName.GetFullPath();
197  break;
198  default:
199  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
200  }
201 
202  PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
203 
204  return pi->FootprintLoad( path, aName );
205 }
Geda PCB file formats.
Definition: io_mgr.h:57
Class RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destruct...
Definition: io_mgr.h:476
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:53
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Function PluginFind returns a PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: io_mgr.cpp:58
static wxFileName prompt_for_module ( wxWindow *  aParent,
const wxString &  aLastPath 
)
static

Prompt the user for a module file to open.

Parameters
aParent- parent window for the dialog
aLastPath- last opened path

Definition at line 106 of file librairi.cpp.

References FMT_IMPORT_MODULE, GedaPcbFootprintLibFileWildcard, KiCadFootprintLibFileWildcard, ModImportFileWildcard(), and ModLegacyExportFileWildcard().

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

107 {
108  static int lastFilterIndex = 0;
109  wxString wildCard;
110 
111  wildCard << wxGetTranslation( KiCadFootprintLibFileWildcard ) << wxChar( '|' )
112  << wxGetTranslation( ModLegacyExportFileWildcard ) << wxChar( '|' )
113  << wxGetTranslation( ModImportFileWildcard ) << wxChar( '|' )
114  << wxGetTranslation( GedaPcbFootprintLibFileWildcard );
115 
116  wxFileDialog dlg( aParent, FMT_IMPORT_MODULE, aLastPath, wxEmptyString, wildCard,
117  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
118 
119  dlg.SetFilterIndex( lastFilterIndex );
120 
121  if( dlg.ShowModal() == wxID_CANCEL )
122  return wxFileName();
123 
124  lastFilterIndex = dlg.GetFilterIndex();
125 
126  return wxFileName( dlg.GetPath() );
127 }
const wxString KiCadFootprintLibFileWildcard
static const wxString ModImportFileWildcard(_("GPcb foot print files (*)|*"))
static const wxString ModLegacyExportFileWildcard(_("Legacy foot print export files (*.emp)|*.emp"))
#define FMT_IMPORT_MODULE
Definition: librairi.cpp:63
const wxString GedaPcbFootprintLibFileWildcard
MODULE* try_load_footprint ( const wxFileName &  aFileName,
IO_MGR::PCB_FILE_T  aFileType,
const wxString &  aName 
)

Try to load a footprint, returning NULL if the file couldn't be accessed.

Parameters
aFileName- file name to load
aFileType- type of the file to load
aName- footprint name

Definition at line 233 of file librairi.cpp.

References IO_MGR::GEDA_PCB, IO_MGR::KICAD, IO_MGR::LEGACY, parse_module_kicad(), and parse_module_with_plugin().

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

235 {
236  MODULE* module;
237 
238  switch( aFileType )
239  {
240  case IO_MGR::GEDA_PCB:
241  case IO_MGR::LEGACY:
242  module = parse_module_with_plugin( aFileName, aFileType, aName );
243  break;
244 
245  case IO_MGR::KICAD:
246  module = parse_module_kicad( aFileName );
247  break;
248 
249  default:
250  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
251  module = NULL;
252  }
253 
254  return module;
255 }
Geda PCB file formats.
Definition: io_mgr.h:57
S-expression Pcbnew file format.
Definition: io_mgr.h:54
static MODULE * parse_module_with_plugin(const wxFileName &aFileName, IO_MGR::PCB_FILE_T aFileType, const wxString &aName)
Parse a footprint using a PLUGIN.
Definition: librairi.cpp:184
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:53
static MODULE * parse_module_kicad(const wxFileName &aFileName)
Parse a KICAD footprint.
Definition: librairi.cpp:212