KiCad PCB EDA Suite
librairi.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
29 #include <wx/ffile.h>
30 #include <wx/stdpaths.h>
31 
32 #include <fctsys.h>
33 #include <pgm_base.h>
34 #include <kiface_i.h>
35 #include <class_drawpanel.h>
36 #include <confirm.h>
37 #include <kicad_string.h>
38 #include <gestfich.h>
39 #include <wxPcbStruct.h>
40 #include <dialog_helpers.h>
41 #include <filter_reader.h>
42 #include <macros.h>
43 #include <fp_lib_table.h>
44 #include <validators.h>
45 
46 #include <class_board.h>
47 #include <class_module.h>
48 
49 #include <pcbnew.h>
50 #include <module_editor_frame.h>
52 #include <kicad_plugin.h>
53 #include <legacy_plugin.h>
54 
56 
57 
58 // unique, "file local" translations:
59 
60 #define FMT_OK_OVERWRITE _( "Library '%s' exists, OK to replace ?" )
61 #define FMT_CREATE_LIB _( "Create New Library Folder (the .pretty folder is the library)" )
62 #define FMT_OK_DELETE _( "OK to delete footprint %s in library '%s'" )
63 #define FMT_IMPORT_MODULE _( "Import Footprint" )
64 #define FMT_FILE_NOT_FOUND _( "File '%s' not found" )
65 #define FMT_NOT_MODULE _( "Not a footprint file" )
66 #define FMT_MOD_NOT_FOUND _( "Unable to find or load footprint %s from lib path '%s'" )
67 #define FMT_BAD_PATH _( "Unable to find or load footprint from path '%s'" )
68 #define FMT_BAD_PATHS _( "The footprint library '%s' could not be found in any of the search paths." )
69 #define FMT_LIB_READ_ONLY _( "Library '%s' is read only, not writable" )
70 
71 #define FMT_EXPORT_MODULE _( "Export Footprint" )
72 #define FMT_SAVE_MODULE _( "Save Footprint" )
73 #define FMT_MOD_REF _( "Enter footprint name:" )
74 #define FMT_EXPORTED _( "Footprint exported to file '%s'" )
75 #define FMT_MOD_DELETED _( "Footprint %s deleted from library '%s'" )
76 #define FMT_MOD_CREATE _( "New Footprint" )
77 
78 #define FMT_MOD_EXISTS _( "Footprint %s already exists in library '%s'" )
79 #define FMT_NO_REF_ABORTED _( "No footprint name defined." )
80 #define FMT_SELECT_LIB _( "Select Library" )
81 
82 static const wxString INFO_LEGACY_LIB_WARN_EDIT(
83  _( "Writing/modifying legacy libraries (.mod files) is not allowed\n"\
84  "Please save the current library to the new .pretty format\n"\
85  "and update your footprint lib table\n"\
86  "to save your footprint (a .kicad_mod file) in the .pretty library folder" ) );
87 
88 static const wxString INFO_LEGACY_LIB_WARN_DELETE(
89  _( "Modifying legacy libraries (.mod files) is not allowed\n"\
90  "Please save the current library under the new .pretty format\n"\
91  "and update your footprint lib table\n"\
92  "before deleting a footprint" ) );
93 
94 
95 #define EXPORT_IMPORT_LASTPATH_KEY wxT( "import_last_path" )
96 
97 
103 static wxFileName getFootprintFilenameFromUser( wxWindow* aParent, const wxString& aLastPath )
104 {
105  static int lastFilterIndex = 0; // To store the last choice during a session.
106  wxString wildCard;
107 
108  wildCard << KiCadFootprintLibFileWildcard() << wxChar( '|' )
109  << ModLegacyExportFileWildcard() << wxChar( '|' )
111 
112  wxFileDialog dlg( aParent, FMT_IMPORT_MODULE, aLastPath, wxEmptyString, wildCard,
113  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
114 
115  dlg.SetFilterIndex( lastFilterIndex );
116 
117  if( dlg.ShowModal() == wxID_CANCEL )
118  return wxFileName();
119 
120  lastFilterIndex = dlg.GetFilterIndex();
121 
122  return wxFileName( dlg.GetPath() );
123 }
124 
125 
132 static IO_MGR::PCB_FILE_T detect_file_type( FILE* aFile, const wxFileName& aFileName, wxString* aName )
133 {
134  FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
135  WHITESPACE_FILTER_READER reader( freader );
136  IO_MGR::PCB_FILE_T file_type;
137 
138  wxASSERT( aName );
139 
140  reader.ReadLine();
141  char* line = reader.Line();
142 
143  if( !strncasecmp( line, "(module", strlen( "(module" ) ) )
144  {
145  file_type = IO_MGR::KICAD_SEXP;
146  *aName = aFileName.GetName();
147  }
148  else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
149  {
150  file_type = IO_MGR::LEGACY;
151  while( reader.ReadLine() )
152  {
153  if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
154  {
155  *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
156  break;
157  }
158  }
159  }
160  else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
161  {
162  file_type = IO_MGR::GEDA_PCB;
163  *aName = aFileName.GetName();
164  }
165  else
166  {
167  file_type = IO_MGR::FILE_TYPE_NONE;
168  }
169 
170  return file_type;
171 }
172 
173 
181  const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
182  const wxString& aName )
183 {
184  wxString path;
185 
186  switch( aFileType )
187  {
188  case IO_MGR::GEDA_PCB:
189  path = aFileName.GetPath();
190  break;
191  case IO_MGR::LEGACY:
192  path = aFileName.GetFullPath();
193  break;
194  default:
195  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
196  }
197 
198  PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
199 
200  return pi->FootprintLoad( path, aName );
201 }
202 
203 
208 static MODULE* parse_module_kicad( const wxFileName& aFileName )
209 {
210  wxString fcontents;
211  PCB_IO pcb_io;
212  wxFFile f( aFileName.GetFullPath() );
213 
214  if( !f.IsOpened() )
215  return NULL;
216 
217  f.ReadAll( &fcontents );
218 
219  return dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
220 }
221 
222 
229 MODULE* try_load_footprint( const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
230  const wxString& aName )
231 {
232  MODULE* module;
233 
234  switch( aFileType )
235  {
236  case IO_MGR::GEDA_PCB:
237  case IO_MGR::LEGACY:
238  module = parse_module_with_plugin( aFileName, aFileType, aName );
239  break;
240 
241  case IO_MGR::KICAD_SEXP:
242  module = parse_module_kicad( aFileName );
243  break;
244 
245  default:
246  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
247  module = NULL;
248  }
249 
250  return module;
251 }
252 
253 
255 {
256  wxString lastOpenedPathForLoading = m_mruPath;
257  wxConfigBase* config = Kiface().KifaceSettings();
258 
259  if( config )
260  config->Read( EXPORT_IMPORT_LASTPATH_KEY, &lastOpenedPathForLoading );
261 
262  wxFileName fn;
263 
264  if( aName != wxT("") )
265  fn = aName;
266  else
267  fn = getFootprintFilenameFromUser( this, lastOpenedPathForLoading );
268 
269  if( !fn.IsOk() )
270  return NULL;
271 
272  FILE* fp = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
273 
274  if( !fp )
275  {
276  wxString msg = wxString::Format( FMT_FILE_NOT_FOUND, GetChars( fn.GetFullPath() ) );
277  DisplayError( this, msg );
278  return NULL;
279  }
280 
281  if( config ) // Save file path
282  {
283  lastOpenedPathForLoading = fn.GetPath();
284  config->Write( EXPORT_IMPORT_LASTPATH_KEY, lastOpenedPathForLoading );
285  }
286 
287  wxString moduleName;
288  IO_MGR::PCB_FILE_T fileType = detect_file_type( fp, fn.GetFullPath(), &moduleName );
289 
290  if( fileType == IO_MGR::FILE_TYPE_NONE )
291  {
292  DisplayError( this, FMT_NOT_MODULE );
293  return NULL;
294  }
295 
296  MODULE* module = NULL;
297 
298  try
299  {
300  module = try_load_footprint( fn, fileType, moduleName );
301 
302  if( !module )
303  {
304  wxString msg = wxString::Format(
305  FMT_MOD_NOT_FOUND, GetChars( moduleName ), GetChars( fn.GetFullPath() ) );
306  DisplayError( this, msg );
307  return NULL;
308  }
309  }
310  catch( const IO_ERROR& ioe )
311  {
312  DisplayError( this, ioe.What() );
313 
314  // if the footprint is not loaded, exit.
315  // However, even if an error happens, it can be loaded, because in KICAD and GPCB format,
316  // a fp library is a set of separate files, and the error(s) are not necessary when
317  // reading the selected file
318 
319  if( !module )
320  return NULL;
321  }
322 
323  // Insert footprint in list
324  GetBoard()->Add( module );
325 
326  // Display info :
327  SetMsgPanel( module );
328  PlaceModule( module, NULL );
329 
330  if( IsGalCanvasActive() )
331  module->SetPosition( wxPoint( 0, 0 ) );
332 
333  GetBoard()->m_Status_Pcb = 0;
335  updateView();
336 
337  return module;
338 }
339 
340 
342 {
343  wxFileName fn;
344  wxConfigBase* config = Kiface().KifaceSettings();
345 
346  if( !aModule )
347  return;
348 
349  fn.SetName( aModule->GetFPID().GetLibItemName() );
350 
351  wxString wildcard = KiCadFootprintLibFileWildcard();
352 
353  fn.SetExt( KiCadFootprintFileExtension );
354 
355  if( config )
356  {
357  wxString path;
358  config->Read( EXPORT_IMPORT_LASTPATH_KEY, &path, m_mruPath );
359  fn.SetPath( path );
360  }
361 
362  wxFileDialog dlg( this, FMT_EXPORT_MODULE, fn.GetPath(), fn.GetFullName(),
363  wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
364 
365  if( dlg.ShowModal() == wxID_CANCEL )
366  return;
367 
368  fn = dlg.GetPath();
369 
370  if( config ) // Save file path
371  {
372  config->Write( EXPORT_IMPORT_LASTPATH_KEY, fn.GetPath() );
373  }
374 
375  try
376  {
377  // Export as *.kicad_pcb format, using a strategy which is specifically chosen
378  // as an example on how it could also be used to send it to the system clipboard.
379 
380  PCB_IO pcb_io( CTL_FOR_LIBRARY );
381 
382  /* This module should *already* be "normalized" in a way such that
383  orientation is zero, etc., since it came from module editor.
384 
385  module->SetTimeStamp( 0 );
386  module->SetParent( 0 );
387  module->SetOrientation( 0 );
388  */
389 
390  pcb_io.Format( aModule );
391 
392  FILE* fp = wxFopen( dlg.GetPath(), wxT( "wt" ) );
393 
394  if( fp == NULL )
395  {
396  wxMessageBox( wxString::Format(
397  _( "Unable to create or write file '%s'" ),
398  GetChars( dlg.GetPath() ) ) );
399  return;
400  }
401 
402  fprintf( fp, "%s", pcb_io.GetStringOutput( false ).c_str() );
403  fclose( fp );
404  }
405  catch( const IO_ERROR& ioe )
406  {
407  DisplayError( this, ioe.What() );
408  return;
409  }
410 
411  wxString msg = wxString::Format( FMT_EXPORTED, GetChars( dlg.GetPath() ) );
412  DisplayInfoMessage( this, msg );
413 }
414 
415 bool FOOTPRINT_EDIT_FRAME::SaveCurrentModule( const wxString* aLibPath )
416 {
417  wxString libPath = aLibPath ? *aLibPath : getLibPath();
418 
420 
421  // Legacy libraries are readable, but writing legacy format is not allowed
422  if( piType == IO_MGR::LEGACY )
423  {
425  return false;
426  }
427 
428  try
429  {
430  PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
431 
432  pi->FootprintSave( libPath, GetBoard()->m_Modules );
433  }
434  catch( const IO_ERROR& ioe )
435  {
436  DisplayError( this, ioe.What() );
437  return false;
438  }
439  return true;
440 }
441 
442 wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary(const wxString& aLibName )
443 {
444  // Kicad cannot write legacy format libraries, only .pretty new format
445  // because the legacy format cannot handle current features.
446  // The footprint library is actually a directory
447 
448  // if a library name is not given, prompt user for footprint library name, ending by ".pretty"
449  // Because there are constraints for the directory name to create,
450  // (the name should have the extension ".pretty", and the folder cannot be inside
451  // a footprint library), we do not use the standard wxDirDialog.
452 
453 
454  wxString initialPath = wxPathOnly( Prj().GetProjectFullName() );
455 
456  wxString libPath;
457 
458  if( aLibName.IsEmpty() )
459  {
460  DIALOG_SELECT_PRETTY_LIB dlg( this, initialPath );
461 
462  if( dlg.ShowModal() != wxID_OK )
463  return wxEmptyString;
464 
465  libPath = dlg.GetFullPrettyLibName();
466  }
467  else
468  {
469  wxFileName fn = aLibName;
470 
471  if( !fn.IsAbsolute() )
472  fn.MakeAbsolute( initialPath );
473 
474  // Enforce the extension:
475  fn.SetExt( KiCadFootprintLibPathExtension );
476 
477  libPath = fn.GetFullPath();
478  }
479 
480 
481  // We can save fp libs only using IO_MGR::KICAD_SEXP format (.pretty libraries)
483 
484  try
485  {
486  PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
487 
488  bool writable = false;
489  bool exists = false;
490 
491  try
492  {
493  writable = pi->IsFootprintLibWritable( libPath );
494  exists = true; // no exception was thrown, lib must exist.
495  }
496  catch( const IO_ERROR& )
497  {
498  // ignore, original values of 'writable' and 'exists' are accurate.
499  }
500 
501  if( exists )
502  {
503  if( !writable )
504  {
505  wxString msg = wxString::Format( FMT_LIB_READ_ONLY, GetChars( libPath ) );
506  DisplayError( this, msg );
507  return wxEmptyString;
508  }
509  else
510  {
511  wxString msg = wxString::Format( FMT_OK_OVERWRITE, GetChars( libPath ) );
512 
513  if( !IsOK( this, msg ) )
514  return wxEmptyString;
515 
516  pi->FootprintLibDelete( libPath );
517  }
518  }
519 
520  pi->FootprintLibCreate( libPath );
521  }
522  catch( const IO_ERROR& ioe )
523  {
524  DisplayError( this, ioe.What() );
525  return wxEmptyString;
526  }
527 
528  return libPath;
529 }
530 
531 
533 {
534  wxString nickname = GetCurrentLib();
535 
536  // Legacy libraries are readable, but modifying legacy format is not allowed
537  // So prompt the user if he try to delete a footprint from a legacy lib
538  wxString libfullname = Prj().PcbFootprintLibs()->FindRow(nickname)->GetFullURI();
540 
541  if( piType == IO_MGR::LEGACY )
542  {
544  return false;
545  }
546 
547  if( !Prj().PcbFootprintLibs()->IsFootprintLibWritable( nickname ) )
548  {
549  wxString msg = wxString::Format(
550  _( "Library '%s' is read only" ),
551  GetChars( nickname )
552  );
553 
554  DisplayError( this, msg );
555  return false;
556  }
557 
558  wxString fpid_txt = PCB_BASE_FRAME::SelectFootprint( this, nickname,
559  wxEmptyString, wxEmptyString, Prj().PcbFootprintLibs() );
560 
561  if( !fpid_txt )
562  return false;
563 
564  LIB_ID fpid( fpid_txt );
565  wxString fpname = fpid.GetLibItemName();
566 
567  // Confirmation
568  wxString msg = wxString::Format( FMT_OK_DELETE, fpname.GetData(), nickname.GetData() );
569 
570  if( !IsOK( this, msg ) )
571  return false;
572 
573  try
574  {
575  Prj().PcbFootprintLibs()->FootprintDelete( nickname, fpname );
576  }
577  catch( const IO_ERROR& ioe )
578  {
579  DisplayError( this, ioe.What() );
580  return false;
581  }
582 
583  msg.Printf( FMT_MOD_DELETED, fpname.GetData(), nickname.GetData() );
584 
585  SetStatusText( msg );
586 
587  return true;
588 }
589 
590 
591 void PCB_EDIT_FRAME::ArchiveModulesOnBoard( bool aStoreInNewLib, const wxString& aLibName )
592 {
593  if( GetBoard()->m_Modules == NULL )
594  {
595  DisplayInfoMessage( this, _( "No footprints to archive!" ) );
596  return;
597  }
598 
599  wxString footprintName;
600 
601  if( !aStoreInNewLib )
602  {
603  // The footprints are saved in an existing .pretty library in the fp lib table
604  PROJECT& prj = Prj();
605  wxString last_nickname = prj.GetRString( PROJECT::PCB_LIB_NICKNAME );
606  wxString nickname = SelectLibrary( last_nickname );
607 
608  if( !nickname ) // Aborted
609  return;
610 
611  prj.SetRString( PROJECT::PCB_LIB_NICKNAME, nickname );
612 
613  try
614  {
615  FP_LIB_TABLE* tbl = prj.PcbFootprintLibs();
616 
617  for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
618  {
619  if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
620  tbl->FootprintSave( nickname, curr_fp, false );
621  }
622  }
623  catch( const IO_ERROR& ioe )
624  {
625  DisplayError( this, ioe.What() );
626  }
627  }
628  else
629  {
630  // The footprints are saved in a new .pretty library.
631  // If this library already exists, all previous footprints will be deleted
632  wxString libPath = CreateNewLibrary( aLibName );
633 
634  if( libPath.IsEmpty() ) // Aborted
635  return;
636 
638  PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
639 
640  for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
641  {
642  try
643  {
644  if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
645  pi->FootprintSave( libPath, curr_fp );
646  }
647  catch( const IO_ERROR& ioe )
648  {
649  DisplayError( this, ioe.What() );
650  }
651  }
652  }
653 }
654 
655 
656 bool FOOTPRINT_EDIT_FRAME::SaveFootprintInLibrary( const wxString& aLibrary,
657  MODULE* aModule,
658  bool aOverwrite,
659  bool aDisplayDialog )
660 {
661  if( aModule == NULL )
662  return false;
663 
664  SetMsgPanel( aModule );
665 
666 
667  // Legacy libraries are readable, but modifying legacy format is not allowed
668  // So prompt the user if he try to add/replace a footprint in a legacy lib
669  wxString libfullname = Prj().PcbFootprintLibs()->FindRow( aLibrary )->GetFullURI();
671 
672  if( piType == IO_MGR::LEGACY )
673  {
675  return false;
676  }
677 
678  // Ask what to use as the footprint name in the library
679  wxString footprintName = aModule->GetFPID().GetLibItemName();
680 
681  if( aDisplayDialog )
682  {
683  wxTextEntryDialog dlg( this, _( "Name:" ), FMT_SAVE_MODULE, footprintName );
684 
685  if( dlg.ShowModal() != wxID_OK )
686  return false; // canceled by user
687 
688  footprintName = dlg.GetValue();
689  footprintName.Trim( true );
690  footprintName.Trim( false );
691 
692  if( footprintName.IsEmpty() )
693  return false;
694 
695  if( ! MODULE::IsLibNameValid( footprintName ) )
696  {
697  wxString msg = wxString::Format(
698  _("Error:\none of invalid chars '%s' found\nin '%s'" ),
700  GetChars( footprintName ) );
701 
702  DisplayError( NULL, msg );
703  return false;
704  }
705 
706  aModule->SetFPID( LIB_ID( footprintName ) );
707  }
708 
709  // Ensure this footprint has a libname
710  if( footprintName.IsEmpty() )
711  {
712  footprintName = wxT("noname");
713  aModule->SetFPID( LIB_ID( footprintName ) );
714  }
715 
716  bool module_exists = false;
717 
718  try
719  {
720  FP_LIB_TABLE* tbl = Prj().PcbFootprintLibs();
721 
722  MODULE* m = tbl->FootprintLoad( aLibrary, footprintName );
723 
724  if( m )
725  {
726  delete m;
727 
728  module_exists = true;
729 
730  // an existing footprint is found in current lib
731  if( aDisplayDialog )
732  {
733  wxString msg = wxString::Format( FMT_MOD_EXISTS,
734  footprintName.GetData(), aLibrary.GetData() );
735 
736  SetStatusText( msg );
737  }
738 
739  if( !aOverwrite )
740  {
741  // Do not save the given footprint: an old one exists
742  return true;
743  }
744  }
745 
746  // this always overwrites any existing footprint, but should yell on its
747  // own if the library or footprint is not writable.
748  tbl->FootprintSave( aLibrary, aModule );
749  }
750  catch( const IO_ERROR& ioe )
751  {
752  DisplayError( this, ioe.What() );
753  return false;
754  }
755 
756  if( aDisplayDialog )
757  {
758  wxString fmt = module_exists ?
759  _( "Component [%s] replaced in '%s'" ) :
760  _( "Component [%s] added in '%s'" );
761 
762  wxString msg = wxString::Format( fmt, footprintName.GetData(), aLibrary.GetData() );
763  SetStatusText( msg );
764  }
765 
766  return true;
767 }
768 
769 
770 MODULE* PCB_BASE_FRAME::CreateNewModule( const wxString& aModuleName )
771 {
772  // Creates a new footprint at position 0,0 which contains the minimal items:
773  // the reference and the value.
774  // Value : initialized to the footprint name.
775  // put on fab layer (front side)
776  // Reference : initialized to a default value (REF**).
777  // put on silkscreen layer (front side)
778 
779  wxString moduleName = aModuleName;
780 
781  // Ask for the new module name
782  if( moduleName.IsEmpty() )
783  {
784  wxTextEntryDialog dlg( this, FMT_MOD_REF, FMT_MOD_CREATE, moduleName );
785  dlg.SetTextValidator( FILE_NAME_CHAR_VALIDATOR( &moduleName ) );
786 
787  if( dlg.ShowModal() != wxID_OK )
788  return NULL; //Aborted by user
789  }
790 
791  moduleName.Trim( true );
792  moduleName.Trim( false );
793 
794  if( moduleName.IsEmpty() )
795  {
797  return NULL;
798  }
799 
800  // Creates the new module and add it to the head of the linked list of modules
801  MODULE* module = new MODULE( GetBoard() );
802 
803  GetBoard()->Add( module );
804 
805  // Update parameters: timestamp ...
806  module->SetLastEditTime();
807 
808  // Update its name in lib
809  module->SetFPID( LIB_ID( moduleName ) );
810 
811  wxPoint default_pos;
813 
814  // Update reference:
815  if( settings.m_RefDefaultText.IsEmpty() )
816  module->SetReference( moduleName );
817  else
818  module->SetReference( settings.m_RefDefaultText );
819 
820  module->Reference().SetThickness( settings.m_ModuleTextWidth );
821  module->Reference().SetTextSize( settings.m_ModuleTextSize );
822  default_pos.y = GetDesignSettings().m_ModuleTextSize.y / 2;
823  module->Reference().SetPosition( default_pos );
824  module->Reference().SetLayer( ToLAYER_ID( settings.m_RefDefaultlayer ) );
825  module->Reference().SetVisible( settings.m_RefDefaultVisibility );
826 
827  // Set the value field to a default value
828  if( settings.m_ValueDefaultText.IsEmpty() )
829  module->SetValue( moduleName );
830  else
831  module->SetValue( settings.m_ValueDefaultText );
832 
833  module->Value().SetThickness( GetDesignSettings().m_ModuleTextWidth );
834  module->Value().SetTextSize( GetDesignSettings().m_ModuleTextSize );
835  default_pos.y = -default_pos.y;
836  module->Value().SetPosition( default_pos );
837  module->Value().SetLayer( ToLAYER_ID( settings.m_ValueDefaultlayer ) );
838  module->Value().SetVisible( settings.m_ValueDefaultVisibility );
839 
840  SetMsgPanel( module );
841  return module;
842 }
843 
844 
845 wxString PCB_BASE_FRAME::SelectLibrary( const wxString& aNicknameExisting )
846 {
847  wxArrayString headers;
848 
849  headers.Add( _( "Nickname" ) );
850  headers.Add( _( "Description" ) );
851 
852  FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
853 
854  std::vector< wxArrayString > itemsToDisplay;
855  std::vector< wxString > nicknames = fptbl->GetLogicalLibs();
856 
857  for( unsigned i = 0; i < nicknames.size(); i++ )
858  {
859  wxArrayString item;
860 
861  item.Add( nicknames[i] );
862  item.Add( fptbl->GetDescription( nicknames[i] ) );
863 
864  itemsToDisplay.push_back( item );
865  }
866 
867  EDA_LIST_DIALOG dlg( this, FMT_SELECT_LIB, headers, itemsToDisplay, aNicknameExisting );
868 
869  if( dlg.ShowModal() != wxID_OK )
870  return wxEmptyString;
871 
872  wxString nickname = dlg.GetTextSelection();
873 
874  wxLogDebug( wxT( "Chose footprint library '%s'." ), GetChars( nickname ) );
875 
876  return nickname;
877 }
A dialog to select/create a .pretty folder which is a footprint library.
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"))
wxString m_RefDefaultText
Default ref text on fp creation.
void BuildListOfNets()
Definition: class_board.h:722
Definition of class FOOTPRINT_EDIT_FRAME.
bool m_ValueDefaultVisibility
Default value text visibility on fp creation.
#define FMT_MOD_NOT_FOUND
Definition: librairi.cpp:66
TEXTE_MODULE & Reference()
Definition: class_module.h:483
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"))
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
wxString GetTextSelection(int aColumn=0)
Function GetTextSelection return the selected text from aColumn in the wxListCtrl in the dialog...
Definition: displlst.cpp:137
Class PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files...
wxString m_mruPath
Most recently used path.
Definition: wxstruct.h:165
Class PROJECT holds project specific data.
Definition: project.h:56
#define FMT_FILE_NOT_FOUND
Definition: librairi.cpp:64
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
This file is part of the common library TODO brief description.
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.
Definition: librairi.cpp:229
#define FMT_MOD_REF
Definition: librairi.cpp:73
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
This file is part of the common library.
int m_ModuleTextWidth
Default footprint texts thickness.
Class BOARD to handle a board.
MODULE * FootprintLoad(const wxString &aNickname, const wxString &aFootprintName)
Function FootprintLoad.
const wxString KiCadFootprintFileExtension
virtual void FootprintSave(const wxString &aLibraryPath, const MODULE *aFootprint, const PROPERTIES *aProperties=NULL)
Function FootprintSave will write aModule to an existing library located at aLibraryPath.
Definition: plugin.cpp:85
#define FMT_MOD_CREATE
Definition: librairi.cpp:76
void SetVisible(bool aVisible)
Definition: eda_text.h:175
BOARD * GetBoard() const
virtual wxConfigBase * config()
Function config returns the wxConfigBase used in SaveSettings(), and is overloaded in KICAD_MANAGER_F...
Definition: basicframe.cpp:380
class EDA_LIST_DIALOG
int m_ValueDefaultlayer
Default value text layer on fp creation.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
static const wxChar * StringLibNameInvalidChars(bool aUserReadable)
static function StringLibNameInvalidChars Test for validity of the name in a library of the footprint...
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
wxSize m_ModuleTextSize
Default footprint texts size.
void SetLastEditTime(time_t aTime)
Definition: class_module.h:302
#define FMT_SELECT_LIB
Definition: librairi.cpp:80
MODULE * CreateNewModule(const wxString &aModuleName)
Function CreateNewModule Creates a new module or footprint, at position 0,0 The new module contains o...
Definition: librairi.cpp:770
#define FMT_OK_DELETE
Definition: librairi.cpp:62
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
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Function GuessPluginTypeFromLibPath returns a plugin type given a footprint library's libPath...
Definition: io_mgr.cpp:132
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
const wxString GetCurrentLib() const
Return the current library nickname.
This file contains miscellaneous commonly used macros and functions.
Class FILE_NAME_CHAR_VALIDATOR.
Definition: validators.h:43
static IO_MGR::PCB_FILE_T detect_file_type(FILE *aFile, const wxFileName &aFileName, wxString *aName)
Read a file to detect the type.
Definition: librairi.cpp:132
void PlaceModule(MODULE *aModule, wxDC *aDC, bool aDoNotRecreateRatsnest=false)
Function PlaceModule places aModule at the current cursor position and updates module coordinates wit...
Definition: modules.cpp:352
virtual bool IsFootprintLibWritable(const wxString &aLibraryPath)
Function IsFootprintLibWritable returns true iff the library at aLibraryPath is writable.
Definition: plugin.cpp:114
BOARD_ITEM * Next() const
Class RELEASER releases a PLUGIN in the context of a potential thrown exception, through its destruct...
Definition: io_mgr.h:546
const LIB_ID & GetFPID() const
Definition: class_module.h:184
#define FMT_EXPORT_MODULE
Definition: librairi.cpp:71
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
static wxFileName getFootprintFilenameFromUser(wxWindow *aParent, const wxString &aLastPath)
Prompt the user for a module file to open.
Definition: librairi.cpp:103
#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
void ArchiveModulesOnBoard(bool aStoreInNewLib, const wxString &aLibName=wxEmptyString)
Function ArchiveModulesOnBoard Save modules in a library:
Definition: librairi.cpp:591
virtual MODULE * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL)
Function FootprintLoad loads a footprint having aFootprintName from the aLibraryPath containing a lib...
Definition: plugin.cpp:76
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Function SetMsgPanel clears the message panel and populates it with the contents of aList...
Definition: draw_frame.cpp:784
#define FMT_NOT_MODULE
Definition: librairi.cpp:65
std::string GetStringOutput(bool doClear)
virtual bool FootprintLibDelete(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Function FootprintLibDelete deletes an existing footprint library and returns true, or if library does not exist returns false, or throws an exception if library exists but is read only or cannot be deleted for some other reason.
Definition: plugin.cpp:106
TEXTE_MODULE & Value()
read/write accessors:
Definition: class_module.h:482
#define FMT_NO_REF_ABORTED
Definition: librairi.cpp:79
const wxString GetDescription(const wxString &aNickname)
const UTF8 & GetLibItemName() const
Definition: lib_id.h:115
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:180
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
Adds an item to the container.
#define EXPORT_IMPORT_LASTPATH_KEY
Definition: librairi.cpp:95
const wxString KiCadFootprintLibPathExtension
wxString ModLegacyExportFileWildcard()
The common library.
Helper dialog and control classes.
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use...
Definition: draw_frame.h:862
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:104
void SetReference(const wxString &aReference)
Function SetReference.
Definition: class_module.h:449
virtual void SetPosition(const wxPoint &aPos) override
void SetPosition(const wxPoint &aPos) override
wxString m_ValueDefaultText
Default value text on fp creation.
void Format(BOARD_ITEM *aItem, int aNestLevel=0) const
Function Format outputs aItem to aFormatter in s-expression format.
#define FOOTPRINT_LIBRARY_HEADER_CNT
Definition: legacy_plugin.h:39
#define FMT_IMPORT_MODULE
Definition: librairi.cpp:63
#define FMT_MOD_EXISTS
Definition: librairi.cpp:78
void Export_Module(MODULE *aModule)
Function Export_Module Create a file containing only one footprint.
Definition: librairi.cpp:341
VTBL_ENTRY void SetRString(RSTRING_T aStringId, const wxString &aString)
Function SetRString stores a "retained string", which is any session and project specific string iden...
Definition: project.cpp:173
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
bool DeleteModuleFromCurrentLibrary()
Function DeleteModuleFromCurrentLibrary prompts user for footprint name, then deletes it from current...
Definition: librairi.cpp:532
bool SaveCurrentModule(const wxString *aLibPath=NULL)
Function SaveCurrentModule saves the module which is currently being edited into aLibPath or into the...
Definition: librairi.cpp:415
const FP_LIB_TABLE_ROW * FindRow(const wxString &aNickName)
Function FindRow.
static bool IsLibNameValid(const wxString &aName)
static function IsLibNameValid Test for validity of a name of a footprint to be used in a footprint l...
wxString CreateNewLibrary(const wxString &aLibName=wxEmptyString)
Function CreateNewLibrary If a library name is given, creates a new footprint library in the project ...
Definition: librairi.cpp:442
VTBL_ENTRY const wxString & GetRString(RSTRING_T aStringId)
Function GetRString returns a "retained string", which is any session and project specific string ide...
Definition: project.cpp:188
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
#define CTL_FOR_LIBRARY
Format output for a footprint library instead of clipboard or BOARD.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:53
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
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
#define FMT_MOD_DELETED
Definition: librairi.cpp:75
const wxString getLibPath()
The libPath is not publicly visible, grab it from the FP_LIB_TABLE if we must.
void updateView()
Reloads displayed items and sets view.
void SetValue(const wxString &aValue)
Function SetValue.
Definition: class_module.h:476
static MODULE * parse_module_kicad(const wxFileName &aFileName)
Parse a KICAD footprint.
Definition: librairi.cpp:208
#define FMT_LIB_READ_ONLY
Definition: librairi.cpp:69
int m_RefDefaultlayer
Default ref text layer on fp creation.
#define FMT_EXPORTED
Definition: librairi.cpp:74
virtual void FootprintLibCreate(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Function FootprintLibCreate creates a new empty footprint library at aLibraryPath empty...
Definition: plugin.cpp:99
#define FMT_SAVE_MODULE
Definition: librairi.cpp:72
wxString SelectLibrary(const wxString &aNicknameExisting)
Function SelectLibrary puts up a dialog and allows the user to pick a library, for unspecified use...
Definition: librairi.cpp:845
Module description (excepted pads)
bool SaveFootprintInLibrary(const wxString &aLibName, MODULE *aModule, bool aOverwrite, bool aDisplayDialog)
Function SaveFootprintInLibrary Save in an existing library a given footprint.
Definition: librairi.cpp:656
SAVE_T FootprintSave(const wxString &aNickname, const MODULE *aFootprint, bool aOverwrite=true)
Function FootprintSave.
void FootprintDelete(const wxString &aNickname, const wxString &aFootprintName)
Function FootprintDelete.
MODULE * Import_Module(const wxString &aName=wxT(""))
Function Import_Module Read a file containing only one footprint.
Definition: librairi.cpp:254
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
#define FMT_OK_OVERWRITE
Definition: librairi.cpp:60
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:131
bool m_RefDefaultVisibility
Default ref text visibility on fp creation.
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:185
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:774
wxString KiCadFootprintLibFileWildcard()
Custom text control validator definitions.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:148
wxString GedaPcbFootprintLibFileWildcard()
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE...
wxString SelectFootprint(EDA_DRAW_FRAME *aWindow, const wxString &aLibraryName, const wxString &aMask, const wxString &aKeyWord, FP_LIB_TABLE *aTable)
Function SelectFootprint displays a list of modules found in all libraries or a given library...
Definition: loadcmp.cpp:362
S-expression Pcbnew file format.
Definition: io_mgr.h:54
Class BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.
int m_Status_Pcb
Flags used in ratsnest calculation and update.
Definition: class_board.h:237