KiCad PCB EDA Suite
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 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 
24 using namespace std::placeholders;
25 
26 
27 PARAM_LAYER_PRESET::PARAM_LAYER_PRESET( const std::string& aPath,
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 
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 }
A saved set of layers that are visible.
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:280
GAL_LAYER_ID
GAL layers are "virtual" layers, i.e.
Definition: layer_ids.h:185
Definition: bitmap.cpp:64
std::vector< LAYER_PRESET > * m_presets
nlohmann::json json
Definition: gerbview.cpp:41
GAL_SET renderLayers
Render layers (e.g. object types) that are visible.
PCB_LAYER_ID activeLayer
Optional layer to set active when this preset is loaded.
LSET layers
Board layers that are visible.
void jsonToPresets(const nlohmann::json &aJson)
PARAM_LAYER_PRESET(const std::string &aPath, std::vector< LAYER_PRESET > *aPresetList)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
nlohmann::json presetsToJson()
GAL_SET & set()
Definition: layer_ids.h:295