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
135PARAM_VIEWPORT::PARAM_VIEWPORT( const std::string& aPath, std::vector<VIEWPORT>* aViewportList ) :
136 PARAM_LAMBDA<nlohmann::json>( aPath,
137 std::bind( &PARAM_VIEWPORT::viewportsToJson, this ),
138 std::bind( &PARAM_VIEWPORT::jsonToViewports, this, _1 ),
139 {} ),
140 m_viewports( aViewportList )
141{
142 wxASSERT( aViewportList );
143}
144
145
147{
148 nlohmann::json ret = nlohmann::json::array();
149
150 for( const VIEWPORT& viewport : *m_viewports )
151 {
152 nlohmann::json js = {
153 { "name", viewport.name },
154 { "x", viewport.rect.GetX() },
155 { "y", viewport.rect.GetY() },
156 { "w", viewport.rect.GetWidth() },
157 { "h", viewport.rect.GetHeight() }
158 };
159
160 ret.push_back( js );
161 }
162
163 return ret;
164}
165
166
167void PARAM_VIEWPORT::jsonToViewports( const nlohmann::json& aJson )
168{
169 if( aJson.empty() || !aJson.is_array() )
170 return;
171
172 m_viewports->clear();
173
174 for( const nlohmann::json& viewport : aJson )
175 {
176 if( viewport.contains( "name" ) )
177 {
178 VIEWPORT v( viewport.at( "name" ).get<wxString>() );
179
180 if( viewport.contains( "x" ) )
181 v.rect.SetX( viewport.at( "x" ).get<double>() );
182
183 if( viewport.contains( "y" ) )
184 v.rect.SetY( viewport.at( "y" ).get<double>() );
185
186 if( viewport.contains( "w" ) )
187 v.rect.SetWidth( viewport.at( "w" ).get<double>() );
188
189 if( viewport.contains( "h" ) )
190 v.rect.SetHeight( viewport.at( "h" ).get<double>() );
191
192 m_viewports->emplace_back( v );
193 }
194 }
195}
196
197
198PARAM_VIEWPORT3D::PARAM_VIEWPORT3D( const std::string& aPath,
199 std::vector<VIEWPORT3D>* aViewportList ) :
200 PARAM_LAMBDA<nlohmann::json>( aPath,
201 std::bind( &PARAM_VIEWPORT3D::viewportsToJson, this ),
202 std::bind( &PARAM_VIEWPORT3D::jsonToViewports, this, _1 ),
203 {} ),
204 m_viewports( aViewportList )
205{
206 wxASSERT( aViewportList );
207}
208
209
211{
212 nlohmann::json ret = nlohmann::json::array();
213
214 for( const VIEWPORT3D& viewport : *m_viewports )
215 {
216 nlohmann::json js = {
217 { "name", viewport.name },
218 { "xx", viewport.matrix[0].x },
219 { "xy", viewport.matrix[0].y },
220 { "xz", viewport.matrix[0].z },
221 { "xw", viewport.matrix[0].w },
222 { "yx", viewport.matrix[1].x },
223 { "yy", viewport.matrix[1].y },
224 { "yz", viewport.matrix[1].z },
225 { "yw", viewport.matrix[1].w },
226 { "zx", viewport.matrix[2].x },
227 { "zy", viewport.matrix[2].y },
228 { "zz", viewport.matrix[2].z },
229 { "zw", viewport.matrix[2].w },
230 { "wx", viewport.matrix[3].x },
231 { "wy", viewport.matrix[3].y },
232 { "wz", viewport.matrix[3].z },
233 { "ww", viewport.matrix[3].w }
234 };
235
236 ret.push_back( js );
237 }
238
239 return ret;
240}
241
242
243void PARAM_VIEWPORT3D::jsonToViewports( const nlohmann::json& aJson )
244{
245 if( aJson.empty() || !aJson.is_array() )
246 return;
247
248 m_viewports->clear();
249
250 for( const nlohmann::json& viewport : aJson )
251 {
252 if( viewport.contains( "name" ) )
253 {
254 VIEWPORT3D v( viewport.at( "name" ).get<wxString>() );
255
256 if( viewport.contains( "xx" ) )
257 v.matrix[0].x = viewport.at( "xx" ).get<double>();
258
259 if( viewport.contains( "xy" ) )
260 v.matrix[0].y = viewport.at( "xy" ).get<double>();
261
262 if( viewport.contains( "xz" ) )
263 v.matrix[0].z = viewport.at( "xz" ).get<double>();
264
265 if( viewport.contains( "xw" ) )
266 v.matrix[0].w = viewport.at( "xw" ).get<double>();
267
268 if( viewport.contains( "yx" ) )
269 v.matrix[1].x = viewport.at( "yx" ).get<double>();
270
271 if( viewport.contains( "yy" ) )
272 v.matrix[1].y = viewport.at( "yy" ).get<double>();
273
274 if( viewport.contains( "yz" ) )
275 v.matrix[1].z = viewport.at( "yz" ).get<double>();
276
277 if( viewport.contains( "yw" ) )
278 v.matrix[1].w = viewport.at( "yw" ).get<double>();
279
280 if( viewport.contains( "zx" ) )
281 v.matrix[2].x = viewport.at( "zx" ).get<double>();
282
283 if( viewport.contains( "zy" ) )
284 v.matrix[2].y = viewport.at( "zy" ).get<double>();
285
286 if( viewport.contains( "zz" ) )
287 v.matrix[2].z = viewport.at( "zz" ).get<double>();
288
289 if( viewport.contains( "zw" ) )
290 v.matrix[2].w = viewport.at( "zw" ).get<double>();
291
292 if( viewport.contains( "wx" ) )
293 v.matrix[3].x = viewport.at( "wx" ).get<double>();
294
295 if( viewport.contains( "wy" ) )
296 v.matrix[3].y = viewport.at( "wy" ).get<double>();
297
298 if( viewport.contains( "wz" ) )
299 v.matrix[3].z = viewport.at( "wz" ).get<double>();
300
301 if( viewport.contains( "ww" ) )
302 v.matrix[3].w = viewport.at( "ww" ).get<double>();
303
304 m_viewports->emplace_back( v );
305 }
306 }
307}
BASE_SET & set(size_t pos=std::numeric_limits< size_t >::max(), bool value=true)
Definition: base_set.h:61
BASE_SET & reset(size_t pos=std::numeric_limits< size_t >::max())
Definition: base_set.h:76
void SetHeight(size_type val)
Definition: box2.h:275
void SetX(coord_type val)
Definition: box2.h:260
void SetWidth(size_type val)
Definition: box2.h:270
void SetY(coord_type val)
Definition: box2.h:265
GAL_SET & set()
Definition: layer_ids.h:324
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:293
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
GAL_LAYER_ID
GAL layers are "virtual" layers, i.e.
Definition: layer_ids.h:194
@ GAL_LAYER_ID_START
Definition: layer_ids.h:195
@ GAL_LAYER_ID_END
Definition: layer_ids.h:269
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:137
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.