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 static const wxString ModLegacyExportFileWildcard( _( "Legacy foot print export files (*.emp)|*.emp" ) );
95 
96 
97 #define EXPORT_IMPORT_LASTPATH_KEY wxT( "import_last_path" )
98 
99 
105 static wxFileName getFootprintFilenameFromUser( wxWindow* aParent, const wxString& aLastPath )
106 {
107  static int lastFilterIndex = 0; // To store the last choice during a session.
108  wxString wildCard;
109 
110  wildCard << wxGetTranslation( KiCadFootprintLibFileWildcard ) << wxChar( '|' )
111  << wxGetTranslation( ModLegacyExportFileWildcard ) << wxChar( '|' )
112  << wxGetTranslation( GedaPcbFootprintLibFileWildcard ) << wxChar( '|' );
113 
114  wxFileDialog dlg( aParent, FMT_IMPORT_MODULE, aLastPath, wxEmptyString, wildCard,
115  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
116 
117  dlg.SetFilterIndex( lastFilterIndex );
118 
119  if( dlg.ShowModal() == wxID_CANCEL )
120  return wxFileName();
121 
122  lastFilterIndex = dlg.GetFilterIndex();
123 
124  return wxFileName( dlg.GetPath() );
125 }
126 
127 
134 static IO_MGR::PCB_FILE_T detect_file_type( FILE* aFile, const wxFileName& aFileName, wxString* aName )
135 {
136  FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
137  WHITESPACE_FILTER_READER reader( freader );
138  IO_MGR::PCB_FILE_T file_type;
139 
140  wxASSERT( aName );
141 
142  reader.ReadLine();
143  char* line = reader.Line();
144 
145  if( !strncasecmp( line, "(module", strlen( "(module" ) ) )
146  {
147  file_type = IO_MGR::KICAD;
148  *aName = aFileName.GetName();
149  }
150  else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
151  {
152  file_type = IO_MGR::LEGACY;
153  while( reader.ReadLine() )
154  {
155  if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
156  {
157  *aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
158  break;
159  }
160  }
161  }
162  else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
163  {
164  file_type = IO_MGR::GEDA_PCB;
165  *aName = aFileName.GetName();
166  }
167  else
168  {
169  file_type = IO_MGR::FILE_TYPE_NONE;
170  }
171 
172  return file_type;
173 }
174 
175 
183  const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
184  const wxString& aName )
185 {
186  wxString path;
187 
188  switch( aFileType )
189  {
190  case IO_MGR::GEDA_PCB:
191  path = aFileName.GetPath();
192  break;
193  case IO_MGR::LEGACY:
194  path = aFileName.GetFullPath();
195  break;
196  default:
197  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
198  }
199 
200  PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
201 
202  return pi->FootprintLoad( path, aName );
203 }
204 
205 
210 static MODULE* parse_module_kicad( const wxFileName& aFileName )
211 {
212  wxString fcontents;
213  PCB_IO pcb_io;
214  wxFFile f( aFileName.GetFullPath() );
215 
216  if( !f.IsOpened() )
217  return NULL;
218 
219  f.ReadAll( &fcontents );
220 
221  return dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
222 }
223 
224 
231 MODULE* try_load_footprint( const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
232  const wxString& aName )
233 {
234  MODULE* module;
235 
236  switch( aFileType )
237  {
238  case IO_MGR::GEDA_PCB:
239  case IO_MGR::LEGACY:
240  module = parse_module_with_plugin( aFileName, aFileType, aName );
241  break;
242 
243  case IO_MGR::KICAD:
244  module = parse_module_kicad( aFileName );
245  break;
246 
247  default:
248  wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
249  module = NULL;
250  }
251 
252  return module;
253 }
254 
255 
257 {
258  wxString lastOpenedPathForLoading = m_mruPath;
259  wxConfigBase* config = Kiface().KifaceSettings();
260 
261  if( config )
262  config->Read( EXPORT_IMPORT_LASTPATH_KEY, &lastOpenedPathForLoading );
263 
264  wxFileName fn = getFootprintFilenameFromUser( this, lastOpenedPathForLoading );
265 
266  if( !fn.IsOk() )
267  return NULL;
268 
269  FILE* fp = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
270 
271  if( !fp )
272  {
273  wxString msg = wxString::Format( FMT_FILE_NOT_FOUND, GetChars( fn.GetFullPath() ) );
274  DisplayError( this, msg );
275  return NULL;
276  }
277 
278  if( config ) // Save file path
279  {
280  lastOpenedPathForLoading = fn.GetPath();
281  config->Write( EXPORT_IMPORT_LASTPATH_KEY, lastOpenedPathForLoading );
282  }
283 
284  wxString moduleName;
285  IO_MGR::PCB_FILE_T fileType = detect_file_type( fp, fn.GetFullPath(), &moduleName );
286 
287  if( fileType == IO_MGR::FILE_TYPE_NONE )
288  {
289  DisplayError( this, FMT_NOT_MODULE );
290  return NULL;
291  }
292 
293  MODULE* module = NULL;
294 
295  try
296  {
297  module = try_load_footprint( fn, fileType, moduleName );
298 
299  if( !module )
300  {
301  wxString msg = wxString::Format(
302  FMT_MOD_NOT_FOUND, GetChars( moduleName ), GetChars( fn.GetFullPath() ) );
303  DisplayError( this, msg );
304  return NULL;
305  }
306  }
307  catch( const IO_ERROR& ioe )
308  {
309  DisplayError( this, ioe.What() );
310 
311  // if the footprint is not loaded, exit.
312  // However, even if an error happens, it can be loaded, because in KICAD and GPCB format,
313  // a fp library is a set of separate files, and the error(s) are not necessary when
314  // reading the selected file
315 
316  if( !module )
317  return NULL;
318  }
319 
320  // Insert footprint in list
321  GetBoard()->Add( module );
322 
323  // Display info :
324  SetMsgPanel( module );
325  PlaceModule( module, NULL );
326 
327  if( IsGalCanvasActive() )
328  module->SetPosition( wxPoint( 0, 0 ) );
329 
330  GetBoard()->m_Status_Pcb = 0;
332  updateView();
333 
334  return module;
335 }
336 
337 
339 {
340  wxFileName fn;
341  wxConfigBase* config = Kiface().KifaceSettings();
342 
343  if( !aModule )
344  return;
345 
346  fn.SetName( FROM_UTF8( aModule->GetFPID().GetLibItemName() ) );
347 
348  wxString wildcard = wxGetTranslation( KiCadFootprintLibFileWildcard );
349 
350  fn.SetExt( KiCadFootprintFileExtension );
351 
352  if( config )
353  {
354  wxString path;
355  config->Read( EXPORT_IMPORT_LASTPATH_KEY, &path, m_mruPath );
356  fn.SetPath( path );
357  }
358 
359  wxFileDialog dlg( this, FMT_EXPORT_MODULE, fn.GetPath(), fn.GetFullName(),
360  wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
361 
362  if( dlg.ShowModal() == wxID_CANCEL )
363  return;
364 
365  fn = dlg.GetPath();
366 
367  if( config ) // Save file path
368  {
369  config->Write( EXPORT_IMPORT_LASTPATH_KEY, fn.GetPath() );
370  }
371 
372  try
373  {
374  // Export as *.kicad_pcb format, using a strategy which is specifically chosen
375  // as an example on how it could also be used to send it to the system clipboard.
376 
377  PCB_IO pcb_io( CTL_FOR_LIBRARY );
378 
379  /* This module should *already* be "normalized" in a way such that
380  orientation is zero, etc., since it came from module editor.
381 
382  module->SetTimeStamp( 0 );
383  module->SetParent( 0 );
384  module->SetOrientation( 0 );
385  */
386 
387  pcb_io.Format( aModule );
388 
389  FILE* fp = wxFopen( dlg.GetPath(), wxT( "wt" ) );
390 
391  if( fp == NULL )
392  {
393  wxMessageBox( wxString::Format(
394  _( "Unable to create or write file '%s'" ),
395  GetChars( dlg.GetPath() ) ) );
396  return;
397  }
398 
399  fprintf( fp, "%s", pcb_io.GetStringOutput( false ).c_str() );
400  fclose( fp );
401  }
402  catch( const IO_ERROR& ioe )
403  {
404  DisplayError( this, ioe.What() );
405  return;
406  }
407 
408  wxString msg = wxString::Format( FMT_EXPORTED, GetChars( dlg.GetPath() ) );
409  DisplayInfoMessage( this, msg );
410 }
411 
412 bool FOOTPRINT_EDIT_FRAME::SaveCurrentModule( const wxString* aLibPath )
413 {
414  wxString libPath = aLibPath ? *aLibPath : getLibPath();
415 
417 
418  // Legacy libraries are readable, but writing legacy format is not allowed
419  if( piType == IO_MGR::LEGACY )
420  {
422  return false;
423  }
424 
425  try
426  {
427  PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
428 
429  pi->FootprintSave( libPath, GetBoard()->m_Modules );
430  }
431  catch( const IO_ERROR& ioe )
432  {
433  DisplayError( this, ioe.What() );
434  return false;
435  }
436  return true;
437 }
438 
440 {
441  // Kicad cannot write legacy format libraries, only .pretty new format
442  // because the legacy format cannot handle current features.
443  // The footprint library is actually a directory
444 
445  // prompt user for footprint library name, ending by ".pretty"
446  // Because there are constraints for the directory name to create,
447  // (the name should have the extension ".pretty", and the folder cannot be inside
448  // a footprint library), we do not use the standard wxDirDialog.
449 
450  wxString initialPath = wxPathOnly( Prj().GetProjectFullName() );
451  DIALOG_SELECT_PRETTY_LIB dlg( this, initialPath );
452 
453  if( dlg.ShowModal() != wxID_OK )
454  return wxEmptyString;
455 
456  wxString libPath = dlg.GetFullPrettyLibName();
457 
458  // We can save fp libs only using IO_MGR::KICAD format (.pretty libraries)
460 
461  try
462  {
463  PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
464 
465  bool writable = false;
466  bool exists = false;
467 
468  try
469  {
470  writable = pi->IsFootprintLibWritable( libPath );
471  exists = true; // no exception was thrown, lib must exist.
472  }
473  catch( const IO_ERROR& )
474  {
475  // ignore, original values of 'writable' and 'exists' are accurate.
476  }
477 
478  if( exists )
479  {
480  if( !writable )
481  {
482  wxString msg = wxString::Format( FMT_LIB_READ_ONLY, GetChars( libPath ) );
483  DisplayError( this, msg );
484  return wxEmptyString;
485  }
486  else
487  {
488  wxString msg = wxString::Format( FMT_OK_OVERWRITE, GetChars( libPath ) );
489 
490  if( !IsOK( this, msg ) )
491  return wxEmptyString;
492 
493  pi->FootprintLibDelete( libPath );
494  }
495  }
496 
497  pi->FootprintLibCreate( libPath );
498  }
499  catch( const IO_ERROR& ioe )
500  {
501  DisplayError( this, ioe.What() );
502  return wxEmptyString;
503  }
504 
505  return libPath;
506 }
507 
508 
510 {
511  wxString nickname = GetCurrentLib();
512 
513  // Legacy libraries are readable, but modifying legacy format is not allowed
514  // So prompt the user if he try to delete a footprint from a legacy lib
515  wxString libfullname = Prj().PcbFootprintLibs()->FindRow(nickname)->GetFullURI();
517 
518  if( piType == IO_MGR::LEGACY )
519  {
521  return false;
522  }
523 
524  if( !Prj().PcbFootprintLibs()->IsFootprintLibWritable( nickname ) )
525  {
526  wxString msg = wxString::Format(
527  _( "Library '%s' is read only" ),
528  GetChars( nickname )
529  );
530 
531  DisplayError( this, msg );
532  return false;
533  }
534 
535  wxString fpid_txt = PCB_BASE_FRAME::SelectFootprint( this, nickname,
536  wxEmptyString, wxEmptyString, Prj().PcbFootprintLibs() );
537 
538  if( !fpid_txt )
539  return false;
540 
541  LIB_ID fpid( fpid_txt );
542  wxString fpname = FROM_UTF8( fpid.GetLibItemName() );
543 
544  // Confirmation
545  wxString msg = wxString::Format( FMT_OK_DELETE, fpname.GetData(), nickname.GetData() );
546 
547  if( !IsOK( this, msg ) )
548  return false;
549 
550  try
551  {
552  Prj().PcbFootprintLibs()->FootprintDelete( nickname, fpname );
553  }
554  catch( const IO_ERROR& ioe )
555  {
556  DisplayError( this, ioe.What() );
557  return false;
558  }
559 
560  msg.Printf( FMT_MOD_DELETED, fpname.GetData(), nickname.GetData() );
561 
562  SetStatusText( msg );
563 
564  return true;
565 }
566 
567 
568 void PCB_EDIT_FRAME::ArchiveModulesOnBoard( bool aStoreInNewLib )
569 {
570  if( GetBoard()->m_Modules == NULL )
571  {
572  DisplayInfoMessage( this, _( "No footprints to archive!" ) );
573  return;
574  }
575 
576  wxString footprintName;
577 
578  if( !aStoreInNewLib )
579  {
580  // The footprints are saved in an existing .pretty library in the fp lib table
581  PROJECT& prj = Prj();
582  wxString last_nickname = prj.GetRString( PROJECT::PCB_LIB_NICKNAME );
583  wxString nickname = SelectLibrary( last_nickname );
584 
585  if( !nickname ) // Aborted
586  return;
587 
588  prj.SetRString( PROJECT::PCB_LIB_NICKNAME, nickname );
589 
590  try
591  {
592  FP_LIB_TABLE* tbl = prj.PcbFootprintLibs();
593 
594  for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
595  {
596  if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
597  tbl->FootprintSave( nickname, curr_fp, false );
598  }
599  }
600  catch( const IO_ERROR& ioe )
601  {
602  DisplayError( this, ioe.What() );
603  }
604  }
605  else
606  {
607  // The footprints are saved in a new .pretty library.
608  // If this library already exists, all previous footprints will be deleted
609  wxString libPath = CreateNewLibrary();
610 
611  if( libPath.IsEmpty() ) // Aborted
612  return;
613 
615  PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
616 
617  for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
618  {
619  try
620  {
621  if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
622  pi->FootprintSave( libPath, curr_fp );
623  }
624  catch( const IO_ERROR& ioe )
625  {
626  DisplayError( this, ioe.What() );
627  }
628  }
629  }
630 }
631 
632 
633 bool FOOTPRINT_EDIT_FRAME::SaveFootprintInLibrary( const wxString& aLibrary,
634  MODULE* aModule,
635  bool aOverwrite,
636  bool aDisplayDialog )
637 {
638  if( aModule == NULL )
639  return false;
640 
641  SetMsgPanel( aModule );
642 
643 
644  // Legacy libraries are readable, but modifying legacy format is not allowed
645  // So prompt the user if he try to add/replace a footprint in a legacy lib
646  wxString libfullname = Prj().PcbFootprintLibs()->FindRow( aLibrary )->GetFullURI();
648 
649  if( piType == IO_MGR::LEGACY )
650  {
652  return false;
653  }
654 
655  // Ask what to use as the footprint name in the library
656  wxString footprintName = FROM_UTF8( aModule->GetFPID().GetLibItemName() );
657 
658  if( aDisplayDialog )
659  {
660  wxTextEntryDialog dlg( this, _( "Name:" ), FMT_SAVE_MODULE, footprintName );
661 
662  if( dlg.ShowModal() != wxID_OK )
663  return false; // canceled by user
664 
665  footprintName = dlg.GetValue();
666  footprintName.Trim( true );
667  footprintName.Trim( false );
668 
669  if( footprintName.IsEmpty() )
670  return false;
671 
672  if( ! MODULE::IsLibNameValid( footprintName ) )
673  {
674  wxString msg = wxString::Format(
675  _("Error:\none of invalid chars '%s' found\nin '%s'" ),
677  GetChars( footprintName ) );
678 
679  DisplayError( NULL, msg );
680  return false;
681  }
682 
683  aModule->SetFPID( LIB_ID( footprintName ) );
684  }
685 
686  // Ensure this footprint has a libname
687  if( footprintName.IsEmpty() )
688  {
689  footprintName = wxT("noname");
690  aModule->SetFPID( LIB_ID( footprintName ) );
691  }
692 
693  bool module_exists = false;
694 
695  try
696  {
697  FP_LIB_TABLE* tbl = Prj().PcbFootprintLibs();
698 
699  MODULE* m = tbl->FootprintLoad( aLibrary, footprintName );
700 
701  if( m )
702  {
703  delete m;
704 
705  module_exists = true;
706 
707  // an existing footprint is found in current lib
708  if( aDisplayDialog )
709  {
710  wxString msg = wxString::Format( FMT_MOD_EXISTS,
711  footprintName.GetData(), aLibrary.GetData() );
712 
713  SetStatusText( msg );
714  }
715 
716  if( !aOverwrite )
717  {
718  // Do not save the given footprint: an old one exists
719  return true;
720  }
721  }
722 
723  // this always overwrites any existing footprint, but should yell on its
724  // own if the library or footprint is not writable.
725  tbl->FootprintSave( aLibrary, aModule );
726  }
727  catch( const IO_ERROR& ioe )
728  {
729  DisplayError( this, ioe.What() );
730  return false;
731  }
732 
733  if( aDisplayDialog )
734  {
735  wxString fmt = module_exists ?
736  _( "Component [%s] replaced in '%s'" ) :
737  _( "Component [%s] added in '%s'" );
738 
739  wxString msg = wxString::Format( fmt, footprintName.GetData(), aLibrary.GetData() );
740  SetStatusText( msg );
741  }
742 
743  return true;
744 }
745 
746 
747 MODULE* PCB_BASE_FRAME::CreateNewModule( const wxString& aModuleName )
748 {
749  // Creates a new footprint at position 0,0 which contains the minimal items:
750  // the reference and the value.
751  // Value : initialized to the footprint name.
752  // put on fab layer (front side)
753  // Reference : initialized to a default value (REF**).
754  // put on silkscreen layer (front side)
755 
756  wxString moduleName = aModuleName;
757 
758  // Ask for the new module name
759  if( moduleName.IsEmpty() )
760  {
761  wxTextEntryDialog dlg( this, FMT_MOD_REF, FMT_MOD_CREATE, moduleName );
762  dlg.SetTextValidator( FILE_NAME_CHAR_VALIDATOR( &moduleName ) );
763 
764  if( dlg.ShowModal() != wxID_OK )
765  return NULL; //Aborted by user
766  }
767 
768  moduleName.Trim( true );
769  moduleName.Trim( false );
770 
771  if( moduleName.IsEmpty() )
772  {
774  return NULL;
775  }
776 
777  // Creates the new module and add it to the head of the linked list of modules
778  MODULE* module = new MODULE( GetBoard() );
779 
780  GetBoard()->Add( module );
781 
782  // Update parameters: timestamp ...
783  module->SetLastEditTime();
784 
785  // Update its name in lib
786  module->SetFPID( LIB_ID( moduleName ) );
787 
788  wxPoint default_pos;
790 
791  // Update reference:
792  if( settings.m_RefDefaultText.IsEmpty() )
793  module->SetReference( moduleName );
794  else
795  module->SetReference( settings.m_RefDefaultText );
796 
797  module->Reference().SetThickness( settings.m_ModuleTextWidth );
798  module->Reference().SetTextSize( settings.m_ModuleTextSize );
799  default_pos.y = GetDesignSettings().m_ModuleTextSize.y / 2;
800  module->Reference().SetPosition( default_pos );
801  module->Reference().SetLayer( ToLAYER_ID( settings.m_RefDefaultlayer ) );
802  module->Reference().SetVisible( settings.m_RefDefaultVisibility );
803 
804  // Set the value field to a default value
805  if( settings.m_ValueDefaultText.IsEmpty() )
806  module->SetValue( moduleName );
807  else
808  module->SetValue( settings.m_ValueDefaultText );
809 
810  module->Value().SetThickness( GetDesignSettings().m_ModuleTextWidth );
811  module->Value().SetTextSize( GetDesignSettings().m_ModuleTextSize );
812  default_pos.y = -default_pos.y;
813  module->Value().SetPosition( default_pos );
814  module->Value().SetLayer( ToLAYER_ID( settings.m_ValueDefaultlayer ) );
815  module->Value().SetVisible( settings.m_ValueDefaultVisibility );
816 
817  SetMsgPanel( module );
818  return module;
819 }
820 
821 
822 wxString PCB_BASE_FRAME::SelectLibrary( const wxString& aNicknameExisting )
823 {
824  wxArrayString headers;
825 
826  headers.Add( _( "Nickname" ) );
827  headers.Add( _( "Description" ) );
828 
829  FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
830 
831  std::vector< wxArrayString > itemsToDisplay;
832  std::vector< wxString > nicknames = fptbl->GetLogicalLibs();
833 
834  for( unsigned i = 0; i < nicknames.size(); i++ )
835  {
836  wxArrayString item;
837 
838  item.Add( nicknames[i] );
839  item.Add( fptbl->GetDescription( nicknames[i] ) );
840 
841  itemsToDisplay.push_back( item );
842  }
843 
844  EDA_LIST_DIALOG dlg( this, FMT_SELECT_LIB, headers, itemsToDisplay, aNicknameExisting );
845 
846  if( dlg.ShowModal() != wxID_OK )
847  return wxEmptyString;
848 
849  wxString nickname = dlg.GetTextSelection();
850 
851  wxLogDebug( wxT( "Chose footprint library '%s'." ), GetChars( nickname ) );
852 
853  return nickname;
854 }
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:736
const wxString KiCadFootprintFileExtension
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:463
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:156
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:231
#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.
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
const wxString KiCadFootprintLibFileWildcard
BOARD * GetBoard() const
virtual wxConfigBase * config()
Function config returns the wxConfigBase used in SaveSettings(), and is overloaded in KICAD_MANAGER_F...
Definition: basicframe.cpp:361
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...
Class LIB_ID.
Definition: lib_id.h:56
wxSize m_ModuleTextSize
Default footprint texts size.
void SetLastEditTime(time_t aTime)
Definition: class_module.h:282
#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:747
#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:182
const wxString GetFullURI(bool aSubstituted=false) const
Function GetFullURI.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
wxString CreateNewLibrary()
Function CreateNewLibrary prompts user for a library path, then creates a new footprint library at th...
Definition: librairi.cpp:439
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:134
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
static const wxString ModLegacyExportFileWildcard(_("Legacy foot print export files (*.emp)|*.emp"))
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:474
const LIB_ID & GetFPID() const
Definition: class_module.h:164
#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:105
#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
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:773
#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:462
#define FMT_NO_REF_ABORTED
Definition: librairi.cpp:79
const wxString GetDescription(const wxString &aNickname)
Function GetDescription.
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
S-expression Pcbnew file format.
Definition: io_mgr.h:54
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:182
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:97
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:809
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
void SetReference(const wxString &aReference)
Function SetReference.
Definition: class_module.h:429
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:338
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:162
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:509
bool SaveCurrentModule(const wxString *aLibPath=NULL)
Function SaveCurrentModule saves the module which is currently being edited into aLibPath or into the...
Definition: librairi.cpp:412
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...
void DisplayInfoMessage(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:89
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:177
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
MODULE * Import_Module()
Function Import_Module Read a file containing only one footprint.
Definition: librairi.cpp:256
#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
void ArchiveModulesOnBoard(bool aStoreInNewLib)
Function ArchiveModulesOnBoard Save modules in a library:
Definition: librairi.cpp:568
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:456
static MODULE * parse_module_kicad(const wxFileName &aFileName)
Parse a KICAD footprint.
Definition: librairi.cpp:210
#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:822
const wxString GedaPcbFootprintLibFileWildcard
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:633
SAVE_T FootprintSave(const wxString &aNickname, const MODULE *aFootprint, bool aOverwrite=true)
Function FootprintSave.
void FootprintDelete(const wxString &aNickname, const wxString &aFootprintName)
Function FootprintDelete.
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:69
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:111
bool m_RefDefaultVisibility
Default ref text visibility on fp creation.
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:165
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:767
Custom text control validator definitions.
void SetThickness(int aNewThickness)
Function SetThickness sets pen width.
Definition: eda_text.h:148
std::vector< wxString > GetLogicalLibs()
Function GetLogicalLibs.
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:353
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