32#include <nlohmann/json.hpp>
37#include <wx/zipstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/txtstrm.h>
45 wxString shortenedName = aProjectName;
46 shortenedName.Replace( wxS(
"ProProject_" ), wxS(
"" ) );
47 shortenedName.Replace( wxS(
"ProDocument_" ), wxS(
"" ) );
48 shortenedName = shortenedName.substr( 0, 10 );
59 wxString key = !aLibName.empty() ? ( aLibName +
':' + libReference ) : libReference;
62 libId.
Parse( key,
true );
68std::vector<IMPORT_PROJECT_DESC>
71 std::vector<IMPORT_PROJECT_DESC> result;
73 std::map<wxString, EASYEDAPRO::PRJ_SCHEMATIC> prjSchematics = aProject.at(
"schematics" );
74 std::map<wxString, EASYEDAPRO::PRJ_BOARD> prjBoards = aProject.at(
"boards" );
75 std::map<wxString, wxString> prjPcbNames = aProject.at(
"pcbs" );
77 for(
const auto& [prjName, board] : prjBoards )
81 desc.
PCBId = board.pcb;
84 auto pcbNameIt = prjPcbNames.find( desc.
PCBId );
85 if( pcbNameIt != prjPcbNames.end() )
87 desc.
PCBName = pcbNameIt->second;
90 desc.
PCBName = pcbNameIt->first;
92 prjPcbNames.erase( pcbNameIt );
95 auto schIt = prjSchematics.find( desc.
SchematicId );
96 if( schIt != prjSchematics.end() )
103 prjSchematics.erase( schIt );
106 result.emplace_back( desc );
111 for(
const auto& [pcbId, pcbName] : prjPcbNames )
120 result.emplace_back( desc );
126 for(
const auto& [schId, schData] : prjSchematics )
135 result.emplace_back( desc );
145 std::shared_ptr<wxZipEntry> entry;
146 wxFFileInputStream in( aZipFileName );
147 wxZipInputStream
zip( in );
149 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
151 wxString
name = entry->GetName();
153 if(
name == wxS(
"project.json" ) )
155 wxMemoryOutputStream memos;
157 wxStreamBuffer* buf = memos.GetOutputStreamBuffer();
158 wxString str( (
char*) buf->GetBufferStart(), buf->GetBufferSize() );
160 return nlohmann::json::parse( str );
164 THROW_IO_ERROR( wxString::Format(
_(
"'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro "
165 "project file. Cannot find project.json." ),
171 const wxString& aFileName,
172 std::function<
bool(
const wxString&,
const wxString&, wxInputStream& )> aCallback )
174 std::shared_ptr<wxZipEntry> entry;
175 wxFFileInputStream in( aFileName );
176 wxZipInputStream
zip( in );
180 THROW_IO_ERROR( wxString::Format(
_(
"Cannot read ZIP archive '%s'" ), aFileName ) );
183 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
185 wxString
name = entry->GetName();
186 wxString baseName =
name.AfterLast(
'\\' ).AfterLast(
'/' ).BeforeFirst(
'.' );
190 if( aCallback(
name, baseName,
zip ) )
193 catch( nlohmann::json::exception& e )
196 wxString::Format(
_(
"JSON error reading '%s': %s" ),
name, e.what() ) );
198 catch( std::exception& e )
207 const wxString& aSource )
209 wxTextInputStream txt( aInput, wxS(
" " ), wxConvUTF8 );
213 std::vector<nlohmann::json> lines;
214 while( aInput.CanRead() )
218 nlohmann::json js = nlohmann::json::parse( txt.ReadLine() );
219 lines.emplace_back( js );
221 catch( nlohmann::json::exception& e )
223 wxLogWarning( wxString::Format(
_(
"Cannot parse JSON line %d in '%s': %s" ),
224 currentLine, aSource, e.what() ) );
A logical library item identifier and consists of various portions much like a URI.
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
#define THROW_IO_ERROR(msg)
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
nlohmann::json ReadProjectFile(const wxString &aZipFileName)
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< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
wxString ShortenLibName(wxString aProjectName)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
Describes how non-KiCad boards and schematics should be imported as KiCad projects.