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.

122 lines
3.9 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-2017 Wayne Stambaugh <stambaughw@verizon.net>
  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 <class_sch_screen.h>
  32. #include <wxstruct.h>
  33. #include <schframe.h>
  34. #include <class_library.h>
  35. #include <sch_component.h>
  36. #include <sch_sheet.h>
  37. #include <wildcards_and_files_ext.h>
  38. bool SCH_EDIT_FRAME::CreateArchiveLibraryCacheFile( bool aUseCurrentSheetFilename )
  39. {
  40. wxFileName fn;
  41. if( aUseCurrentSheetFilename )
  42. fn = GetScreen()->GetFileName();
  43. else
  44. fn = g_RootSheet->GetScreen()->GetFileName();
  45. fn.SetName( fn.GetName() + "-cache" );
  46. fn.SetExt( SchematicLibraryFileExtension );
  47. return CreateArchiveLibrary( fn.GetFullPath() );
  48. }
  49. bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
  50. {
  51. wxString msg;
  52. SCH_SCREENS screens;
  53. PART_LIBS* libs = Prj().SchLibs();
  54. // Create a new empty library to archive components:
  55. PART_LIB* cacheLib = new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName );
  56. cacheLib->SetCache();
  57. cacheLib->EnableBuffering();
  58. /* Examine all screens (not hierarchical sheets) used in the schematic and build a
  59. * library of unique symbols found in all screens. Complex hierarchies are not a
  60. * problem because we just want to know the library symbols used in the schematic.
  61. */
  62. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  63. {
  64. for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
  65. {
  66. if( item->Type() != SCH_COMPONENT_T )
  67. continue;
  68. SCH_COMPONENT* component = (SCH_COMPONENT*) item;
  69. if( !cacheLib->FindAlias( FROM_UTF8( component->GetLibId().GetLibItemName() ) ) )
  70. {
  71. LIB_PART* part = NULL;
  72. try
  73. {
  74. part = libs->FindLibPart( component->GetLibId() );
  75. if( part )
  76. {
  77. // AddPart() does first clone the part before adding.
  78. cacheLib->AddPart( part );
  79. }
  80. }
  81. catch( ... /* IO_ERROR ioe */ )
  82. {
  83. msg.Printf( _( "Failed to add symbol %s to library file '%s'" ),
  84. FROM_UTF8( component->GetLibId().GetLibItemName() ), aFileName );
  85. DisplayError( this, msg );
  86. return false;
  87. }
  88. }
  89. }
  90. }
  91. try
  92. {
  93. cacheLib->Save( false );
  94. delete cacheLib;
  95. }
  96. catch( ... /* IO_ERROR ioe */ )
  97. {
  98. msg.Printf( _( "Failed to save symbol library file '%s'" ), aFileName );
  99. DisplayError( this, msg );
  100. return false;
  101. }
  102. return true;
  103. }