KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_sch.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) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <symbol_library.h>
21#include <confirm.h>
23#include <kiface_base.h>
24#include <pgm_base.h>
25#include <wx/app.h>
26#include <core/utf8.h>
27#include <symbol_lib_table.h>
28#include <project_sch.h>
29
30static std::mutex s_symbolTableMutex;
31
32// non-member so it can be moved easily, and kept REALLY private.
33// Do NOT Clear() in here.
34static void add_search_paths( SEARCH_STACK* aDst, const SEARCH_STACK& aSrc, int aIndex )
35{
36 for( unsigned i=0; i<aSrc.GetCount(); ++i )
37 aDst->AddPaths( aSrc[i], aIndex );
38}
39
40
42{
44
45 wxASSERT( !ss || dynamic_cast<SEARCH_STACK*>( ss ) );
46
47 if( !ss )
48 {
49 ss = new SEARCH_STACK();
50
51 // Make PROJECT the new SEARCH_STACK owner.
53
54 // to the empty SEARCH_STACK for SchSearchS(), add project dir as first
55 ss->AddPaths( aProject->GetProjectDirectory() );
56
57 // next add the paths found in *.pro, variable "LibDir"
58 wxString libDir;
59
60 try
61 {
62 SYMBOL_LIBS::GetLibNamesAndPaths( aProject, &libDir );
63 }
64 catch( const IO_ERROR& )
65 {
66 }
67
68 if( !!libDir )
69 {
70 wxArrayString paths;
71
72 SEARCH_STACK::Split( &paths, libDir );
73
74 for( unsigned i =0; i<paths.GetCount(); ++i )
75 {
76 wxString path = aProject->AbsolutePath( paths[i] );
77
78 ss->AddPaths( path ); // at the end
79 }
80 }
81
82 // append all paths from aSList
83 add_search_paths( ss, Kiface().KifaceSearch(), -1 );
84 }
85
86 return ss;
87}
88
89
91{
93
94 wxASSERT( !libs || libs->ProjectElementType() == PROJECT::ELEM::SCH_SYMBOL_LIBS );
95
96 if( !libs )
97 {
98 libs = new SYMBOL_LIBS();
99
100 // Make PROJECT the new SYMBOL_LIBS owner.
101 aProject->SetElem( PROJECT::ELEM::SCH_SYMBOL_LIBS, libs );
102
103 try
104 {
105 libs->LoadAllLibraries( aProject );
106 }
107 catch( const PARSE_ERROR& pe )
108 {
109 wxString lib_list = UTF8( pe.inputLine );
110 wxWindow* parent = Pgm().App().GetTopWindow();
111
112 // parent of this dialog cannot be NULL since that breaks the Kiway() chain.
113 HTML_MESSAGE_BOX dlg( parent, _( "Not Found" ) );
114
115 dlg.MessageSet( _( "The following libraries were not found:" ) );
116 dlg.ListSet( lib_list );
117 dlg.Layout();
118
119 dlg.ShowModal();
120 }
121 catch( const IO_ERROR& ioe )
122 {
123 wxWindow* parent = Pgm().App().GetTopWindow();
124
125 DisplayError( parent, ioe.What() );
126 }
127 }
128
129 return libs;
130}
131
132
134{
135 std::lock_guard<std::mutex> lock( s_symbolTableMutex );
136
137 // This is a lazy loading function, it loads the project specific table when
138 // that table is asked for, not before.
139 SYMBOL_LIB_TABLE* tbl =
141
142 // its gotta be NULL or a SYMBOL_LIB_TABLE, or a bug.
143 wxASSERT( !tbl || tbl->ProjectElementType() == PROJECT::ELEM::SYMBOL_LIB_TABLE );
144
145 if( !tbl )
146 {
147 // Stack the project specific SYMBOL_LIB_TABLE overlay on top of the global table.
148 // ~SYMBOL_LIB_TABLE() will not touch the fallback table, so multiple projects may
149 // stack this way, all using the same global fallback table.
151
153
154 wxString prjPath;
155
156 wxGetEnv( PROJECT_VAR_NAME, &prjPath );
157
158 if( !prjPath.IsEmpty() )
159 {
160 wxFileName fn( prjPath, SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
161
162 try
163 {
164 tbl->Load( fn.GetFullPath() );
165 }
166 catch( const IO_ERROR& ioe )
167 {
168 wxString msg;
169 msg.Printf( _( "Error loading the symbol library table '%s'." ), fn.GetFullPath() );
170 DisplayErrorMessage( nullptr, msg, ioe.What() );
171 }
172 }
173 }
174
175 return tbl;
176}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
int ShowModal() override
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void ListSet(const wxString &aList)
Add a list of items.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:182
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
static SEARCH_STACK * SchSearchS(PROJECT *aProject)
Accessor for Eeschema search stack.
Definition: project_sch.cpp:41
static SYMBOL_LIBS * SchLibs(PROJECT *aProject)
Definition: project_sch.cpp:90
Container for project specific data.
Definition: project.h:64
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition: project.cpp:348
virtual _ELEM * GetElem(PROJECT::ELEM aIndex)
Get and set the elements for this project.
Definition: project.cpp:337
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:359
virtual const wxString GetProjectDirectory() const
Return the full path of the project DIRECTORY.
Definition: project.cpp:141
Look for files in a number of paths.
Definition: search_stack.h:43
static int Split(wxArrayString *aResult, const wxString &aPathString)
Separate aPathString into individual paths.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).
A collection of SYMBOL_LIB objects.
PROJECT::ELEM ProjectElementType() override
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Load all of the project's libraries into this container, which should be cleared before calling it.
static void GetLibNamesAndPaths(PROJECT *aProject, wxString *aPaths, wxArrayString *aNames=nullptr)
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
static const wxString & GetSymbolLibTableFileName()
PROJECT::ELEM ProjectElementType() override
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
#define _(s)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:40
static void add_search_paths(SEARCH_STACK *aDst, const SEARCH_STACK &aSrc, int aIndex)
Definition: project_sch.cpp:34
static std::mutex s_symbolTableMutex
Definition: project_sch.cpp:30
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:120
std::string inputLine
problem line of input [say, from a LINE_READER].
Definition: ki_exception.h:128
Definition for symbol library class.