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_services.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
125
126 if( !filterFootprints )
127 return returnFailure();
128
129 wxString response = filterFootprints( wxString::FromUTF8( request.dump() ) );
130
131 return FOOTPRINT_MATCH_RESULT::FromJsonStr( response );
132 }
133 catch( const std::exception& )
134 {
135 }
136
137 return returnFailure();
138}
139
140
142{
143 try
144 {
145 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
146
147 if( !kiface )
148 return false;
149
152
153 if( !startLoad )
154 return false;
155
156 return startLoad();
157 }
158 catch( const std::exception& )
159 {
160 }
161
162 return false;
163}
164
165
167{
168 try
169 {
170 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
171
172 if( !kiface )
173 return 1.0f;
174
177
178 if( !getProgress )
179 return 1.0f;
180
181 return getProgress();
182 }
183 catch( const std::exception& )
184 {
185 return 1.0f;
186 }
187}
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:216
@ 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
Service::Type GetKifaceService(KIFACE *aKiface)
Look up a service exported by a KIFACE.
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.