Make multiple names of the net on a track

Rather than moving the netname around, we want to keep the correct
number of netnames on a track such that there is always at least one
displayed on long tracks even when panning.

This reduces calculation time while keeping the netnames visible

Fixes https://gitlab.com/kicad/code/kicad/issues/12436
This commit is contained in:
Seth Hillbrand 2022-11-04 11:46:43 -07:00
parent 685185bd68
commit c1f2ca1d8d
4 changed files with 52 additions and 23 deletions

View File

@ -525,8 +525,9 @@ void PCB_EDIT_FRAME::redrawNetnames( wxTimerEvent& aEvent )
for( PCB_TRACK* track : GetBoard()->Tracks() )
{
double lod = track->ViewGetLOD( GetNetnameLayer( track->GetLayer() ), view );
double scale = view->GetScale();
if( lod != track->GetCachedLOD() )
if( lod != track->GetCachedLOD() || scale != track->GetCachedScale() )
{
if( lod < view->GetScale() )
{
@ -535,6 +536,7 @@ void PCB_EDIT_FRAME::redrawNetnames( wxTimerEvent& aEvent )
}
track->SetCachedLOD( lod );
track->SetCachedScale( scale );
}
}

View File

@ -636,42 +636,40 @@ void PCB_PAINTER::draw( const PCB_TRACK* aTrack, int aLayer )
ClipLine( &clipBox, visibleSeg.A.x, visibleSeg.A.y, visibleSeg.B.x, visibleSeg.B.y );
wxString netName = UnescapeString( aTrack->GetShortNetname() );
size_t num_char = netName.size();
// Check if the track is long enough to have a netname displayed
int seg_minlength = track_width * 6;
int seg_minlength = track_width * num_char;
if( visibleSeg.Length() < seg_minlength )
return;
const wxString& netName = UnescapeString( aTrack->GetShortNetname() );
double textSize = track_width;
double penWidth = textSize / 12.0;
VECTOR2D textPosition = ( visibleSeg.A + visibleSeg.B ) / 2.0; // center of the track
EDA_ANGLE textOrientation;
// If the last position is still on the track, and it's some reasonable distance inside
// the viewport then don't move the netname; just use the last position.
if( visibleSeg.Distance( aTrack->m_LastNetnamePosition ) < penWidth
&& clipBox.Inflate( -seg_minlength / 2 ).Contains( aTrack->m_LastNetnamePosition ) )
{
textPosition = aTrack->m_LastNetnamePosition;
}
else
{
aTrack->m_LastNetnamePosition = textPosition;
}
int num_names = 1;
if( end.y == start.y ) // horizontal
{
textOrientation = ANGLE_HORIZONTAL;
num_names = std::max( num_names,
static_cast<int>( aTrack->GetLength() / viewport.GetWidth() ) );
}
else if( end.x == start.x ) // vertical
{
textOrientation = ANGLE_VERTICAL;
num_names = std::max( num_names,
static_cast<int>( aTrack->GetLength() / viewport.GetHeight() ) );
}
else
{
textOrientation = EDA_ANGLE( visibleSeg.B - visibleSeg.A ) + ANGLE_90;
textOrientation.Normalize90();
double min_size = std::min( viewport.GetWidth(), viewport.GetHeight() );
num_names = std::max( num_names,
static_cast<int>( aTrack->GetLength() / ( M_SQRT2 * min_size ) ) );
}
m_gal->SetIsStroke( true );
@ -685,7 +683,14 @@ void PCB_PAINTER::draw( const PCB_TRACK* aTrack, int aLayer )
m_gal->SetGlyphSize( VECTOR2D( textSize * 0.55, textSize * 0.55 ) );
m_gal->SetHorizontalJustify( GR_TEXT_H_ALIGN_CENTER );
m_gal->SetVerticalJustify( GR_TEXT_V_ALIGN_CENTER );
m_gal->BitmapText( netName, textPosition, textOrientation );
for( int ii = 0; ii < num_names; ++ii )
{
VECTOR2I textPosition =
VECTOR2D( start ) * static_cast<double>( num_names - ii ) / ( num_names + 1 )
+ VECTOR2D( end ) * static_cast<double>( ii + 1 ) / ( num_names + 1 );
m_gal->BitmapText( netName, textPosition, textOrientation );
}
return;
}

View File

@ -53,6 +53,7 @@ PCB_TRACK::PCB_TRACK( BOARD_ITEM* aParent, KICAD_T idtype ) :
BOARD_CONNECTED_ITEM( aParent, idtype )
{
m_Width = pcbIUScale.mmToIU( 0.2 ); // Gives a reasonable default width
m_CachedScale = -1.0; // Set invalid to force update
m_CachedLOD = 0.0; // Set to always display
}
@ -667,6 +668,13 @@ double PCB_TRACK::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
return HIDE;
}
// Pick the approximate size of the netname (square chars)
wxString netName = UnescapeString( GetShortNetname() );
size_t num_chars = netName.size();
if( GetLength() < num_chars * GetWidth() )
return HIDE;
// When drawing netnames, clip the track to the viewport
VECTOR2I start( GetStart() );
VECTOR2I end( GetEnd() );
@ -676,10 +684,8 @@ double PCB_TRACK::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
ClipLine( &clipBox, start.x, start.y, end.x, end.y );
VECTOR2I line = ( end - start );
double length = line.EuclideanNorm();
// Check if the track is long enough to have a netname displayed
if( length < 6 * GetWidth() )
if( line.EuclideanNorm() == 0 )
return HIDE;
// Netnames will be shown only if zoom is appropriate

View File

@ -238,6 +238,24 @@ public:
m_CachedLOD = aLOD;
}
/**
* Get last used zoom scale for the track net name
* @return scale from GetScale()
*/
double GetCachedScale()
{
return m_CachedScale;
}
/**
* Set the cached scale
* @param aScale value from GetScale()
*/
void SetCachedScale( double aScale )
{
m_CachedScale = aScale;
}
struct cmp_tracks
{
bool operator()( const PCB_TRACK* aFirst, const PCB_TRACK* aSecond ) const;
@ -250,15 +268,13 @@ public:
protected:
void GetMsgPanelInfoBase_Common( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) const;
public:
mutable VECTOR2I m_LastNetnamePosition;
protected:
int m_Width; ///< Thickness of track, or via diameter
VECTOR2I m_Start; ///< Line start point
VECTOR2I m_End; ///< Line end point
double m_CachedLOD; ///< Last LOD used to draw this track's net
double m_CachedScale; ///< Last zoom scale used to draw this track's net (we want to redraw when changing zoom)
};