1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 CERN
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
* @author Jon Evans <jon@craftyjon.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <limits>
#include <json_common.h>
#include <project/net_settings.h>
#include <settings/parameters.h>
#include <settings/settings_manager.h>
#include <string_utils.h>
#include <base_units.h>
#include <unordered_set>
// const int netSettingsSchemaVersion = 0;
// const int netSettingsSchemaVersion = 1; // new overbar syntax
// const int netSettingsSchemaVersion = 2; // exclude buses from netclass members
// const int netSettingsSchemaVersion = 3; // netclass assignment patterns
const int netSettingsSchemaVersion = 4; // netclass ordering
static std::optional<int> getInPcbUnits( const nlohmann::json& aObj, const std::string& aKey,
std::optional<int> aDefault = std::optional<int>() )
{
if( aObj.contains( aKey ) && aObj[aKey].is_number() )
return pcbIUScale.mmToIU( aObj[aKey].get<double>() );
else
return aDefault;
};
static std::optional<int> getInSchUnits( const nlohmann::json& aObj, const std::string& aKey,
std::optional<int> aDefault = std::optional<int>() )
{
if( aObj.contains( aKey ) && aObj[aKey].is_number() )
return schIUScale.MilsToIU( aObj[aKey].get<double>() );
else
return aDefault;
};
NET_SETTINGS::NET_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
NESTED_SETTINGS( "net_settings", netSettingsSchemaVersion, aParent, aPath, false )
{
m_defaultNetClass = std::make_shared<NETCLASS>( NETCLASS::Default, true );
m_defaultNetClass->SetDescription( _( "This is the default net class." ) );
m_defaultNetClass->SetPriority( std::numeric_limits<int>::max() );
auto saveNetclass =
[]( nlohmann::json& json_array, const std::shared_ptr<NETCLASS>& nc )
{
// Note: we're in common/, but we do happen to know which of these
// fields are used in which units system.
nlohmann::json nc_json = { { "name", nc->GetName().ToUTF8() },
{ "priority", nc->GetPriority() },
{ "schematic_color", nc->GetSchematicColor( true ) },
{ "pcb_color", nc->GetPcbColor( true ) } };
auto saveInPcbUnits =
[]( nlohmann::json& json, const std::string& aKey, int aValue )
{
json.push_back( { aKey, pcbIUScale.IUTomm( aValue ) } );
};
if( nc->HasWireWidth() )
nc_json.push_back(
{ "wire_width", schIUScale.IUToMils( nc->GetWireWidth() ) } );
if( nc->HasBusWidth() )
nc_json.push_back( { "bus_width", schIUScale.IUToMils( nc->GetBusWidth() ) } );
if( nc->HasLineStyle() )
nc_json.push_back( { "line_style", nc->GetLineStyle() } );
if( nc->HasClearance() )
saveInPcbUnits( nc_json, "clearance", nc->GetClearance() );
if( nc->HasTrackWidth() )
saveInPcbUnits( nc_json, "track_width", nc->GetTrackWidth() );
if( nc->HasViaDiameter() )
saveInPcbUnits( nc_json, "via_diameter", nc->GetViaDiameter() );
if( nc->HasViaDrill() )
saveInPcbUnits( nc_json, "via_drill", nc->GetViaDrill() );
if( nc->HasuViaDiameter() )
saveInPcbUnits( nc_json, "microvia_diameter", nc->GetuViaDiameter() );
if( nc->HasuViaDrill() )
saveInPcbUnits( nc_json, "microvia_drill", nc->GetuViaDrill() );
if( nc->HasDiffPairWidth() )
saveInPcbUnits( nc_json, "diff_pair_width", nc->GetDiffPairWidth() );
if( nc->HasDiffPairGap() )
saveInPcbUnits( nc_json, "diff_pair_gap", nc->GetDiffPairGap() );
if( nc->HasDiffPairViaGap() )
saveInPcbUnits( nc_json, "diff_pair_via_gap", nc->GetDiffPairViaGap() );
json_array.push_back( nc_json );
};
auto readNetClass =
[]( const nlohmann::json& entry )
{
wxString name = entry["name"];
std::shared_ptr<NETCLASS> nc = std::make_shared<NETCLASS>( name, false );
int priority = entry["priority"];
nc->SetPriority( priority );
if( auto value = getInPcbUnits( entry, "clearance" ) )
nc->SetClearance( *value );
if( auto value = getInPcbUnits( entry, "track_width" ) )
nc->SetTrackWidth( *value );
if( auto value = getInPcbUnits( entry, "via_diameter" ) )
nc->SetViaDiameter( *value );
if( auto value = getInPcbUnits( entry, "via_drill" ) )
nc->SetViaDrill( *value );
if( auto value = getInPcbUnits( entry, "microvia_diameter" ) )
nc->SetuViaDiameter( *value );
if( auto value = getInPcbUnits( entry, "microvia_drill" ) )
nc->SetuViaDrill( *value );
if( auto value = getInPcbUnits( entry, "diff_pair_width" ) )
nc->SetDiffPairWidth( *value );
if( auto value = getInPcbUnits( entry, "diff_pair_gap" ) )
nc->SetDiffPairGap( *value );
if( auto value = getInPcbUnits( entry, "diff_pair_via_gap" ) )
nc->SetDiffPairViaGap( *value );
if( auto value = getInSchUnits( entry, "wire_width" ) )
nc->SetWireWidth( *value );
if( auto value = getInSchUnits( entry, "bus_width" ) )
nc->SetBusWidth( *value );
if( entry.contains( "line_style" ) && entry["line_style"].is_number() )
nc->SetLineStyle( entry["line_style"].get<int>() );
if( entry.contains( "pcb_color" ) && entry["pcb_color"].is_string() )
nc->SetPcbColor( entry["pcb_color"].get<KIGFX::COLOR4D>() );
if( entry.contains( "schematic_color" )
&& entry["schematic_color"].is_string() )
{
nc->SetSchematicColor( entry["schematic_color"].get<KIGFX::COLOR4D>() );
}
return nc;
};
m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "classes",
[&]() -> nlohmann::json
{
nlohmann::json ret = nlohmann::json::array();
if( m_defaultNetClass )
saveNetclass( ret, m_defaultNetClass );
for( const auto& [name, netclass] : m_netClasses )
saveNetclass( ret, netclass );
return ret;
},
[&]( const nlohmann::json& aJson )
{
if( !aJson.is_array() )
return;
m_netClasses.clear();
for( const nlohmann::json& entry : aJson )
{
if( !entry.is_object() || !entry.contains( "name" ) )
continue;
std::shared_ptr<NETCLASS> nc = readNetClass( entry );
if( nc->IsDefault() )
m_defaultNetClass = nc;
else
m_netClasses[nc->GetName()] = nc;
}
},
{} ) );
m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "net_colors",
[&]() -> nlohmann::json
{
nlohmann::json ret = {};
for( const auto& [netname, color] : m_netColorAssignments )
{
std::string key( netname.ToUTF8() );
ret[ std::move( key ) ] = color;
}
return ret;
},
[&]( const nlohmann::json& aJson )
{
if( !aJson.is_object() )
return;
m_netColorAssignments.clear();
for( const auto& pair : aJson.items() )
{
wxString key( pair.key().c_str(), wxConvUTF8 );
m_netColorAssignments[std::move( key )] = pair.value().get<KIGFX::COLOR4D>();
}
},
{} ) );
m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "netclass_assignments",
[&]() -> nlohmann::json
{
nlohmann::json ret = {};
for( const auto& [netname, netclassNames] : m_netClassLabelAssignments )
{
nlohmann::json netclassesJson = nlohmann::json::array();
for( const auto& netclass : netclassNames )
{
std::string netclassStr( netclass.ToUTF8() );
netclassesJson.push_back( std::move( netclassStr ) );
}
std::string key( netname.ToUTF8() );
ret[std::move( key )] = netclassesJson;
}
return ret;
},
[&]( const nlohmann::json& aJson )
{
if( !aJson.is_object() )
return;
m_netClassLabelAssignments.clear();
for( const auto& pair : aJson.items() )
{
wxString key( pair.key().c_str(), wxConvUTF8 );
for( const auto& netclassName : pair.value() )
m_netClassLabelAssignments[key].insert( netclassName.get<wxString>() );
}
},
{} ) );
m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "netclass_patterns",
[&]() -> nlohmann::json
{
nlohmann::json ret = nlohmann::json::array();
for( const auto& [matcher, netclassName] : m_netClassPatternAssignments )
{
nlohmann::json pattern_json = {
{ "pattern", matcher->GetPattern().ToUTF8() },
{ "netclass", netclassName.ToUTF8() }
};
ret.push_back( std::move( pattern_json ) );
}
return ret;
},
[&]( const nlohmann::json& aJson )
{
if( !aJson.is_array() )
return;
m_netClassPatternAssignments.clear();
for( const nlohmann::json& entry : aJson )
{
if( !entry.is_object() )
continue;
if( entry.contains( "pattern" ) && entry["pattern"].is_string()
&& entry.contains( "netclass" ) && entry["netclass"].is_string() )
{
wxString pattern = entry["pattern"].get<wxString>();
wxString netclass = entry["netclass"].get<wxString>();
m_netClassPatternAssignments.push_back(
{ std::make_unique<EDA_COMBINED_MATCHER>( pattern, CTX_NETCLASS ),
netclass } );
}
}
},
{} ) );
registerMigration( 0, 1, std::bind( &NET_SETTINGS::migrateSchema0to1, this ) );
registerMigration( 1, 2, std::bind( &NET_SETTINGS::migrateSchema1to2, this ) );
registerMigration( 2, 3, std::bind( &NET_SETTINGS::migrateSchema2to3, this ) );
registerMigration( 3, 4, std::bind( &NET_SETTINGS::migrateSchema3to4, this ) );
}
NET_SETTINGS::~NET_SETTINGS()
{
// Release early before destroying members
if( m_parent )
{
m_parent->ReleaseNestedSettings( this );
m_parent = nullptr;
}
}
bool NET_SETTINGS::operator==( const NET_SETTINGS& aOther ) const
{
if( !std::equal( std::begin( m_netClasses ), std::end( m_netClasses ),
std::begin( aOther.m_netClasses ) ) )
return false;
if( !std::equal( std::begin( m_netClassPatternAssignments ),
std::end( m_netClassPatternAssignments ),
std::begin( aOther.m_netClassPatternAssignments ) ) )
return false;
if( !std::equal( std::begin( m_netClassLabelAssignments ),
std::end( m_netClassLabelAssignments ),
std::begin( aOther.m_netClassLabelAssignments ) ) )
return false;
if( !std::equal( std::begin( m_netColorAssignments ), std::end( m_netColorAssignments ),
std::begin( aOther.m_netColorAssignments ) ) )
return false;
return true;
}
bool NET_SETTINGS::migrateSchema0to1()
{
if( m_internals->contains( "classes" ) && m_internals->At( "classes" ).is_array() )
{
for( auto& netClass : m_internals->At( "classes" ).items() )
{
if( netClass.value().contains( "nets" ) && netClass.value()["nets"].is_array() )
{
nlohmann::json migrated = nlohmann::json::array();
for( auto& net : netClass.value()["nets"].items() )
migrated.push_back( ConvertToNewOverbarNotation( net.value().get<wxString>() ) );
netClass.value()["nets"] = migrated;
}
}
}
return true;
}
bool NET_SETTINGS::migrateSchema1to2()
{
return true;
}
bool NET_SETTINGS::migrateSchema2to3()
{
if( m_internals->contains( "classes" ) && m_internals->At( "classes" ).is_array() )
{
nlohmann::json patterns = nlohmann::json::array();
for( auto& netClass : m_internals->At( "classes" ).items() )
{
if( netClass.value().contains( "name" )
&& netClass.value().contains( "nets" )
&& netClass.value()["nets"].is_array() )
{
wxString netClassName = netClass.value()["name"].get<wxString>();
for( auto& net : netClass.value()["nets"].items() )
{
nlohmann::json pattern_json = {
{ "pattern", net.value().get<wxString>() },
{ "netclass", netClassName }
};
patterns.push_back( pattern_json );
}
}
}
m_internals->SetFromString( "netclass_patterns", patterns );
}
return true;
}
bool NET_SETTINGS::migrateSchema3to4()
{
// Add priority field to netclasses
if( m_internals->contains( "classes" ) && m_internals->At( "classes" ).is_array() )
{
int priority = 0;
for( auto& netClass : m_internals->At( "classes" ).items() )
{
if( netClass.value()["name"].get<wxString>() == NETCLASS::Default )
netClass.value()["priority"] = std::numeric_limits<int>::max();
else
netClass.value()["priority"] = priority++;
}
}
// Move netclass assignments to a list
if( m_internals->contains( "netclass_assignments" )
&& m_internals->At( "netclass_assignments" ).is_object() )
{
nlohmann::json migrated = {};
for( const auto& pair : m_internals->At( "netclass_assignments" ).items() )
{
nlohmann::json netclassesJson = nlohmann::json::array();
if( pair.value().get<wxString>() != wxEmptyString )
netclassesJson.push_back( pair.value() );
migrated[pair.key()] = netclassesJson;
}
m_internals->SetFromString( "netclass_assignments", migrated );
}
return true;
}
void NET_SETTINGS::SetDefaultNetclass( std::shared_ptr<NETCLASS> netclass )
{
m_defaultNetClass = netclass;
}
std::shared_ptr<NETCLASS> NET_SETTINGS::GetDefaultNetclass()
{
return m_defaultNetClass;
}
bool NET_SETTINGS::HasNetclass( const wxString& netclassName ) const
{
return m_netClasses.find( netclassName ) != m_netClasses.end();
}
void NET_SETTINGS::SetNetclass( const wxString& netclassName, std::shared_ptr<NETCLASS>& netclass )
{
m_netClasses[netclassName] = netclass;
}
void NET_SETTINGS::SetNetclasses( const std::map<wxString, std::shared_ptr<NETCLASS>>& netclasses )
{
m_netClasses = netclasses;
ClearAllCaches();
}
const std::map<wxString, std::shared_ptr<NETCLASS>>& NET_SETTINGS::GetNetclasses() const
{
return m_netClasses;
}
const std::map<wxString, std::shared_ptr<NETCLASS>>& NET_SETTINGS::GetCompositeNetclasses() const
{
return m_compositeNetClasses;
}
void NET_SETTINGS::ClearNetclasses()
{
m_netClasses.clear();
m_impicitNetClasses.clear();
ClearAllCaches();
}
const std::map<wxString, std::set<wxString>>& NET_SETTINGS::GetNetclassLabelAssignments() const
{
return m_netClassLabelAssignments;
}
void NET_SETTINGS::ClearNetclassLabelAssignments()
{
m_netClassLabelAssignments.clear();
}
void NET_SETTINGS::ClearNetclassLabelAssignment( const wxString& netName )
{
m_netClassLabelAssignments.erase( netName );
}
void NET_SETTINGS::SetNetclassLabelAssignment( const wxString& netName,
const std::set<wxString>& netclasses )
{
m_netClassLabelAssignments[netName] = netclasses;
}
void NET_SETTINGS::AppendNetclassLabelAssignment( const wxString& netName,
const std::set<wxString>& netclasses )
{
m_netClassLabelAssignments[netName].insert( netclasses.begin(), netclasses.end() );
}
bool NET_SETTINGS::HasNetclassLabelAssignment( const wxString& netName ) const
{
return m_netClassLabelAssignments.find( netName ) != m_netClassLabelAssignments.end();
}
void NET_SETTINGS::SetNetclassPatternAssignment( const wxString& pattern, const wxString& netclass )
{
// Replace existing assignment if we have one
for( auto& assignment : m_netClassPatternAssignments )
{
if( assignment.first->GetPattern() == pattern )
{
assignment.second = netclass;
ClearAllCaches();
return;
}
}
// No assignment, add a new one
m_netClassPatternAssignments.push_back(
{ std::make_unique<EDA_COMBINED_MATCHER>( pattern, CTX_NETCLASS ), netclass } );
ClearAllCaches();
}
void NET_SETTINGS::SetNetclassPatternAssignments(
std::vector<std::pair<std::unique_ptr<EDA_COMBINED_MATCHER>, wxString>>&& netclassPatterns )
{
m_netClassPatternAssignments = std::move( netclassPatterns );
ClearAllCaches();
}
std::vector<std::pair<std::unique_ptr<EDA_COMBINED_MATCHER>, wxString>>&
NET_SETTINGS::GetNetclassPatternAssignments()
{
return m_netClassPatternAssignments;
}
void NET_SETTINGS::ClearNetclassPatternAssignments()
{
m_netClassPatternAssignments.clear();
}
void NET_SETTINGS::ClearCacheForNet( const wxString& netName )
{
if( m_effectiveNetclassCache.count( netName ) )
{
wxString compositeNetclassName = m_effectiveNetclassCache[netName]->GetName();
m_compositeNetClasses.erase( compositeNetclassName );
m_effectiveNetclassCache.erase( netName );
}
}
void NET_SETTINGS::ClearAllCaches()
{
m_effectiveNetclassCache.clear();
m_compositeNetClasses.clear();
}
void NET_SETTINGS::SetNetColorAssignment( const wxString& netName, const KIGFX::COLOR4D& color )
{
m_netColorAssignments[netName] = color;
}
const std::map<wxString, KIGFX::COLOR4D>& NET_SETTINGS::GetNetColorAssignments() const
{
return m_netColorAssignments;
}
void NET_SETTINGS::ClearNetColorAssignments()
{
m_netColorAssignments.clear();
}
bool NET_SETTINGS::HasEffectiveNetClass( const wxString& aNetName ) const
{
return m_effectiveNetclassCache.count( aNetName ) > 0;
}
std::shared_ptr<NETCLASS> NET_SETTINGS::GetCachedEffectiveNetClass( const wxString& aNetName ) const
{
return m_effectiveNetclassCache.at( aNetName );
}
std::shared_ptr<NETCLASS> NET_SETTINGS::GetEffectiveNetClass( const wxString& aNetName )
{
// Lambda to fetch an explicit netclass. Returns a nullptr if not found
auto getExplicitNetclass = [this]( const wxString& netclass ) -> std::shared_ptr<NETCLASS>
{
if( netclass == NETCLASS::Default )
return m_defaultNetClass;
auto ii = m_netClasses.find( netclass );
if( ii == m_netClasses.end() )
return {};
else
return ii->second;
};
// Lambda to fetch or create an implicit netclass (defined with a label, but not configured)
// These are needed as while they do not provide any netclass parameters, they do now appear in
// DRC matching strings as an assigned netclass.
auto getOrAddImplicitNetcless = [this]( const wxString& netclass ) -> std::shared_ptr<NETCLASS>
{
auto ii = m_impicitNetClasses.find( netclass );
if( ii == m_impicitNetClasses.end() )
{
std::shared_ptr<NETCLASS> nc = std::make_shared<NETCLASS>( netclass, false );
nc->SetPriority( std::numeric_limits<int>::max() - 1 ); // Priority > default netclass
m_impicitNetClasses[netclass] = nc;
return nc;
}
else
{
return ii->second;
}
};
// <no net> is forced to be part of the default netclass.
if( aNetName.IsEmpty() )
return m_defaultNetClass;
// First check if we have a cached resolved netclass
auto cacheItr = m_effectiveNetclassCache.find( aNetName );
if( cacheItr != m_effectiveNetclassCache.end() )
return cacheItr->second;
// No cache found - build a vector of all netclasses assigned to or matching this net
std::unordered_set<std::shared_ptr<NETCLASS>> resolvedNetclasses;
// First find explicit netclass assignments
auto it = m_netClassLabelAssignments.find( aNetName );
if( it != m_netClassLabelAssignments.end() && it->second.size() > 0 )
{
for( const wxString& netclassName : it->second )
{
std::shared_ptr<NETCLASS> netclass = getExplicitNetclass( netclassName );
if( netclass )
{
resolvedNetclasses.insert( std::move( netclass ) );
}
else
{
resolvedNetclasses.insert( getOrAddImplicitNetcless( netclassName ) );
}
}
}
// Now find any pattern-matched netclass assignments
for( const auto& [matcher, netclassName] : m_netClassPatternAssignments )
{
if( matcher->StartsWith( aNetName ) )
{
std::shared_ptr<NETCLASS> netclass = getExplicitNetclass( netclassName );
if( netclass )
{
resolvedNetclasses.insert( std::move( netclass ) );
}
else
{
resolvedNetclasses.insert( getOrAddImplicitNetcless( netclassName ) );
}
}
}
// Handle zero resolved netclasses
if( resolvedNetclasses.size() == 0 )
{
m_effectiveNetclassCache[aNetName] = m_defaultNetClass;
return m_defaultNetClass;
}
// Make and cache the effective netclass. Note that makeEffectiveNetclass will add the default
// netclass to resolvedNetclasses if it is needed to complete the netclass paramters set. It
// will also sort resolvedNetclasses by priority order.
std::vector<NETCLASS*> netclassPtrs;
for( const std::shared_ptr<NETCLASS>& nc : resolvedNetclasses )
netclassPtrs.push_back( nc.get() );
wxString name;
name.Printf( "Effective for net: %s", aNetName );
std::shared_ptr<NETCLASS> effectiveNetclass = std::make_shared<NETCLASS>( name, false );
makeEffectiveNetclass( effectiveNetclass, netclassPtrs );
if( netclassPtrs.size() == 1 )
{
// No defaults were added - just return the primary netclass
m_effectiveNetclassCache[aNetName] = *resolvedNetclasses.begin();
return *resolvedNetclasses.begin();
}
else
{
effectiveNetclass->SetConstituentNetclasses( std::move( netclassPtrs ) );
m_compositeNetClasses[effectiveNetclass->GetName()] = effectiveNetclass;
m_effectiveNetclassCache[aNetName] = effectiveNetclass;
return effectiveNetclass;
}
}
void NET_SETTINGS::RecomputeEffectiveNetclasses()
{
for( auto& [ncName, nc] : m_compositeNetClasses )
{
// Note this needs to be a copy in case we now need to add the default netclass
std::vector<NETCLASS*> constituents = nc->GetConstituentNetclasses();
wxASSERT( constituents.size() > 0 );
// If the last netclass is Default, remove it (it will be re-added if still needed)
if( ( *constituents.rbegin() )->GetName() == NETCLASS::Default )
{
constituents.pop_back();
}
// Remake the netclass from original constituents
nc->ResetParameters();
makeEffectiveNetclass( nc, constituents );
nc->SetConstituentNetclasses( std::move( constituents ) );
}
}
void NET_SETTINGS::makeEffectiveNetclass( std::shared_ptr<NETCLASS>& effectiveNetclass,
std::vector<NETCLASS*>& constituentNetclasses ) const
{
// Sort the resolved netclasses by priority (highest first), with same-priority netclasses
// ordered alphabetically
std::sort( constituentNetclasses.begin(), constituentNetclasses.end(),
[]( NETCLASS* nc1, NETCLASS* nc2 )
{
int p1 = nc1->GetPriority();
int p2 = nc2->GetPriority();
if( p1 < p2 )
return true;
if (p1 == p2)
return nc1->GetName().Cmp( nc2->GetName() ) < 0;
return false;
} );
// Iterate from lowest priority netclass and fill effective netclass parameters
for( auto itr = constituentNetclasses.rbegin(); itr != constituentNetclasses.rend(); ++itr )
{
NETCLASS* nc = *itr;
if( nc->HasClearance() )
{
effectiveNetclass->SetClearance( nc->GetClearance() );
effectiveNetclass->SetClearanceParent( nc );
}
if( nc->HasTrackWidth() )
{
effectiveNetclass->SetTrackWidth( nc->GetTrackWidth() );
effectiveNetclass->SetTrackWidthParent( nc );
}
if( nc->HasViaDiameter() )
{
effectiveNetclass->SetViaDiameter( nc->GetViaDiameter() );
effectiveNetclass->SetViaDiameterParent( nc );
}
if( nc->HasViaDrill() )
{
effectiveNetclass->SetViaDrill( nc->GetViaDrill() );
effectiveNetclass->SetViaDrillParent( nc );
}
if( nc->HasuViaDiameter() )
{
effectiveNetclass->SetuViaDiameter( nc->GetuViaDiameter() );
effectiveNetclass->SetuViaDiameterParent( nc );
}
if( nc->HasuViaDrill() )
{
effectiveNetclass->SetuViaDrill( nc->GetuViaDrill() );
effectiveNetclass->SetuViaDrillParent( nc );
}
if( nc->HasDiffPairWidth() )
{
effectiveNetclass->SetDiffPairWidth( nc->GetDiffPairWidth() );
effectiveNetclass->SetDiffPairWidthParent( nc );
}
if( nc->HasDiffPairGap() )
{
effectiveNetclass->SetDiffPairGap( nc->GetDiffPairGap() );
effectiveNetclass->SetDiffPairGapParent( nc );
}
if( nc->HasDiffPairViaGap() )
{
effectiveNetclass->SetDiffPairViaGap( nc->GetDiffPairViaGap() );
effectiveNetclass->SetDiffPairViaGapParent( nc );
}
if( nc->HasWireWidth() )
{
effectiveNetclass->SetWireWidth( nc->GetWireWidth() );
effectiveNetclass->SetWireWidthParent( nc );
}
if( nc->HasBusWidth() )
{
effectiveNetclass->SetBusWidth( nc->GetBusWidth() );
effectiveNetclass->SetBusWidthParent( nc );
}
if( nc->HasLineStyle() )
{
effectiveNetclass->SetLineStyle( nc->GetLineStyle() );
effectiveNetclass->SetLineStyleParent( nc );
}
COLOR4D pcbColor = nc->GetPcbColor();
if( pcbColor != COLOR4D::UNSPECIFIED )
{
effectiveNetclass->SetPcbColor( pcbColor );
effectiveNetclass->SetPcbColorParent( nc );
}
COLOR4D schColor = nc->GetSchematicColor();
if( schColor != COLOR4D::UNSPECIFIED )
{
effectiveNetclass->SetSchematicColor( schColor );
effectiveNetclass->SetSchematicColorParent( nc );
}
}
// Fill in any required defaults
if( addMissingDefaults( effectiveNetclass.get() ) )
constituentNetclasses.push_back( m_defaultNetClass.get() );
}
bool NET_SETTINGS::addMissingDefaults( NETCLASS* nc ) const
{
bool addedDefault = false;
if( !nc->HasClearance() )
{
addedDefault = true;
nc->SetClearance( m_defaultNetClass->GetClearance() );
nc->SetClearanceParent( m_defaultNetClass.get() );
}
if( !nc->HasTrackWidth() )
{
addedDefault = true;
nc->SetTrackWidth( m_defaultNetClass->GetTrackWidth() );
nc->SetTrackWidthParent( m_defaultNetClass.get() );
}
if( !nc->HasViaDiameter() )
{
addedDefault = true;
nc->SetViaDiameter( m_defaultNetClass->GetViaDiameter() );
nc->SetViaDiameterParent( m_defaultNetClass.get() );
}
if( !nc->HasViaDrill() )
{
addedDefault = true;
nc->SetViaDrill( m_defaultNetClass->GetViaDrill() );
nc->SetViaDrillParent( m_defaultNetClass.get() );
}
if( !nc->HasuViaDiameter() )
{
addedDefault = true;
nc->SetuViaDiameter( m_defaultNetClass->GetuViaDiameter() );
nc->SetuViaDiameterParent( m_defaultNetClass.get() );
}
if( !nc->HasuViaDrill() )
{
addedDefault = true;
nc->SetuViaDrill( m_defaultNetClass->GetuViaDrill() );
nc->SetuViaDrillParent( m_defaultNetClass.get() );
}
if( !nc->HasDiffPairWidth() )
{
addedDefault = true;
nc->SetDiffPairWidth( m_defaultNetClass->GetDiffPairWidth() );
nc->SetDiffPairWidthParent( m_defaultNetClass.get() );
}
if( !nc->HasDiffPairGap() )
{
addedDefault = true;
nc->SetDiffPairGap( m_defaultNetClass->GetDiffPairGap() );
nc->SetDiffPairGapParent( m_defaultNetClass.get() );
}
// Currently this is only on the default netclass, and not editable in the setup panel
// if( !nc->HasDiffPairViaGap() )
// {
// addedDefault = true;
// nc->SetDiffPairViaGap( m_defaultNetClass->GetDiffPairViaGap() );
// nc->SetDiffPairViaGapParent( m_defaultNetClass.get() );
// }
if( !nc->HasWireWidth() )
{
addedDefault = true;
nc->SetWireWidth( m_defaultNetClass->GetWireWidth() );
nc->SetWireWidthParent( m_defaultNetClass.get() );
}
if( !nc->HasBusWidth() )
{
addedDefault = true;
nc->SetBusWidth( m_defaultNetClass->GetBusWidth() );
nc->SetBusWidthParent( m_defaultNetClass.get() );
}
return addedDefault;
}
std::shared_ptr<NETCLASS> NET_SETTINGS::GetNetClassByName( const wxString& aNetClassName ) const
{
auto ii = m_netClasses.find( aNetClassName );
if( ii == m_netClasses.end() )
return m_defaultNetClass;
else
return ii->second;
}
static bool isSuperSubOverbar( wxChar c )
{
return c == '_' || c == '^' || c == '~';
}
bool NET_SETTINGS::ParseBusVector( const wxString& aBus, wxString* aName,
std::vector<wxString>* aMemberList )
{
auto isDigit = []( wxChar c )
{
static wxString digits( wxT( "0123456789" ) );
return digits.Contains( c );
};
size_t busLen = aBus.length();
size_t i = 0;
wxString prefix;
wxString suffix;
wxString tmp;
long begin = 0;
long end = 0;
int braceNesting = 0;
prefix.reserve( busLen );
// Parse prefix
//
for( ; i < busLen; ++i )
{
if( aBus[i] == '{' )
{
if( i > 0 && isSuperSubOverbar( aBus[i-1] ) )
braceNesting++;
else
return false;
}
else if( aBus[i] == '}' )
{
braceNesting--;
}
if( aBus[i] == ' ' || aBus[i] == ']' )
return false;
if( aBus[i] == '[' )
break;
prefix += aBus[i];
}
// Parse start number
//
i++; // '[' character
if( i >= busLen )
return false;
for( ; i < busLen; ++i )
{
if( aBus[i] == '.' && i + 1 < busLen && aBus[i+1] == '.' )
{
tmp.ToLong( &begin );
i += 2;
break;
}
if( !isDigit( aBus[i] ) )
return false;
tmp += aBus[i];
}
// Parse end number
//
tmp = wxEmptyString;
if( i >= busLen )
return false;
for( ; i < busLen; ++i )
{
if( aBus[i] == ']' )
{
tmp.ToLong( &end );
++i;
break;
}
if( !isDigit( aBus[i] ) )
return false;
tmp += aBus[i];
}
// Parse suffix
//
for( ; i < busLen; ++i )
{
if( aBus[i] == '}' )
{
braceNesting--;
suffix += aBus[i];
}
else
{
return false;
}
}
if( braceNesting != 0 )
return false;
if( begin == end )
return false;
else if( begin > end )
std::swap( begin, end );
if( aName )
*aName = prefix;
if( aMemberList )
{
for( long idx = begin; idx <= end; ++idx )
{
wxString str = prefix;
str << idx;
str << suffix;
aMemberList->emplace_back( str );
}
}
return true;
}
bool NET_SETTINGS::ParseBusGroup( const wxString& aGroup, wxString* aName,
std::vector<wxString>* aMemberList )
{
size_t groupLen = aGroup.length();
size_t i = 0;
wxString prefix;
wxString tmp;
int braceNesting = 0;
prefix.reserve( groupLen );
// Parse prefix
//
for( ; i < groupLen; ++i )
{
if( aGroup[i] == '{' )
{
if( i > 0 && isSuperSubOverbar( aGroup[i-1] ) )
braceNesting++;
else
break;
}
else if( aGroup[i] == '}' )
{
braceNesting--;
}
if( aGroup[i] == ' ' || aGroup[i] == '[' || aGroup[i] == ']' )
return false;
prefix += aGroup[i];
}
if( braceNesting != 0 )
return false;
if( aName )
*aName = prefix;
// Parse members
//
i++; // '{' character
if( i >= groupLen )
return false;
for( ; i < groupLen; ++i )
{
if( aGroup[i] == '{' )
{
if( i > 0 && isSuperSubOverbar( aGroup[i-1] ) )
braceNesting++;
else
return false;
}
else if( aGroup[i] == '}' )
{
if( braceNesting )
{
braceNesting--;
}
else
{
if( aMemberList && !tmp.IsEmpty() )
aMemberList->push_back( EscapeString( tmp, CTX_NETNAME ) );
return true;
}
}
// Commas aren't strictly legal, but we can be pretty sure what the author had in mind.
if( aGroup[i] == ' ' || aGroup[i] == ',' )
{
if( aMemberList && !tmp.IsEmpty() )
aMemberList->push_back( EscapeString( tmp, CTX_NETNAME ) );
tmp.Clear();
continue;
}
tmp += aGroup[i];
}
return false;
}
|