22#include <nlohmann/json.hpp>
37static std::optional<int>
getInPcbUnits(
const nlohmann::json& aObj,
const std::string& aKey,
38 std::optional<int> aDefault = std::optional<int>() )
40 if( aObj.contains( aKey ) && aObj[aKey].is_number() )
47static int getInSchUnits(
const nlohmann::json& aObj,
const std::string& aKey,
int aDefault )
49 if( aObj.contains( aKey ) && aObj[aKey].is_number() )
63 []( nlohmann::json& json_array,
const std::shared_ptr<NETCLASS>& nc )
67 nlohmann::json nc_json = {
68 {
"name", nc->GetName().ToUTF8() },
71 {
"line_style", nc->GetLineStyle() },
72 {
"schematic_color", nc->GetSchematicColor() },
73 {
"pcb_color", nc->GetPcbColor() }
77 []( nlohmann::json&
json,
const std::string& aKey,
int aValue )
82 if( nc->HasClearance() )
83 saveInPcbUnits( nc_json,
"clearance", nc->GetClearance() );
85 if( nc->HasTrackWidth() )
86 saveInPcbUnits( nc_json,
"track_width", nc->GetTrackWidth() );
88 if( nc->HasViaDiameter() )
89 saveInPcbUnits( nc_json,
"via_diameter", nc->GetViaDiameter() );
91 if( nc->HasViaDrill() )
92 saveInPcbUnits( nc_json,
"via_drill", nc->GetViaDrill() );
94 if( nc->HasuViaDiameter() )
95 saveInPcbUnits( nc_json,
"microvia_diameter", nc->GetuViaDiameter() );
97 if( nc->HasuViaDrill() )
98 saveInPcbUnits( nc_json,
"microvia_drill", nc->GetuViaDrill() );
100 if( nc->HasDiffPairWidth() )
101 saveInPcbUnits( nc_json,
"diff_pair_width", nc->GetDiffPairWidth() );
103 if( nc->HasDiffPairGap() )
104 saveInPcbUnits( nc_json,
"diff_pair_gap", nc->GetDiffPairGap() );
106 if( nc->HasDiffPairViaGap() )
107 saveInPcbUnits( nc_json,
"diff_pair_via_gap", nc->GetDiffPairViaGap() );
109 json_array.push_back( nc_json );
113 [](
const nlohmann::json& entry )
115 wxString
name = entry[
"name"];
117 std::shared_ptr<NETCLASS> nc = std::make_shared<NETCLASS>(
name );
120 nc->SetClearance( *value );
123 nc->SetTrackWidth( *value );
126 nc->SetViaDiameter( *value );
129 nc->SetViaDrill( *value );
131 if(
auto value =
getInPcbUnits( entry,
"microvia_diameter" ) )
132 nc->SetuViaDiameter( *value );
135 nc->SetuViaDrill( *value );
138 nc->SetDiffPairWidth( *value );
141 nc->SetDiffPairGap( *value );
143 if(
auto value =
getInPcbUnits( entry,
"diff_pair_via_gap" ) )
144 nc->SetDiffPairViaGap( *value );
146 nc->SetWireWidth(
getInSchUnits( entry,
"wire_width", nc->GetWireWidth() ) );
147 nc->SetBusWidth(
getInSchUnits( entry,
"bus_width", nc->GetBusWidth() ) );
149 if( entry.contains(
"line_style" ) && entry[
"line_style"].is_number() )
150 nc->SetLineStyle( entry[
"line_style"].get<int>() );
152 if( entry.contains(
"pcb_color" ) && entry[
"pcb_color"].is_string() )
153 nc->SetPcbColor( entry[
"pcb_color"].get<KIGFX::COLOR4D>() );
155 if( entry.contains(
"schematic_color" )
156 && entry[
"schematic_color"].is_string() )
158 nc->SetSchematicColor( entry[
"schematic_color"].get<KIGFX::COLOR4D>() );
165 [&]() -> nlohmann::json
167 nlohmann::json ret = nlohmann::json::array();
173 saveNetclass( ret, netclass );
177 [&](
const nlohmann::json& aJson )
179 if( !aJson.is_array() )
184 for(
const nlohmann::json& entry : aJson )
186 if( !entry.is_object() || !entry.contains(
"name" ) )
189 std::shared_ptr<NETCLASS> nc = readNetClass( entry );
200 [&]() -> nlohmann::json
202 nlohmann::json ret = {};
206 std::string key( netname.ToUTF8() );
207 ret[ std::move( key ) ] =
color;
212 [&](
const nlohmann::json& aJson )
214 if( !aJson.is_object() )
219 for(
const auto& pair : aJson.items() )
221 wxString key( pair.key().c_str(), wxConvUTF8 );
228 [&]() -> nlohmann::json
230 nlohmann::json ret = {};
234 std::string key( netname.ToUTF8() );
235 ret[ std::move( key ) ] = netclassName;
240 [&](
const nlohmann::json& aJson )
242 if( !aJson.is_object() )
247 for(
const auto& pair : aJson.items() )
249 wxString key( pair.key().c_str(), wxConvUTF8 );
256 [&]() -> nlohmann::json
258 nlohmann::json ret = nlohmann::json::array();
262 nlohmann::json pattern_json = {
263 {
"pattern", matcher->GetPattern().ToUTF8() },
264 {
"netclass", netclassName.ToUTF8() }
267 ret.push_back( std::move( pattern_json ) );
272 [&](
const nlohmann::json& aJson )
274 if( !aJson.is_array() )
279 for(
const nlohmann::json& entry : aJson )
281 if( !entry.is_object() )
284 if( entry.contains(
"pattern" ) && entry[
"pattern"].is_string()
285 && entry.contains(
"netclass" ) && entry[
"netclass"].is_string() )
287 wxString pattern = entry[
"pattern"].get<wxString>();
288 wxString netclass = entry[
"netclass"].get<wxString>();
292 std::make_unique<EDA_COMBINED_MATCHER>( pattern,
CTX_NETCLASS ),
345 for(
auto& netClass :
m_internals->At(
"classes" ).items() )
347 if( netClass.value().contains(
"nets" ) && netClass.value()[
"nets"].is_array() )
349 nlohmann::json migrated = nlohmann::json::array();
351 for(
auto& net : netClass.value()[
"nets"].items() )
354 netClass.value()[
"nets"] = migrated;
367 nlohmann::json patterns = nlohmann::json::array();
369 for(
auto& netClass :
m_internals->At(
"classes" ).items() )
371 if( netClass.value().contains(
"name" )
372 && netClass.value().contains(
"nets" )
373 && netClass.value()[
"nets"].is_array() )
375 wxString netClassName = netClass.value()[
"name"].get<wxString>();
377 for(
auto& net : netClass.value()[
"nets"].items() )
379 nlohmann::json pattern_json = {
380 {
"pattern", net.value().get<wxString>() },
381 {
"netclass", netClassName }
384 patterns.push_back( pattern_json );
389 m_internals->SetFromString(
"netclass_patterns", patterns );
399 [&](
const wxString& netclass )
410 if( aNetName.IsEmpty() )
416 return getNetclass( it->second );
421 return getNetclass( it2->second );
425 if( matcher->StartsWith( aNetName ) )
428 return getNetclass( netclassName );
450 return c ==
'_' || c ==
'^' || c ==
'~';
455 std::vector<wxString>* aMemberList )
459 static wxString digits( wxT(
"0123456789" ) );
460 return digits.Contains( c );
463 size_t busLen = aBus.length();
470 int braceNesting = 0;
472 prefix.reserve( busLen );
476 for( ; i < busLen; ++i )
485 else if( aBus[i] ==
'}' )
490 if( aBus[i] ==
' ' || aBus[i] ==
']' )
506 for( ; i < busLen; ++i )
508 if( aBus[i] ==
'.' && i + 1 < busLen && aBus[i+1] ==
'.' )
510 tmp.ToLong( &begin );
528 for( ; i < busLen; ++i )
545 for( ; i < busLen; ++i )
558 if( braceNesting != 0 )
563 else if( begin > end )
564 std::swap( begin, end );
571 for(
long idx = begin; idx <= end; ++idx )
573 wxString str = prefix;
577 aMemberList->emplace_back( str );
586 std::vector<wxString>* aMemberList )
588 size_t groupLen = aGroup.length();
592 int braceNesting = 0;
594 prefix.reserve( groupLen );
598 for( ; i < groupLen; ++i )
600 if( aGroup[i] ==
'{' )
607 else if( aGroup[i] ==
'}' )
612 if( aGroup[i] ==
' ' || aGroup[i] ==
'[' || aGroup[i] ==
']' )
618 if( braceNesting != 0 )
631 for( ; i < groupLen; ++i )
633 if( aGroup[i] ==
'{' )
640 else if( aGroup[i] ==
'}' )
648 if( aMemberList && !tmp.IsEmpty() )
656 if( aGroup[i] ==
' ' || aGroup[i] ==
',' )
658 if( aMemberList && !tmp.IsEmpty() )
constexpr EDA_IU_SCALE schIUScale
constexpr EDA_IU_SCALE pcbIUScale
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
A color representation with 4 components: red, green, blue, alpha.
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
static const char Default[]
the name of the default NETCLASS
NET_SETTINGS stores various net-related settings in a project context.
std::shared_ptr< NETCLASS > m_DefaultNetClass
std::map< wxString, KIGFX::COLOR4D > m_NetColorAssignments
A map of fully-qualified net names to colors used in the board context.
bool operator==(const NET_SETTINGS &aOther) const
std::map< wxString, wxString > m_NetClassLabelAssignments
static bool ParseBusGroup(const wxString &aGroup, wxString *name, std::vector< wxString > *aMemberList)
Parse a bus group label into the name and a list of components.
std::map< wxString, wxString > m_NetClassPatternAssignmentCache
std::shared_ptr< NETCLASS > GetEffectiveNetClass(const wxString &aNetName) const
std::vector< std::pair< std::unique_ptr< EDA_COMBINED_MATCHER >, wxString > > m_NetClassPatternAssignments
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parse a bus vector (e.g.
std::map< wxString, std::shared_ptr< NETCLASS > > m_NetClasses
std::shared_ptr< NETCLASS > GetNetClassByName(const wxString &aNetName) const
Get a NETCLASS object from a given Netclass name string.
NET_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
Like a normal param, but with custom getter and setter functions.
static bool isSuperSubOverbar(wxChar c)
const int netSettingsSchemaVersion
static int getInSchUnits(const nlohmann::json &aObj, const std::string &aKey, int aDefault)
static std::optional< int > getInPcbUnits(const nlohmann::json &aObj, const std::string &aKey, std::optional< int > aDefault=std::optional< int >())
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
constexpr double IUTomm(int iu) const
constexpr int IUToMils(int iu) const
constexpr int MilsToIU(int mils) const
constexpr int mmToIU(double mm) const