KiCad PCB EDA Suite
ratsnest_viewitem.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 CERN
5  * Copyright (C) 2018-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
33 
35 #include <pcb_painter.h>
36 #include <ratsnest/ratsnest_data.h>
37 
39 #include <pcb_base_frame.h>
40 
41 #include <memory>
42 #include <utility>
43 
44 #include <view/view.h>
45 
46 namespace KIGFX {
47 
48 RATSNEST_VIEWITEM::RATSNEST_VIEWITEM( std::shared_ptr<CONNECTIVITY_DATA> aData ) :
49  EDA_ITEM( NOT_USED ), m_data( std::move(aData) )
50 {
51 }
52 
53 
55 {
56  // Make it always visible
57  BOX2I bbox;
58  bbox.SetMaximum();
59 
60  return bbox;
61 }
62 
63 
64 void RATSNEST_VIEWITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
65 {
66  std::unique_lock<std::mutex> lock( m_data->GetLock(), std::try_to_lock );
67 
68  if( !lock )
69  return;
70 
71  constexpr int CROSS_SIZE = 200000;
72 
73  auto gal = aView->GetGAL();
74  gal->SetIsStroke( true );
75  gal->SetIsFill( false );
76  gal->SetLineWidth( 1.0 );
77  auto rs = static_cast<PCB_RENDER_SETTINGS*>(aView->GetPainter()->GetSettings());
78  auto color = rs->GetColor( NULL, LAYER_RATSNEST );
79 
80  std::set<int> highlightedNets = rs->GetHighlightNetCodes();
81 
82  gal->SetStrokeColor( color.Brightened(0.5) );
83 
84  const bool curved_ratsnest = rs->GetCurvedRatsnestLinesEnabled();
85 
86  // Draw the "dynamic" ratsnest (i.e. for objects that may be currently being moved)
87  for( const auto& l : m_data->GetDynamicRatsnest() )
88  {
89  if ( l.a == l.b )
90  {
91  gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y - CROSS_SIZE ),
92  VECTOR2I( l.b.x + CROSS_SIZE, l.b.y + CROSS_SIZE ) );
93  gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y + CROSS_SIZE ),
94  VECTOR2I( l.b.x + CROSS_SIZE, l.b.y - CROSS_SIZE ) );
95  }
96  else
97  {
98  if( curved_ratsnest )
99  {
100  auto dx = l.b.x - l.a.x;
101  auto dy = l.b.y - l.a.y;
102  const auto center = VECTOR2I( l.a.x + 0.5 * dx - 0.1 * dy,
103  l.a.y + 0.5 * dy + 0.1 * dx );
104  gal->DrawCurve( l.a, center, center, l.b );
105  }
106  else
107  {
108  gal->DrawLine( l.a, l.b );
109  }
110  }
111  }
112 
113  for( int i = 1 /* skip "No Net" at [0] */; i < m_data->GetNetCount(); ++i )
114  {
115  RN_NET* net = m_data->GetRatsnestForNet( i );
116 
117  if( !net )
118  continue;
119 
120  // Draw the "static" ratsnest
121  if( highlightedNets.count( i ) )
122  gal->SetStrokeColor( color.Brightened(0.8) );
123  else
124  gal->SetStrokeColor( color ); // using the default ratsnest color for not highlighted
125 
126  for( const auto& edge : net->GetUnconnected() )
127  {
128  //if ( !edge.IsVisible() )
129  // continue;
130 
131  const auto& sourceNode = edge.GetSourceNode();
132  const auto& targetNode = edge.GetTargetNode();
133  const VECTOR2I source( sourceNode->Pos() );
134  const VECTOR2I target( targetNode->Pos() );
135 
136  if( !sourceNode->Valid() || !targetNode->Valid() )
137  continue;
138 
139  bool enable = !sourceNode->GetNoLine() && !targetNode->GetNoLine();
140  bool show;
141 
142  // If the global ratsnest is currently enabled, the local ratsnest
143  // should be easy to turn off, so either element can disable it
144  // If the global ratsnest is disabled, the local ratsnest should be easy to turn on
145  // so either element can enable it.
146  if( rs->GetGlobalRatsnestLinesEnabled() )
147  show = sourceNode->Parent()->GetLocalRatsnestVisible() &&
148  targetNode->Parent()->GetLocalRatsnestVisible();
149  else
150  show = sourceNode->Parent()->GetLocalRatsnestVisible() ||
151  targetNode->Parent()->GetLocalRatsnestVisible();
152 
153  if ( enable && show )
154  {
155  if ( source == target )
156  {
157  gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y - CROSS_SIZE ),
158  VECTOR2I( source.x + CROSS_SIZE, source.y + CROSS_SIZE ) );
159  gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y + CROSS_SIZE ),
160  VECTOR2I( source.x + CROSS_SIZE, source.y - CROSS_SIZE ) );
161  }
162  else
163  {
164  if( curved_ratsnest )
165  {
166  auto dx = target.x - source.x;
167  auto dy = target.y - source.y;
168  const auto center = VECTOR2I( source.x + 0.5 * dx - 0.1 * dy,
169  source.y + 0.5 * dy + 0.1 * dx );
170  gal->DrawCurve( source, center, center, target );
171  }
172  else
173  {
174  gal->DrawLine( source, target );
175  }
176  }
177  }
178  }
179  }
180 }
181 
182 
183 void RATSNEST_VIEWITEM::ViewGetLayers( int aLayers[], int& aCount ) const
184 {
185  aCount = 1;
186  aLayers[0] = LAYER_RATSNEST;
187 }
188 
189 }
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers.
Class that draws missing connections on a PCB.
const std::vector< CN_EDGE > GetUnconnected() const
Function GetUnconnected() Returns pointer to a vector of edges that makes ratsnest for a given net.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
RATSNEST_VIEWITEM(std::shared_ptr< CONNECTIVITY_DATA > aData)
std::shared_ptr< CONNECTIVITY_DATA > m_data
Object containing ratsnest data.
Class that computes missing connections on a PCB.
the 3d code uses this value
Definition: typeinfo.h:80
int color
Definition: DXF_plotter.cpp:61
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
Classes used in Pcbnew, CvPcb and GerbView.
#define NULL
void SetMaximum()
Definition: box2.h:72
virtual RENDER_SETTINGS * GetSettings()=0
Function GetAdapter Returns pointer to current settings that are going to be used when drawing items.
void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers() Returns the all the layers within the VIEW the object is painted on.
Board layer functions and definitions.
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Function ViewDraw() Draws the parts of the object belonging to layer aLayer.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
RN_NET Describes ratsnest for a single net.
Definition: ratsnest_data.h:61
VIEW.
Definition: view.h:61
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.