bool LIB_EDIT_FRAME::addLibraryFile( bool aCreateNew ) { wxFileName fn = m_libMgr->GetUniqueLibraryName(); if( !LibraryFileBrowser( !aCreateNew, fn, SchematicLibraryFileWildcard(), SchematicLibraryFileExtension, false ) ) { return false; } wxString libName = fn.GetName(); if( libName.IsEmpty() ) return false; if( m_libMgr->LibraryExists( libName ) ) { DisplayError( this, wxString::Format( _( "Library \"%s\" already exists" ), libName ) ); return false; } // Select the target library table (global/project) SYMBOL_LIB_TABLE* libTable = selectSymLibTable(); if( !libTable ) return false; if( aCreateNew ) { if( !m_libMgr->CreateLibrary( fn.GetFullPath(), libTable ) ) { DisplayError( this, wxString::Format( _( "Could not create the library file '%s'.\n" "Check write permission." ), fn.GetFullPath() ) ); return false; } } else { if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) ) { DisplayError( this, _( "Could not open the library file." ) ); return false; } } bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() ); saveSymbolLibTables( globalTable, !globalTable ); return true; }
bool PCB_BASE_EDIT_FRAME::AddLibrary( const wxString& aFilename ) { wxFileName fn( aFilename ); if( aFilename.IsEmpty() ) { if( !LibraryFileBrowser( true, fn, KiCadFootprintLibPathWildcard(), KiCadFootprintLibPathExtension, true ) ) { return false; } } wxString libPath = fn.GetFullPath(); wxString libName = fn.GetName(); if( libName.IsEmpty() ) return false; bool saveInGlobalTable = false; bool saveInProjectTable = false; wxArrayString libTableNames; libTableNames.Add( _( "Global" ) ); libTableNames.Add( _( "Project" ) ); switch( SelectSingleOption( this, _( "Select Library Table" ), _( "Choose the Library Table to add the library to:" ), libTableNames ) ) { case 0: saveInGlobalTable = true; break; case 1: saveInProjectTable = true; break; default: return false; } wxString type = IO_MGR::ShowType( IO_MGR::GuessPluginTypeFromLibPath( libPath ) ); // try to use path normalized to an environmental variable or project path wxString normalizedPath = NormalizePath( libPath, &Pgm().GetLocalEnvVariables(), &Prj() ); if( normalizedPath.IsEmpty() ) normalizedPath = libPath; try { if( saveInGlobalTable ) { auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString ); GFootprintTable.InsertRow( row ); GFootprintTable.Save( FP_LIB_TABLE::GetGlobalTableFileName() ); } else if( saveInProjectTable ) { auto row = new FP_LIB_TABLE_ROW( libName, normalizedPath, type, wxEmptyString ); Prj().PcbFootprintLibs()->InsertRow( row ); Prj().PcbFootprintLibs()->Save( Prj().FootprintLibTblName() ); } } catch( const IO_ERROR& ioe ) { DisplayError( this, ioe.What() ); return false; } auto editor = (FOOTPRINT_EDIT_FRAME*) Kiway().Player( FRAME_PCB_MODULE_EDITOR, false ); if( editor ) editor->SyncLibraryTree( true ); auto viewer = (FOOTPRINT_VIEWER_FRAME*) Kiway().Player( FRAME_PCB_MODULE_VIEWER, false ); if( viewer ) viewer->ReCreateLibraryList(); 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 wxString initialPath = wxPathOnly( Prj().GetProjectFullName() ); wxFileName fn; bool doAdd = false; if( aLibName.IsEmpty() ) { fn = initialPath; if( !LibraryFileBrowser( false, fn, KiCadFootprintLibPathWildcard(), KiCadFootprintLibPathExtension) ) { return wxEmptyString; } doAdd = true; } else { fn = aLibName; if( !fn.IsAbsolute() ) { fn.SetName( aLibName ); fn.MakeAbsolute( initialPath ); } // Enforce the .pretty extension: fn.SetExt( KiCadFootprintLibPathExtension ); } // We can save fp libs only using IO_MGR::KICAD_SEXP format (.pretty libraries) IO_MGR::PCB_FILE_T piType = IO_MGR::KICAD_SEXP; wxString libPath = fn.GetFullPath(); 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& ) { } if( exists ) { if( !writable ) { wxString msg = wxString::Format( FMT_LIB_READ_ONLY, libPath ); DisplayError( this, msg ); return wxEmptyString; } else { wxString msg = wxString::Format( _( "Library %s already exists." ), libPath ); KIDIALOG dlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING ); dlg.SetOKLabel( _( "Overwrite" ) ); dlg.DoNotShowCheckbox( __FILE__, __LINE__ ); if( dlg.ShowModal() == wxID_CANCEL ) return wxEmptyString; pi->FootprintLibDelete( libPath ); } } pi->FootprintLibCreate( libPath ); } catch( const IO_ERROR& ioe ) { DisplayError( this, ioe.What() ); return wxEmptyString; } if( doAdd ) AddLibrary( libPath ); return libPath; }