KiCad PCB EDA Suite
Loading...
Searching...
No Matches
easyedapro_import_utils.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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26#include "easyedapro_parser.h"
27
29
30#include <ki_exception.h>
31#include <string_utils.h>
32#include <json_common.h>
34
35#include <wx/log.h>
36#include <wx/stream.h>
37#include <wx/zipstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/txtstrm.h>
41
42
43wxString EASYEDAPRO::ShortenLibName( wxString aProjectName )
44{
45 wxString shortenedName = aProjectName;
46 shortenedName.Replace( wxS( "ProProject_" ), wxS( "" ) );
47 shortenedName.Replace( wxS( "ProDocument_" ), wxS( "" ) );
48 shortenedName = shortenedName.substr( 0, 10 );
49
50 return LIB_ID::FixIllegalChars( shortenedName + wxS( "-easyedapro" ), true );
51}
52
53
54LIB_ID EASYEDAPRO::ToKiCadLibID( const wxString& aLibName, const wxString& aLibReference )
55{
56 wxString libName = LIB_ID::FixIllegalChars( aLibName, true );
57 wxString libReference = EscapeString( aLibReference, CTX_LIBID );
58
59 wxString key = !aLibName.empty() ? ( aLibName + ':' + libReference ) : libReference;
60
61 LIB_ID libId;
62 libId.Parse( key, true );
63
64 return libId;
65}
66
67
68std::vector<IMPORT_PROJECT_DESC>
69EASYEDAPRO::ProjectToSelectorDialog( const nlohmann::json& aProject, bool aPcbOnly, bool aSchOnly )
70{
71 std::vector<IMPORT_PROJECT_DESC> result;
72
73 std::map<wxString, EASYEDAPRO::PRJ_SCHEMATIC> prjSchematics = aProject.at( "schematics" );
74 std::map<wxString, EASYEDAPRO::PRJ_BOARD> prjBoards = aProject.at( "boards" );
75
76 std::map<wxString, wxString> prjPcbNames;
77 std::map<wxString, nlohmann::json> prjPcbs = aProject.at( "pcbs" );
78
79 for( const auto& [pcbUuid, pcbJsonEntry] : prjPcbs )
80 {
81 if( pcbJsonEntry.is_string() )
82 prjPcbNames.emplace( pcbUuid, pcbJsonEntry );
83 else if( pcbJsonEntry.is_object() )
84 prjPcbNames.emplace( pcbUuid, pcbJsonEntry.at( "title" ) );
85 }
86
87 for( const auto& [prjName, board] : prjBoards )
88 {
90 desc.ComboName = desc.ComboId = prjName;
91 desc.PCBId = board.pcb;
92 desc.SchematicId = board.schematic;
93
94 auto pcbNameIt = prjPcbNames.find( desc.PCBId );
95 if( pcbNameIt != prjPcbNames.end() )
96 {
97 desc.PCBName = pcbNameIt->second;
98
99 if( desc.PCBName.empty() )
100 desc.PCBName = pcbNameIt->first;
101
102 prjPcbNames.erase( pcbNameIt );
103 }
104
105 auto schIt = prjSchematics.find( desc.SchematicId );
106 if( schIt != prjSchematics.end() )
107 {
108 desc.SchematicName = schIt->second.name;
109
110 if( desc.SchematicName.empty() )
111 desc.SchematicName = schIt->first;
112
113 prjSchematics.erase( schIt );
114 }
115
116 result.emplace_back( desc );
117 }
118
119 if( !aSchOnly )
120 {
121 for( const auto& [pcbId, pcbName] : prjPcbNames )
122 {
124 desc.PCBId = pcbId;
125 desc.PCBName = pcbName;
126
127 if( desc.PCBName.empty() )
128 desc.PCBName = pcbId;
129
130 result.emplace_back( desc );
131 }
132 }
133
134 if( !aPcbOnly )
135 {
136 for( const auto& [schId, schData] : prjSchematics )
137 {
139 desc.SchematicId = schId;
140 desc.SchematicName = schData.name;
141
142 if( desc.SchematicName.empty() )
143 desc.SchematicName = schId;
144
145 result.emplace_back( desc );
146 }
147 }
148
149 return result;
150}
151
152
153nlohmann::json EASYEDAPRO::FindJsonFile( const wxString& aZipFileName,
154 const std::set<wxString>& aFileNames )
155{
156 std::shared_ptr<wxZipEntry> entry;
157 wxFFileInputStream in( aZipFileName );
158 wxZipInputStream zip( in );
159
160 while( entry.reset( zip.GetNextEntry() ), entry.get() != NULL )
161 {
162 wxString name = entry->GetName();
163
164 try
165 {
166 if( aFileNames.find( name ) != aFileNames.end() )
167 {
168 wxMemoryOutputStream memos;
169 memos << zip;
170 wxStreamBuffer* buf = memos.GetOutputStreamBuffer();
171
172 wxString str =
173 wxString::FromUTF8( (char*) buf->GetBufferStart(), buf->GetBufferSize() );
174
175 return nlohmann::json::parse( str );
176 }
177 }
178 catch( nlohmann::json::exception& e )
179 {
181 wxString::Format( _( "JSON error reading '%s': %s" ), name, e.what() ) );
182 }
183 catch( std::exception& e )
184 {
185 THROW_IO_ERROR( wxString::Format( _( "Error reading '%s': %s" ), name, e.what() ) );
186 }
187 }
188
189 return nlohmann::json{};
190}
191
192
193nlohmann::json EASYEDAPRO::ReadProjectOrDeviceFile( const wxString& aZipFileName )
194{
195 static const std::set<wxString> c_files = { wxS( "project.json" ), wxS( "device.json" ),
196 wxS( "footprint.json" ), wxS( "symbol.json" ) };
197
198 nlohmann::json j = FindJsonFile( aZipFileName, c_files );
199
200 if( !j.is_null() )
201 return j;
202
203 THROW_IO_ERROR( wxString::Format(
204 _( "'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro "
205 "project or library file. Cannot find project.json or device.json." ),
206 aZipFileName ) );
207}
208
209
211 const wxString& aFileName,
212 std::function<bool( const wxString&, const wxString&, wxInputStream& )> aCallback )
213{
214 std::shared_ptr<wxZipEntry> entry;
215 wxFFileInputStream in( aFileName );
216 wxZipInputStream zip( in );
217
218 if( !zip.IsOk() )
219 {
220 THROW_IO_ERROR( wxString::Format( _( "Cannot read ZIP archive '%s'" ), aFileName ) );
221 }
222
223 while( entry.reset( zip.GetNextEntry() ), entry.get() != NULL )
224 {
225 wxString name = entry->GetName();
226 wxString baseName = name.AfterLast( '\\' ).AfterLast( '/' ).BeforeFirst( '.' );
227
228 try
229 {
230 if( aCallback( name, baseName, zip ) )
231 break;
232 }
233 catch( nlohmann::json::exception& e )
234 {
236 wxString::Format( _( "JSON error reading '%s': %s" ), name, e.what() ) );
237 }
238 catch( std::exception& e )
239 {
240 THROW_IO_ERROR( wxString::Format( _( "Error reading '%s': %s" ), name, e.what() ) );
241 }
242 }
243}
244
245
246std::vector<nlohmann::json> EASYEDAPRO::ParseJsonLines( wxInputStream& aInput,
247 const wxString& aSource )
248{
249 wxTextInputStream txt( aInput, wxS( " " ), wxConvUTF8 );
250
251 int currentLine = 1;
252
253 std::vector<nlohmann::json> lines;
254 while( aInput.CanRead() )
255 {
256 try
257 {
258 wxString line = txt.ReadLine();
259
260 if( !line.IsEmpty() )
261 {
262 nlohmann::json js = nlohmann::json::parse( line );
263 lines.emplace_back( js );
264 }
265 else
266 {
267 lines.emplace_back( nlohmann::json() );
268 }
269 }
270 catch( nlohmann::json::exception& e )
271 {
272 wxLogWarning( wxString::Format( _( "Cannot parse JSON line %d in '%s': %s" ),
273 currentLine, aSource, e.what() ) );
274 }
275
276 currentLine++;
277 }
278
279 return lines;
280}
281
282
283std::vector<std::vector<nlohmann::json>>
284EASYEDAPRO::ParseJsonLinesWithSeparation( wxInputStream& aInput, const wxString& aSource )
285{
286 wxTextInputStream txt( aInput, wxS( " " ), wxConvUTF8 );
287
288 int currentLine = 1;
289
290 std::vector<std::vector<nlohmann::json>> lineBlocks;
291 lineBlocks.emplace_back();
292
293 while( aInput.CanRead() )
294 {
295 try
296 {
297 wxString line = txt.ReadLine();
298
299 if( !line.IsEmpty() )
300 {
301 nlohmann::json js = nlohmann::json::parse( line );
302 lineBlocks.back().emplace_back( js );
303 }
304 else
305 {
306 lineBlocks.emplace_back();
307 }
308 }
309 catch( nlohmann::json::exception& e )
310 {
311 wxLogWarning( wxString::Format( _( "Cannot parse JSON line %d in '%s': %s" ),
312 currentLine, aSource, e.what() ) );
313 }
314
315 currentLine++;
316 }
317
318 return lineBlocks;
319}
320
321
322std::map<wxString, wxString>
323EASYEDAPRO::AnyMapToStringMap( const std::map<wxString, nlohmann::json>& aInput )
324{
325 std::map<wxString, wxString> stringMap;
326
327 for( auto& [key, value] : aInput )
328 {
329 if( value.is_string() )
330 stringMap[key] = value.get<wxString>();
331 else if( value.is_number() )
332 stringMap[key] = wxString::FromCDouble( value.get<double>() );
333 }
334
335 return stringMap;
336}
const char * name
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:52
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition lib_id.cpp:192
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
void IterateZipFiles(const wxString &aFileName, std::function< bool(const wxString &, const wxString &, wxInputStream &)> aCallback)
std::vector< nlohmann::json > ParseJsonLines(wxInputStream &aInput, const wxString &aSource)
std::vector< std::vector< nlohmann::json > > ParseJsonLinesWithSeparation(wxInputStream &aInput, const wxString &aSource)
Multiple document types (e.g.
nlohmann::json FindJsonFile(const wxString &aZipFileName, const std::set< wxString > &aFileNames)
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
nlohmann::json ReadProjectOrDeviceFile(const wxString &aZipFileName)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, wxString > AnyMapToStringMap(const std::map< wxString, nlohmann::json > &aInput)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID
Describes how non-KiCad boards and schematics should be imported as KiCad projects.
wxString result
Test unit parsing edge cases and error handling.