37#include <wx/txtstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/zipstrm.h>
51 std::map<wxString, EASYEDAPRO::BLOB>
m_Blobs;
52 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>>
m_Poured;
70 if( aFileName.Lower().EndsWith( wxS(
".epro" ) ) )
74 else if( aFileName.Lower().EndsWith( wxS(
".zip" ) ) )
76 std::shared_ptr<wxZipEntry> entry;
77 wxFFileInputStream in( aFileName );
78 wxZipInputStream
zip( in );
83 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
85 wxString
name = entry->GetName();
87 if(
name == wxS(
"project.json" ) )
98std::vector<std::pair<wxString, wxString>>
102 const nlohmann::json& pcbsIndex = projectIndex.at(
"pcbs" );
104 std::vector<std::pair<wxString, wxString>>
result;
105 result.reserve( pcbsIndex.size() );
107 for(
const auto& [uuid, entry] : pcbsIndex.items() )
109 wxString title = wxString::FromUTF8( uuid );
111 if( entry.is_string() && !entry.get<std::string>().empty() )
112 title = wxString::FromUTF8( entry.get_ref<
const std::string&>() );
113 else if( entry.is_object() )
115 auto titleIt = entry.find(
"title" );
117 if( titleIt != entry.end() && titleIt->is_string() )
118 title = wxString::FromUTF8( titleIt->get_ref<
const std::string&>() );
121 result.emplace_back( wxString::FromUTF8( uuid ), title );
135 const std::map<std::string, UTF8>* aProperties,
PROJECT* aProject )
155 wxFileName fname( aFileName );
158 if( fname.GetExt() == wxS(
"epro" ) || fname.GetExt() == wxS(
"zip" ) )
166 pcbToLoad = wxString::FromUTF8(
m_props->at(
"pcb_id" ) );
171 std::map<wxString, nlohmann::json> prjPcbData =
project.at(
"pcbs" );
173 if( prjPcbData.size() == 1 )
175 pcbToLoad = prjPcbData.begin()->first;
183 if( chosen.size() == 0 )
186 pcbToLoad = chosen[0].PCBId;
188 else if( !prjPcbData.empty() )
191 pcbToLoad = prjPcbData.begin()->first;
195 if( pcbToLoad.empty() )
203 auto cb = [&](
const wxString&
name,
const wxString& pcbUuid, wxInputStream&
zip ) ->
bool
205 if( !
name.EndsWith( wxS(
".epcb" ) ) )
208 if( pcbUuid != pcbToLoad )
211 std::vector<nlohmann::json>* pcbLines =
nullptr;
213 std::vector<std::vector<nlohmann::json>> lineBlocks =
216 if( lineBlocks.empty() )
219 if( lineBlocks.size() > 1 )
221 for( std::vector<nlohmann::json>& block : lineBlocks )
224 nlohmann::json headData;
226 for(
const nlohmann::json& line : block )
228 if( line.size() < 2 )
231 if( !line.at( 0 ).is_string() )
234 wxString lineType = line.at( 0 ).get<wxString>();
236 if( lineType == wxS(
"DOCTYPE" ) )
238 if( !line.at( 1 ).is_string() )
241 docType = line.at( 1 ).get<wxString>();
243 else if( lineType == wxS(
"HEAD" ) )
245 if( !line.at( 1 ).is_object() )
248 headData = line.at( 1 );
252 if( docType == wxS(
"FOOTPRINT" ) )
254 wxString fpUuid = headData.at(
"uuid" );
255 wxString fpTitle = headData.at(
"title" );
263 footprint->SetFPID( fpID );
265 m_projectData->m_Footprints.emplace( fpUuid, std::move( footprint ) );
267 else if( docType == wxS(
"PCB" ) )
274 if( pcbLines ==
nullptr )
275 pcbLines = &lineBlocks[0];
277 wxString boardKey = pcbUuid + wxS(
"_0" );
278 wxScopedCharBuffer boardKeyBuffer = boardKey.ToUTF8();
279 wxString boardPouredKey = wxBase64Encode( boardKeyBuffer.data(), boardKeyBuffer.length() );
281 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
304 const wxString& aLibraryPath,
bool aBestEfforts,
305 const std::map<std::string, UTF8>* aProperties )
307 wxFileName fname( aLibraryPath );
309 if( fname.GetExt() == wxS(
"efoo" ) )
311 wxFFileInputStream ffis( aLibraryPath );
312 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
314 while( ffis.CanRead() )
316 wxString line = txt.ReadLine();
318 if( !line.Contains( wxS(
"ATTR" ) ) )
321 nlohmann::json js = nlohmann::json::parse( line );
322 if( js.at( 0 ) ==
"ATTR" && js.at( 7 ) ==
"Footprint" )
324 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
328 else if( fname.GetExt() == wxS(
"elibz" ) || fname.GetExt() == wxS(
"epro" )
329 || fname.GetExt() == wxS(
"zip" ) )
332 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
334 for(
auto& [key, value] : footprintMap )
338 if( value.contains(
"display_title" ) )
339 title = value.at(
"display_title" ).get<wxString>();
341 title = value.at(
"title" ).get<wxString>();
343 aFootprintNames.Add( title );
350 const nlohmann::json& aProject )
358 wxFileName fname( aProjectPath );
361 std::map<wxString, std::unique_ptr<FOOTPRINT>>
result;
363 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
365 if( !
name.EndsWith( wxS(
".efoo" ) ) && !
name.EndsWith( wxS(
".eblob" ) )
366 && !
name.EndsWith( wxS(
".ecop" ) ) )
373 if(
name.EndsWith( wxS(
".efoo" ) ) )
375 nlohmann::json fpData = aProject.at(
"footprints" ).at( baseName );
376 wxString fpTitle = fpData.at(
"title" );
378 std::unique_ptr<FOOTPRINT> footprint = parser.
ParseFootprint( aProject, baseName, lines );
384 footprint->SetFPID( fpID );
386 m_projectData->m_Footprints.emplace( baseName, std::move( footprint ) );
388 else if(
name.EndsWith( wxS(
".eblob" ) ) )
390 for(
const nlohmann::json& line : lines )
392 if( line.at( 0 ) ==
"BLOB" )
401 for(
const nlohmann::json& line : lines )
403 if( line.at( 0 ) ==
"POURED" )
405 if( !line.at( 2 ).is_string() )
420 const wxString& aFootprintName,
bool aKeepUUID,
421 const std::map<std::string, UTF8>* aProperties )
427 std::unique_ptr<FOOTPRINT> footprint;
429 wxFileName libFname( aLibraryPath );
431 if( libFname.GetExt() == wxS(
"efoo" ) )
433 wxFFileInputStream ffis( aLibraryPath );
434 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
438 for(
const nlohmann::json& js : lines )
440 if( js.at( 0 ) ==
"ATTR" )
444 if( attr.
key == wxS(
"Footprint" ) && attr.
value != aFootprintName )
449 footprint = parser.
ParseFootprint( nlohmann::json(), wxEmptyString, lines );
452 THROW_IO_ERRORF(
_(
"Cannot load footprint '%s' from '%s'" ), aFootprintName, aLibraryPath );
455 footprint->SetFPID( fpID );
457 footprint->Reference().SetVisible(
true );
458 footprint->Value().SetText( aFootprintName );
459 footprint->Value().SetVisible(
true );
460 footprint->AutoPositionFields();
462 else if( libFname.GetExt() == wxS(
"elibz" ) || libFname.GetExt() == wxS(
"epro" )
463 || libFname.GetExt() == wxS(
"zip" ) )
469 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
470 for(
auto& [uuid, data] : footprintMap )
474 if( data.contains(
"display_title" ) )
475 title = data.at(
"display_title" ).get<wxString>();
477 title = data.at(
"title" ).get<wxString>();
479 if( title == aFootprintName )
487 THROW_IO_ERRORF(
_(
"Footprint '%s' not found in project '%s'" ), aFootprintName, aLibraryPath );
489 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
491 if( !
name.EndsWith( wxS(
".efoo" ) ) )
494 if( baseName != fpUuid )
502 THROW_IO_ERRORF(
_(
"Cannot load footprint '%s' from '%s'" ), aFootprintName, aLibraryPath );
505 footprint->SetFPID( fpID );
507 footprint->Reference().SetVisible(
true );
508 footprint->Value().SetText( aFootprintName );
509 footprint->Value().SetVisible(
true );
510 footprint->AutoPositionFields();
523 std::vector<FOOTPRINT*>
result;
528 for(
auto& [fpUuid, footprint] :
m_projectData->m_Footprints )
Information pertinent to a Pcbnew printed circuit board.
RAII class to set and restore the fontconfig reporter.
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.
static LOAD_INFO_REPORTER & GetInstance()
std::unique_ptr< FOOTPRINT > ParseFootprint(const nlohmann::json &aProject, const wxString &aFpUuid, const std::vector< nlohmann::json > &aLines)
wxString GetSafeSpacingRules() const
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)
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.
bool CanReadBoard(const wxString &aFileName) const override
wxString m_importedDesignRules
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
std::unique_ptr< 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 loadBoard(const wxString &aFileName, BOARD &aBoard, bool aIsNewLoad, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Parse aFileName into aBoard.
std::vector< std::pair< wxString, wxString > > EnumerateProjectBoards(const wxString &aFileName) const override
Check if this PCB_IO can read the specified board file.
std::vector< IMPORT_PROJECT_DESC > EnumerateProjectEntries(const wxString &aFileName) const override
Return the KiCad projects a project container splits into, one per board entry and one per PCB or sch...
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.
BOARD * m_board
The board BOARD being worked on, no ownership here.
PCB_IO(const wxString &aName)
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
#define THROW_IO_CANCELLED()
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_ECOP
std::vector< std::vector< nlohmann::json > > ParseJsonLinesWithSeparation(wxInputStream &aInput, const wxString &aSource)
Multiple document types (e.g.
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
List the project's board entries, then the PCBs and schematics no entry names.
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
wxString result
Test unit parsing edge cases and error handling.