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 The 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>
26
27using namespace std::placeholders;
28
29
31 std::vector<LAYER_PRESET>* aPresetList ) :
32 PARAM_LAMBDA<nlohmann::json>( aPath,
33 std::bind( &PARAM_LAYER_PRESET::presetsToJson, this ),
34 std::bind( &PARAM_LAYER_PRESET::jsonToPresets, this, _1 ),
35 {} ),
36 m_presets( aPresetList )
37{
38 wxASSERT( aPresetList );
39}
40
41
43{
44 nlohmann::json ret = nlohmann::json::array();
45
46 for( const LAYER_PRESET& preset : *m_presets )
47 {
48 nlohmann::json js = {
49 { "name", preset.name },
50 { "activeLayer", preset.activeLayer }
51 };
52
53 nlohmann::json layers = nlohmann::json::array();
54
55 for( PCB_LAYER_ID layer : preset.layers.Seq() )
56 layers.push_back( static_cast<int>( layer ) );
57
58 js["layers"] = layers;
59
60 nlohmann::json renderLayers = nlohmann::json::array();
61
62 for( GAL_LAYER_ID layer : preset.renderLayers.Seq() )
63 {
64 if( std::optional<VISIBILITY_LAYER> vl = VisibilityLayerFromRenderLayer( layer ) )
65 renderLayers.push_back( VisibilityLayerToString( *vl ) );
66 }
67
68 js["renderLayers"] = renderLayers;
69
70 ret.push_back( js );
71 }
72
73 return ret;
74}
75
76
77void PARAM_LAYER_PRESET::jsonToPresets( const nlohmann::json& aJson )
78{
79 if( aJson.empty() || !aJson.is_array() )
80 return;
81
82 m_presets->clear();
83
84 for( const nlohmann::json& preset : aJson )
85 {
86 if( preset.contains( "name" ) )
87 {
88 LAYER_PRESET p( preset.at( "name" ).get<wxString>() );
89
90 if( preset.contains( "activeLayer" ) &&
91 preset.at( "activeLayer" ).is_number_integer() )
92 {
93 int active = preset.at( "activeLayer" ).get<int>();
94
95 if( active >= 0 && active < PCB_LAYER_ID_COUNT )
96 p.activeLayer = static_cast<PCB_LAYER_ID>( active );
97 }
98
99 if( preset.contains( "layers" ) && preset.at( "layers" ).is_array() )
100 {
101 p.layers.reset();
102
103 for( const nlohmann::json& layer : preset.at( "layers" ) )
104 {
105 if( layer.is_number_integer() )
106 {
107 int layerNum = layer.get<int>();
108
109 if( layerNum >= 0 && layerNum < PCB_LAYER_ID_COUNT )
110 p.layers.set( layerNum );
111 }
112 }
113 }
114
115 if( preset.contains( "renderLayers" )
116 && preset.at( "renderLayers" ).is_array() )
117 {
118 p.renderLayers.reset();
119
120 for( const nlohmann::json& layer : preset.at( "renderLayers" ) )
121 {
122 if( layer.is_string() )
123 {
124 std::string vs = layer.get<std::string>();
125
126 if( std::optional<GAL_LAYER_ID> rl = RenderLayerFromVisbilityString( vs ) )
127 p.renderLayers.set( *rl );
128 }
129 }
130 }
131
132 m_presets->emplace_back( p );
133 }
134 }
135}
136
137
138void PARAM_LAYER_PRESET::MigrateToV9Layers( nlohmann::json& aJson )
139{
140 if( !aJson.is_object() || !aJson.contains( "layers" ) )
141 return;
142
143 std::vector<int> newLayers;
144
145 for( const nlohmann::json& layer : aJson.at( "layers" ) )
146 {
147 wxCHECK2( layer.is_number_integer(), continue );
148 newLayers.emplace_back( BoardLayerFromLegacyId( layer.get<int>() ) );
149 }
150
151 aJson["layers"] = newLayers;
152
153 if( aJson.contains( "activeLayer" ) )
154 aJson["activeLayer"] = BoardLayerFromLegacyId( aJson.at( "activeLayer" ).get<int>() );
155}
156
157
159{
160 static constexpr int V8_GAL_LAYER_ID_START = 125;
161
162 if( !aJson.is_object() || !aJson.contains( "renderLayers" ) )
163 return;
164
165 std::vector<std::string> newLayers;
166
167 for( const nlohmann::json& layer : aJson.at( "renderLayers" ) )
168 {
169 wxCHECK2( layer.is_number_integer(), continue );
170 GAL_LAYER_ID layerId = GAL_LAYER_ID_START + ( layer.get<int>() - V8_GAL_LAYER_ID_START );
171
172 if( std::optional<VISIBILITY_LAYER> vl = VisibilityLayerFromRenderLayer( layerId ) )
173 newLayers.emplace_back( VisibilityLayerToString( *vl ) );
174 }
175
176 aJson["renderLayers"] = newLayers;
177}
178
179
180PARAM_VIEWPORT::PARAM_VIEWPORT( const std::string& aPath, std::vector<VIEWPORT>* aViewportList ) :
181 PARAM_LAMBDA<nlohmann::json>( aPath,
182 std::bind( &PARAM_VIEWPORT::viewportsToJson, this ),
183 std::bind( &PARAM_VIEWPORT::jsonToViewports, this, _1 ),
184 {} ),
185 m_viewports( aViewportList )
186{
187 wxASSERT( aViewportList );
188}
189
190
192{
193 nlohmann::json ret = nlohmann::json::array();
194
195 for( const VIEWPORT& viewport : *m_viewports )
196 {
197 nlohmann::json js = {
198 { "name", viewport.name },
199 { "x", viewport.rect.GetX() },
200 { "y", viewport.rect.GetY() },
201 { "w", viewport.rect.GetWidth() },
202 { "h", viewport.rect.GetHeight() }
203 };
204
205 ret.push_back( js );
206 }
207
208 return ret;
209}
210
211
212void PARAM_VIEWPORT::jsonToViewports( const nlohmann::json& aJson )
213{
214 if( aJson.empty() || !aJson.is_array() )
215 return;
216
217 m_viewports->clear();
218
219 for( const nlohmann::json& viewport : aJson )
220 {
221 if( viewport.contains( "name" ) )
222 {
223 VIEWPORT v( viewport.at( "name" ).get<wxString>() );
224
225 if( viewport.contains( "x" ) )
226 v.rect.SetX( viewport.at( "x" ).get<double>() );
227
228 if( viewport.contains( "y" ) )
229 v.rect.SetY( viewport.at( "y" ).get<double>() );
230
231 if( viewport.contains( "w" ) )
232 v.rect.SetWidth( viewport.at( "w" ).get<double>() );
233
234 if( viewport.contains( "h" ) )
235 v.rect.SetHeight( viewport.at( "h" ).get<double>() );
236
237 m_viewports->emplace_back( v );
238 }
239 }
240}
241
242
243PARAM_VIEWPORT3D::PARAM_VIEWPORT3D( const std::string& aPath,
244 std::vector<VIEWPORT3D>* aViewportList ) :
245 PARAM_LAMBDA<nlohmann::json>( aPath,
246 std::bind( &PARAM_VIEWPORT3D::viewportsToJson, this ),
247 std::bind( &PARAM_VIEWPORT3D::jsonToViewports, this, _1 ),
248 {} ),
249 m_viewports( aViewportList )
250{
251 wxASSERT( aViewportList );
252}
253
254
256{
257 nlohmann::json ret = nlohmann::json::array();
258
259 for( const VIEWPORT3D& viewport : *m_viewports )
260 {
261 nlohmann::json js = {
262 { "name", viewport.name },
263 { "xx", viewport.matrix[0].x },
264 { "xy", viewport.matrix[0].y },
265 { "xz", viewport.matrix[0].z },
266 { "xw", viewport.matrix[0].w },
267 { "yx", viewport.matrix[1].x },
268 { "yy", viewport.matrix[1].y },
269 { "yz", viewport.matrix[1].z },
270 { "yw", viewport.matrix[1].w },
271 { "zx", viewport.matrix[2].x },
272 { "zy", viewport.matrix[2].y },
273 { "zz", viewport.matrix[2].z },
274 { "zw", viewport.matrix[2].w },
275 { "wx", viewport.matrix[3].x },
276 { "wy", viewport.matrix[3].y },
277 { "wz", viewport.matrix[3].z },
278 { "ww", viewport.matrix[3].w }
279 };
280
281 ret.push_back( js );
282 }
283
284 return ret;
285}
286
287
288void PARAM_VIEWPORT3D::jsonToViewports( const nlohmann::json& aJson )
289{
290 if( aJson.empty() || !aJson.is_array() )
291 return;
292
293 m_viewports->clear();
294
295 for( const nlohmann::json& viewport : aJson )
296 {
297 if( viewport.contains( "name" ) )
298 {
299 VIEWPORT3D v( viewport.at( "name" ).get<wxString>() );
300
301 if( viewport.contains( "xx" ) )
302 v.matrix[0].x = viewport.at( "xx" ).get<double>();
303
304 if( viewport.contains( "xy" ) )
305 v.matrix[0].y = viewport.at( "xy" ).get<double>();
306
307 if( viewport.contains( "xz" ) )
308 v.matrix[0].z = viewport.at( "xz" ).get<double>();
309
310 if( viewport.contains( "xw" ) )
311 v.matrix[0].w = viewport.at( "xw" ).get<double>();
312
313 if( viewport.contains( "yx" ) )
314 v.matrix[1].x = viewport.at( "yx" ).get<double>();
315
316 if( viewport.contains( "yy" ) )
317 v.matrix[1].y = viewport.at( "yy" ).get<double>();
318
319 if( viewport.contains( "yz" ) )
320 v.matrix[1].z = viewport.at( "yz" ).get<double>();
321
322 if( viewport.contains( "yw" ) )
323 v.matrix[1].w = viewport.at( "yw" ).get<double>();
324
325 if( viewport.contains( "zx" ) )
326 v.matrix[2].x = viewport.at( "zx" ).get<double>();
327
328 if( viewport.contains( "zy" ) )
329 v.matrix[2].y = viewport.at( "zy" ).get<double>();
330
331 if( viewport.contains( "zz" ) )
332 v.matrix[2].z = viewport.at( "zz" ).get<double>();
333
334 if( viewport.contains( "zw" ) )
335 v.matrix[2].w = viewport.at( "zw" ).get<double>();
336
337 if( viewport.contains( "wx" ) )
338 v.matrix[3].x = viewport.at( "wx" ).get<double>();
339
340 if( viewport.contains( "wy" ) )
341 v.matrix[3].y = viewport.at( "wy" ).get<double>();
342
343 if( viewport.contains( "wz" ) )
344 v.matrix[3].z = viewport.at( "wz" ).get<double>();
345
346 if( viewport.contains( "ww" ) )
347 v.matrix[3].w = viewport.at( "ww" ).get<double>();
348
349 m_viewports->emplace_back( v );
350 }
351 }
352}
353
354
355PARAM_LAYER_PAIRS::PARAM_LAYER_PAIRS( const std::string& aPath,
356 std::vector<LAYER_PAIR_INFO>& aLayerPairInfos ) :
357 PARAM_LAMBDA<nlohmann::json>( aPath,
358 std::bind( &PARAM_LAYER_PAIRS::layerPairsToJson, this ),
359 std::bind( &PARAM_LAYER_PAIRS::jsonToLayerPairs, this, _1 ),
360 {} ),
361 m_layerPairInfos( aLayerPairInfos )
362{
363}
364
365
367{
368 nlohmann::json ret = nlohmann::json::array();
369
370 for( const LAYER_PAIR_INFO& pairInfo : m_layerPairInfos )
371 {
372 const LAYER_PAIR& pair = pairInfo.GetLayerPair();
373 nlohmann::json js = {
374 { "topLayer", pair.GetLayerA() },
375 { "bottomLayer", pair.GetLayerB() },
376 { "enabled", pairInfo.IsEnabled() },
377 };
378
379 if( pairInfo.GetName().has_value() )
380 {
381 js["name"] = pairInfo.GetName().value();
382 }
383
384 ret.push_back( std::move( js ) );
385 }
386
387 return ret;
388}
389
390
391void PARAM_LAYER_PAIRS::jsonToLayerPairs( const nlohmann::json& aJson )
392{
393 if( aJson.empty() || !aJson.is_array() )
394 return;
395
396 m_layerPairInfos.clear();
397
398 for( const nlohmann::json& pairJson : aJson )
399 {
400 if( pairJson.contains( "topLayer" ) && pairJson.contains( "bottomLayer" ) )
401 {
402 LAYER_PAIR pair( pairJson.at( "topLayer" ).get<PCB_LAYER_ID>(),
403 pairJson.at( "bottomLayer" ).get<PCB_LAYER_ID>() );
404
405 bool enabled = true;
406 if( pairJson.contains( "enabled" ) )
407 enabled = pairJson.at( "enabled" ).get<bool>();
408
409 std::optional<wxString> name;
410 if( pairJson.contains( "name" ) )
411 name = pairJson.at( "name" ).get<wxString>();
412
413 m_layerPairInfos.emplace_back( LAYER_PAIR_INFO( pair, enabled, std::move( name ) ) );
414 }
415 }
416}
const char * name
Definition: DXF_plotter.cpp:59
BASE_SET & reset(size_t pos)
Definition: base_set.h:143
BASE_SET & set(size_t pos)
Definition: base_set.h:116
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:371
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)
static void MigrateToNamedRenderLayers(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)
Retrieve a layer ID from an integer converted from a legacy (pre-V9) enum value.
Definition: layer_id.cpp:256
GAL_LAYER_ID
GAL layers are "virtual" layers, i.e.
Definition: layer_ids.h:191
@ GAL_LAYER_ID_START
Definition: layer_ids.h:192
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:135
std::optional< VISIBILITY_LAYER > VisibilityLayerFromRenderLayer(GAL_LAYER_ID aLayerId)
std::string VisibilityLayerToString(VISIBILITY_LAYER aLayerId)
std::optional< GAL_LAYER_ID > RenderLayerFromVisbilityString(const std::string &aLayer)
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.