2013-11-25 15:50:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2023-04-16 13:36:34 +00:00
|
|
|
* Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-11-28 14:58:41 +00:00
|
|
|
*
|
2013-11-25 15:50:03 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Class that draws missing connections on a PCB.
|
|
|
|
*/
|
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
#include <ratsnest/ratsnest_view_item.h>
|
2020-06-16 18:15:14 +00:00
|
|
|
|
2021-09-07 20:34:10 +00:00
|
|
|
#include <kiface_base.h>
|
|
|
|
#include <pcbnew_settings.h>
|
2014-01-31 10:16:01 +00:00
|
|
|
#include <pcb_painter.h>
|
2020-06-16 18:15:14 +00:00
|
|
|
#include <ratsnest/ratsnest_data.h>
|
2021-07-29 09:47:43 +00:00
|
|
|
#include <layer_ids.h>
|
2018-04-30 07:52:49 +00:00
|
|
|
#include <pcb_base_frame.h>
|
2021-09-07 20:34:10 +00:00
|
|
|
#include <view/view.h>
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
#include <memory>
|
2017-09-23 09:20:10 +00:00
|
|
|
#include <utility>
|
2017-03-22 13:43:10 +00:00
|
|
|
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
RATSNEST_VIEW_ITEM::RATSNEST_VIEW_ITEM( std::shared_ptr<CONNECTIVITY_DATA> aData ) :
|
2023-04-16 13:36:34 +00:00
|
|
|
EDA_ITEM( NOT_USED ),
|
|
|
|
m_data( std::move( aData ) )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
const BOX2I RATSNEST_VIEW_ITEM::ViewBBox() const
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
|
|
|
// Make it always visible
|
|
|
|
BOX2I bbox;
|
|
|
|
bbox.SetMaximum();
|
|
|
|
|
|
|
|
return bbox;
|
|
|
|
}
|
|
|
|
|
2018-11-28 14:58:41 +00:00
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
void RATSNEST_VIEW_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
Fix issues with zone filling connectivity locking
Two issues found with the locking system used to prevent access to
stale connectivity data during the zone fill process:
1) a std::mutex has undefined behavior if you try to use it to guard
against access from the same thread. Because of the use of wx event
loops (and coroutines) it is entirely possible, and in some situations
inevitable, that the same thread will try to redraw the ratsnest in the
middle of zone refilling.
2) The mutex was only guarding the ZONE_FILLER::Fill method, but the callers
of that method also do connectivity updates as part of the COMMIT::Push.
Redrawing the ratsnest after the Fill but before the Push will result in
stale connectivity pointers to zone filled areas.
Fixed (1) by switching to a trivial spinlock implementation. Spinlocks would
generally not be desirable if the contention for the connectivity data crossed
thread boundaries, but at the moment I believe it's guaranteed that the reads
and writes to connectivity that are guarded by this lock happen from the main
UI thread. The writes are also quite rare compared to reads, and reads are
generally fast, so I'm not really worried about the UI thread spinning for any
real amount of time.
Fixed (2) by moving the locking location up to the call sites of
ZONE_FILLER::Fill.
This issue was quite difficult to reproduce, but I found a fairly reliable way:
It only happens (for me) on Windows, MSYS2 build, with wxWidgets 3.0
It also only happens if I restrict PcbNew to use 2 CPU cores.
With those conditions, I can reproduce the issue described in #6471 by
repeatedly editing a zone properties and changing its net. The crash is
especially easy to trigger if you press some keys (such as 'e' for edit)
while the progress dialog is displayed. It's easiest to do this in a debug
build as the slower KiCad is running, the bigger the window is to trigger this
bug.
Fixes https://gitlab.com/kicad/code/kicad/-/issues/6471
Fixes https://gitlab.com/kicad/code/kicad/-/issues/7048
2021-01-18 17:24:07 +00:00
|
|
|
std::unique_lock<KISPINLOCK> lock( m_data->GetLock(), std::try_to_lock );
|
2018-12-20 05:15:53 +00:00
|
|
|
|
|
|
|
if( !lock )
|
2017-12-14 22:49:46 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-28 16:30:49 +00:00
|
|
|
constexpr int CROSS_SIZE = 200000;
|
2017-03-22 13:43:10 +00:00
|
|
|
|
2023-07-08 21:46:41 +00:00
|
|
|
PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
|
2022-02-23 23:10:35 +00:00
|
|
|
|
|
|
|
if( !cfg )
|
|
|
|
return;
|
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
auto rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( aView->GetPainter()->GetSettings() );
|
2023-05-20 23:06:51 +00:00
|
|
|
KIGFX::GAL* gal = aView->GetGAL();
|
|
|
|
gal->SetIsStroke( true );
|
|
|
|
gal->SetIsFill( false );
|
|
|
|
gal->SetLineWidth( cfg->m_Display.m_RatsnestThickness / gal->GetWorldScale() );
|
2020-07-08 02:21:45 +00:00
|
|
|
|
2020-07-09 01:59:34 +00:00
|
|
|
std::set<int> highlightedNets = rs->GetHighlightNetCodes();
|
|
|
|
const std::set<int>& hiddenNets = rs->GetHiddenNets();
|
2014-01-29 14:24:19 +00:00
|
|
|
|
2022-12-07 18:01:49 +00:00
|
|
|
COLOR4D defaultColor = rs->GetColor( nullptr, LAYER_RATSNEST );
|
|
|
|
COLOR4D color = defaultColor;
|
|
|
|
const bool colorByNet = rs->GetNetColorMode() != NET_COLOR_MODE::OFF;
|
|
|
|
const bool dimStatic = m_data->GetLocalRatsnest().size() > 0 || highlightedNets.size() > 0;
|
|
|
|
|
2021-09-07 20:34:10 +00:00
|
|
|
std::map<int, KIGFX::COLOR4D>& netColors = rs->GetNetColorMap();
|
|
|
|
std::map<wxString, KIGFX::COLOR4D>& ncColors = rs->GetNetclassColorMap();
|
|
|
|
const std::map<int, wxString>& ncMap = m_data->GetNetclassMap();
|
2014-01-31 10:16:01 +00:00
|
|
|
|
2021-09-07 20:34:10 +00:00
|
|
|
const bool onlyVisibleLayers = cfg->m_Display.m_RatsnestMode == RATSNEST_MODE::VISIBLE;
|
|
|
|
LSET visibleLayers;
|
2020-09-07 20:43:31 +00:00
|
|
|
|
2021-02-24 01:07:19 +00:00
|
|
|
// If we are in "other layers off" mode, the active layer is the only visible layer
|
2021-12-26 00:36:12 +00:00
|
|
|
if( rs->m_ContrastModeDisplay == HIGH_CONTRAST_MODE::HIDDEN )
|
2021-02-24 01:07:19 +00:00
|
|
|
{
|
|
|
|
visibleLayers.set( rs->GetPrimaryHighContrastLayer() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( PCB_LAYER_ID layer : LSET::AllCuMask().Seq() )
|
2021-09-07 20:34:10 +00:00
|
|
|
{
|
2021-02-24 01:07:19 +00:00
|
|
|
if( aView->IsLayerVisible( layer ) )
|
|
|
|
visibleLayers.set( layer );
|
2021-09-07 20:34:10 +00:00
|
|
|
}
|
2021-02-24 01:07:19 +00:00
|
|
|
}
|
2020-09-07 20:43:31 +00:00
|
|
|
|
2021-09-07 20:34:10 +00:00
|
|
|
const bool curved_ratsnest = cfg->m_Display.m_DisplayRatsnestLinesCurved;
|
2018-04-30 07:52:49 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
// Draw the "dynamic" ratsnest (i.e. for objects that may be currently being moved)
|
2022-09-03 18:29:02 +00:00
|
|
|
for( const RN_DYNAMIC_LINE& l : m_data->GetLocalRatsnest() )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
2020-07-09 01:59:34 +00:00
|
|
|
if( hiddenNets.count( l.netCode ) )
|
|
|
|
continue;
|
|
|
|
|
2020-07-11 01:37:53 +00:00
|
|
|
if( colorByNet && netColors.count( l.netCode ) )
|
|
|
|
color = netColors.at( l.netCode );
|
2021-09-07 20:34:10 +00:00
|
|
|
else if( colorByNet && ncMap.count( l.netCode ) && ncColors.count( ncMap.at( l.netCode ) ) )
|
|
|
|
color = ncColors.at( ncMap.at( l.netCode ) );
|
2020-07-11 01:37:53 +00:00
|
|
|
else
|
|
|
|
color = defaultColor;
|
|
|
|
|
2020-07-11 17:42:00 +00:00
|
|
|
if( color == COLOR4D::UNSPECIFIED )
|
|
|
|
color = defaultColor;
|
|
|
|
|
2022-12-07 18:01:49 +00:00
|
|
|
gal->SetStrokeColor( color.Brightened( 0.5 ).WithAlpha( std::min( 1.0, color.a + 0.3 ) ) );
|
2020-07-11 01:37:53 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
if( l.a == l.b )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2018-11-28 14:58:41 +00:00
|
|
|
gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y - CROSS_SIZE ),
|
|
|
|
VECTOR2I( l.b.x + CROSS_SIZE, l.b.y + CROSS_SIZE ) );
|
|
|
|
gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y + CROSS_SIZE ),
|
|
|
|
VECTOR2I( l.b.x + CROSS_SIZE, l.b.y - CROSS_SIZE ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-15 15:16:38 +00:00
|
|
|
if( curved_ratsnest )
|
|
|
|
{
|
2021-09-07 20:34:10 +00:00
|
|
|
int dx = l.b.x - l.a.x;
|
|
|
|
int dy = l.b.y - l.a.y;
|
|
|
|
const VECTOR2I center = VECTOR2I( l.a.x + 0.5 * dx - 0.1 * dy,
|
|
|
|
l.a.y + 0.5 * dy + 0.1 * dx );
|
2019-06-15 15:16:38 +00:00
|
|
|
gal->DrawCurve( l.a, center, center, l.b );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gal->DrawLine( l.a, l.b );
|
|
|
|
}
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2018-09-21 12:41:28 +00:00
|
|
|
for( int i = 1 /* skip "No Net" at [0] */; i < m_data->GetNetCount(); ++i )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
2020-07-09 01:59:34 +00:00
|
|
|
if( hiddenNets.count( i ) )
|
|
|
|
continue;
|
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
RN_NET* net = m_data->GetRatsnestForNet( i );
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2023-03-13 18:29:14 +00:00
|
|
|
if( !net || m_data->GetConnectivityAlgo()->IsNetDirty( i ) )
|
2017-06-29 22:45:50 +00:00
|
|
|
continue;
|
|
|
|
|
2020-07-11 01:37:53 +00:00
|
|
|
if( colorByNet && netColors.count( i ) )
|
|
|
|
color = netColors.at( i );
|
2021-09-07 20:34:10 +00:00
|
|
|
else if( colorByNet && ncMap.count( i ) && ncColors.count( ncMap.at( i ) ) )
|
|
|
|
color = ncColors.at( ncMap.at( i ) );
|
2020-07-11 01:37:53 +00:00
|
|
|
else
|
|
|
|
color = defaultColor;
|
2020-07-08 02:21:45 +00:00
|
|
|
|
2020-07-11 17:42:00 +00:00
|
|
|
if( color == COLOR4D::UNSPECIFIED )
|
|
|
|
color = defaultColor;
|
|
|
|
|
2022-12-07 18:01:49 +00:00
|
|
|
if( dimStatic )
|
|
|
|
color = color.WithAlpha( std::max( 0.0, color.a / 2 ) );
|
|
|
|
|
2013-11-25 15:50:03 +00:00
|
|
|
// Draw the "static" ratsnest
|
2020-05-24 17:30:23 +00:00
|
|
|
if( highlightedNets.count( i ) )
|
2022-12-07 18:01:49 +00:00
|
|
|
gal->SetStrokeColor(
|
|
|
|
color.Brightened( 0.8 ).WithAlpha( std::min( 1.0, color.a + 0.4 ) ) );
|
2020-05-24 17:30:23 +00:00
|
|
|
else
|
|
|
|
gal->SetStrokeColor( color ); // using the default ratsnest color for not highlighted
|
2014-01-31 10:16:01 +00:00
|
|
|
|
2021-12-01 14:42:44 +00:00
|
|
|
for( const CN_EDGE& edge : net->GetEdges() )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2021-12-01 14:42:44 +00:00
|
|
|
if( !edge.IsVisible() )
|
|
|
|
continue;
|
2017-03-28 16:30:49 +00:00
|
|
|
|
2022-11-22 12:19:22 +00:00
|
|
|
const std::shared_ptr<const CN_ANCHOR>& sourceNode = edge.GetSourceNode();
|
|
|
|
const std::shared_ptr<const CN_ANCHOR>& targetNode = edge.GetTargetNode();
|
|
|
|
const VECTOR2I source( sourceNode->Pos() );
|
|
|
|
const VECTOR2I target( targetNode->Pos() );
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2018-06-08 06:15:10 +00:00
|
|
|
if( !sourceNode->Valid() || !targetNode->Valid() )
|
|
|
|
continue;
|
|
|
|
|
2017-03-28 16:30:49 +00:00
|
|
|
bool enable = !sourceNode->GetNoLine() && !targetNode->GetNoLine();
|
2018-11-19 03:54:01 +00:00
|
|
|
bool show;
|
|
|
|
|
2021-09-07 20:34:10 +00:00
|
|
|
// If the global ratsnest is currently enabled, the local ratsnest should be easy to
|
|
|
|
// turn off, so either element can disable it.
|
2019-06-13 00:32:47 +00:00
|
|
|
// If the global ratsnest is disabled, the local ratsnest should be easy to turn on
|
2018-11-19 03:54:01 +00:00
|
|
|
// so either element can enable it.
|
2021-09-07 20:34:10 +00:00
|
|
|
if( cfg->m_Display.m_ShowGlobalRatsnest )
|
|
|
|
{
|
2018-11-19 03:54:01 +00:00
|
|
|
show = sourceNode->Parent()->GetLocalRatsnestVisible() &&
|
|
|
|
targetNode->Parent()->GetLocalRatsnestVisible();
|
2021-09-07 20:34:10 +00:00
|
|
|
}
|
2018-11-19 03:54:01 +00:00
|
|
|
else
|
2021-09-07 20:34:10 +00:00
|
|
|
{
|
2018-11-19 03:54:01 +00:00
|
|
|
show = sourceNode->Parent()->GetLocalRatsnestVisible() ||
|
|
|
|
targetNode->Parent()->GetLocalRatsnestVisible();
|
2021-09-07 20:34:10 +00:00
|
|
|
}
|
2017-03-28 16:30:49 +00:00
|
|
|
|
2020-09-07 20:43:31 +00:00
|
|
|
if( onlyVisibleLayers && show )
|
|
|
|
{
|
|
|
|
LSET sourceLayers = sourceNode->Parent()->GetLayerSet();
|
|
|
|
LSET targetLayers = targetNode->Parent()->GetLayerSet();
|
|
|
|
|
|
|
|
if( !( sourceLayers & visibleLayers ).any() ||
|
|
|
|
!( targetLayers & visibleLayers ).any() )
|
2021-09-07 20:34:10 +00:00
|
|
|
{
|
2020-09-07 20:43:31 +00:00
|
|
|
show = false;
|
2021-09-07 20:34:10 +00:00
|
|
|
}
|
2020-09-07 20:43:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 16:30:49 +00:00
|
|
|
if ( enable && show )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
|
|
|
if ( source == target )
|
|
|
|
{
|
2018-11-28 14:58:41 +00:00
|
|
|
gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y - CROSS_SIZE ),
|
|
|
|
VECTOR2I( source.x + CROSS_SIZE, source.y + CROSS_SIZE ) );
|
|
|
|
gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y + CROSS_SIZE ),
|
|
|
|
VECTOR2I( source.x + CROSS_SIZE, source.y - CROSS_SIZE ) );
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-15 15:16:38 +00:00
|
|
|
if( curved_ratsnest )
|
|
|
|
{
|
2021-09-07 20:34:10 +00:00
|
|
|
int dx = target.x - source.x;
|
|
|
|
int dy = target.y - source.y;
|
|
|
|
const VECTOR2I center = VECTOR2I( source.x + 0.5 * dx - 0.1 * dy,
|
|
|
|
source.y + 0.5 * dy + 0.1 * dx );
|
2018-04-30 07:52:49 +00:00
|
|
|
gal->DrawCurve( source, center, center, target );
|
2019-06-15 15:16:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-30 07:52:49 +00:00
|
|
|
gal->DrawLine( source, target );
|
|
|
|
}
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
void RATSNEST_VIEW_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
|
|
|
aCount = 1;
|
2017-03-13 03:19:33 +00:00
|
|
|
aLayers[0] = LAYER_RATSNEST;
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
2016-05-28 16:57:19 +00:00
|
|
|
|