KiCad PCB EDA Suite
libarch.cpp
Go to the documentation of this file.
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 
31 #include <fctsys.h>
32 #include <confirm.h>
33 #include <class_sch_screen.h>
34 #include <wxstruct.h>
35 #include <schframe.h>
36 
37 #include <class_library.h>
38 #include <sch_component.h>
39 #include <sch_sheet.h>
41 
42 
43 bool SCH_EDIT_FRAME::CreateArchiveLibraryCacheFile( bool aUseCurrentSheetFilename )
44 {
45  wxFileName fn;
46 
47  if( aUseCurrentSheetFilename )
48  fn = GetScreen()->GetFileName();
49  else
51 
52  fn.SetName( fn.GetName() + "-cache" );
53  fn.SetExt( SchematicLibraryFileExtension );
54 
55  bool success = CreateArchiveLibrary( fn.GetFullPath() );
56 
57  // Update the schematic symbol library links.
58  // because the lib cache has changed
59  SCH_SCREENS schematic;
60  schematic.UpdateSymbolLinks();
61 
62  return success;
63 }
64 
65 
66 bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
67 {
68  wxString msg;
69  SCH_SCREENS screens;
70  PART_LIBS* libs = Prj().SchLibs();
71 
72  // Create a new empty library to archive components:
73  std::unique_ptr<PART_LIB> archLib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName ) );
74 
75  // Save symbols to file only when the library will be fully filled
76  archLib->EnableBuffering();
77 
78  /* Examine all screens (not hierarchical sheets) used in the schematic and build a
79  * library of unique symbols found in all screens. Complex hierarchies are not a
80  * problem because we just want to know the library symbols used in the schematic
81  * not their reference.
82  */
83  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
84  {
85  for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
86  {
87  if( item->Type() != SCH_COMPONENT_T )
88  continue;
89 
90  SCH_COMPONENT* component = (SCH_COMPONENT*) item;
91 
92  if( !archLib->FindAlias( component->GetLibId().GetLibItemName() ) )
93  {
94  LIB_PART* part = NULL;
95 
96  try
97  {
98  part = libs->FindLibPart( component->GetLibId() );
99 
100  if( part )
101  {
102  // AddPart() does first clone the part before adding.
103  archLib->AddPart( part );
104  }
105  }
106  catch( ... /* IO_ERROR ioe */ )
107  {
108  msg.Printf( _( "Failed to add symbol %s to library file '%s'" ),
109  component->GetLibId().GetLibItemName().wx_str(), aFileName );
110  DisplayError( this, msg );
111  return false;
112  }
113  }
114  }
115  }
116 
117  try
118  {
119  archLib->Save( false );
120  }
121  catch( ... /* IO_ERROR ioe */ )
122  {
123  msg.Printf( _( "Failed to save symbol library file '%s'" ), aFileName );
124  DisplayError( this, msg );
125  return false;
126  }
127 
128  return true;
129 }
Definition of the SCH_SHEET class for Eeschema.
SCH_SCREEN * GetNext()
This file is part of the common library.
const wxString & GetFileName() const
SCH_SCREEN * GetScreen()
Definition: sch_sheet.h:286
SCH_ITEM * Next() const
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: schframe.cpp:528
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
wxString wx_str() const
Definition: utf8.cpp:48
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:55
Base window classes and related definitions.
Definitions for the Eeschema program SCH_SCREEN class.
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
void UpdateSymbolLinks()
initializes or reinitializes the weak reference to the LIB_PART for each SCH_COMPONENT found in the f...
Class LIB_PART defines a library part object.
The common library.
bool CreateArchiveLibraryCacheFile(bool aUseCurrentSheetFilename=false)
Function CreateArchiveLibraryCacheFile creates a library file with the name of the root document plus...
Definition: libarch.cpp:43
LIB_PART * FindLibPart(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Function FindLibPart searches all libraries in the list for a part.
const wxString SchematicLibraryFileExtension
Class PART_LIBS is a collection of PART_LIBs.
Definition the SCH_COMPONENT class for Eeschema.
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
const LIB_ID & GetLibId() const
SCH_SCREEN * GetFirst()
Definition for part library class.
bool CreateArchiveLibrary(const wxString &aFileName)
Function CreateArchiveLibrary creates a library aFileName that contains all components used in the cu...
Definition: libarch.cpp:66
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Class SCH_SCREENS is a container class that holds multiple SCH_SCREENs in a hierarchy.