KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_library_query.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <exception>
23
24#include <kiface_ids.h>
25#include <kiway.h>
26#include <json_common.h>
27
28
30{
31 using json = nlohmann::json;
32
34
35 try
36 {
37 const json responseJson = json::parse( aJson.utf8_string() );
38
39 // Only a result object reporting a performed query carries matches.
40 if( !responseJson.is_object() )
41 return result;
42
43 result.m_Success = responseJson.value( "success", false );
44
45 if( !result.m_Success || !responseJson.contains( "matches" ) || !responseJson["matches"].is_array() )
46 {
47 result.m_Success = false;
48 return result;
49 }
50
51 const json& matches = responseJson["matches"];
52
53 result.m_MatchingNames.reserve( matches.size() );
54
55 for( const json& match : matches )
56 {
57 if( match.is_string() )
58 result.m_MatchingNames.emplace_back( wxString::FromUTF8( match.get<std::string>() ) );
59 }
60
61 result.m_IsLimited = responseJson.value( "limited", false );
62 }
63 catch( const std::exception& )
64 {
65 result.m_MatchingNames.clear();
66 result.m_Success = false;
67 }
68
69 return result;
70}
71
72
74{
75 using json = nlohmann::json;
76
77 json output = json::object();
78 output["matches"] = json::array();
79 output["limited"] = m_IsLimited;
80 output["success"] = m_Success;
81
82 for( const wxString& match : m_MatchingNames )
83 output["matches"].push_back( match.utf8_string() );
84
85 return wxString::FromUTF8( output.dump() );
86}
87
88
90{
91 using json = nlohmann::json;
92
93 json request;
94
95 int rawPinCount = aQuery.m_PinCount.value_or( 0 );
96 request["pin_count"] = rawPinCount > 0 ? rawPinCount : 0;
97 request["zero_filters"] = aQuery.m_ZeroFilters;
98 request["max_results"] = aQuery.m_MaxResults;
99
100 json filters = json::array();
101
102 for( const wxString& pattern : aQuery.m_Patterns )
103 filters.push_back( pattern.utf8_string() );
104
105 request["filters"] = filters;
106
107 const auto returnFailure = []() -> FOOTPRINT_MATCH_RESULT
108 {
110 result.m_Success = false;
111 return result;
112 };
113
114 // Loading or reaching the pcbnew kiface, and the JSON round-trip itself, can all fail.
115 // Callers cannot act on the difference, so every failure is reported as an empty result.
116 try
117 {
118 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
119
120 if( !kiface )
121 return returnFailure();
122
123 void* address = kiface->IfaceOrAddress( KIFACE_FILTER_FOOTPRINTS );
124
125 if( !address )
126 return returnFailure();
127
128 using FILTER_FUNC = wxString ( * )( const wxString& );
129 FILTER_FUNC filterFootprints = reinterpret_cast<FILTER_FUNC>( address );
130
131 wxString response = filterFootprints( wxString::FromUTF8( request.dump() ) );
132
133 return FOOTPRINT_MATCH_RESULT::FromJsonStr( response );
134 }
135 catch( const std::exception& )
136 {
137 return returnFailure();
138 }
139
140 return returnFailure();
141}
142
143
145{
146 try
147 {
148 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
149
150 if( !kiface )
151 return false;
152
153 void* address = kiface->IfaceOrAddress( KIFACE_TRIGGER_FOOTPRINTS_LOAD );
154
155 if( !address )
156 return false;
157
158 using LOAD_FUNC = bool ( * )();
159 LOAD_FUNC startLoad = reinterpret_cast<LOAD_FUNC>( address );
160
161 return startLoad();
162 }
163 catch( const std::exception& )
164 {
165 return false;
166 }
167
168 return false;
169}
170
171
173{
174 try
175 {
176 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
177
178 if( !kiface )
179 return 1.0f;
180
181 void* address = kiface->IfaceOrAddress( KIFACE_FOOTPRINTS_LOAD_PROGRESS );
182
183 if( !address )
184 return 1.0f;
185
186 using PROGRESS_FUNC = float ( * )();
187 PROGRESS_FUNC getProgress = reinterpret_cast<PROGRESS_FUNC>( address );
188
189 return getProgress();
190 }
191 catch( const std::exception& )
192 {
193 return 1.0f;
194 }
195}
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition kiway.cpp:207
@ FACE_PCB
pcbnew DSO
Definition kiway.h:356
float GetFootprintLibrariesLoadProgress(KIWAY &aKiway)
Return how far the background load of the footprint libraries has got.
bool StartFootprintLibrariesLoad(KIWAY &aKiway)
Start loading the footprint libraries in the background.
FOOTPRINT_MATCH_RESULT QueryMatchingFootprints(KIWAY &aKiway, const FOOTPRINT_MATCH_QUERY &aQuery)
Ask the footprint libraries, for the footprints matching a query (over the Pcbnew kiface).
nlohmann::json json
Definition gerbview.cpp:50
@ KIFACE_FOOTPRINTS_LOAD_PROGRESS
Function pointer type: float (*)() Returns how far the library loader has gotten as a fraction of the...
Definition kiface_ids.h:45
@ KIFACE_FILTER_FOOTPRINTS
Function pointer type: wxString (*)(const wxString& aFilterJson) Input JSON: {"pin_count": N,...
Definition kiface_ids.h:35
@ KIFACE_TRIGGER_FOOTPRINTS_LOAD
Function pointer type: bool (*)() Starts loading the footprint libraries in the background.
Definition kiface_ids.h:40
static wxString filterFootprints(const wxString &aFilterJson)
Filter footprints based on criteria passed as JSON.
Definition pcbnew.cpp:144
static const std::vector< std::string > filters
Definition plugin.cpp:64
All the information needed to query the footprint libraries for matching footprints.
std::vector< wxString > m_Patterns
Wildcard patterns matched against the footprint name, or against "library:footprint" when a pattern c...
bool m_ZeroFilters
When true, a query with no patterns (and no pin count) matches nothing rather than everything.
std::optional< int > m_PinCount
When greater than zero, only footprints with this many pads are returned.
int m_MaxResults
Limits the number of returned footprints; zero means no limit.
The outcome of a footprint match query, and the JSON encoding used to move it across the Pcbnew kifac...
std::vector< wxString > m_MatchingNames
The matching "library:footprint" names.
bool m_Success
False if the query could not be performed, in which case MatchingNames is empty.
bool m_IsLimited
True if the result set is limited by the maximum number of results.
static FOOTPRINT_MATCH_RESULT FromJsonStr(const wxString &aJson)
Parse the JSON object written by ToJsonStr().
wxString ToJsonStr() const
Serialise to the JSON object that carries a result over the Pcbnew kiface.
Implement a participant in the KIWAY alchemy.
Definition kiway.h:153
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
wxString result
Test unit parsing edge cases and error handling.