KiCad PCB EDA Suite
footprint_libraries_utils.cpp File Reference
#include <wx/ffile.h>
#include <pgm_base.h>
#include <kiface_i.h>
#include <confirm.h>
#include <kicad_string.h>
#include <macros.h>
#include <pcb_edit_frame.h>
#include <dialog_helpers.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 <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"

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 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  // first .kicad_mod file versions starts by "(module"
116  // recent .kicad_mod file versions starts by "(footprint"
117  if( strncasecmp( line, "(module", strlen( "(module" ) ) == 0
118  || strncasecmp( line, "(footprint", strlen( "(footprint" ) ) == 0 )
119  {
120  file_type = IO_MGR::KICAD_SEXP;
121  *aName = aFileName.GetName();
122  }
123  else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
124  {
125  file_type = IO_MGR::LEGACY;
126 
127  while( reader.ReadLine() )
128  {
129  if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
130  {
131  *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
132  break;
133  }
134  }
135  }
136  else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
137  {
138  file_type = IO_MGR::GEDA_PCB;
139  *aName = aFileName.GetName();
140  }
141  else
142  {
143  file_type = IO_MGR::FILE_TYPE_NONE;
144  }
145 
146  return file_type;
147 }
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
char * StrPurge(char *text)
Remove leading and training spaces, tabs and end of line chars in text.
Definition: string.cpp:396
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:63
#define FOOTPRINT_LIBRARY_HEADER
Definition: legacy_plugin.h:38
A LINE_READER that reads from an open file.
Definition: richio.h:172
#define FOOTPRINT_LIBRARY_HEADER_CNT
Definition: legacy_plugin.h:39
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:54
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:52
S-expression Pcbnew file format.
Definition: io_mgr.h:55

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

180 {
181  wxString fcontents;
182  PCB_IO pcb_io;
183  wxFFile f( aFileName.GetFullPath() );
184 
185  if( !f.IsOpened() )
186  return nullptr;
187 
188  f.ReadAll( &fcontents );
189 
190  return dynamic_cast<FOOTPRINT*>( pcb_io.Parse( fcontents ) );
191 }
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 156 of file footprint_libraries_utils.cpp.

159 {
160  wxString path;
161 
162  switch( aFileType )
163  {
164  case IO_MGR::GEDA_PCB: path = aFileName.GetPath(); break;
165  case IO_MGR::LEGACY: path = aFileName.GetFullPath(); break;
166  default: wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) ); break;
167  }
168 
169  PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
170 
171  return pi->FootprintLoad( path, aName );
172 }
Geda PCB file formats.
Definition: io_mgr.h:63
Releases a PLUGIN in the context of a potential thrown exception through its destructor.
Definition: io_mgr.h:550
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:54
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 200 of file footprint_libraries_utils.cpp.

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

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