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>
23
24using namespace std::placeholders;
25
26
28 std::vector<LAYER_PRESET>* aPresetList ) :
29 PARAM_LAMBDA<nlohmann::json>( aPath,
30 std::bind( &PARAM_LAYER_PRESET::presetsToJson, this ),
31 std::bind( &PARAM_LAYER_PRESET::jsonToPresets, this, _1 ),
32 {} ),
33 m_presets( aPresetList )
34{
35 wxASSERT( aPresetList );
36}
37
38
40{
41 nlohmann::json ret = nlohmann::json::array();
42
43 for( const LAYER_PRESET& preset : *m_presets )
44 {
45 nlohmann::json js = {
46 { "name", preset.name },
47 { "activeLayer", preset.activeLayer }
48 };
49
50 nlohmann::json layers = nlohmann::json::array();
51
52 for( PCB_LAYER_ID layer : preset.layers.Seq() )
53 layers.push_back( static_cast<int>( layer ) );
54
55 js["layers"] = layers;
56
57 nlohmann::json renderLayers = nlohmann::json::array();
58
59 for( GAL_LAYER_ID layer : preset.renderLayers.Seq() )
60 renderLayers.push_back( static_cast<int>( layer ) );
61
62 js["renderLayers"] = renderLayers;
63
64 ret.push_back( js );
65 }
66
67 return ret;
68}
69
70
71void PARAM_LAYER_PRESET::jsonToPresets( const nlohmann::json& aJson )
72{
73 if( aJson.empty() || !aJson.is_array() )
74 return;
75
76 m_presets->clear();
77
78 for( const nlohmann::json& preset : aJson )
79 {
80 if( preset.contains( "name" ) )
81 {
82 LAYER_PRESET p( preset.at( "name" ).get<wxString>() );
83
84 if( preset.contains( "activeLayer" ) &&
85 preset.at( "activeLayer" ).is_number_integer() )
86 {
87 int active = preset.at( "activeLayer" ).get<int>();
88
89 if( active >= 0 && active < PCB_LAYER_ID_COUNT )
90 p.activeLayer = static_cast<PCB_LAYER_ID>( active );
91 }
92
93 if( preset.contains( "layers" ) && preset.at( "layers" ).is_array() )
94 {
95 p.layers.reset();
96
97 for( const nlohmann::json& layer : preset.at( "layers" ) )
98 {
99 if( layer.is_number_integer() )
100 {
101 int layerNum = layer.get<int>();
102
103 if( layerNum >= 0 && layerNum < PCB_LAYER_ID_COUNT )
104 p.layers.set( layerNum );
105 }
106 }
107 }
108
109 if( preset.contains( "renderLayers" )
110 && preset.at( "renderLayers" ).is_array() )
111 {
112 p.renderLayers.reset();
113
114 for( const nlohmann::json& layer : preset.at( "renderLayers" ) )
115 {
116 if( layer.is_number_integer() )
117 {
118 int layerNum = layer.get<int>();
119
120 if( layerNum >= GAL_LAYER_ID_START
121 && layerNum < GAL_LAYER_ID_END )
122 p.renderLayers.set( static_cast<GAL_LAYER_ID>( layerNum ) );
123 }
124 }
125 }
126
127 m_presets->emplace_back( p );
128 }
129 }
130}
131
132
133PARAM_VIEWPORT::PARAM_VIEWPORT( const std::string& aPath, std::vector<VIEWPORT>* aViewportList ) :
134 PARAM_LAMBDA<nlohmann::json>( aPath,
135 std::bind( &PARAM_VIEWPORT::viewportsToJson, this ),
136 std::bind( &PARAM_VIEWPORT::jsonToViewports, this, _1 ),
137 {} ),
138 m_viewports( aViewportList )
139{
140 wxASSERT( aViewportList );
141}
142
143
145{
146 nlohmann::json ret = nlohmann::json::array();
147
148 for( const VIEWPORT& viewport : *m_viewports )
149 {
150 nlohmann::json js = {
151 { "name", viewport.name },
152 { "x", viewport.rect.GetX() },
153 { "y", viewport.rect.GetY() },
154 { "w", viewport.rect.GetWidth() },
155 { "h", viewport.rect.GetHeight() }
156 };
157
158 ret.push_back( js );
159 }
160
161 return ret;
162}
163
164
165void PARAM_VIEWPORT::jsonToViewports( const nlohmann::json& aJson )
166{
167 if( aJson.empty() || !aJson.is_array() )
168 return;
169
170 m_viewports->clear();
171
172 for( const nlohmann::json& viewport : aJson )
173 {
174 if( viewport.contains( "name" ) )
175 {
176 VIEWPORT v( viewport.at( "name" ).get<wxString>() );
177
178 if( viewport.contains( "x" ) )
179 v.rect.SetX( viewport.at( "x" ).get<double>() );
180
181 if( viewport.contains( "y" ) )
182 v.rect.SetY( viewport.at( "y" ).get<double>() );
183
184 if( viewport.contains( "w" ) )
185 v.rect.SetWidth( viewport.at( "w" ).get<double>() );
186
187 if( viewport.contains( "h" ) )
188 v.rect.SetHeight( viewport.at( "h" ).get<double>() );
189
190 m_viewports->emplace_back( v );
191 }
192 }
193}
194
195
196PARAM_VIEWPORT3D::PARAM_VIEWPORT3D( const std::string& aPath,
197 std::vector<VIEWPORT3D>* aViewportList ) :
198 PARAM_LAMBDA<nlohmann::json>( aPath,
199 std::bind( &PARAM_VIEWPORT3D::viewportsToJson, this ),
200 std::bind( &PARAM_VIEWPORT3D::jsonToViewports, this, _1 ),
201 {} ),
202 m_viewports( aViewportList )
203{
204 wxASSERT( aViewportList );
205}
206
207
209{
210 nlohmann::json ret = nlohmann::json::array();
211
212 for( const VIEWPORT3D& viewport : *m_viewports )
213 {
214 nlohmann::json js = {
215 { "name", viewport.name },
216 { "xx", viewport.matrix[0].x },
217 { "xy", viewport.matrix[0].y },
218 { "xz", viewport.matrix[0].z },
219 { "xw", viewport.matrix[0].w },
220 { "yx", viewport.matrix[1].x },
221 { "yy", viewport.matrix[1].y },
222 { "yz", viewport.matrix[1].z },
223 { "yw", viewport.matrix[1].w },
224 { "zx", viewport.matrix[2].x },
225 { "zy", viewport.matrix[2].y },
226 { "zz", viewport.matrix[2].z },
227 { "zw", viewport.matrix[2].w },
228 { "wx", viewport.matrix[3].x },
229 { "wy", viewport.matrix[3].y },
230 { "wz", viewport.matrix[3].z },
231 { "ww", viewport.matrix[3].w }
232 };
233
234 ret.push_back( js );
235 }
236
237 return ret;
238}
239
240
241void PARAM_VIEWPORT3D::jsonToViewports( const nlohmann::json& aJson )
242{
243 if( aJson.empty() || !aJson.is_array() )
244 return;
245
246 m_viewports->clear();
247
248 for( const nlohmann::json& viewport : aJson )
249 {
250 if( viewport.contains( "name" ) )
251 {
252 VIEWPORT3D v( viewport.at( "name" ).get<wxString>() );
253
254 if( viewport.contains( "xx" ) )
255 v.matrix[0].x = viewport.at( "xx" ).get<double>();
256
257 if( viewport.contains( "xy" ) )
258 v.matrix[0].y = viewport.at( "xy" ).get<double>();
259
260 if( viewport.contains( "xz" ) )
261 v.matrix[0].z = viewport.at( "xz" ).get<double>();
262
263 if( viewport.contains( "xw" ) )
264 v.matrix[0].w = viewport.at( "xw" ).get<double>();
265
266 if( viewport.contains( "yx" ) )
267 v.matrix[1].x = viewport.at( "yx" ).get<double>();
268
269 if( viewport.contains( "yy" ) )
270 v.matrix[1].y = viewport.at( "yy" ).get<double>();
271
272 if( viewport.contains( "yz" ) )
273 v.matrix[1].z = viewport.at( "yz" ).get<double>();
274
275 if( viewport.contains( "yw" ) )
276 v.matrix[1].w = viewport.at( "yw" ).get<double>();
277
278 if( viewport.contains( "zx" ) )
279 v.matrix[2].x = viewport.at( "zx" ).get<double>();
280
281 if( viewport.contains( "zy" ) )
282 v.matrix[2].y = viewport.at( "zy" ).get<double>();
283
284 if( viewport.contains( "zz" ) )
285 v.matrix[2].z = viewport.at( "zz" ).get<double>();
286
287 if( viewport.contains( "zw" ) )
288 v.matrix[2].w = viewport.at( "zw" ).get<double>();
289
290 if( viewport.contains( "wx" ) )
291 v.matrix[3].x = viewport.at( "wx" ).get<double>();
292
293 if( viewport.contains( "wy" ) )
294 v.matrix[3].y = viewport.at( "wy" ).get<double>();
295
296 if( viewport.contains( "wz" ) )
297 v.matrix[3].z = viewport.at( "wz" ).get<double>();
298
299 if( viewport.contains( "ww" ) )
300 v.matrix[3].w = viewport.at( "ww" ).get<double>();
301
302 m_viewports->emplace_back( v );
303 }
304 }
305}
void SetX(coord_type val)
Definition: box2.h:236
void SetY(coord_type val)
Definition: box2.h:241
void SetWidth(coord_type val)
Definition: box2.h:246
void SetHeight(coord_type val)
Definition: box2.h:251
GAL_SET & set()
Definition: layer_ids.h:323
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:268
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.