1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file librairi.cpp
* @brief Manage module (footprint) libraries.
*/
#include <wx/ffile.h>
#include <wx/stdpaths.h>
#include <fctsys.h>
#include <pgm_base.h>
#include <kiface_i.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <pcb_edit_frame.h>
#include <dialog_helpers.h>
#include <filter_reader.h>
#include <macros.h>
#include <fp_lib_table.h>
#include <validators.h>
#include <dialog_text_entry.h>
#include <class_board.h>
#include <class_module.h>
#include <pcbnew.h>
#include <footprint_edit_frame.h>
#include <wildcards_and_files_ext.h>
#include <kicad_plugin.h>
#include <legacy_plugin.h>
#include <dialog_select_pretty_lib.h>
// unique, "file local" translations:
#define FMT_OK_OVERWRITE _( "Library \"%s\" exists, OK to replace ?" )
#define FMT_CREATE_LIB _( "Create New Library Folder (the .pretty folder is the library)" )
#define FMT_OK_DELETE _( "OK to delete footprint \"%s\" in library \"%s\"" )
#define FMT_IMPORT_MODULE _( "Import Footprint" )
#define FMT_FILE_NOT_FOUND _( "File \"%s\" not found" )
#define FMT_NOT_MODULE _( "Not a footprint file" )
#define FMT_MOD_NOT_FOUND _( "Unable to find or load footprint \"%s\" from lib path \"%s\"" )
#define FMT_BAD_PATH _( "Unable to find or load footprint from path \"%s\"" )
#define FMT_BAD_PATHS _( "The footprint library \"%s\" could not be found in any of the search paths." )
#define FMT_LIB_READ_ONLY _( "Library \"%s\" is read only, not writable" )
#define FMT_EXPORT_MODULE _( "Export Footprint" )
#define FMT_SAVE_MODULE _( "Save Footprint" )
#define FMT_MOD_REF _( "Enter footprint name:" )
#define FMT_EXPORTED _( "Footprint exported to file \"%s\"" )
#define FMT_MOD_DELETED _( "Footprint \"%s\" deleted from library \"%s\"" )
#define FMT_MOD_CREATE _( "New Footprint" )
#define FMT_MOD_EXISTS _( "Footprint \"%s\" already exists in library \"%s\"" )
#define FMT_NO_REF_ABORTED _( "No footprint name defined." )
#define FMT_SELECT_LIB _( "Select Library" )
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" ) );
#define EXPORT_IMPORT_LASTPATH_KEY wxT( "import_last_path" )
/**
* Prompt the user for a module file to open.
* @param aParent - parent window for the dialog
* @param aLastPath - last opened path
*/
static wxFileName getFootprintFilenameFromUser( wxWindow* aParent, const wxString& aLastPath )
{
static int lastFilterIndex = 0; // To store the last choice during a session.
wxString wildCard;
wildCard << KiCadFootprintLibFileWildcard() << wxChar( '|' )
<< ModLegacyExportFileWildcard() << wxChar( '|' )
<< GedaPcbFootprintLibFileWildcard();
wxFileDialog dlg( aParent, FMT_IMPORT_MODULE, aLastPath, wxEmptyString, wildCard,
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
dlg.SetFilterIndex( lastFilterIndex );
if( dlg.ShowModal() == wxID_CANCEL )
return wxFileName();
lastFilterIndex = dlg.GetFilterIndex();
return wxFileName( dlg.GetPath() );
}
/**
* Read a file to detect the type.
* @param aFile - open file to be read. File pointer will be closed.
* @param aFileName - file name to be read
* @param aName - wxString to receive the module name iff type is LEGACY
*/
static IO_MGR::PCB_FILE_T detect_file_type( FILE* aFile, const wxFileName& aFileName,
wxString* aName )
{
FILE_LINE_READER freader( aFile, aFileName.GetFullPath() );
WHITESPACE_FILTER_READER reader( freader );
IO_MGR::PCB_FILE_T file_type;
wxASSERT( aName );
reader.ReadLine();
char* line = reader.Line();
if( !strncasecmp( line, "(module", strlen( "(module" ) ) )
{
file_type = IO_MGR::KICAD_SEXP;
*aName = aFileName.GetName();
}
else if( !strncasecmp( line, FOOTPRINT_LIBRARY_HEADER, FOOTPRINT_LIBRARY_HEADER_CNT ) )
{
file_type = IO_MGR::LEGACY;
while( reader.ReadLine() )
{
if( !strncasecmp( line, "$MODULE", strlen( "$MODULE" ) ) )
{
*aName = FROM_UTF8( StrPurge( line + strlen( "$MODULE" ) ) );
break;
}
}
}
else if( !strncasecmp( line, "Element", strlen( "Element" ) ) )
{
file_type = IO_MGR::GEDA_PCB;
*aName = aFileName.GetName();
}
else
{
file_type = IO_MGR::FILE_TYPE_NONE;
}
return file_type;
}
/**
* Parse a footprint using a PLUGIN.
* @param aFileName - file name to parse
* @param aFileType - type of the file
* @param aName - name of the footprint
*/
static MODULE* parse_module_with_plugin(
const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
const wxString& aName )
{
wxString path;
switch( aFileType )
{
case IO_MGR::GEDA_PCB:
path = aFileName.GetPath();
break;
case IO_MGR::LEGACY:
path = aFileName.GetFullPath();
break;
default:
wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
}
PLUGIN::RELEASER pi( IO_MGR::PluginFind( aFileType ) );
return pi->FootprintLoad( path, aName );
}
/**
* Parse a KICAD footprint.
* @param aFileName - file name to parse
*/
static MODULE* parse_module_kicad( const wxFileName& aFileName )
{
wxString fcontents;
PCB_IO pcb_io;
wxFFile f( aFileName.GetFullPath() );
if( !f.IsOpened() )
return NULL;
f.ReadAll( &fcontents );
return dynamic_cast<MODULE*>( pcb_io.Parse( fcontents ) );
}
/**
* Try to load a footprint, returning NULL if the file couldn't be accessed.
* @param aFileName - file name to load
* @param aFileType - type of the file to load
* @param aName - footprint name
*/
MODULE* try_load_footprint( const wxFileName& aFileName, IO_MGR::PCB_FILE_T aFileType,
const wxString& aName )
{
MODULE* module;
switch( aFileType )
{
case IO_MGR::GEDA_PCB:
case IO_MGR::LEGACY:
module = parse_module_with_plugin( aFileName, aFileType, aName );
break;
case IO_MGR::KICAD_SEXP:
module = parse_module_kicad( aFileName );
break;
default:
wxFAIL_MSG( wxT( "unexpected IO_MGR::PCB_FILE_T" ) );
module = NULL;
}
return module;
}
MODULE* FOOTPRINT_EDIT_FRAME::Import_Module( const wxString& aName )
{
wxString lastOpenedPathForLoading = m_mruPath;
wxConfigBase* config = Kiface().KifaceSettings();
if( config )
config->Read( EXPORT_IMPORT_LASTPATH_KEY, &lastOpenedPathForLoading );
wxFileName fn;
if( aName != wxT("") )
fn = aName;
else
fn = getFootprintFilenameFromUser( this, lastOpenedPathForLoading );
if( !fn.IsOk() )
return NULL;
FILE* fp = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
if( !fp )
{
wxString msg = wxString::Format( FMT_FILE_NOT_FOUND, GetChars( fn.GetFullPath() ) );
DisplayError( this, msg );
return NULL;
}
if( config ) // Save file path
{
lastOpenedPathForLoading = fn.GetPath();
config->Write( EXPORT_IMPORT_LASTPATH_KEY, lastOpenedPathForLoading );
}
wxString moduleName;
IO_MGR::PCB_FILE_T fileType = detect_file_type( fp, fn.GetFullPath(), &moduleName );
if( fileType == IO_MGR::FILE_TYPE_NONE )
{
DisplayError( this, FMT_NOT_MODULE );
return NULL;
}
MODULE* module = NULL;
try
{
module = try_load_footprint( fn, fileType, moduleName );
if( !module )
{
wxString msg = wxString::Format(
FMT_MOD_NOT_FOUND, GetChars( moduleName ), GetChars( fn.GetFullPath() ) );
DisplayError( this, msg );
return NULL;
}
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
// if the footprint is not loaded, exit.
// However, even if an error happens, it can be loaded, because in KICAD and GPCB format,
// a fp library is a set of separate files, and the error(s) are not necessary when
// reading the selected file
if( !module )
return NULL;
}
// Insert footprint in list
GetBoard()->Add( module );
// Display info :
SetMsgPanel( module );
PlaceModule( module, NULL );
if( IsGalCanvasActive() )
module->SetPosition( wxPoint( 0, 0 ) );
GetBoard()->m_Status_Pcb = 0;
GetBoard()->BuildListOfNets();
updateView();
return module;
}
void FOOTPRINT_EDIT_FRAME::Export_Module( MODULE* aModule )
{
wxFileName fn;
wxConfigBase* config = Kiface().KifaceSettings();
if( !aModule )
return;
fn.SetName( aModule->GetFPID().GetLibItemName() );
wxString wildcard = KiCadFootprintLibFileWildcard();
fn.SetExt( KiCadFootprintFileExtension );
if( config )
{
wxString path;
config->Read( EXPORT_IMPORT_LASTPATH_KEY, &path, m_mruPath );
fn.SetPath( path );
}
wxFileDialog dlg( this, FMT_EXPORT_MODULE, fn.GetPath(), fn.GetFullName(),
wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
if( dlg.ShowModal() == wxID_CANCEL )
return;
fn = dlg.GetPath();
if( config ) // Save file path
{
config->Write( EXPORT_IMPORT_LASTPATH_KEY, fn.GetPath() );
}
try
{
// Export as *.kicad_pcb format, using a strategy which is specifically chosen
// as an example on how it could also be used to send it to the system clipboard.
PCB_IO pcb_io( CTL_FOR_LIBRARY );
/* This module should *already* be "normalized" in a way such that
orientation is zero, etc., since it came from module editor.
module->SetTimeStamp( 0 );
module->SetParent( 0 );
module->SetOrientation( 0 );
*/
pcb_io.Format( aModule );
FILE* fp = wxFopen( dlg.GetPath(), wxT( "wt" ) );
if( fp == NULL )
{
wxMessageBox( wxString::Format(
_( "Unable to create or write file \"%s\"" ),
GetChars( dlg.GetPath() ) ) );
return;
}
fprintf( fp, "%s", pcb_io.GetStringOutput( false ).c_str() );
fclose( fp );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return;
}
wxString msg = wxString::Format( FMT_EXPORTED, GetChars( dlg.GetPath() ) );
DisplayInfoMessage( this, msg );
}
bool FOOTPRINT_EDIT_FRAME::SaveCurrentModule( const wxString* aLibPath )
{
wxString libPath = aLibPath ? *aLibPath : getLibPath();
IO_MGR::PCB_FILE_T piType = IO_MGR::GuessPluginTypeFromLibPath( libPath );
// Legacy libraries are readable, but writing legacy format is not allowed
if( piType == IO_MGR::LEGACY )
{
DisplayInfoMessage( this, INFO_LEGACY_LIB_WARN_EDIT );
return false;
}
try
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
pi->FootprintSave( libPath, GetBoard()->m_Modules );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return false;
}
return true;
}
wxString PCB_BASE_EDIT_FRAME::CreateNewLibrary(const wxString& aLibName )
{
// Kicad cannot write legacy format libraries, only .pretty new format
// because the legacy format cannot handle current features.
// The footprint library is actually a directory
// if a library name is not given, prompt user for footprint library name, ending by ".pretty"
// Because there are constraints for the directory name to create,
// (the name should have the extension ".pretty", and the folder cannot be inside
// a footprint library), we do not use the standard wxDirDialog.
wxString initialPath = wxPathOnly( Prj().GetProjectFullName() );
wxString libPath;
if( aLibName.IsEmpty() )
{
DIALOG_SELECT_PRETTY_LIB dlg( this, initialPath );
if( dlg.ShowModal() != wxID_OK )
return wxEmptyString;
libPath = dlg.GetFullPrettyLibName();
}
else
{
wxFileName fn = aLibName;
if( !fn.IsAbsolute() )
{
fn.SetName( aLibName );
fn.MakeAbsolute( initialPath );
}
// Enforce the .pretty extension:
fn.SetExt( KiCadFootprintLibPathExtension );
libPath = fn.GetFullPath();
}
// We can save fp libs only using IO_MGR::KICAD_SEXP format (.pretty libraries)
IO_MGR::PCB_FILE_T piType = IO_MGR::KICAD_SEXP;
try
{
PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
bool writable = false;
bool exists = false;
try
{
writable = pi->IsFootprintLibWritable( libPath );
exists = true; // no exception was thrown, lib must exist.
}
catch( const IO_ERROR& )
{
// ignore, original values of 'writable' and 'exists' are accurate.
}
if( exists )
{
if( !writable )
{
wxString msg = wxString::Format( FMT_LIB_READ_ONLY, GetChars( libPath ) );
DisplayError( this, msg );
return wxEmptyString;
}
else
{
wxString msg = wxString::Format( FMT_OK_OVERWRITE, GetChars( libPath ) );
if( !IsOK( this, msg ) )
return wxEmptyString;
pi->FootprintLibDelete( libPath );
}
}
pi->FootprintLibCreate( libPath );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return wxEmptyString;
}
return libPath;
}
bool FOOTPRINT_EDIT_FRAME::DeleteModuleFromCurrentLibrary()
{
wxString nickname = GetCurrentLib();
// Legacy libraries are readable, but modifying legacy format is not allowed
// So prompt the user if he try to delete a footprint from a legacy lib
wxString libfullname = Prj().PcbFootprintLibs()->FindRow(nickname)->GetFullURI();
IO_MGR::PCB_FILE_T piType = IO_MGR::GuessPluginTypeFromLibPath( libfullname );
if( piType == IO_MGR::LEGACY )
{
DisplayInfoMessage( this, INFO_LEGACY_LIB_WARN_DELETE );
return false;
}
if( !Prj().PcbFootprintLibs()->IsFootprintLibWritable( nickname ) )
{
wxString msg = wxString::Format( _( "Library \"%s\" is read only" ), nickname );
DisplayError( this, msg );
return false;
}
LIB_ID fpid;
wxString fpid_txt = PCB_BASE_FRAME::SelectFootprint( this, nickname, wxEmptyString,
wxEmptyString, Prj().PcbFootprintLibs() );
fpid.Parse( fpid_txt, LIB_ID::ID_PCB );
if( !fpid.IsValid() )
return false;
wxString fpname = fpid.GetLibItemName();
// Confirmation
wxString msg = wxString::Format( FMT_OK_DELETE, fpname.GetData(), nickname.GetData() );
if( !IsOK( this, msg ) )
return false;
try
{
Prj().PcbFootprintLibs()->FootprintDelete( nickname, fpname );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return false;
}
msg.Printf( FMT_MOD_DELETED, fpname.GetData(), nickname.GetData() );
SetStatusText( msg );
return true;
}
void PCB_EDIT_FRAME::ArchiveModulesOnBoard( bool aStoreInNewLib, const wxString& aLibName,
wxString* aLibPath )
{
if( GetBoard()->m_Modules == NULL )
{
DisplayInfoMessage( this, _( "No footprints to archive!" ) );
return;
}
wxString footprintName;
if( !aStoreInNewLib )
{
// The footprints are saved in an existing .pretty library in the fp lib table
PROJECT& prj = Prj();
wxString last_nickname = prj.GetRString( PROJECT::PCB_LIB_NICKNAME );
wxString nickname = SelectLibrary( last_nickname );
if( !nickname ) // Aborted
return;
prj.SetRString( PROJECT::PCB_LIB_NICKNAME, nickname );
try
{
FP_LIB_TABLE* tbl = prj.PcbFootprintLibs();
for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
{
if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
tbl->FootprintSave( nickname, curr_fp, false );
}
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
}
}
else
{
// The footprints are saved in a new .pretty library.
// If this library already exists, all previous footprints will be deleted
wxString libPath = CreateNewLibrary( aLibName );
if( libPath.IsEmpty() ) // Aborted
return;
if( aLibPath ) *aLibPath = libPath;
IO_MGR::PCB_FILE_T piType = IO_MGR::KICAD_SEXP;
PLUGIN::RELEASER pi( IO_MGR::PluginFind( piType ) );
for( MODULE* curr_fp = GetBoard()->m_Modules; curr_fp; curr_fp = curr_fp->Next() )
{
try
{
if( !curr_fp->GetFPID().GetLibItemName().empty() ) // Can happen with old boards.
pi->FootprintSave( libPath, curr_fp );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
}
}
}
}
class LIBRARY_NAME_CLEARER
{
MODULE* m_module;
LIB_ID m_savedFPID;
public:
LIBRARY_NAME_CLEARER( MODULE* aModule )
{
m_module = aModule;
m_savedFPID = aModule->GetFPID();
m_module->SetFPID( LIB_ID( wxEmptyString, m_savedFPID.GetLibItemName() ) );
}
~LIBRARY_NAME_CLEARER()
{
m_module->SetFPID( m_savedFPID );
}
};
bool FOOTPRINT_EDIT_FRAME::SaveFootprintInLibrary( wxString activeLibrary, MODULE* aModule )
{
if( aModule == NULL )
return false;
FP_LIB_TABLE* tbl = Prj().PcbFootprintLibs();
SetMsgPanel( aModule );
// For 6.0 Save should silently save the footprint in its own library.
// Save As... should allow a rename and/or reparent to a different library.
//
// However, for 5.0 we need a more limited intervention. We'll always display
// the dialog, but add a Library selection widget that will default to saving
// in the footprint's own library but allow switching to the active library
// (or even some other library).
wxString libraryName = aModule->GetFPID().GetLibNickname();
wxString footprintName = aModule->GetFPID().GetLibItemName();
bool updateValue = ( aModule->GetValue() == footprintName );
if( libraryName.IsEmpty() )
libraryName = activeLibrary;
wxArrayString headers;
std::vector<wxArrayString> itemsToDisplay;
std::vector<wxString> nicknames = tbl->GetLogicalLibs();
headers.Add( _( "Nickname" ) );
headers.Add( _( "Description" ) );
for( unsigned i = 0; i < nicknames.size(); i++ )
{
wxArrayString item;
item.Add( nicknames[i] );
item.Add( tbl->GetDescription( nicknames[i] ) );
itemsToDisplay.push_back( item );
}
EDA_LIST_DIALOG dlg( this, FMT_SAVE_MODULE, headers, itemsToDisplay, libraryName );
dlg.SetFilterLabel( _( "Library Filter:" ) );
dlg.SetListLabel( _( "Save in Library:" ) );
wxSizer* mainSizer = dlg.GetSizer();
wxStaticLine* separator = new wxStaticLine( &dlg, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxLI_HORIZONTAL );
mainSizer->Prepend( separator, 0, wxEXPAND|wxBOTTOM|wxTOP, 10 );
wxTextCtrl* nameTextCtrl = new wxTextCtrl( &dlg, wxID_ANY, footprintName,
wxDefaultPosition, wxDefaultSize, 0 );
mainSizer->Prepend( nameTextCtrl, 0, wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT, 5 );
wxTextValidator nameValidator( wxFILTER_EXCLUDE_CHAR_LIST );
nameValidator.SetCharExcludes( MODULE::StringLibNameInvalidChars( false ) );
nameTextCtrl->SetValidator( nameValidator );
wxStaticText* label = new wxStaticText( &dlg, wxID_ANY, _( "Footprint Name:" ),
wxDefaultPosition, wxDefaultSize, 0 );
mainSizer->Prepend( label, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
// Move nameTextCtrl to the head of the tab-order
if( dlg.GetChildren().DeleteObject( nameTextCtrl ) )
dlg.GetChildren().Insert( nameTextCtrl );
nameTextCtrl->SetFocus();
dlg.Layout();
mainSizer->Fit( &dlg );
if( dlg.ShowModal() != wxID_OK )
return false; // canceled by user
libraryName = dlg.GetTextSelection();
if( libraryName.IsEmpty() )
{
DisplayError( NULL, _( "No library specified. Footprint could not be saved." ) );
return false;
}
footprintName = nameTextCtrl->GetValue();
footprintName.Trim( true );
footprintName.Trim( false );
if( footprintName.IsEmpty() )
{
DisplayError( NULL, _( "No footprint name specified. Footprint could not be saved." ) );
return false;
}
aModule->SetFPID( LIB_ID( libraryName, footprintName ) );
if( updateValue )
aModule->SetValue( footprintName );
// Legacy libraries are readable, but modifying legacy format is not allowed
// So prompt the user if he try to add/replace a footprint in a legacy lib
wxString libfullname = Prj().PcbFootprintLibs()->FindRow( libraryName )->GetFullURI();
IO_MGR::PCB_FILE_T piType = IO_MGR::GuessPluginTypeFromLibPath( libfullname );
if( piType == IO_MGR::LEGACY )
{
DisplayInfoMessage( this, INFO_LEGACY_LIB_WARN_EDIT );
return false;
}
bool module_exists = false;
try
{
MODULE* m = tbl->FootprintLoad( libraryName, footprintName );
module_exists = m != nullptr;
delete m;
LIBRARY_NAME_CLEARER temp( aModule );
// this always overwrites any existing footprint, but should yell on its
// own if the library or footprint is not writable.
tbl->FootprintSave( libraryName, aModule );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return false;
}
wxString fmt = module_exists ? _( "Component \"%s\" replaced in \"%s\"" ) :
_( "Component \"%s\" added in \"%s\"" );
wxString msg = wxString::Format( fmt, footprintName.GetData(), libraryName.GetData() );
SetStatusText( msg );
return true;
}
MODULE* PCB_BASE_FRAME::CreateNewModule( const wxString& aModuleName )
{
// Creates a new footprint at position 0,0 which contains the minimal items:
// the reference and the value.
// Value : initialized to the footprint name.
// put on fab layer (front side)
// Reference : initialized to a default value (REF**).
// put on silkscreen layer (front side)
wxString moduleName = aModuleName;
// Ask for the new module name
if( moduleName.IsEmpty() )
{
WX_TEXT_ENTRY_DIALOG dlg( this, FMT_MOD_REF, FMT_MOD_CREATE, moduleName );
dlg.SetTextValidator( FILE_NAME_CHAR_VALIDATOR( &moduleName ) );
if( dlg.ShowModal() != wxID_OK )
return NULL; //Aborted by user
}
moduleName.Trim( true );
moduleName.Trim( false );
if( moduleName.IsEmpty() )
{
DisplayInfoMessage( this, FMT_NO_REF_ABORTED );
return NULL;
}
// Creates the new module and add it to the head of the linked list of modules
MODULE* module = new MODULE( GetBoard() );
// Update parameters: timestamp ...
module->SetLastEditTime();
// Update its name in lib
module->SetFPID( LIB_ID( wxEmptyString, moduleName ) );
wxPoint default_pos;
BOARD_DESIGN_SETTINGS& settings = GetDesignSettings();
// Update reference:
if( settings.m_RefDefaultText.IsEmpty() )
module->SetReference( moduleName );
else
module->SetReference( settings.m_RefDefaultText );
module->Reference().SetThickness( settings.m_ModuleTextWidth );
module->Reference().SetTextSize( settings.m_ModuleTextSize );
default_pos.y = GetDesignSettings().m_ModuleTextSize.y / 2;
module->Reference().SetPosition( default_pos );
module->Reference().SetLayer( ToLAYER_ID( settings.m_RefDefaultlayer ) );
module->Reference().SetVisible( settings.m_RefDefaultVisibility );
// Set the value field to a default value
if( settings.m_ValueDefaultText.IsEmpty() )
module->SetValue( moduleName );
else
module->SetValue( settings.m_ValueDefaultText );
module->Value().SetThickness( GetDesignSettings().m_ModuleTextWidth );
module->Value().SetTextSize( GetDesignSettings().m_ModuleTextSize );
default_pos.y = -default_pos.y;
module->Value().SetPosition( default_pos );
module->Value().SetLayer( ToLAYER_ID( settings.m_ValueDefaultlayer ) );
module->Value().SetVisible( settings.m_ValueDefaultVisibility );
SetMsgPanel( module );
return module;
}
wxString PCB_BASE_FRAME::SelectLibrary( const wxString& aNicknameExisting )
{
wxArrayString headers;
headers.Add( _( "Nickname" ) );
headers.Add( _( "Description" ) );
FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
std::vector< wxArrayString > itemsToDisplay;
std::vector< wxString > nicknames = fptbl->GetLogicalLibs();
for( unsigned i = 0; i < nicknames.size(); i++ )
{
wxArrayString item;
item.Add( nicknames[i] );
item.Add( fptbl->GetDescription( nicknames[i] ) );
itemsToDisplay.push_back( item );
}
EDA_LIST_DIALOG dlg( this, FMT_SELECT_LIB, headers, itemsToDisplay, aNicknameExisting );
if( dlg.ShowModal() != wxID_OK )
return wxEmptyString;
wxString nickname = dlg.GetTextSelection();
wxLogDebug( wxT( "Chose footprint library \"%s\"." ), GetChars( nickname ) );
return nickname;
}
|