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.

191 lines
5.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 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 2004-2017 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 <lib_edit_frame.h>
  35. #include <class_library.h>
  36. #include <wildcards_and_files_ext.h>
  37. #include <eeschema_id.h>
  38. #include <lib_manager.h>
  39. #include <wx/filename.h>
  40. void LIB_EDIT_FRAME::OnImportPart( wxCommandEvent& event )
  41. {
  42. wxString msg;
  43. m_lastDrawItem = NULL;
  44. wxString libName = getTargetLib();
  45. if( !m_libMgr->LibraryExists( libName ) )
  46. {
  47. libName = SelectLibraryFromList();
  48. if( !m_libMgr->LibraryExists( libName ) )
  49. return;
  50. }
  51. wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath,
  52. wxEmptyString, SchematicLibraryFileWildcard(),
  53. wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  54. if( dlg.ShowModal() == wxID_CANCEL )
  55. return;
  56. wxFileName fn = dlg.GetPath();
  57. m_mruPath = fn.GetPath();
  58. wxArrayString symbols;
  59. SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
  60. // TODO dialog to select the part to be imported if there is more than one
  61. try
  62. {
  63. pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
  64. }
  65. catch( const IO_ERROR& ioe )
  66. {
  67. msg.Printf( _( "Cannot import symbol library \"%s\"." ), fn.GetFullPath() );
  68. DisplayErrorMessage( this, msg, ioe.What() );
  69. return;
  70. }
  71. if( symbols.empty() )
  72. {
  73. msg.Printf( _( "Symbol library file \"%s\" is empty." ), fn.GetFullPath() );
  74. DisplayError( this, msg );
  75. return;
  76. }
  77. wxString symbolName = symbols[0];
  78. LIB_ALIAS* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
  79. if( m_libMgr->PartExists( symbols[0], libName ) )
  80. {
  81. msg.Printf( _( "Symbol \"%s\" already exists in library \"%s\"." ), symbolName, libName );
  82. DisplayError( this, msg );
  83. return;
  84. }
  85. m_libMgr->UpdatePart( entry->GetPart(), libName );
  86. loadPart( symbolName, libName, 1 );
  87. }
  88. void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
  89. {
  90. wxString msg, title;
  91. LIB_PART* part = getTargetPart();
  92. if( !part )
  93. {
  94. DisplayError( this, _( "There is no symbol selected to save." ) );
  95. return;
  96. }
  97. wxFileName fn;
  98. fn.SetName( part->GetName().Lower() );
  99. fn.SetExt( SchematicLibraryFileExtension );
  100. wxFileDialog dlg( this, _( "Export Symbol" ), m_mruPath, fn.GetFullName(),
  101. SchematicLibraryFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  102. if( dlg.ShowModal() == wxID_CANCEL )
  103. return;
  104. fn = dlg.GetPath();
  105. fn.MakeAbsolute();
  106. LIB_PART* old_part = NULL;
  107. SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
  108. if( fn.FileExists() )
  109. {
  110. try
  111. {
  112. LIB_ALIAS* alias = pi->LoadSymbol( fn.GetFullPath(), part->GetName() );
  113. if( alias )
  114. old_part = alias->GetPart();
  115. }
  116. catch( const IO_ERROR& ioe )
  117. {
  118. msg.Printf( _( "Error occurred attempting to load symbol library file \"%s\"" ),
  119. fn.GetFullPath() );
  120. DisplayErrorMessage( this, msg, ioe.What() );
  121. return;
  122. }
  123. if( old_part )
  124. {
  125. msg.Printf( _( "Symbol \"%s\" already exists. Overwrite it?" ), part->GetName() );
  126. if( !IsOK( this, msg ) )
  127. return;
  128. }
  129. }
  130. if( fn.Exists() && !fn.IsDirWritable() )
  131. {
  132. msg.Printf( _( "Write permissions are required to save library \"%s\"." ), fn.GetFullPath() );
  133. DisplayError( this, msg );
  134. return;
  135. }
  136. try
  137. {
  138. if( !fn.FileExists() )
  139. pi->CreateSymbolLib( fn.GetFullPath() );
  140. pi->SaveSymbol( fn.GetFullPath(), new LIB_PART( *part ) );
  141. }
  142. catch( const IO_ERROR& ioe )
  143. {
  144. msg = _( "Failed to create symbol library file " ) + fn.GetFullPath();
  145. DisplayErrorMessage( this, msg, ioe.What() );
  146. msg.Printf( _( "Error creating symbol library \"%s\"" ), fn.GetFullName() );
  147. SetStatusText( msg );
  148. return;
  149. }
  150. m_mruPath = fn.GetPath();
  151. m_lastDrawItem = NULL;
  152. SetDrawItem( NULL );
  153. msg.Printf( _( "Symbol \"%s\" saved in library \"%s\"" ), part->GetName(), fn.GetFullPath() );
  154. SetStatusText( msg );
  155. }