KiCad PCB EDA Suite
footprint_libraries_utils.cpp File Reference
#include <wx/ffile.h>
#include <fctsys.h>
#include <pgm_base.h>
#include <kiface_i.h>
#include <confirm.h>
#include <kicad_string.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 <footprint_edit_frame.h>
#include <wildcards_and_files_ext.h>
#include <kicad_plugin.h>
#include <legacy_plugin.h>
#include <env_paths.h>
#include <settings/settings_manager.h>
#include <footprint_editor_settings.h>
#include "footprint_viewer_frame.h"

Go to the source code of this file.

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...
 

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

105 {
106  FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
107  WHITESPACE_FILTER_READER reader( freader );
108  IO_MGR::PCB_FILE_T file_type;
109 
110  wxASSERT( aName );
111 
112  reader.ReadLine();
113  char* line = reader.Line();
114 
115  if( !strncasecmp( line, "(module", strlen( "(module" ) ) )
116  {
117  file_type = IO_MGR::KICAD_SEXP;
118  *aName = aFileName.GetName();
119  }
120  else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
121  {
122  file_type = IO_MGR::LEGACY;
123 
124  while( reader.ReadLine() )
125  {
126  if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
127  {
128  *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
129  break;
130  }
131  }
132  }
133  else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
134  {
135  file_type = IO_MGR::GEDA_PCB;
136  *aName = aFileName.GetName();
137  }
138  else
139  {
140  file_type = IO_MGR::FILE_TYPE_NONE;
141  }
142 
143  return file_type;
144 }
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:114
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:338
WHITESPACE_FILTER_READER reads lines of text from another LINE_READER, but only returns non-comment l...
Definition: filter_reader.h:71
Geda PCB file formats.
Definition: io_mgr.h:63
#define FOOTPRINT_LIBRARY_HEADER
Definition: legacy_plugin.h:38
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 73 of file footprint_libraries_utils.cpp.

74 {
75  static int lastFilterIndex = 0; // To store the last choice during a session.
76  wxString wildCard;
77 
78  wildCard << KiCadFootprintLibFileWildcard() << wxChar( '|' )
79  << ModLegacyExportFileWildcard() << wxChar( '|' )
80  << GedaPcbFootprintLibFileWildcard() << wxChar( '|' )
81  << AllFilesWildcard();
82 
83  wxFileDialog dlg( aParent, _( "Import Footprint" ), aLastPath, wxEmptyString, wildCard,
84  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
85 
86  dlg.SetFilterIndex( lastFilterIndex );
87 
88  if( dlg.ShowModal() == wxID_CANCEL )
89  return wxFileName();
90 
91  lastFilterIndex = dlg.GetFilterIndex();
92 
93  return wxFileName( dlg.GetPath() );
94 }
wxString AllFilesWildcard()
wxString ModLegacyExportFileWildcard()
#define _(s)
Definition: 3d_actions.cpp:33
wxString KiCadFootprintLibFileWildcard()
wxString GedaPcbFootprintLibFileWildcard()

References _, AllFilesWildcard(), 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 181 of file footprint_libraries_utils.cpp.

182 {
183  wxString fcontents;
184  PCB_IO pcb_io;
185  wxFFile f( aFileName.GetFullPath() );
186 
187  if( !f.IsOpened() )
188  return NULL;
189 
190  f.ReadAll( &fcontents );
191 
192  return dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
193 }
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
#define NULL

References NULL, and 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 153 of file footprint_libraries_utils.cpp.

156 {
157  wxString path;
158 
159  switch( aFileType )
160  {
161  case IO_MGR::GEDA_PCB:
162  path = aFileName.GetPath();
163  break;
164  case IO_MGR::LEGACY:
165  path = aFileName.GetFullPath();
166  break;
167  default:
168  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
169  }
170 
171  PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
172 
173  return pi->FootprintLoad( path, aName );
174 }
Geda PCB file formats.
Definition: io_mgr.h:63
RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destructor.
Definition: io_mgr.h:579
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:61

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

204 {
205  MODULE* module;
206 
207  switch( aFileType )
208  {
209  case IO_MGR::GEDA_PCB:
210  case IO_MGR::LEGACY:
211  module = parse_module_with_plugin( aFileName, aFileType, aName );
212  break;
213 
214  case IO_MGR::KICAD_SEXP:
215  module = parse_module_kicad( aFileName );
216  break;
217 
218  default:
219  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
220  module = NULL;
221  }
222 
223  return module;
224 }
Geda PCB file formats.
Definition: io_mgr.h:63
#define NULL
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, NULL, parse_module_kicad(), and parse_module_with_plugin().

Referenced by FOOTPRINT_EDIT_FRAME::Import_Module().