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  return CreateArchiveLibrary( fn.GetFullPath() );
56 }
57 
58 
59 bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
60 {
61  wxString msg;
62  SCH_SCREENS screens;
63  PART_LIBS* libs = Prj().SchLibs();
64  PART_LIB* cacheLib = libs->FindLibraryByFullFileName( aFileName );
65 
66  if( !cacheLib )
67  {
68  cacheLib = new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName );
69  libs->push_back( cacheLib );
70  }
71 
72  cacheLib->SetCache();
73  cacheLib->EnableBuffering();
74 
75  /* Examine all screens (not hierarchical sheets) used in the schematic and build a
76  * library of unique symbols found in all screens. Complex hierarchies are not a
77  * problem because we just want to know the library symbols used in the schematic.
78  */
79  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
80  {
81  for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
82  {
83  if( item->Type() != SCH_COMPONENT_T )
84  continue;
85 
86  SCH_COMPONENT* component = (SCH_COMPONENT*) item;
87 
88  if( !cacheLib->FindAlias( FROM_UTF8( component->GetLibId().GetLibItemName() ) ) )
89  {
90  LIB_PART* part = NULL;
91 
92  try
93  {
94  part = libs->FindLibPart( component->GetLibId() );
95 
96  if( part )
97  {
98  // AddPart() does first clone the part before adding.
99  cacheLib->AddPart( part );
100  }
101  }
102  catch( ... /* IO_ERROR ioe */ )
103  {
104  msg.Printf( _( "Failed to add symbol %s to library file '%s'" ),
105  FROM_UTF8( component->GetLibId().GetLibItemName() ), aFileName );
106  DisplayError( this, msg );
107  return false;
108  }
109  }
110  }
111  }
112 
113  try
114  {
115  cacheLib->Save( false );
116  cacheLib->EnableBuffering( false );
117  }
118  catch( ... /* IO_ERROR ioe */ )
119  {
120  msg.Printf( _( "Failed to save symbol library file '%s'" ), aFileName );
121  DisplayError( this, msg );
122  return false;
123  }
124 
125  return true;
126 }
Definition of the SCH_SHEET class for Eeschema.
SCH_SCREEN * GetNext()
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
void Save(bool aSaveDocFile=true)
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:520
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void EnableBuffering(bool aEnable=true)
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:54
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
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
void SetCache()
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.
void AddPart(LIB_PART *aPart)
Add aPart entry to library.
Definition the SCH_COMPONENT class for Eeschema.
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:68
const LIB_ID & GetLibId() const
PART_LIB * FindLibraryByFullFileName(const wxString &aFullFileName)
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:59
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:69
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
LIB_ALIAS * FindAlias(const wxString &aName)
Find LIB_ALIAS by aName.
Class SCH_SCREENS is a container class that holds multiple SCH_SCREENs in a hierarchy.