KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_project_settings.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) 2020 Jon Evans <[email protected]>
5 * Copyright (C) 2020-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <functional>
22#include <lset.h>
23#include <lseq.h>
25
26using namespace std::placeholders;
27
28
30 std::vector<LAYER_PRESET>* aPresetList ) :
31 PARAM_LAMBDA<nlohmann::json>( aPath,
32 std::bind( &PARAM_LAYER_PRESET::presetsToJson, this ),
33 std::bind( &PARAM_LAYER_PRESET::jsonToPresets, this, _1 ),
34 {} ),
35 m_presets( aPresetList )
36{
37 wxASSERT( aPresetList );
38}
39
40
42{
43 nlohmann::json ret = nlohmann::json::array();
44
45 for( const LAYER_PRESET& preset : *m_presets )
46 {
47 nlohmann::json js = {
48 { "name", preset.name },
49 { "activeLayer", preset.activeLayer }
50 };
51
52 nlohmann::json layers = nlohmann::json::array();
53
54 for( PCB_LAYER_ID layer : preset.layers.Seq() )
55 layers.push_back( static_cast<int>( layer ) );
56
57 js["layers"] = layers;
58
59 nlohmann::json renderLayers = nlohmann::json::array();
60
61 for( GAL_LAYER_ID layer : preset.renderLayers.Seq() )
62 renderLayers.push_back( static_cast<int>( layer ) );
63
64 js["renderLayers"] = renderLayers;
65
66 ret.push_back( js );
67 }
68
69 return ret;
70}
71
72
73void PARAM_LAYER_PRESET::jsonToPresets( const nlohmann::json& aJson )
74{
75 if( aJson.empty() || !aJson.is_array() )
76 return;
77
78 m_presets->clear();
79
80 for( const nlohmann::json& preset : aJson )
81 {
82 if( preset.contains( "name" ) )
83 {
84 LAYER_PRESET p( preset.at( "name" ).get<wxString>() );
85
86 if( preset.contains( "activeLayer" ) &&
87 preset.at( "activeLayer" ).is_number_integer() )
88 {
89 int active = preset.at( "activeLayer" ).get<int>();
90
91 if( active >= 0 && active < PCB_LAYER_ID_COUNT )
92 p.activeLayer = static_cast<PCB_LAYER_ID>( active );
93 }
94
95 if( preset.contains( "layers" ) && preset.at( "layers" ).is_array() )
96 {
97 p.layers.reset();
98
99 for( const nlohmann::json& layer : preset.at( "layers" ) )
100 {
101 if( layer.is_number_integer() )
102 {
103 int layerNum = layer.get<int>();
104
105 if( layerNum >= 0 && layerNum < PCB_LAYER_ID_COUNT )
106 p.layers.set( layerNum );
107 }
108 }
109 }
110
111 if( preset.contains( "renderLayers" )
112 && preset.at( "renderLayers" ).is_array() )
113 {
114 p.renderLayers.reset();
115
116 for( const nlohmann::json& layer : preset.at( "renderLayers" ) )
117 {
118 if( layer.is_number_integer() )
119 {
120 int layerNum = layer.get<int>();
121
122 if( layerNum >= GAL_LAYER_ID_START
123 && layerNum < GAL_LAYER_ID_END )
124 p.renderLayers.set( static_cast<GAL_LAYER_ID>( layerNum ) );
125 }
126 }
127 }
128
129 m_presets->emplace_back( p );
130 }
131 }
132}
133
134
135void PARAM_LAYER_PRESET::MigrateToV9Layers( nlohmann::json& aJson )
136{
137 if( !aJson.is_object() || !aJson.contains( "layers" ) )
138 return;
139
140 std::vector<int> newLayers;
141
142 for( const nlohmann::json& layer : aJson.at( "layers" ) )
143 {
144 wxCHECK2( layer.is_number_integer(), continue );
145 newLayers.emplace_back( BoardLayerFromLegacyId( layer.get<int>() ) );
146 }
147
148 aJson["layers"] = newLayers;
149}
150
151
152PARAM_VIEWPORT::PARAM_VIEWPORT( const std::string& aPath, std::vector<VIEWPORT>* aViewportList ) :
153 PARAM_LAMBDA<nlohmann::json>( aPath,
154 std::bind( &PARAM_VIEWPORT::viewportsToJson, this ),
155 std::bind( &PARAM_VIEWPORT::jsonToViewports, this, _1 ),
156 {} ),
157 m_viewports( aViewportList )
158{
159 wxASSERT( aViewportList );
160}
161
162
164{
165 nlohmann::json ret = nlohmann::json::array();
166
167 for( const VIEWPORT& viewport : *m_viewports )
168 {
169 nlohmann::json js = {
170 { "name", viewport.name },
171 { "x", viewport.rect.GetX() },
172 { "y", viewport.rect.GetY() },
173 { "w", viewport.rect.GetWidth() },
174 { "h", viewport.rect.GetHeight() }
175 };
176
177 ret.push_back( js );
178 }
179
180 return ret;
181}
182
183
184void PARAM_VIEWPORT::jsonToViewports( const nlohmann::json& aJson )
185{
186 if( aJson.empty() || !aJson.is_array() )
187 return;
188
189 m_viewports->clear();
190
191 for( const nlohmann::json& viewport : aJson )
192 {
193 if( viewport.contains( "name" ) )
194 {
195 VIEWPORT v( viewport.at( "name" ).get<wxString>() );
196
197 if( viewport.contains( "x" ) )
198 v.rect.SetX( viewport.at( "x" ).get<double>() );
199
200 if( viewport.contains( "y" ) )
201 v.rect.SetY( viewport.at( "y" ).get<double>() );
202
203 if( viewport.contains( "w" ) )
204 v.rect.SetWidth( viewport.at( "w" ).get<double>() );
205
206 if( viewport.contains( "h" ) )
207 v.rect.SetHeight( viewport.at( "h" ).get<double>() );
208
209 m_viewports->emplace_back( v );
210 }
211 }
212}
213
214
215PARAM_VIEWPORT3D::PARAM_VIEWPORT3D( const std::string& aPath,
216 std::vector<VIEWPORT3D>* aViewportList ) :
217 PARAM_LAMBDA<nlohmann::json>( aPath,
218 std::bind( &PARAM_VIEWPORT3D::viewportsToJson, this ),
219 std::bind( &PARAM_VIEWPORT3D::jsonToViewports, this, _1 ),
220 {} ),
221 m_viewports( aViewportList )
222{
223 wxASSERT( aViewportList );
224}
225
226
228{
229 nlohmann::json ret = nlohmann::json::array();
230
231 for( const VIEWPORT3D& viewport : *m_viewports )
232 {
233 nlohmann::json js = {
234 { "name", viewport.name },
235 { "xx", viewport.matrix[0].x },
236 { "xy", viewport.matrix[0].y },
237 { "xz", viewport.matrix[0].z },
238 { "xw", viewport.matrix[0].w },
239 { "yx", viewport.matrix[1].x },
240 { "yy", viewport.matrix[1].y },
241 { "yz", viewport.matrix[1].z },
242 { "yw", viewport.matrix[1].w },
243 { "zx", viewport.matrix[2].x },
244 { "zy", viewport.matrix[2].y },
245 { "zz", viewport.matrix[2].z },
246 { "zw", viewport.matrix[2].w },
247 { "wx", viewport.matrix[3].x },
248 { "wy", viewport.matrix[3].y },
249 { "wz", viewport.matrix[3].z },
250 { "ww", viewport.matrix[3].w }
251 };
252
253 ret.push_back( js );
254 }
255
256 return ret;
257}
258
259
260void PARAM_VIEWPORT3D::jsonToViewports( const nlohmann::json& aJson )
261{
262 if( aJson.empty() || !aJson.is_array() )
263 return;
264
265 m_viewports->clear();
266
267 for( const nlohmann::json& viewport : aJson )
268 {
269 if( viewport.contains( "name" ) )
270 {
271 VIEWPORT3D v( viewport.at( "name" ).get<wxString>() );
272
273 if( viewport.contains( "xx" ) )
274 v.matrix[0].x = viewport.at( "xx" ).get<double>();
275
276 if( viewport.contains( "xy" ) )
277 v.matrix[0].y = viewport.at( "xy" ).get<double>();
278
279 if( viewport.contains( "xz" ) )
280 v.matrix[0].z = viewport.at( "xz" ).get<double>();
281
282 if( viewport.contains( "xw" ) )
283 v.matrix[0].w = viewport.at( "xw" ).get<double>();
284
285 if( viewport.contains( "yx" ) )
286 v.matrix[1].x = viewport.at( "yx" ).get<double>();
287
288 if( viewport.contains( "yy" ) )
289 v.matrix[1].y = viewport.at( "yy" ).get<double>();
290
291 if( viewport.contains( "yz" ) )
292 v.matrix[1].z = viewport.at( "yz" ).get<double>();
293
294 if( viewport.contains( "yw" ) )
295 v.matrix[1].w = viewport.at( "yw" ).get<double>();
296
297 if( viewport.contains( "zx" ) )
298 v.matrix[2].x = viewport.at( "zx" ).get<double>();
299
300 if( viewport.contains( "zy" ) )
301 v.matrix[2].y = viewport.at( "zy" ).get<double>();
302
303 if( viewport.contains( "zz" ) )
304 v.matrix[2].z = viewport.at( "zz" ).get<double>();
305
306 if( viewport.contains( "zw" ) )
307 v.matrix[2].w = viewport.at( "zw" ).get<double>();
308
309 if( viewport.contains( "wx" ) )
310 v.matrix[3].x = viewport.at( "wx" ).get<double>();
311
312 if( viewport.contains( "wy" ) )
313 v.matrix[3].y = viewport.at( "wy" ).get<double>();
314
315 if( viewport.contains( "wz" ) )
316 v.matrix[3].z = viewport.at( "wz" ).get<double>();
317
318 if( viewport.contains( "ww" ) )
319 v.matrix[3].w = viewport.at( "ww" ).get<double>();
320
321 m_viewports->emplace_back( v );
322 }
323 }
324}
325
326
327PARAM_LAYER_PAIRS::PARAM_LAYER_PAIRS( const std::string& aPath,
328 std::vector<LAYER_PAIR_INFO>& aLayerPairInfos ) :
329 PARAM_LAMBDA<nlohmann::json>( aPath,
330 std::bind( &PARAM_LAYER_PAIRS::layerPairsToJson, this ),
331 std::bind( &PARAM_LAYER_PAIRS::jsonToLayerPairs, this, _1 ),
332 {} ),
333 m_layerPairInfos( aLayerPairInfos )
334{
335}
336
337
339{
340 nlohmann::json ret = nlohmann::json::array();
341
342 for( const LAYER_PAIR_INFO& pairInfo : m_layerPairInfos )
343 {
344 const LAYER_PAIR& pair = pairInfo.GetLayerPair();
345 nlohmann::json js = {
346 { "topLayer", pair.GetLayerA() },
347 { "bottomLayer", pair.GetLayerB() },
348 { "enabled", pairInfo.IsEnabled() },
349 };
350
351 if( pairInfo.GetName().has_value() )
352 {
353 js["name"] = pairInfo.GetName().value();
354 }
355
356 ret.push_back( std::move( js ) );
357 }
358
359 return ret;
360}
361
362
363void PARAM_LAYER_PAIRS::jsonToLayerPairs( const nlohmann::json& aJson )
364{
365 if( aJson.empty() || !aJson.is_array() )
366 return;
367
368 m_layerPairInfos.clear();
369
370 for( const nlohmann::json& pairJson : aJson )
371 {
372 if( pairJson.contains( "topLayer" ) && pairJson.contains( "bottomLayer" ) )
373 {
374 LAYER_PAIR pair( pairJson.at( "topLayer" ).get<PCB_LAYER_ID>(),
375 pairJson.at( "bottomLayer" ).get<PCB_LAYER_ID>() );
376
377 bool enabled = true;
378 if( pairJson.contains( "enabled" ) )
379 enabled = pairJson.at( "enabled" ).get<bool>();
380
381 std::optional<wxString> name;
382 if( pairJson.contains( "name" ) )
383 name = pairJson.at( "name" ).get<wxString>();
384
385 m_layerPairInfos.emplace_back( LAYER_PAIR_INFO( pair, enabled, std::move( name ) ) );
386 }
387 }
388}
const char * name
Definition: DXF_plotter.cpp:57
BASE_SET & reset(size_t pos)
Definition: base_set.h:142
BASE_SET & set(size_t pos)
Definition: base_set.h:115
constexpr void SetHeight(size_type val)
Definition: box2.h:292
constexpr void SetWidth(size_type val)
Definition: box2.h:287
constexpr void SetX(coord_type val)
Definition: box2.h:277
constexpr void SetY(coord_type val)
Definition: box2.h:282
GAL_SET & set()
Definition: layer_ids.h:323
All information about a layer pair as stored in the layer pair store.
PCB_LAYER_ID GetLayerA() const
PCB_LAYER_ID GetLayerB() const
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:295
nlohmann::json layerPairsToJson()
void jsonToLayerPairs(const nlohmann::json &aJson)
std::vector< LAYER_PAIR_INFO > & m_layerPairInfos
PARAM_LAYER_PAIRS(const std::string &aPath, std::vector< LAYER_PAIR_INFO > &m_layerPairInfos)
static void MigrateToV9Layers(nlohmann::json &aJson)
nlohmann::json presetsToJson()
PARAM_LAYER_PRESET(const std::string &aPath, std::vector< LAYER_PRESET > *aPresetList)
std::vector< LAYER_PRESET > * m_presets
void jsonToPresets(const nlohmann::json &aJson)
void jsonToViewports(const nlohmann::json &aJson)
PARAM_VIEWPORT3D(const std::string &aPath, std::vector< VIEWPORT3D > *aViewportList)
nlohmann::json viewportsToJson()
std::vector< VIEWPORT3D > * m_viewports
std::vector< VIEWPORT > * m_viewports
PARAM_VIEWPORT(const std::string &aPath, std::vector< VIEWPORT > *aViewportList)
nlohmann::json viewportsToJson()
void jsonToViewports(const nlohmann::json &aJson)
nlohmann::json json
Definition: gerbview.cpp:47
PCB_LAYER_ID BoardLayerFromLegacyId(int aLegacyId)
Retrieves a layer ID from an integer converted from a legacy (pre-V9) enum value.
Definition: layer_id.cpp:254
GAL_LAYER_ID
GAL layers are "virtual" layers, i.e.
Definition: layer_ids.h:191
@ GAL_LAYER_ID_START
Definition: layer_ids.h:192
@ GAL_LAYER_ID_END
Definition: layer_ids.h:268
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:135
STL namespace.
A saved set of layers that are visible.
GAL_SET renderLayers
Render layers (e.g. object types) that are visible.
LSET layers
Board layers that are visible.
PCB_LAYER_ID activeLayer
Optional layer to set active when this preset is loaded.