KiCad PCB EDA Suite
Loading...
Searching...
No Matches
easyeda_parser_structs.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) 2023 Alex Shvartzkop <[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
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <json_common.h>
25
26
27#define PARSE_VALUE( name ) \
28 if( j.find( #name ) != j.end() ) \
29 j.at( #name ).get_to( d.name )
30
31#define PARSE_TO_DOUBLE( name, def ) \
32 if( j.find( #name ) == j.end() ) \
33 { \
34 d.name = def; \
35 } \
36 else if( j.at( #name ).is_string() ) \
37 { \
38 wxString str = j.at( #name ).get<wxString>(); \
39 \
40 double out = 0; \
41 str.ToCDouble( &out ); \
42 d.name = out; \
43 } \
44 else if( j.at( #name ).is_number() ) \
45 { \
46 d.name = j.at( #name ).get<double>(); \
47 }
48
49
50void EASYEDA::from_json( const nlohmann::json& j, EASYEDA::DOC_TYPE& d )
51{
52 if( j.is_string() )
53 {
54 wxString str = j.get<wxString>();
55
56 int out = 0;
57 str.ToInt( &out );
58 d = static_cast<EASYEDA::DOC_TYPE>( out );
59 }
60 else if( j.is_number() )
61 {
62 d = static_cast<EASYEDA::DOC_TYPE>( j.get<int>() );
63 }
64}
65
66
67void EASYEDA::from_json( const nlohmann::json& j, EASYEDA::HEAD& d )
68{
69 PARSE_VALUE( docType );
70
71 PARSE_VALUE( editorVersion );
72 PARSE_VALUE( title );
73 PARSE_VALUE( description );
74
75 if( j.find( "c_para" ) != j.end() && j.at( "c_para" ).is_object() )
76 d.c_para = j.at( "c_para" );
77
78 PARSE_TO_DOUBLE( x, 0 );
79 PARSE_TO_DOUBLE( y, 0 );
80}
81
82
83void EASYEDA::from_json( const nlohmann::json& j, EASYEDA::DOCUMENT& d )
84{
85 PARSE_VALUE( docType );
86 PARSE_VALUE( head );
87
88 PARSE_VALUE( canvas );
89 PARSE_VALUE( title );
90 PARSE_VALUE( shape );
91 PARSE_VALUE( dataStr );
92
93 // Some exports store the sheet document as JSON text instead of as an object
94 if( d.dataStr && d.dataStr->is_string() )
95 {
96 nlohmann::json inner = nlohmann::json::parse( d.dataStr->get<std::string>(), nullptr, false );
97
98 if( inner.is_object() )
99 d.dataStr = std::move( inner );
100 }
101}
102
103
104void EASYEDA::from_json( const nlohmann::json& j, EASYEDA::DOCUMENT_PCB& d )
105{
106 PARSE_VALUE( c_para );
107 d.layers = j.at( "layers" );
108
109 if( j.find( "DRCRULE" ) != j.end() && j.at( "DRCRULE" ).is_object() )
110 d.DRCRULE = j.at( "DRCRULE" );
111}
112
113
114void EASYEDA::from_json( const nlohmann::json& j, EASYEDA::DOCUMENT_SYM& d )
115{
116 PARSE_VALUE( c_para );
117}
118
119
120void EASYEDA::from_json( const nlohmann::json& j, EASYEDA::DOCUMENT_SCHEMATICS& d )
121{
122 PARSE_VALUE( schematics );
123}
124
125
126bool EASYEDA::UnwrapDataStr( nlohmann::json& aJson )
127{
128 if( !aJson.is_object() || aJson.contains( "head" ) )
129 return false;
130
131 auto it = aJson.find( "dataStr" );
132
133 if( it == aJson.end() || !it->is_object() || !it->contains( "head" ) )
134 return false;
135
136 nlohmann::json inner = std::move( *it );
137 aJson = std::move( inner );
138 return true;
139}
140
141
142void EASYEDA::from_json( const nlohmann::json& j, EASYEDA::C_PARA& d )
143{
144 PARSE_VALUE( package );
145 PARSE_VALUE( pre );
146 PARSE_VALUE( Contributor );
147 PARSE_VALUE( link );
148 PARSE_VALUE( Model_3D );
149}
#define PARSE_TO_DOUBLE(name, def)
#define PARSE_VALUE(name)
void from_json(const nlohmann::json &j, EASYEDA::DOC_TYPE &d)
bool UnwrapDataStr(nlohmann::json &aJson)
Replace a document the EasyEDA server wraps in a metadata envelope with the document it carries.
std::optional< std::map< wxString, nlohmann::json > > DRCRULE
std::vector< wxString > layers
std::optional< nlohmann::json > dataStr
std::optional< std::map< wxString, wxString > > c_para