KiCad PCB EDA Suite
project_rescue.cpp File Reference
#include <class_drawpanel.h>
#include <class_library.h>
#include <confirm.h>
#include <invoke_sch_dialog.h>
#include <kicad_device_context.h>
#include <project_rescue.h>
#include <sch_component.h>
#include <sch_sheet.h>
#include <schframe.h>
#include <wildcards_and_files_ext.h>
#include <cctype>
#include <map>

Go to the source code of this file.

Classes

class  RESCUE_CASE_CANDIDATE
 
class  RESCUE_CACHE_CANDIDATE
 

Typedefs

typedef std::pair< SCH_COMPONENT *, wxString > COMPONENT_NAME_PAIR
 

Functions

static bool save_library (PART_LIB *aLibrary, SCH_EDIT_FRAME *aEditFrame)
 Function save_library writes the library out to disk. More...
 
static bool insert_library (PROJECT *aProject, PART_LIB *aLibrary, size_t aIndex)
 Function insert_library inserts a library into the project and refreshes libraries. More...
 
static bool sort_by_libid (const SCH_COMPONENT *ref, SCH_COMPONENT *cmp)
 
static void get_components (std::vector< SCH_COMPONENT * > &aComponents)
 Function get_components Fills a vector with all of the project's components, to ease iterating over them. More...
 
static LIB_PARTfind_component (wxString aName, PART_LIBS *aLibs, bool aCached)
 Function find_component Search the libraries for the first component with a given name. More...
 

Typedef Documentation

typedef std::pair<SCH_COMPONENT*, wxString> COMPONENT_NAME_PAIR

Definition at line 39 of file project_rescue.cpp.

Function Documentation

static LIB_PART* find_component ( wxString  aName,
PART_LIBS aLibs,
bool  aCached 
)
static

Function find_component Search the libraries for the first component with a given name.

Parameters
aName- name to search for
aLibs- the loaded PART_LIBS
aCached- whether we are looking for the cached part

Definition at line 188 of file project_rescue.cpp.

Referenced by RESCUE_CACHE_CANDIDATE::FindRescues().

189 {
190  LIB_PART *part = NULL;
191 
192  for( PART_LIB& each_lib : *aLibs )
193  {
194  if( aCached && !each_lib.IsCache() )
195  continue;
196 
197  if( !aCached && each_lib.IsCache() )
198  continue;
199 
200  part = each_lib.FindPart( aName );
201  if( part )
202  break;
203  }
204 
205  return part;
206 }
Class LIB_PART defines a library part object.
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
static void get_components ( std::vector< SCH_COMPONENT * > &  aComponents)
static

Function get_components Fills a vector with all of the project's components, to ease iterating over them.

The list is sorted by lib id, therefore components using the same library symbol are grouped, allowing later faster calculations (one library search by group of symbols)

Parameters
aComponents- a vector that will take the components

Definition at line 159 of file project_rescue.cpp.

References SCH_SCREENS::GetFirst(), SCH_SCREENS::GetNext(), SCH_ITEM::Next(), SCH_COMPONENT_T, and sort_by_libid().

Referenced by RESCUER::RESCUER().

160 {
161  SCH_SCREENS screens;
162 
163  // Get the full list
164  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
165  {
166  for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
167  {
168  if( item->Type() != SCH_COMPONENT_T )
169  continue;
170  SCH_COMPONENT* component = static_cast<SCH_COMPONENT*>( item );
171  aComponents.push_back( component );
172  }
173  }
174 
175  // sort aComponents by lib part. Components will be grouped by same lib part.
176  std::sort( aComponents.begin(), aComponents.end(), sort_by_libid );
177 }
SCH_SCREEN * GetNext()
static bool sort_by_libid(const SCH_COMPONENT *ref, SCH_COMPONENT *cmp)
SCH_ITEM * Next() const
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
SCH_SCREEN * GetFirst()
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.
static bool insert_library ( PROJECT aProject,
PART_LIB aLibrary,
size_t  aIndex 
)
static

Function insert_library inserts a library into the project and refreshes libraries.

Parameters
aProject- project that will be modified
aLibrary- PART_LIB to add
aIndex- index in the list at which the library is to be inserted
Returns
true on success, false on failure

Definition at line 78 of file project_rescue.cpp.

References PROJECT::ELEM_SCH_PART_LIBS, PROJECT::GetElem(), PART_LIB::GetName(), PART_LIBS::LibNamesAndPaths(), PART_LIBS::LoadAllLibraries(), PROJECT::SetElem(), and SCH_SCREENS::UpdateSymbolLinks().

Referenced by RESCUE_CACHE_CANDIDATE::WriteRescueLibrary().

79 {
80  wxArrayString libNames;
81  wxString libPaths;
82 
83  wxString libName = aLibrary->GetName();
84  PART_LIBS *libs = dynamic_cast<PART_LIBS*>( aProject->GetElem( PROJECT::ELEM_SCH_PART_LIBS ) );
85  if( !libs )
86  {
87  libs = new PART_LIBS();
88  aProject->SetElem( PROJECT::ELEM_SCH_PART_LIBS, libs );
89  }
90 
91  try
92  {
93  PART_LIBS::LibNamesAndPaths( aProject, false, &libPaths, &libNames );
94 
95  // Make sure the library is not already in the list
96  while( libNames.Index( libName ) != wxNOT_FOUND )
97  libNames.Remove( libName );
98 
99  // Add the library to the list and save
100  libNames.Insert( libName, aIndex );
101  PART_LIBS::LibNamesAndPaths( aProject, true, &libPaths, &libNames );
102  }
103  catch( const IO_ERROR& )
104  {
105  // Could not get or save the current libraries.
106  return false;
107  }
108 
109  // Save the old libraries in case there is a problem after clear(). We'll
110  // put them back in.
111  boost::ptr_vector<PART_LIB> libsSave;
112  libsSave.transfer( libsSave.end(), libs->begin(), libs->end(), *libs );
113 
114  aProject->SetElem( PROJECT::ELEM_SCH_PART_LIBS, NULL );
115 
116  libs = new PART_LIBS();
117  try
118  {
119  libs->LoadAllLibraries( aProject );
120  }
121  catch( const PARSE_ERROR& )
122  {
123  // Some libraries were not found. There's no point in showing the error,
124  // because it was already shown. Just don't do anything.
125  }
126  catch( const IO_ERROR& )
127  {
128  // Restore the old list
129  libs->clear();
130  libs->transfer( libs->end(), libsSave.begin(), libsSave.end(), libsSave );
131  return false;
132  }
133  aProject->SetElem( PROJECT::ELEM_SCH_PART_LIBS, libs );
134 
135  // Update the schematic symbol library links since the library list has changed.
136  SCH_SCREENS schematic;
137 
138  schematic.UpdateSymbolLinks();
139 
140  return true;
141 }
static void LibNamesAndPaths(PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=NULL)
Function LibNamesAndPaths either saves or loads the names of the currently configured part libraries ...
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Function LoadAllLibraries loads all of the project's libraries into this container, which should be cleared before calling it.
VTBL_ENTRY _ELEM * GetElem(ELEM_T aIndex)
Typically wrapped somewhere else in a more meaningful function wrapper.
Definition: project.cpp:196
VTBL_ENTRY void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:208
void UpdateSymbolLinks()
initializes or reinitializes the weak reference to the LIB_PART for each SCH_COMPONENT found in the f...
const wxString GetName() const
Return the file name without path or extension.
Class PART_LIBS is a collection of PART_LIBs.
Struct PARSE_ERROR contains a filename or source description, a problem input line, a line number, a byte offset, and an error message which contains the the caller's report and his call site information: CPP source file, function, and line number.
Definition: ki_exception.h:94
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
Class SCH_SCREENS is a container class that holds multiple SCH_SCREENs in a hierarchy.
static bool save_library ( PART_LIB aLibrary,
SCH_EDIT_FRAME aEditFrame 
)
static

Function save_library writes the library out to disk.

Returns true on success.

Parameters
aLibrary- Library to write
aEditFrame- the calling SCH_EDIT_FRAME

Definition at line 49 of file project_rescue.cpp.

References DisplayError(), Format(), GetChars(), PART_LIB::GetFullFileName(), and PART_LIB::Save().

Referenced by RESCUE_CACHE_CANDIDATE::WriteRescueLibrary().

50 {
51  try
52  {
53  aLibrary->Save( false );
54  }
55  catch( ... /* IO_ERROR ioe */ )
56  {
57  wxString msg = wxString::Format( _( "Failed to create component library file '%s'" ),
58  GetChars( aLibrary->GetFullFileName() )
59  );
60  DisplayError( aEditFrame, msg );
61  return false;
62  }
63 
64  return true;
65 }
void Save(bool aSaveDocFile=true)
wxString GetFullFileName()
Function GetFullFileName returns the full file library name with path and extension.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
static bool sort_by_libid ( const SCH_COMPONENT ref,
SCH_COMPONENT cmp 
)
static

Definition at line 145 of file project_rescue.cpp.

References SCH_COMPONENT::GetLibId().

Referenced by get_components().

146 {
147  return ref->GetLibId() < cmp->GetLibId();
148 }
const LIB_ID & GetLibId() const