From 1613816e77af0352f9ffd5d2d9495950862b38e5 Mon Sep 17 00:00:00 2001 From: Alex Shvartzkop Date: Fri, 21 Jun 2024 22:20:54 +0300 Subject: [PATCH] Fix EasyEDA/JLCEDA Pro 2.2 footprint/symbol library import. Fixes https://gitlab.com/kicad/code/kicad/-/issues/18250 --- .../io/easyedapro/easyedapro_import_utils.cpp | 19 +++++++++++++++++-- .../io/easyedapro/easyedapro_import_utils.h | 3 +++ .../sch_io/easyedapro/sch_io_easyedapro.cpp | 10 ++++++++-- .../easyedapro/pcb_io_easyedapro_parser.cpp | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/common/io/easyedapro/easyedapro_import_utils.cpp b/common/io/easyedapro/easyedapro_import_utils.cpp index 14f211af63..ca82e319fb 100644 --- a/common/io/easyedapro/easyedapro_import_utils.cpp +++ b/common/io/easyedapro/easyedapro_import_utils.cpp @@ -140,7 +140,8 @@ EASYEDAPRO::ProjectToSelectorDialog( const nlohmann::json& aProject, bool aPcbOn } -nlohmann::json EASYEDAPRO::ReadProjectOrDeviceFile( const wxString& aZipFileName ) +nlohmann::json EASYEDAPRO::FindJsonFile( const wxString& aZipFileName, + const std::set& aFileNames ) { std::shared_ptr entry; wxFFileInputStream in( aZipFileName ); @@ -152,7 +153,7 @@ nlohmann::json EASYEDAPRO::ReadProjectOrDeviceFile( const wxString& aZipFileName try { - if( name == wxS( "project.json" ) || name == wxS( "device.json" ) ) + if( aFileNames.find( name ) != aFileNames.end() ) { wxMemoryOutputStream memos; memos << zip; @@ -175,6 +176,20 @@ nlohmann::json EASYEDAPRO::ReadProjectOrDeviceFile( const wxString& aZipFileName } } + return nlohmann::json{}; +} + + +nlohmann::json EASYEDAPRO::ReadProjectOrDeviceFile( const wxString& aZipFileName ) +{ + static const std::set c_files = { wxS( "project.json" ), wxS( "device.json" ), + wxS( "footprint.json" ), wxS( "symbol.json" ) }; + + nlohmann::json j = FindJsonFile( aZipFileName, c_files ); + + if( !j.is_null() ) + return j; + THROW_IO_ERROR( wxString::Format( _( "'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro " "project or library file. Cannot find project.json or device.json." ), diff --git a/common/io/easyedapro/easyedapro_import_utils.h b/common/io/easyedapro/easyedapro_import_utils.h index d4b6477a8c..6117d01ff0 100644 --- a/common/io/easyedapro/easyedapro_import_utils.h +++ b/common/io/easyedapro/easyedapro_import_utils.h @@ -25,6 +25,7 @@ #ifndef EASYEDAPRO_IMPORT_UTILS_H_ #define EASYEDAPRO_IMPORT_UTILS_H_ +#include #include #include #include @@ -46,6 +47,8 @@ std::vector ProjectToSelectorDialog( const nlohmann::json& bool aPcbOnly = false, bool aSchOnly = false ); +nlohmann::json FindJsonFile( const wxString& aZipFileName, const std::set& aFileNames ); + nlohmann::json ReadProjectOrDeviceFile( const wxString& aZipFileName ); void IterateZipFiles( diff --git a/eeschema/sch_io/easyedapro/sch_io_easyedapro.cpp b/eeschema/sch_io/easyedapro/sch_io_easyedapro.cpp index 3f0e446c09..aa1f6485fd 100644 --- a/eeschema/sch_io/easyedapro/sch_io_easyedapro.cpp +++ b/eeschema/sch_io/easyedapro/sch_io_easyedapro.cpp @@ -143,8 +143,14 @@ static LIB_SYMBOL* loadSymbol( nlohmann::json project, const wxString& aLibraryP || libFname.GetExt() == wxS( "zip" ) ) { std::map prjSymbols = project.at( "symbols" ); - std::map prjFootprints = project.at( "footprints" ); - std::map prjDevices = project.at( "devices" ); + std::map prjFootprints; + std::map prjDevices; + + if( project.contains( "footprints" ) ) + prjFootprints = project.at( "footprints" ); + + if( project.contains( "devices" ) ) + prjDevices = project.at( "devices" ); auto prjSymIt = std::find_if( prjSymbols.begin(), prjSymbols.end(), [&]( const auto& pair ) diff --git a/pcbnew/pcb_io/easyedapro/pcb_io_easyedapro_parser.cpp b/pcbnew/pcb_io/easyedapro/pcb_io_easyedapro_parser.cpp index 7dd822552f..56e9432404 100644 --- a/pcbnew/pcb_io/easyedapro/pcb_io_easyedapro_parser.cpp +++ b/pcbnew/pcb_io/easyedapro/pcb_io_easyedapro_parser.cpp @@ -897,7 +897,7 @@ FOOTPRINT* PCB_IO_EASYEDAPRO_PARSER::ParseFootprint( const nlohmann::json& } } - if( aProject.is_object() ) + if( aProject.is_object() && aProject.contains( "devices" ) ) { std::map devicesMap = aProject.at( "devices" ); std::map compAttrs;