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.

168 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2011 KiCad Developers, see change_log.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. wxString errMsg;
  42. wxFileName fn;
  43. CMP_LIBRARY* LibTmp;
  44. LIB_ALIAS* LibEntry;
  45. m_lastDrawItem = NULL;
  46. wxFileDialog dlg( this, _( "Import Component" ), m_lastLibImportPath,
  47. wxEmptyString, SchematicLibraryFileWildcard,
  48. wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  49. if( dlg.ShowModal() == wxID_CANCEL )
  50. return;
  51. fn = dlg.GetPath();
  52. LibTmp = CMP_LIBRARY::LoadLibrary( fn, errMsg );
  53. if( LibTmp == NULL )
  54. return;
  55. LibEntry = LibTmp->GetFirstEntry();
  56. if( LibEntry == NULL )
  57. {
  58. wxString msg;
  59. msg.Printf( _( "Component library file <%s> is empty." ), GetChars( fn.GetFullPath() ) );
  60. DisplayError( this, msg );
  61. return;
  62. }
  63. if( LoadOneLibraryPartAux( LibEntry, LibTmp ) )
  64. {
  65. fn = dlg.GetPath();
  66. m_lastLibImportPath = fn.GetPath();
  67. DisplayLibInfos();
  68. GetScreen()->ClearUndoRedoList();
  69. m_canvas->Refresh();
  70. }
  71. delete LibTmp;
  72. }
  73. void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
  74. {
  75. wxFileName fn;
  76. wxString msg, title;
  77. CMP_LIBRARY* CurLibTmp;
  78. bool createLib = ( event.GetId() == ExportPartId ) ? false : true;
  79. if( m_component == NULL )
  80. {
  81. DisplayError( this, _( "There is no component selected to save." ) );
  82. return;
  83. }
  84. fn = m_component->GetName().Lower();
  85. fn.SetExt( SchematicLibraryFileExtension );
  86. title = createLib ? _( "New Library" ) : _( "Export Component" );
  87. wxFileDialog dlg( this, title, wxGetCwd(), fn.GetFullName(),
  88. SchematicLibraryFileWildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  89. if( dlg.ShowModal() == wxID_CANCEL )
  90. return;
  91. fn = dlg.GetPath();
  92. CurLibTmp = m_library;
  93. m_library = new CMP_LIBRARY( LIBRARY_TYPE_EESCHEMA, fn );
  94. SaveOnePartInMemory();
  95. bool result = false;
  96. try
  97. {
  98. FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
  99. result = m_library->Save( formatter );
  100. }
  101. catch( ... /* IO_ERROR ioe */ )
  102. {
  103. fn.MakeAbsolute();
  104. msg = wxT( "Failed to create component library file " ) + fn.GetFullPath();
  105. DisplayError( this, msg );
  106. return;
  107. }
  108. if( result )
  109. m_lastLibExportPath = fn.GetPath();
  110. delete m_library;
  111. m_library = CurLibTmp;
  112. if( result )
  113. {
  114. if( createLib )
  115. {
  116. msg = fn.GetFullPath() + _( " - OK" );
  117. DisplayInfoMessage( this, _( "This library will not be available \
  118. until it is loaded by Eeschema.\n\nModify the Eeschema library configuration \
  119. if you want to include it as part of this project." ) );
  120. }
  121. else
  122. {
  123. msg = fn.GetFullPath() + _( " - Export OK" );
  124. }
  125. } // Error
  126. else
  127. {
  128. msg = _( "Error creating " ) + fn.GetFullName();
  129. }
  130. SetStatusText( msg );
  131. }