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.

141 lines
4.9 KiB

3 years ago
  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-2021 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 symbol archive files.
  28. */
  29. #include <confirm.h>
  30. #include <wildcards_and_files_ext.h>
  31. #include <sch_edit_frame.h>
  32. #include <symbol_library.h>
  33. #include <sch_symbol.h>
  34. #include <sch_sheet.h>
  35. #include <schematic.h>
  36. bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
  37. {
  38. wxString tmp;
  39. wxString errorMsg;
  40. SCH_SCREENS screens( Schematic().Root() );
  41. // Create a new empty library to archive symbols:
  42. std::unique_ptr<SYMBOL_LIB> archLib = std::make_unique<SYMBOL_LIB>( SCH_LIB_TYPE::LT_EESCHEMA,
  43. aFileName );
  44. // Save symbols to file only when the library will be fully filled
  45. archLib->EnableBuffering();
  46. /* Examine all screens (not hierarchical sheets) used in the schematic and build a
  47. * library of unique symbols found in all screens. Complex hierarchies are not a
  48. * problem because we just want to know the library symbols used in the schematic
  49. * not their reference.
  50. */
  51. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  52. {
  53. for( SCH_ITEM* aItem : screen->Items().OfType( SCH_SYMBOL_T ) )
  54. {
  55. LIB_SYMBOL* libSymbol = nullptr;
  56. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( aItem );
  57. try
  58. {
  59. if( archLib->FindSymbol( symbol->GetLibId() ) )
  60. continue;
  61. libSymbol = GetLibSymbol( symbol->GetLibId(), true );
  62. }
  63. catch( const IO_ERROR& )
  64. {
  65. // Queue up error messages for later.
  66. tmp.Printf( _( "Failed to add symbol %s to library file '%s'." ),
  67. symbol->GetLibId().GetUniStringLibItemName(),
  68. aFileName );
  69. // Don't bail out here. Attempt to add as many of the symbols to the library
  70. // as possible.
  71. }
  72. catch( ... )
  73. {
  74. tmp = _( "Unexpected exception occurred." );
  75. }
  76. if( libSymbol )
  77. {
  78. std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
  79. // Use the full LIB_ID as the symbol name to prevent symbol name collisions.
  80. flattenedSymbol->SetName( symbol->GetLibId().GetUniStringLibId() );
  81. // AddSymbol() does first clone the symbol before adding.
  82. archLib->AddSymbol( flattenedSymbol.get() );
  83. }
  84. else
  85. {
  86. tmp.Printf( _( "Symbol %s not found in any library or cache." ),
  87. symbol->GetLibId().GetUniStringLibId() );
  88. }
  89. if( !tmp.empty() && !errorMsg.Contains( symbol->GetLibId().GetUniStringLibId() ) )
  90. {
  91. if( errorMsg.empty() )
  92. errorMsg += tmp;
  93. else
  94. errorMsg += wxS( "\n" ) + tmp;
  95. }
  96. }
  97. }
  98. if( !errorMsg.empty() )
  99. {
  100. tmp.Printf( _( "Errors occurred creating symbol library %s." ), aFileName );
  101. DisplayErrorMessage( this, tmp, errorMsg );
  102. }
  103. archLib->EnableBuffering( false );
  104. try
  105. {
  106. archLib->Save( false );
  107. }
  108. catch( const IO_ERROR& ioe )
  109. {
  110. errorMsg.Printf( _( "Failed to save symbol library file '%s'." ), aFileName );
  111. DisplayErrorMessage( this, errorMsg, ioe.What() );
  112. return false;
  113. }
  114. catch( std::exception& error )
  115. {
  116. errorMsg.Printf( _( "Failed to save symbol library file '%s'." ), aFileName );
  117. DisplayErrorMessage( this, errorMsg, error.what() );
  118. return false;
  119. }
  120. return true;
  121. }