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.

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