37#include <wx/txtstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/zipstrm.h>
43#include <nlohmann/json.hpp>
52 std::map<wxString, EASYEDAPRO::BLOB>
m_Blobs;
53 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>>
m_Poured;
73 if( aFileName.Lower().EndsWith( wxS(
".epro" ) ) )
77 else if( aFileName.Lower().EndsWith( wxS(
".zip" ) ) )
79 std::shared_ptr<wxZipEntry> entry;
80 wxFFileInputStream in( aFileName );
81 wxZipInputStream
zip( in );
86 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
88 wxString
name = entry->GetName();
90 if(
name == wxS(
"project.json" ) )
113 if( aProgressReporter )
115 aProgressReporter->
Report( wxString::Format(
_(
"Loading %s..." ), aFileName ) );
123 wxFileName fname( aFileName );
125 if( fname.GetExt() == wxS(
"epro" ) || fname.GetExt() == wxS(
"zip" ) )
133 pcbToLoad = wxString::FromUTF8( aProperties->at(
"pcb_id" ) );
137 std::map<wxString, wxString> prjPcbNames =
project.at(
"pcbs" );
139 if( prjPcbNames.size() == 1 )
141 pcbToLoad = prjPcbNames.begin()->first;
148 if( chosen.size() > 0 )
149 pcbToLoad = chosen[0].PCBId;
153 if( pcbToLoad.empty() )
161 auto cb = [&](
const wxString&
name,
const wxString& pcbUuid, wxInputStream&
zip ) ->
bool
163 if( !
name.EndsWith( wxS(
".epcb" ) ) )
166 if( pcbUuid != pcbToLoad )
171 wxString boardKey = pcbUuid + wxS(
"_0" );
172 wxScopedCharBuffer cb = boardKey.ToUTF8();
173 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
175 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
198 const wxString& aLibraryPath,
bool aBestEfforts,
201 wxFileName fname( aLibraryPath );
203 if( fname.GetExt() == wxS(
"efoo" ) )
205 wxFFileInputStream ffis( aLibraryPath );
206 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
208 while( ffis.CanRead() )
210 wxString line = txt.ReadLine();
212 if( !line.Contains( wxS(
"ATTR" ) ) )
215 nlohmann::json js = nlohmann::json::parse( line );
216 if( js.at( 0 ) ==
"ATTR" && js.at( 7 ) ==
"Footprint" )
218 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
222 else if( fname.GetExt() == wxS(
"epro" ) || fname.GetExt() == wxS(
"zip" ) )
225 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
227 for(
auto& [key, value] : footprintMap )
228 aFootprintNames.Add( value.at(
"title" ) );
234 const nlohmann::json& aProject )
242 wxFileName fname( aProjectPath );
245 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
247 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
249 if( !
name.EndsWith( wxS(
".efoo" ) ) && !
name.EndsWith( wxS(
".eblob" ) )
250 && !
name.EndsWith( wxS(
".ecop" ) ) )
257 if(
name.EndsWith( wxS(
".efoo" ) ) )
259 nlohmann::json fpData = aProject.at(
"footprints" ).at( baseName );
260 wxString fpTitle = fpData.at(
"title" );
272 else if(
name.EndsWith( wxS(
".eblob" ) ) )
274 for(
const nlohmann::json& line : lines )
276 if( line.at( 0 ) ==
"BLOB" )
285 for(
const nlohmann::json& line : lines )
287 if( line.at( 0 ) ==
"POURED" )
289 if( !line.at( 2 ).is_string() )
304 const wxString& aFootprintName,
bool aKeepUUID,
310 wxFileName libFname( aLibraryPath );
312 if( libFname.GetExt() == wxS(
"efoo" ) )
314 wxFFileInputStream ffis( aLibraryPath );
315 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
319 for(
const nlohmann::json& js : lines )
321 if( js.at( 0 ) ==
"ATTR" )
325 if( attr.
key == wxS(
"Footprint" ) && attr.
value != aFootprintName )
330 footprint = parser.
ParseFootprint( nlohmann::json(), wxEmptyString, lines );
334 THROW_IO_ERROR( wxString::Format(
_(
"Cannot load footprint '%s' from '%s'" ),
335 aFootprintName, aLibraryPath ) );
346 else if( libFname.GetExt() == wxS(
"epro" ) || libFname.GetExt() == wxS(
"zip" ) )
352 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
353 for(
auto& [uuid, data] : footprintMap )
355 wxString title = data.at(
"title" );
357 if( title == aFootprintName )
366 THROW_IO_ERROR( wxString::Format(
_(
"Footprint '%s' not found in project '%s'" ),
367 aFootprintName, aLibraryPath ) );
370 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
372 if( !
name.EndsWith( wxS(
".efoo" ) ) )
375 if( baseName != fpUuid )
384 THROW_IO_ERROR( wxString::Format(
_(
"Cannot load footprint '%s' from '%s'" ),
385 aFootprintName, aLibraryPath ) );
407 std::vector<FOOTPRINT*> result;
414 result.push_back(
static_cast<FOOTPRINT*
>( footprint->Clone() ) );
Information pertinent to a Pcbnew printed circuit board.
void SetFileName(const wxString &aFileName)
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
const STRING_UTF8_MAP * m_props
void LoadAllDataFromProject(const wxString &aLibraryPath, const nlohmann::json &aProject)
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PL...
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PLUGIN can read the specified board file.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Load information from some input file format that this PLUGIN implementation knows about into either ...
virtual void SetVisible(bool aVisible)
virtual void SetText(const wxString &aText)
A logical library item identifier and consists of various portions much like a URI.
FOOTPRINT * ParseFootprint(const nlohmann::json &aProject, const wxString &aFpUuid, const std::vector< nlohmann::json > &aLines)
void ParseBoard(BOARD *aBoard, const nlohmann::json &aProject, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const std::multimap< wxString, EASYEDAPRO::POURED > &aPouredMap, const std::vector< nlohmann::json > &aLines, const wxString &aFpLibName)
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
A name/value tuple with unique names and optional values.
bool Exists(const std::string &aProperty) const
#define THROW_IO_ERROR(msg)
This file contains miscellaneous commonly used macros and functions.
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
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)
static const bool IMPORT_POURED
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, std::multimap< wxString, EASYEDAPRO::POURED > > m_Poured
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_Footprints
std::map< wxString, EASYEDAPRO::BLOB > m_Blobs