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.




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


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 40 of file project_rescue.cpp.

Function Documentation

static LIB_PART* find_component ( wxString  aName,
bool  aCached 

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

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

Definition at line 189 of file project_rescue.cpp.

Referenced by RESCUE_CACHE_CANDIDATE::FindRescues().

190 {
191  LIB_PART *part = NULL;
193  for( PART_LIB& each_lib : *aLibs )
194  {
195  if( aCached && !each_lib.IsCache() )
196  continue;
198  if( !aCached && each_lib.IsCache() )
199  continue;
201  part = each_lib.FindPart( aName );
202  if( part )
203  break;
204  }
206  return part;
207 }
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)

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)

aComponents- a vector that will take the components

Definition at line 160 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().

161 {
162  SCH_SCREENS screens;
164  // Get the full list
165  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
166  {
167  for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
168  {
169  if( item->Type() != SCH_COMPONENT_T )
170  continue;
171  SCH_COMPONENT* component = static_cast<SCH_COMPONENT*>( item );
172  aComponents.push_back( component );
173  }
174  }
176  // sort aComponents by lib part. Components will be grouped by same lib part.
177  std::sort( aComponents.begin(), aComponents.end(), sort_by_libid );
178 }
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 

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

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

Definition at line 79 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().

80 {
81  wxArrayString libNames;
82  wxString libPaths;
84  wxString libName = aLibrary->GetName();
85  PART_LIBS *libs = dynamic_cast<PART_LIBS*>( aProject->GetElem( PROJECT::ELEM_SCH_PART_LIBS ) );
86  if( !libs )
87  {
88  libs = new PART_LIBS();
89  aProject->SetElem( PROJECT::ELEM_SCH_PART_LIBS, libs );
90  }
92  try
93  {
94  PART_LIBS::LibNamesAndPaths( aProject, false, &libPaths, &libNames );
96  // Make sure the library is not already in the list
97  while( libNames.Index( libName ) != wxNOT_FOUND )
98  libNames.Remove( libName );
100  // Add the library to the list and save
101  libNames.Insert( libName, aIndex );
102  PART_LIBS::LibNamesAndPaths( aProject, true, &libPaths, &libNames );
103  }
104  catch( const IO_ERROR& )
105  {
106  // Could not get or save the current libraries.
107  return false;
108  }
110  // Save the old libraries in case there is a problem after clear(). We'll
111  // put them back in.
112  boost::ptr_vector<PART_LIB> libsSave;
113  libsSave.transfer( libsSave.end(), libs->begin(), libs->end(), *libs );
115  aProject->SetElem( PROJECT::ELEM_SCH_PART_LIBS, NULL );
117  libs = new PART_LIBS();
118  try
119  {
120  libs->LoadAllLibraries( aProject );
121  }
122  catch( const PARSE_ERROR& )
123  {
124  // Some libraries were not found. There's no point in showing the error,
125  // because it was already shown. Just don't do anything.
126  }
127  catch( const IO_ERROR& )
128  {
129  // Restore the old list
130  libs->clear();
131  libs->transfer( libs->end(), libsSave.begin(), libsSave.end(), libsSave );
132  return false;
133  }
134  aProject->SetElem( PROJECT::ELEM_SCH_PART_LIBS, libs );
136  // Update the schematic symbol library links since the library list has changed.
137  SCH_SCREENS schematic;
139  schematic.UpdateSymbolLinks();
141  return true;
142 }
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.
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,

Function save_library writes the library out to disk.

Returns true on success.

aLibrary- Library to write
aEditFrame- the calling SCH_EDIT_FRAME

Definition at line 50 of file project_rescue.cpp.

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

Referenced by RESCUE_CACHE_CANDIDATE::WriteRescueLibrary().

51 {
52  try
53  {
54  aLibrary->Save( false );
55  }
56  catch( ... /* IO_ERROR ioe */ )
57  {
58  wxString msg = wxString::Format( _( "Failed to create component library file '%s'" ),
59  GetChars( aLibrary->GetFullFileName() )
60  );
61  DisplayError( aEditFrame, msg );
62  return false;
63  }
65  return true;
66 }
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:71
static bool sort_by_libid ( const SCH_COMPONENT ref,

Definition at line 146 of file project_rescue.cpp.

References SCH_COMPONENT::GetLibId().

Referenced by get_components().

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