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.

118 lines
3.8 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 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 <sch_item_struct.h>
  34. #include <wxEeschemaStruct.h>
  35. #include <general.h>
  36. #include <netlist.h>
  37. #include <protos.h>
  38. #include <class_library.h>
  39. #include <sch_component.h>
  40. #include <sch_sheet.h>
  41. #include <wildcards_and_files_ext.h>
  42. bool SCH_EDIT_FRAME::CreateArchiveLibraryCacheFile( bool aUseCurrentSheetFilename )
  43. {
  44. wxFileName fn;
  45. if( aUseCurrentSheetFilename )
  46. fn = GetScreen()->GetFileName();
  47. else
  48. fn = g_RootSheet->GetScreen()->GetFileName();
  49. fn.SetName( fn.GetName() + wxT( "-cache" ) );
  50. fn.SetExt( SchematicLibraryFileExtension );
  51. return CreateArchiveLibrary( fn.GetFullPath() );
  52. }
  53. bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
  54. {
  55. wxString msg;
  56. LIB_COMPONENT* libComponent;
  57. CMP_LIBRARY* libCache;
  58. SCH_SCREENS screens;
  59. libCache = new CMP_LIBRARY( LIBRARY_TYPE_EESCHEMA, aFileName );
  60. libCache->SetCache();
  61. /* examine all screens (not sheets) used and build the list of components
  62. * found in lib complex hierarchies are not a problem because we just want
  63. * to know used components in libraries
  64. */
  65. for( SCH_SCREEN* screen = screens.GetFirst(); screen != NULL; screen = screens.GetNext() )
  66. {
  67. for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
  68. {
  69. if( item->Type() != SCH_COMPONENT_T )
  70. continue;
  71. SCH_COMPONENT* component = (SCH_COMPONENT*) item;
  72. // If not already saved in the new cache, put it:
  73. if( libCache->FindEntry( component->GetLibName()) == NULL )
  74. {
  75. libComponent = CMP_LIBRARY::FindLibraryComponent( component->GetLibName() );
  76. if( libComponent ) // if NULL : component not found, cannot be stored
  77. libCache->AddComponent( libComponent );
  78. }
  79. }
  80. }
  81. try
  82. {
  83. FILE_OUTPUTFORMATTER formatter( aFileName );
  84. if( !libCache->Save( formatter ) )
  85. {
  86. msg.Printf( _( "An error occurred attempting to save component library <%s>." ),
  87. GetChars( aFileName ) );
  88. DisplayError( this, msg );
  89. return false;
  90. }
  91. }
  92. catch( ... /* IO_ERROR ioe */ )
  93. {
  94. msg.Printf( _( "Failed to create component library file <%s>" ),
  95. GetChars( aFileName ) );
  96. DisplayError( this, msg );
  97. return false;
  98. }
  99. return true;
  100. }