KiCad PCB EDA Suite
footprint_libraries_utils.cpp File Reference
#include <wx/ffile.h>
#include <wx/stdpaths.h>
#include <fctsys.h>
#include <pgm_base.h>
#include <kiface_i.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <pcb_edit_frame.h>
#include <dialog_helpers.h>
#include <filter_reader.h>
#include <macros.h>
#include <fp_lib_table.h>
#include <validators.h>
#include <dialog_text_entry.h>
#include <tool/tool_manager.h>
#include <tools/pcb_actions.h>
#include <class_board.h>
#include <class_module.h>
#include <board_commit.h>
#include <pcbnew.h>
#include <footprint_edit_frame.h>
#include <wildcards_and_files_ext.h>
#include <kicad_plugin.h>
#include <legacy_plugin.h>
#include <env_paths.h>
#include "footprint_viewer_frame.h"

Go to the source code of this file.

Macros

#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_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_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 wxFileName getFootprintFilenameFromUser (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...
 

Macro Definition Documentation

◆ EXPORT_IMPORT_LASTPATH_KEY

#define EXPORT_IMPORT_LASTPATH_KEY   wxT( "import_last_path" )

Definition at line 90 of file footprint_libraries_utils.cpp.

◆ FMT_EXPORT_MODULE

#define FMT_EXPORT_MODULE   _( "Export Footprint" )

Definition at line 67 of file footprint_libraries_utils.cpp.

◆ FMT_EXPORTED

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

Definition at line 70 of file footprint_libraries_utils.cpp.

◆ FMT_FILE_NOT_FOUND

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

Definition at line 62 of file footprint_libraries_utils.cpp.

◆ FMT_IMPORT_MODULE

#define FMT_IMPORT_MODULE   _( "Import Footprint" )

Definition at line 61 of file footprint_libraries_utils.cpp.

◆ FMT_LIB_READ_ONLY

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

Definition at line 65 of file footprint_libraries_utils.cpp.

◆ FMT_MOD_CREATE

#define FMT_MOD_CREATE   _( "New Footprint" )

Definition at line 72 of file footprint_libraries_utils.cpp.

◆ FMT_MOD_DELETED

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

Definition at line 71 of file footprint_libraries_utils.cpp.

◆ FMT_MOD_NOT_FOUND

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

Definition at line 64 of file footprint_libraries_utils.cpp.

◆ FMT_MOD_REF

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

Definition at line 69 of file footprint_libraries_utils.cpp.

◆ FMT_NO_REF_ABORTED

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

Definition at line 74 of file footprint_libraries_utils.cpp.

◆ FMT_NOT_MODULE

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

Definition at line 63 of file footprint_libraries_utils.cpp.

◆ FMT_OK_DELETE

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

Definition at line 60 of file footprint_libraries_utils.cpp.

◆ FMT_SAVE_MODULE

#define FMT_SAVE_MODULE   _( "Save Footprint" )

Definition at line 68 of file footprint_libraries_utils.cpp.

◆ FMT_SELECT_LIB

#define FMT_SELECT_LIB   _( "Select Library" )

Definition at line 75 of file footprint_libraries_utils.cpp.

Function Documentation

◆ detect_file_type()

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 128 of file footprint_libraries_utils.cpp.

130 {
131  FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
132  WHITESPACE_FILTER_READER reader( freader );
133  IO_MGR::PCB_FILE_T file_type;
134 
135  wxASSERT( aName );
136 
137  reader.ReadLine();
138  char* line = reader.Line();
139 
140  if( !strncasecmp( line, "(module", strlen( "(module" ) ) )
141  {
142  file_type = IO_MGR::KICAD_SEXP;
143  *aName = aFileName.GetName();
144  }
145  else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
146  {
147  file_type = IO_MGR::LEGACY;
148 
149  while( reader.ReadLine() )
150  {
151  if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
152  {
153  *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
154  break;
155  }
156  }
157  }
158  else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
159  {
160  file_type = IO_MGR::GEDA_PCB;
161  *aName = aFileName.GetName();
162  }
163  else
164  {
165  file_type = IO_MGR::FILE_TYPE_NONE;
166  }
167 
168  return file_type;
169 }
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:62
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:310
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:60
#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
#define FOOTPRINT_LIBRARY_HEADER_CNT
Definition: legacy_plugin.h:39
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:56
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:54
S-expression Pcbnew file format.
Definition: io_mgr.h:57

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

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

◆ getFootprintFilenameFromUser()

static wxFileName getFootprintFilenameFromUser ( 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 98 of file footprint_libraries_utils.cpp.

99 {
100  static int lastFilterIndex = 0; // To store the last choice during a session.
101  wxString wildCard;
102 
103  wildCard << KiCadFootprintLibFileWildcard() << wxChar( '|' )
104  << ModLegacyExportFileWildcard() << wxChar( '|' )
105  << GedaPcbFootprintLibFileWildcard() << wxChar( '|' )
106  << AllFilesWildcard();
107 
108  wxFileDialog dlg( aParent, FMT_IMPORT_MODULE, aLastPath, wxEmptyString, wildCard,
109  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
110 
111  dlg.SetFilterIndex( lastFilterIndex );
112 
113  if( dlg.ShowModal() == wxID_CANCEL )
114  return wxFileName();
115 
116  lastFilterIndex = dlg.GetFilterIndex();
117 
118  return wxFileName( dlg.GetPath() );
119 }
wxString AllFilesWildcard()
wxString ModLegacyExportFileWildcard()
#define FMT_IMPORT_MODULE
wxString KiCadFootprintLibFileWildcard()
wxString GedaPcbFootprintLibFileWildcard()

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

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().

◆ INFO_LEGACY_LIB_WARN_DELETE()

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

◆ INFO_LEGACY_LIB_WARN_EDIT()

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

◆ parse_module_kicad()

static MODULE* parse_module_kicad ( const wxFileName &  aFileName)
static

Parse a KICAD footprint.

Parameters
aFileName- file name to parse

Definition at line 206 of file footprint_libraries_utils.cpp.

207 {
208  wxString fcontents;
209  PCB_IO pcb_io;
210  wxFFile f( aFileName.GetFullPath() );
211 
212  if( !f.IsOpened() )
213  return NULL;
214 
215  f.ReadAll( &fcontents );
216 
217  return dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
218 }
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
Class PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.

References PCB_IO::Parse().

Referenced by try_load_footprint().

◆ parse_module_with_plugin()

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 178 of file footprint_libraries_utils.cpp.

181 {
182  wxString path;
183 
184  switch( aFileType )
185  {
186  case IO_MGR::GEDA_PCB:
187  path = aFileName.GetPath();
188  break;
189  case IO_MGR::LEGACY:
190  path = aFileName.GetFullPath();
191  break;
192  default:
193  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
194  }
195 
196  PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
197 
198  return pi->FootprintLoad( path, aName );
199 }
Geda PCB file formats.
Definition: io_mgr.h:60
Class RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destruct...
Definition: io_mgr.h:577
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:56
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Function PluginFind returns a PLUGIN which the caller can use to import, export, save,...
Definition: io_mgr.cpp:58

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

Referenced by try_load_footprint().

◆ try_load_footprint()

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 227 of file footprint_libraries_utils.cpp.

229 {
230  MODULE* module;
231 
232  switch( aFileType )
233  {
234  case IO_MGR::GEDA_PCB:
235  case IO_MGR::LEGACY:
236  module = parse_module_with_plugin( aFileName, aFileType, aName );
237  break;
238 
239  case IO_MGR::KICAD_SEXP:
240  module = parse_module_kicad( aFileName );
241  break;
242 
243  default:
244  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
245  module = NULL;
246  }
247 
248  return module;
249 }
Geda PCB file formats.
Definition: io_mgr.h:60
static MODULE * parse_module_kicad(const wxFileName &aFileName)
Parse a KICAD footprint.
static MODULE * parse_module_with_plugin(const wxFileName &aFileName, IO_MGR::PCB_FILE_T aFileType, const wxString &aName)
Parse a footprint using a PLUGIN.
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:56
S-expression Pcbnew file format.
Definition: io_mgr.h:57

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

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().