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.

165 lines
5.3 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, jp.charras ar 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 libarch.cpp
  27. * @brief Module for generation of component archive files.
  28. */
  29. #include <fctsys.h>
  30. #include <confirm.h>
  31. #include <wildcards_and_files_ext.h>
  32. #include <sch_edit_frame.h>
  33. #include <symbol_lib_table.h>
  34. #include <class_library.h>
  35. #include <sch_component.h>
  36. #include <sch_sheet.h>
  37. bool SCH_EDIT_FRAME::CreateArchiveLibraryCacheFile( bool aUseCurrentSheetFilename )
  38. {
  39. wxFileName fn;
  40. if( aUseCurrentSheetFilename )
  41. fn = GetScreen()->GetFileName();
  42. else
  43. fn = g_RootSheet->GetScreen()->GetFileName();
  44. fn.SetName( fn.GetName() + "-cache" );
  45. fn.SetExt( SchematicLibraryFileExtension );
  46. bool success = CreateArchiveLibrary( fn.GetFullPath() );
  47. // Update the schematic symbol library links.
  48. // because the lib cache has changed
  49. SCH_SCREENS schematic;
  50. schematic.UpdateSymbolLinks();
  51. return success;
  52. }
  53. bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
  54. {
  55. wxString tmp;
  56. wxString errorMsg;
  57. SCH_SCREENS screens;
  58. // Create a new empty library to archive components:
  59. std::unique_ptr<PART_LIB> archLib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName ) );
  60. // Save symbols to file only when the library will be fully filled
  61. archLib->EnableBuffering();
  62. /* Examine all screens (not hierarchical sheets) used in the schematic and build a
  63. * library of unique symbols found in all screens. Complex hierarchies are not a
  64. * problem because we just want to know the library symbols used in the schematic
  65. * not their reference.
  66. */
  67. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  68. {
  69. for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
  70. {
  71. if( item->Type() != SCH_COMPONENT_T )
  72. continue;
  73. LIB_PART* part = nullptr;
  74. SCH_COMPONENT* component = (SCH_COMPONENT*) item;
  75. try
  76. {
  77. if( archLib->FindAlias( component->GetLibId() ) )
  78. continue;
  79. part = GetLibPart( component->GetLibId(), true );
  80. }
  81. catch( const IO_ERROR& )
  82. {
  83. // Queue up error messages for later.
  84. tmp.Printf( _( "Failed to add symbol \"%s\" to library file \"%s\"." ),
  85. component->GetLibId().GetUniStringLibItemName(), aFileName );
  86. // Don't bail out here. Attempt to add as many of the symbols to the library
  87. // as possible.
  88. }
  89. catch( ... )
  90. {
  91. tmp = _( "Unexpected exception occurred." );
  92. }
  93. if( part )
  94. {
  95. // Use the full LIB_ID as the symbol name to prevent symbol name collisions.
  96. wxString oldName = part->GetName();
  97. part->SetName( component->GetLibId().GetUniStringLibId() );
  98. // AddPart() does first clone the part before adding.
  99. archLib->AddPart( part );
  100. part->SetName( oldName );
  101. }
  102. else
  103. {
  104. tmp.Printf( _( "Symbol %s not found in any library or cache." ),
  105. component->GetLibId().GetUniStringLibId() );
  106. }
  107. if( !tmp.empty() )
  108. {
  109. if( errorMsg.empty() )
  110. errorMsg += tmp;
  111. else
  112. errorMsg += "\n" + tmp;
  113. }
  114. }
  115. }
  116. if( !errorMsg.empty() )
  117. {
  118. tmp.Printf( _( "Errors occurred creating symbol library %s." ), aFileName );
  119. DisplayErrorMessage( this, tmp, errorMsg );
  120. }
  121. archLib->EnableBuffering( false );
  122. try
  123. {
  124. archLib->Save( false );
  125. }
  126. catch( const IO_ERROR& ioe )
  127. {
  128. errorMsg.Printf( _( "Failed to save symbol library file \"%s\"" ), aFileName );
  129. DisplayErrorMessage( this, errorMsg, ioe.What() );
  130. return false;
  131. }
  132. catch( std::exception& error )
  133. {
  134. errorMsg.Printf( _( "Failed to save symbol library file \"%s\"" ), aFileName );
  135. DisplayErrorMessage( this, errorMsg, error.what() );
  136. return false;
  137. }
  138. return true;
  139. }