KiCad PCB EDA Suite
footprint_libraries_utils.cpp File Reference
#include <wx/ffile.h>
#include <pgm_base.h>
#include <kiface_base.h>
#include <confirm.h>
#include <string_utils.h>
#include <macros.h>
#include <pcb_edit_frame.h>
#include <eda_list_dialog.h>
#include <filter_reader.h>
#include <fp_lib_table.h>
#include <validators.h>
#include <dialogs/dialog_text_entry.h>
#include <tool/tool_manager.h>
#include <tools/pcb_actions.h>
#include <tools/board_editor_control.h>
#include <board.h>
#include <footprint.h>
#include <board_commit.h>
#include <footprint_edit_frame.h>
#include <wildcards_and_files_ext.h>
#include <plugins/kicad/kicad_plugin.h>
#include <plugins/legacy/legacy_plugin.h>
#include <env_paths.h>
#include <paths.h>
#include <settings/settings_manager.h>
#include <footprint_editor_settings.h>
#include "footprint_viewer_frame.h"
#include <wx/choicdlg.h>
#include <wx/filedlg.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 footprint 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 FOOTPRINTparse_footprint_with_plugin (const wxFileName &aFileName, IO_MGR::PCB_FILE_T aFileType, const wxString &aName)
 Parse a footprint using a PLUGIN. More...
 
static FOOTPRINTparse_footprint_kicad (const wxFileName &aFileName)
 Parse a KICAD footprint. More...
 
FOOTPRINTtry_load_footprint (const wxFileName &aFileName, IO_MGR::PCB_FILE_T aFileType, const wxString &aName)
 Try to load a footprint, returning nullptr 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 footprint name iff type is LEGACY

Definition at line 106 of file footprint_libraries_utils.cpp.

108 {
109  FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
110  WHITESPACE_FILTER_READER reader( freader );
111  IO_MGR::PCB_FILE_T file_type;
112 
113  wxASSERT( aName );
114 
115  reader.ReadLine();
116  char* line = reader.Line();
117 
118  // first .kicad_mod file versions starts by "(module"
119  // recent .kicad_mod file versions starts by "(footprint"
120  if( strncasecmp( line, "(module", strlen( "(module" ) ) == 0
121  || strncasecmp( line, "(footprint", strlen( "(footprint" ) ) == 0 )
122  {
123  file_type = IO_MGR::KICAD_SEXP;
124  *aName = aFileName.GetName();
125  }
126  else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
127  {
128  file_type = IO_MGR::LEGACY;
129 
130  while( reader.ReadLine() )
131  {
132  if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
133  {
134  *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
135  break;
136  }
137  }
138  }
139  else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
140  {
141  file_type = IO_MGR::GEDA_PCB;
142  *aName = aFileName.GetName();
143  }
144  else
145  {
146  file_type = IO_MGR::FILE_TYPE_NONE;
147  }
148 
149  return file_type;
150 }
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
Read lines of text from another LINE_READER but only returns non-comment lines and non-blank lines wi...
Definition: filter_reader.h:68
Geda PCB file formats.
Definition: io_mgr.h:64
#define FOOTPRINT_LIBRARY_HEADER
Definition: legacy_plugin.h:38
A LINE_READER that reads from an open file.
Definition: richio.h:172
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
#define FOOTPRINT_LIBRARY_HEADER_CNT
Definition: legacy_plugin.h:39
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:55
PCB_FILE_T
The set of file types that the IO_MGR knows about, and for which there has been a plugin written.
Definition: io_mgr.h:53
S-expression Pcbnew file format.
Definition: io_mgr.h:56

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::ImportFootprint().

◆ getFootprintFilenameFromUser()

static wxFileName getFootprintFilenameFromUser ( wxWindow *  aParent,
const wxString &  aLastPath 
)
static

Prompt the user for a footprint file to open.

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

Definition at line 76 of file footprint_libraries_utils.cpp.

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

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

Referenced by FOOTPRINT_EDIT_FRAME::ImportFootprint().

◆ 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_footprint_kicad()

static FOOTPRINT* parse_footprint_kicad ( const wxFileName &  aFileName)
static

Parse a KICAD footprint.

Parameters
aFileName- file name to parse

Definition at line 182 of file footprint_libraries_utils.cpp.

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

References PCB_IO::Parse().

Referenced by try_load_footprint().

◆ parse_footprint_with_plugin()

static FOOTPRINT* parse_footprint_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 159 of file footprint_libraries_utils.cpp.

162 {
163  wxString path;
164 
165  switch( aFileType )
166  {
167  case IO_MGR::GEDA_PCB: path = aFileName.GetPath(); break;
168  case IO_MGR::LEGACY: path = aFileName.GetFullPath(); break;
169  default: wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) ); break;
170  }
171 
172  PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
173 
174  return pi->FootprintLoad( path, aName );
175 }
Geda PCB file formats.
Definition: io_mgr.h:64
Releases a PLUGIN in the context of a potential thrown exception through its destructor.
Definition: io_mgr.h:555
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:55
static PLUGIN * PluginFind(PCB_FILE_T aFileType)
Return a PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: io_mgr.cpp:58

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

Referenced by try_load_footprint().

◆ try_load_footprint()

FOOTPRINT* try_load_footprint ( const wxFileName &  aFileName,
IO_MGR::PCB_FILE_T  aFileType,
const wxString &  aName 
)

Try to load a footprint, returning nullptr 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 203 of file footprint_libraries_utils.cpp.

205 {
206  FOOTPRINT* footprint;
207 
208  switch( aFileType )
209  {
210  case IO_MGR::GEDA_PCB:
211  case IO_MGR::LEGACY:
212  footprint = parse_footprint_with_plugin( aFileName, aFileType, aName );
213  break;
214 
215  case IO_MGR::KICAD_SEXP:
216  footprint = parse_footprint_kicad( aFileName );
217  break;
218 
219  default:
220  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
221  footprint = nullptr;
222  }
223 
224  return footprint;
225 }
static FOOTPRINT * parse_footprint_kicad(const wxFileName &aFileName)
Parse a KICAD footprint.
Geda PCB file formats.
Definition: io_mgr.h:64
static FOOTPRINT * parse_footprint_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:55
S-expression Pcbnew file format.
Definition: io_mgr.h:56

References IO_MGR::GEDA_PCB, IO_MGR::KICAD_SEXP, IO_MGR::LEGACY, parse_footprint_kicad(), and parse_footprint_with_plugin().

Referenced by FOOTPRINT_EDIT_FRAME::ImportFootprint().