Fix typo initializing buswidth with linewidth value.

Also reduces line-wrapping a bit.

I don't think this fixes the linked bug, but I can't reproduce it
and this was found while reviewing the code.

Fixes https://gitlab.com/kicad/code/kicad/issues/8810
This commit is contained in:
Jeff Young 2021-08-01 19:32:41 +01:00
parent 0d539a84a2
commit e636fb32cf
3 changed files with 85 additions and 90 deletions

View File

@ -31,15 +31,15 @@
const char NETCLASS::Default[] = "Default"; const char NETCLASS::Default[] = "Default";
// Initial values for netclass initialization // Initial values for netclass initialization
const int DEFAULT_CLEARANCE = PcbMillimeter2iu( 0.2 ); // track to track and track to pads clearance const int DEFAULT_CLEARANCE = PcbMm2iu( 0.2 ); // track to track and track to pads clearance
const int DEFAULT_VIA_DIAMETER = PcbMillimeter2iu( 0.8 ); const int DEFAULT_VIA_DIAMETER = PcbMm2iu( 0.8 );
const int DEFAULT_VIA_DRILL = PcbMillimeter2iu( 0.4 ); const int DEFAULT_VIA_DRILL = PcbMm2iu( 0.4 );
const int DEFAULT_UVIA_DIAMETER = PcbMillimeter2iu( 0.3 ); const int DEFAULT_UVIA_DIAMETER = PcbMm2iu( 0.3 );
const int DEFAULT_UVIA_DRILL = PcbMillimeter2iu( 0.1 ); const int DEFAULT_UVIA_DRILL = PcbMm2iu( 0.1 );
const int DEFAULT_TRACK_WIDTH = PcbMillimeter2iu( 0.25 ); const int DEFAULT_TRACK_WIDTH = PcbMm2iu( 0.25 );
const int DEFAULT_DIFF_PAIR_WIDTH = PcbMillimeter2iu( 0.2 ); const int DEFAULT_DIFF_PAIR_WIDTH = PcbMm2iu( 0.2 );
const int DEFAULT_DIFF_PAIR_GAP = PcbMillimeter2iu( 0.25 ); const int DEFAULT_DIFF_PAIR_GAP = PcbMm2iu( 0.25 );
const int DEFAULT_DIFF_PAIR_VIAGAP = PcbMillimeter2iu( 0.25 ); const int DEFAULT_DIFF_PAIR_VIAGAP = PcbMm2iu( 0.25 );
const int DEFAULT_WIRE_WIDTH = SchMils2iu( 6 ); const int DEFAULT_WIRE_WIDTH = SchMils2iu( 6 );
const int DEFAULT_BUS_WIDTH = SchMils2iu( 12 ); const int DEFAULT_BUS_WIDTH = SchMils2iu( 12 );

View File

@ -38,7 +38,16 @@ static OPT<int> getInPcbUnits( const nlohmann::json& aObj, const std::string& aK
OPT<int> aDefault = OPT<int>() ) OPT<int> aDefault = OPT<int>() )
{ {
if( aObj.contains( aKey ) && aObj[aKey].is_number() ) if( aObj.contains( aKey ) && aObj[aKey].is_number() )
return PcbMillimeter2iu( aObj[aKey].get<double>() ); return PcbMm2iu( aObj[aKey].get<double>() );
else
return aDefault;
};
static int getInSchUnits( const nlohmann::json& aObj, const std::string& aKey, int aDefault )
{
if( aObj.contains( aKey ) && aObj[aKey].is_number() )
return SchMils2iu( aObj[aKey].get<double>() );
else else
return aDefault; return aDefault;
}; };
@ -53,79 +62,75 @@ NET_SETTINGS::NET_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
{ {
nlohmann::json ret = nlohmann::json::array(); nlohmann::json ret = nlohmann::json::array();
NETCLASSPTR netclass = m_NetClasses.GetDefault(); NETCLASSPTR nc = m_NetClasses.GetDefault();
NETCLASSES::const_iterator nc = m_NetClasses.begin(); NETCLASSES::const_iterator nc_ii = m_NetClasses.begin();
for( unsigned int idx = 0; idx <= m_NetClasses.GetCount(); idx++ ) for( unsigned int idx = 0; idx <= m_NetClasses.GetCount(); idx++ )
{ {
if( idx > 0 ) if( idx > 0 )
{ {
netclass = nc->second; nc = nc_ii->second;
++nc; ++nc_ii;
} }
// Note: we're in common/, but we do happen to know which of these fields // Note: we're in common/, but we do happen to know which of these fields
// are used in which units system. // are used in which units system.
nlohmann::json netclassJson = { nlohmann::json nc_json = {
{ "name", netclass->GetName().ToUTF8() }, { "name", nc->GetName().ToUTF8() },
{ "wire_width", SchIu2Mils( netclass->GetWireWidth() ) }, { "wire_width", SchIu2Mils( nc->GetWireWidth() ) },
{ "bus_width", SchIu2Mils( netclass->GetBusWidth() ) }, { "bus_width", SchIu2Mils( nc->GetBusWidth() ) },
{ "line_style", netclass->GetLineStyle() }, { "line_style", nc->GetLineStyle() },
{ "schematic_color", netclass->GetSchematicColor() }, { "schematic_color", nc->GetSchematicColor() },
{ "pcb_color", netclass->GetPcbColor() } { "pcb_color", nc->GetPcbColor() }
}; };
auto saveInPcbUnits =
[]( nlohmann::json& json, const std::string& aKey, int aValue )
{
json.push_back( { aKey, PcbIu2mm( aValue ) } );
};
if( netclass->HasClearance() ) if( nc->HasClearance() )
netclassJson.push_back( { "clearance", saveInPcbUnits( nc_json, "clearance", nc->GetClearance() );
PcbIu2Millimeter( netclass->GetClearance() ) } );
if( netclass->HasTrackWidth() ) if( nc->HasTrackWidth() )
netclassJson.push_back( { "track_width", saveInPcbUnits( nc_json, "track_width", nc->GetTrackWidth() );
PcbIu2Millimeter( netclass->GetTrackWidth() ) } );
if( netclass->HasViaDiameter() ) if( nc->HasViaDiameter() )
netclassJson.push_back( { "via_diameter", saveInPcbUnits( nc_json, "via_diameter", nc->GetViaDiameter() );
PcbIu2Millimeter( netclass->GetViaDiameter() ) } );
if( netclass->HasViaDrill() ) if( nc->HasViaDrill() )
netclassJson.push_back( { "via_drill", saveInPcbUnits( nc_json, "via_drill", nc->GetViaDrill() );
PcbIu2Millimeter( netclass->GetViaDrill() ) } );
if( netclass->HasuViaDiameter() ) if( nc->HasuViaDiameter() )
netclassJson.push_back( { "microvia_diameter", saveInPcbUnits( nc_json, "microvia_diameter", nc->GetuViaDiameter() );
PcbIu2Millimeter( netclass->GetuViaDiameter() ) } );
if( netclass->HasuViaDrill() ) if( nc->HasuViaDrill() )
netclassJson.push_back( { "microvia_drill", saveInPcbUnits( nc_json, "microvia_drill", nc->GetuViaDrill() );
PcbIu2Millimeter( netclass->GetuViaDrill() ) } );
if( netclass->HasDiffPairWidth() ) if( nc->HasDiffPairWidth() )
netclassJson.push_back( { "diff_pair_width", saveInPcbUnits( nc_json, "diff_pair_width", nc->GetDiffPairWidth() );
PcbIu2Millimeter( netclass->GetDiffPairWidth() ) } );
if( netclass->HasDiffPairGap() ) if( nc->HasDiffPairGap() )
netclassJson.push_back( { "diff_pair_gap", saveInPcbUnits( nc_json, "diff_pair_gap", nc->GetDiffPairGap() );
PcbIu2Millimeter( netclass->GetDiffPairGap() ) } );
if( netclass->HasDiffPairViaGap() ) if( nc->HasDiffPairViaGap() )
netclassJson.push_back( { "diff_pair_via_gap", saveInPcbUnits( nc_json, "diff_pair_via_gap", nc->GetDiffPairViaGap() );
PcbIu2Millimeter( netclass->GetDiffPairViaGap() ) } );
if( idx > 0 ) // No need to store members of Default netclass if( idx > 0 ) // No need to store members of Default nc
{ {
nlohmann::json membersJson = nlohmann::json::array(); nlohmann::json membersJson = nlohmann::json::array();
for( const wxString& member : *netclass ) for( const wxString& member : *nc )
{ {
if( !member.empty() ) if( !member.empty() )
membersJson.push_back( member ); membersJson.push_back( member );
} }
netclassJson["nets"] = membersJson; nc_json["nets"] = membersJson;
} }
ret.push_back( netclassJson ); ret.push_back( nc_json );
} }
return ret; return ret;
@ -137,18 +142,9 @@ NET_SETTINGS::NET_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
m_NetClasses.Clear(); m_NetClasses.Clear();
m_NetClassAssignments.clear(); m_NetClassAssignments.clear();
NETCLASSPTR netclass; NETCLASSPTR nc;
NETCLASSPTR defaultClass = m_NetClasses.GetDefault(); NETCLASSPTR defaultClass = m_NetClasses.GetDefault();
auto getInSchematicUnits =
[]( const nlohmann::json& aObj, const std::string& aKey, int aDefault )
{
if( aObj.contains( aKey ) && aObj[aKey].is_number() )
return SchMils2iu( aObj[aKey].get<double>() );
else
return aDefault;
};
for( const nlohmann::json& entry : aJson ) for( const nlohmann::json& entry : aJson )
{ {
if( !entry.is_object() || !entry.contains( "name" ) ) if( !entry.is_object() || !entry.contains( "name" ) )
@ -157,64 +153,63 @@ NET_SETTINGS::NET_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
wxString name = entry["name"]; wxString name = entry["name"];
if( name == defaultClass->GetName() ) if( name == defaultClass->GetName() )
netclass = defaultClass; nc = defaultClass;
else else
netclass = std::make_shared<NETCLASS>( name ); nc = std::make_shared<NETCLASS>( name );
if( auto value = getInPcbUnits( entry, "clearance" ) ) if( auto value = getInPcbUnits( entry, "clearance" ) )
netclass->SetClearance( *value ); nc->SetClearance( *value );
if( auto value = getInPcbUnits( entry, "track_width" ) ) if( auto value = getInPcbUnits( entry, "track_width" ) )
netclass->SetTrackWidth( *value ); nc->SetTrackWidth( *value );
if( auto value = getInPcbUnits( entry, "via_diameter" ) ) if( auto value = getInPcbUnits( entry, "via_diameter" ) )
netclass->SetViaDiameter( *value ); nc->SetViaDiameter( *value );
if( auto value = getInPcbUnits( entry, "via_drill" ) ) if( auto value = getInPcbUnits( entry, "via_drill" ) )
netclass->SetViaDrill( *value ); nc->SetViaDrill( *value );
if( auto value = getInPcbUnits( entry, "microvia_diameter" ) ) if( auto value = getInPcbUnits( entry, "microvia_diameter" ) )
netclass->SetuViaDiameter( *value ); nc->SetuViaDiameter( *value );
if( auto value = getInPcbUnits( entry, "microvia_drill" ) ) if( auto value = getInPcbUnits( entry, "microvia_drill" ) )
netclass->SetuViaDrill( *value ); nc->SetuViaDrill( *value );
if( auto value = getInPcbUnits( entry, "diff_pair_width" ) ) if( auto value = getInPcbUnits( entry, "diff_pair_width" ) )
netclass->SetDiffPairWidth( *value ); nc->SetDiffPairWidth( *value );
if( auto value = getInPcbUnits( entry, "diff_pair_gap" ) ) if( auto value = getInPcbUnits( entry, "diff_pair_gap" ) )
netclass->SetDiffPairGap( *value ); nc->SetDiffPairGap( *value );
if( auto value = getInPcbUnits( entry, "diff_pair_via_gap" ) ) if( auto value = getInPcbUnits( entry, "diff_pair_via_gap" ) )
netclass->SetDiffPairViaGap( *value ); nc->SetDiffPairViaGap( *value );
netclass->SetWireWidth( getInSchematicUnits( entry, "wire_width", nc->SetWireWidth( getInSchUnits( entry, "wire_width", nc->GetWireWidth() ) );
netclass->GetWireWidth() ) ); nc->SetBusWidth( getInSchUnits( entry, "bus_width", nc->GetBusWidth() ) );
netclass->SetBusWidth( getInSchematicUnits( entry, "bus_width",
netclass->GetWireWidth() ) );
if( entry.contains( "line_style" ) && entry["line_style"].is_number() ) if( entry.contains( "line_style" ) && entry["line_style"].is_number() )
netclass->SetLineStyle( entry["line_style"].get<int>() ); nc->SetLineStyle( entry["line_style"].get<int>() );
if( entry.contains( "nets" ) && entry["nets"].is_array() ) if( entry.contains( "nets" ) && entry["nets"].is_array() )
{ {
for( const auto& net : entry["nets"].items() ) for( const auto& net : entry["nets"].items() )
netclass->Add( net.value().get<wxString>() ); nc->Add( net.value().get<wxString>() );
} }
if( entry.contains( "pcb_color" ) && entry["pcb_color"].is_string() ) if( entry.contains( "pcb_color" ) && entry["pcb_color"].is_string() )
netclass->SetPcbColor( entry["pcb_color"].get<KIGFX::COLOR4D>() ); nc->SetPcbColor( entry["pcb_color"].get<KIGFX::COLOR4D>() );
if( entry.contains( "schematic_color" ) if( entry.contains( "schematic_color" )
&& entry["schematic_color"].is_string() ) && entry["schematic_color"].is_string() )
netclass->SetSchematicColor( {
entry["schematic_color"].get<KIGFX::COLOR4D>() ); nc->SetSchematicColor( entry["schematic_color"].get<KIGFX::COLOR4D>() );
}
if( netclass != defaultClass ) if( nc != defaultClass )
m_NetClasses.Add( netclass ); m_NetClasses.Add( nc );
for( const wxString& net : *netclass ) for( const wxString& net : *nc )
m_NetClassAssignments[ net ] = netclass->GetName(); m_NetClassAssignments[ net ] = nc->GetName();
} }
ResolveNetClassAssignments(); ResolveNetClassAssignments();

View File

@ -152,11 +152,11 @@ constexpr inline double SchIu2Mils( int iu )
return iu / SCH_IU_PER_MILS; return iu / SCH_IU_PER_MILS;
} }
constexpr inline int PcbMillimeter2iu( double mm ) constexpr inline int PcbMm2iu( double mm )
{ {
return (int) ( mm < 0 ? mm * PCB_IU_PER_MM - 0.5 : mm * PCB_IU_PER_MM + 0.5 ); return (int) ( mm < 0 ? mm * PCB_IU_PER_MM - 0.5 : mm * PCB_IU_PER_MM + 0.5 );
} }
constexpr inline double PcbIu2Millimeter( int iu ) constexpr inline double PcbIu2mm( int iu )
{ {
return iu / PCB_IU_PER_MM; return iu / PCB_IU_PER_MM;
} }