You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2008-2016 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2016 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file lib_export.cpp
  27. * @brief Eeschema library maintenance routines to backup modified libraries and
  28. * create, edit, and delete components.
  29. */
  30. #include <fctsys.h>
  31. #include <class_drawpanel.h>
  32. #include <confirm.h>
  33. #include <general.h>
  34. #include <libeditframe.h>
  35. #include <class_library.h>
  36. #include <wildcards_and_files_ext.h>
  37. #include <wx/filename.h>
  38. extern int ExportPartId;
  39. void LIB_EDIT_FRAME::OnImportPart( wxCommandEvent& event )
  40. {
  41. m_lastDrawItem = NULL;
  42. wxFileDialog dlg( this, _( "Import Component" ), m_mruPath,
  43. wxEmptyString, SchematicLibraryFileWildcard,
  44. wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  45. if( dlg.ShowModal() == wxID_CANCEL )
  46. return;
  47. wxFileName fn = dlg.GetPath();
  48. m_mruPath = fn.GetPath();
  49. std::unique_ptr<PART_LIB> lib;
  50. try
  51. {
  52. std::unique_ptr<PART_LIB> new_lib( PART_LIB::LoadLibrary( fn.GetFullPath() ) );
  53. lib = std::move( new_lib );
  54. }
  55. catch( const IO_ERROR& )
  56. {
  57. wxString msg = wxString::Format( _(
  58. "Unable to import library '%s'. Error:\n"
  59. "%s" ),
  60. GetChars( fn.GetFullPath() )
  61. );
  62. DisplayError( this, msg );
  63. return;
  64. }
  65. wxArrayString aliasNames;
  66. lib->GetAliasNames( aliasNames );
  67. if( aliasNames.IsEmpty() )
  68. {
  69. wxString msg = wxString::Format( _( "Part library file '%s' is empty." ),
  70. GetChars( fn.GetFullPath() ) );
  71. DisplayError( this, msg );
  72. return;
  73. }
  74. LIB_ALIAS* entry = lib->FindAlias( aliasNames[0] );
  75. if( LoadOneLibraryPartAux( entry, lib.get() ) )
  76. {
  77. DisplayLibInfos();
  78. GetScreen()->ClearUndoRedoList();
  79. Zoom_Automatique( false );
  80. }
  81. }
  82. void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
  83. {
  84. wxString msg, title;
  85. bool createLib = ( event.GetId() == ExportPartId ) ? false : true;
  86. LIB_PART* part = GetCurPart();
  87. if( !part )
  88. {
  89. DisplayError( this, _( "There is no component selected to save." ) );
  90. return;
  91. }
  92. wxFileName fn = part->GetName().Lower();
  93. fn.SetExt( SchematicLibraryFileExtension );
  94. title = createLib ? _( "New Library" ) : _( "Export Component" );
  95. wxFileDialog dlg( this, title, m_mruPath, fn.GetFullName(),
  96. SchematicLibraryFileWildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  97. if( dlg.ShowModal() == wxID_CANCEL )
  98. return;
  99. fn = dlg.GetPath();
  100. std::unique_ptr<PART_LIB> temp_lib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, fn.GetFullPath() ) );
  101. try
  102. {
  103. SaveOnePart( temp_lib.get() );
  104. }
  105. catch( ... /* IO_ERROR ioe */ )
  106. {
  107. fn.MakeAbsolute();
  108. msg = wxT( "Failed to create symbol library file " ) + fn.GetFullPath();
  109. DisplayError( this, msg );
  110. msg.Printf( _( "Error creating symbol library '%s'" ), fn.GetFullName() );
  111. SetStatusText( msg );
  112. return;
  113. }
  114. m_mruPath = fn.GetPath();
  115. msg.Printf( _( "'%s' - OK" ), GetChars( fn.GetFullPath() ) );
  116. DisplayInfoMessage( this, _( "This library will not be available until it is loaded by "
  117. "Eeschema.\n\n"
  118. "Modify the Eeschema library configuration if you want to "
  119. "include it as part of this project." ) );
  120. msg.Printf( _( "'%s' - Export OK" ), GetChars( fn.GetFullPath() ) );
  121. SetStatusText( msg );
  122. }