40#include <wx/txtstrm.h>
41#include <wx/wfstream.h>
42#include <wx/mstream.h>
43#include <wx/zipstrm.h>
54 std::map<wxString, EASYEDAPRO::BLOB>
m_Blobs;
55 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" ) )
102 const std::map<std::string, UTF8>* aProperties,
PROJECT* aProject )
124 wxFileName fname( aFileName );
126 if( fname.GetExt() == wxS(
"epro" ) || fname.GetExt() == wxS(
"zip" ) )
134 pcbToLoad = wxString::FromUTF8(
m_props->at(
"pcb_id" ) );
138 std::map<wxString, wxString> prjPcbNames =
project.at(
"pcbs" );
140 if( prjPcbNames.size() == 1 )
142 pcbToLoad = prjPcbNames.begin()->first;
149 if( chosen.size() > 0 )
150 pcbToLoad = chosen[0].PCBId;
154 if( pcbToLoad.empty() )
162 auto cb = [&](
const wxString&
name,
const wxString& pcbUuid, wxInputStream&
zip ) ->
bool
164 if( !
name.EndsWith( wxS(
".epcb" ) ) )
167 if( pcbUuid != pcbToLoad )
172 wxString boardKey = pcbUuid + wxS(
"_0" );
173 wxScopedCharBuffer cb = boardKey.ToUTF8();
174 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
176 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
199 const wxString& aLibraryPath,
bool aBestEfforts,
200 const std::map<std::string, UTF8>* aProperties )
202 wxFileName fname( aLibraryPath );
204 if( fname.GetExt() == wxS(
"efoo" ) )
206 wxFFileInputStream ffis( aLibraryPath );
207 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
209 while( ffis.CanRead() )
211 wxString line = txt.ReadLine();
213 if( !line.Contains( wxS(
"ATTR" ) ) )
216 nlohmann::json js = nlohmann::json::parse( line );
217 if( js.at( 0 ) ==
"ATTR" && js.at( 7 ) ==
"Footprint" )
219 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
223 else if( fname.GetExt() == wxS(
"elibz" ) || fname.GetExt() == wxS(
"epro" )
224 || fname.GetExt() == wxS(
"zip" ) )
227 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
229 for(
auto& [key, value] : footprintMap )
233 if( value.contains(
"display_title" ) )
234 title = value.at(
"display_title" ).get<wxString>();
236 title = value.at(
"title" ).get<wxString>();
238 aFootprintNames.Add( title );
245 const nlohmann::json& aProject )
253 wxFileName fname( aProjectPath );
256 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
258 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
260 if( !
name.EndsWith( wxS(
".efoo" ) ) && !
name.EndsWith( wxS(
".eblob" ) )
261 && !
name.EndsWith( wxS(
".ecop" ) ) )
268 if(
name.EndsWith( wxS(
".efoo" ) ) )
270 nlohmann::json fpData = aProject.at(
"footprints" ).at( baseName );
271 wxString fpTitle = fpData.at(
"title" );
283 else if(
name.EndsWith( wxS(
".eblob" ) ) )
285 for(
const nlohmann::json& line : lines )
287 if( line.at( 0 ) ==
"BLOB" )
296 for(
const nlohmann::json& line : lines )
298 if( line.at( 0 ) ==
"POURED" )
300 if( !line.at( 2 ).is_string() )
315 const wxString& aFootprintName,
bool aKeepUUID,
316 const std::map<std::string, UTF8>* aProperties )
323 wxFileName libFname( aLibraryPath );
325 if( libFname.GetExt() == wxS(
"efoo" ) )
327 wxFFileInputStream ffis( aLibraryPath );
328 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
332 for(
const nlohmann::json& js : lines )
334 if( js.at( 0 ) ==
"ATTR" )
338 if( attr.
key == wxS(
"Footprint" ) && attr.
value != aFootprintName )
343 footprint = parser.
ParseFootprint( nlohmann::json(), wxEmptyString, lines );
347 THROW_IO_ERROR( wxString::Format(
_(
"Cannot load footprint '%s' from '%s'" ),
348 aFootprintName, aLibraryPath ) );
359 else if( libFname.GetExt() == wxS(
"elibz" ) || libFname.GetExt() == wxS(
"epro" )
360 || libFname.GetExt() == wxS(
"zip" ) )
366 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
367 for(
auto& [uuid, data] : footprintMap )
371 if( data.contains(
"display_title" ) )
372 title = data.at(
"display_title" ).get<wxString>();
374 title = data.at(
"title" ).get<wxString>();
376 if( title == aFootprintName )
385 THROW_IO_ERROR( wxString::Format(
_(
"Footprint '%s' not found in project '%s'" ),
386 aFootprintName, aLibraryPath ) );
389 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
391 if( !
name.EndsWith( wxS(
".efoo" ) ) )
394 if( baseName != fpUuid )
403 THROW_IO_ERROR( wxString::Format(
_(
"Cannot load footprint '%s' from '%s'" ),
404 aFootprintName, aLibraryPath ) );
426 std::vector<FOOTPRINT*> result;
433 result.push_back(
static_cast<FOOTPRINT*
>( footprint->Clone() ) );
Information pertinent to a Pcbnew printed circuit board.
void SetFileName(const wxString &aFileName)
virtual void SetVisible(bool aVisible)
virtual void SetText(const wxString &aText)
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
A logical library item identifier and consists of various portions much like a URI.
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)
FOOTPRINT * ParseFootprint(const nlohmann::json &aProject, const wxString &aFpUuid, const std::vector< nlohmann::json > &aLines)
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.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
A base class that BOARD loading and saving plugins should derive from.
BOARD * m_board
The board BOARD being worked on, no ownership here.
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
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.
static REPORTER & GetInstance()
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
#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)
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)
nlohmann::json ReadProjectOrDeviceFile(const wxString &aZipFileName)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, std::multimap< wxString, EASYEDAPRO::POURED > > m_Poured
std::map< wxString, EASYEDAPRO::BLOB > m_Blobs
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_Footprints