From c07477b94c8ecabcd84a8b90ee96a15b6aa6279c Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Tue, 8 Nov 2022 22:18:29 -0500 Subject: [PATCH] Move to explicit symbol properties mapping Fixes https://gitlab.com/kicad/code/kicad/-/issues/12845 --- common/database/database_lib_settings.cpp | 166 ++++++++++++------ .../database/sch_database_plugin.cpp | 50 ++++-- include/database/database_lib_settings.h | 10 ++ qa/data/dblib/qa_dblib.kicad_dbl | 22 +-- 4 files changed, 165 insertions(+), 83 deletions(-) diff --git a/common/database/database_lib_settings.cpp b/common/database/database_lib_settings.cpp index 3b8ebc2cd2..5e74247f10 100644 --- a/common/database/database_lib_settings.cpp +++ b/common/database/database_lib_settings.cpp @@ -29,7 +29,7 @@ #include -const int dblibSchemaVersion = 0; +const int dblibSchemaVersion = 1; DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) : @@ -48,71 +48,131 @@ DATABASE_LIB_SETTINGS::DATABASE_LIB_SETTINGS( const std::string& aFilename ) : m_params.emplace_back( new PARAM( "source.timeout_seconds", &m_Source.timeout, 2 ) ); m_params.emplace_back( new PARAM_LAMBDA( - "libraries", - [&]() -> nlohmann::json - { - // TODO: implement this; libraries are read-only from KiCad at the moment - return {}; - }, - [&]( const nlohmann::json aObj ) - { - m_Tables.clear(); + "libraries", + [&]() -> nlohmann::json + { + // TODO: implement this; libraries are read-only from KiCad at the moment + return {}; + }, + [&]( const nlohmann::json aObj ) + { + m_Tables.clear(); - if( !aObj.is_array() ) - return; + if( !aObj.is_array() ) + return; - for( const nlohmann::json& entry : aObj ) + for( const nlohmann::json& entry : aObj ) + { + if( entry.empty() || !entry.is_object() ) + continue; + + DATABASE_LIB_TABLE table; + + table.name = entry["name"].get(); + table.table = entry["table"].get(); + table.key_col = entry["key"].get(); + table.symbols_col = entry["symbols"].get(); + table.footprints_col = entry["footprints"].get(); + + if( entry.contains( "properties" ) && entry["properties"].is_object() ) { - if( entry.empty() || !entry.is_object() ) - continue; + const nlohmann::json& pj = entry["properties"]; - DATABASE_LIB_TABLE table; + if( pj.contains( "description" ) ) + table.properties.description = pj["description"].get(); - table.name = entry["name"].get(); - table.table = entry["table"].get(); - table.key_col = entry["key"].get(); - table.symbols_col = entry["symbols"].get(); - table.footprints_col = entry["footprints"].get(); - - if( entry.contains( "fields" ) && entry["fields"].is_array() ) + if( pj.contains( "footprint_filters" ) ) { - for( const nlohmann::json& fieldJson : entry["fields"] ) - { - if( fieldJson.empty() || !fieldJson.is_object() ) - continue; - - std::string column = fieldJson.contains( "column" ) - ? fieldJson["column"].get() : ""; - - std::string name = fieldJson.contains( "name" ) - ? fieldJson["name"].get() : ""; - - bool visible_on_add = !fieldJson.contains( "visible_on_add" ) - || fieldJson["visible_on_add"].get(); - - bool visible_in_chooser = - !fieldJson.contains( "visible_in_chooser" ) - || fieldJson["visible_in_chooser"].get(); - - bool show_name = fieldJson.contains( "show_name" ) - && fieldJson["show_name"].get(); - - table.fields.emplace_back( - DATABASE_FIELD_MAPPING( - { - column, name, visible_on_add, visible_in_chooser, show_name - } ) ); - } + table.properties.footprint_filters = + pj["footprint_filters"].get(); } - m_Tables.emplace_back( std::move( table ) ); + if( pj.contains( "exclude_from_bom" ) ) + { + table.properties.exclude_from_bom = + pj["exclude_from_bom"].get(); + } + + if( pj.contains( "exclude_from_board" ) ) + { + table.properties.exclude_from_board = + pj["exclude_from_board"].get(); + } } - }, - {} ) ); + + if( entry.contains( "fields" ) && entry["fields"].is_array() ) + { + for( const nlohmann::json& fieldJson : entry["fields"] ) + { + if( fieldJson.empty() || !fieldJson.is_object() ) + continue; + + std::string column = fieldJson.contains( "column" ) + ? fieldJson["column"].get() : ""; + + std::string name = fieldJson.contains( "name" ) + ? fieldJson["name"].get() : ""; + + bool visible_on_add = !fieldJson.contains( "visible_on_add" ) + || fieldJson["visible_on_add"].get(); + + bool visible_in_chooser = + !fieldJson.contains( "visible_in_chooser" ) + || fieldJson["visible_in_chooser"].get(); + + bool show_name = fieldJson.contains( "show_name" ) + && fieldJson["show_name"].get(); + + table.fields.emplace_back( + DATABASE_FIELD_MAPPING( + { + column, name, visible_on_add, visible_in_chooser, show_name + } ) ); + } + } + + m_Tables.emplace_back( std::move( table ) ); + } + }, + {} ) ); m_params.emplace_back( new PARAM( "cache.max_size", &m_Cache.max_size, 256 ) ); m_params.emplace_back( new PARAM( "cache.max_age", &m_Cache.max_age, 10 ) ); + + registerMigration( 0, 1, + [&]() -> bool + { + /* + * Schema 0 -> 1 + * Move internal symbol properties from fields with special names to + * a separate place in the schema. + */ + if( !Contains( "libraries" ) || !At( "libraries" ).is_array() ) + return true; + + for( nlohmann::json& library : At( "libraries" ) ) + { + if( !library.contains( "fields" ) ) + continue; + + for( const nlohmann::json& field : library["fields"] ) + { + if( !field.contains( "name" ) || !field.contains( "column" ) ) + continue; + + std::string name = field["name"].get(); + std::string col = field["column"].get(); + + if( name == "ki_description" ) + library["properties"]["description"] = col; + else if( name == "ki_fp_filters" ) + library["properties"]["footprint_filters"] = col; + } + } + + return true; + } ); } diff --git a/eeschema/sch_plugins/database/sch_database_plugin.cpp b/eeschema/sch_plugins/database/sch_database_plugin.cpp index 9eaf2bd57e..4f9be2a1b3 100644 --- a/eeschema/sch_plugins/database/sch_database_plugin.cpp +++ b/eeschema/sch_plugins/database/sch_database_plugin.cpp @@ -309,6 +309,39 @@ LIB_SYMBOL* SCH_DATABASE_PLUGIN::loadSymbolFromRow( const wxString& aSymbolName, aTable.footprints_col ); } + if( !aTable.properties.description.empty() && aRow.count( aTable.properties.description ) ) + { + wxString value( + std::any_cast( aRow.at( aTable.properties.description ) ).c_str(), + wxConvUTF8 ); + symbol->SetDescription( value ); + } + + if( !aTable.properties.footprint_filters.empty() + && aRow.count( aTable.properties.footprint_filters ) ) + { + wxString value( std::any_cast( aRow.at( aTable.properties.footprint_filters ) ) + .c_str(), + wxConvUTF8 ); + wxArrayString filters; + filters.push_back( value ); + symbol->SetFPFilters( filters ); + } + + if( !aTable.properties.exclude_from_board.empty() + && aRow.count( aTable.properties.exclude_from_board ) ) + { + bool exclude = std::any_cast( aRow.at( aTable.properties.exclude_from_board ) ); + symbol->SetIncludeOnBoard( !exclude ); + } + + if( !aTable.properties.exclude_from_bom.empty() + && aRow.count( aTable.properties.exclude_from_bom ) ) + { + bool exclude = std::any_cast( aRow.at( aTable.properties.exclude_from_bom ) ); + symbol->SetIncludeInBom( !exclude ); + } + for( const DATABASE_FIELD_MAPPING& mapping : aTable.fields ) { if( !aRow.count( mapping.column ) ) @@ -321,22 +354,7 @@ LIB_SYMBOL* SCH_DATABASE_PLUGIN::loadSymbolFromRow( const wxString& aSymbolName, wxString value( std::any_cast( aRow.at( mapping.column ) ).c_str(), wxConvUTF8 ); - if( mapping.name == wxT( "ki_description" ) ) - { - symbol->SetDescription( value ); - continue; - } - else if( mapping.name == wxT( "ki_keywords" ) ) - { - symbol->SetKeyWords( value ); - continue; - } - else if( mapping.name == wxT( "ki_fp_filters" ) ) - { - // TODO: Handle this here? - continue; - } - else if( mapping.name == wxT( "Value" ) ) + if( mapping.name == wxT( "Value" ) ) { LIB_FIELD& field = symbol->GetValueField(); field.SetText( value ); diff --git a/include/database/database_lib_settings.h b/include/database/database_lib_settings.h index b08641f6df..ea0cb51c35 100644 --- a/include/database/database_lib_settings.h +++ b/include/database/database_lib_settings.h @@ -52,6 +52,15 @@ struct DATABASE_FIELD_MAPPING }; +struct MAPPABLE_SYMBOL_PROPERTIES +{ + std::string description; + std::string footprint_filters; + std::string exclude_from_bom; + std::string exclude_from_board; +}; + + /** * A database library table will be mapped to a sub-library provided by the database library entry * in the KiCad symbol/footprint library table. A single database library config file (managed by @@ -73,6 +82,7 @@ struct DATABASE_LIB_TABLE std::string symbols_col; ///< Column name containing KiCad symbol refs std::string footprints_col; ///< Column name containing KiCad footprint refs + MAPPABLE_SYMBOL_PROPERTIES properties; std::vector fields; }; diff --git a/qa/data/dblib/qa_dblib.kicad_dbl b/qa/data/dblib/qa_dblib.kicad_dbl index 5516d0ab89..865eb4c684 100644 --- a/qa/data/dblib/qa_dblib.kicad_dbl +++ b/qa/data/dblib/qa_dblib.kicad_dbl @@ -60,14 +60,11 @@ "visible_on_add": true, "visible_in_chooser": false, "show_name": true - }, - { - "column": "Description", - "name": "ki_description", - "visible_on_add": false, - "visible_in_chooser": true } - ] + ], + "properties": { + "description": "Description" + } }, { "name": "Capacitors", @@ -105,14 +102,11 @@ "name": "Voltage Rating", "visible_on_add": true, "visible_in_chooser": true - }, - { - "column": "Description", - "name": "ki_description", - "visible_on_add": false, - "visible_in_chooser": true } - ] + ], + "properties": { + "description": "Description" + } } ] } \ No newline at end of file