Move to explicit symbol properties mapping

Fixes https://gitlab.com/kicad/code/kicad/-/issues/12845
This commit is contained in:
Jon Evans 2022-11-08 22:18:29 -05:00
parent 39774463db
commit c07477b94c
4 changed files with 165 additions and 83 deletions

View File

@ -29,7 +29,7 @@
#include <wildcards_and_files_ext.h>
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<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>(
"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<std::string>();
table.table = entry["table"].get<std::string>();
table.key_col = entry["key"].get<std::string>();
table.symbols_col = entry["symbols"].get<std::string>();
table.footprints_col = entry["footprints"].get<std::string>();
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<std::string>();
table.name = entry["name"].get<std::string>();
table.table = entry["table"].get<std::string>();
table.key_col = entry["key"].get<std::string>();
table.symbols_col = entry["symbols"].get<std::string>();
table.footprints_col = entry["footprints"].get<std::string>();
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>() : "";
std::string name = fieldJson.contains( "name" )
? fieldJson["name"].get<std::string>() : "";
bool visible_on_add = !fieldJson.contains( "visible_on_add" )
|| fieldJson["visible_on_add"].get<bool>();
bool visible_in_chooser =
!fieldJson.contains( "visible_in_chooser" )
|| fieldJson["visible_in_chooser"].get<bool>();
bool show_name = fieldJson.contains( "show_name" )
&& fieldJson["show_name"].get<bool>();
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<std::string>();
}
m_Tables.emplace_back( std::move( table ) );
if( pj.contains( "exclude_from_bom" ) )
{
table.properties.exclude_from_bom =
pj["exclude_from_bom"].get<std::string>();
}
if( pj.contains( "exclude_from_board" ) )
{
table.properties.exclude_from_board =
pj["exclude_from_board"].get<std::string>();
}
}
},
{} ) );
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>() : "";
std::string name = fieldJson.contains( "name" )
? fieldJson["name"].get<std::string>() : "";
bool visible_on_add = !fieldJson.contains( "visible_on_add" )
|| fieldJson["visible_on_add"].get<bool>();
bool visible_in_chooser =
!fieldJson.contains( "visible_in_chooser" )
|| fieldJson["visible_in_chooser"].get<bool>();
bool show_name = fieldJson.contains( "show_name" )
&& fieldJson["show_name"].get<bool>();
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<int>( "cache.max_size", &m_Cache.max_size, 256 ) );
m_params.emplace_back( new PARAM<int>( "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>();
std::string col = field["column"].get<std::string>();
if( name == "ki_description" )
library["properties"]["description"] = col;
else if( name == "ki_fp_filters" )
library["properties"]["footprint_filters"] = col;
}
}
return true;
} );
}

View File

@ -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<std::string>( 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<std::string>( 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<bool>( 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<bool>( 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<std::string>( 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 );

View File

@ -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<DATABASE_FIELD_MAPPING> fields;
};

View File

@ -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"
}
}
]
}