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 
32 #include <ratsnest_viewitem.h>
33 #include <ratsnest_data.h>
35 #include <pcb_painter.h>
37 #include <pcb_base_frame.h>
38 
39 #include <memory>
40 #include <utility>
41 
42 #include <view/view.h>
43 
44 namespace KIGFX {
45 
46 RATSNEST_VIEWITEM::RATSNEST_VIEWITEM( std::shared_ptr<CONNECTIVITY_DATA> aData ) :
47  EDA_ITEM( NOT_USED ), m_data( std::move(aData) )
48 {
49 }
50 
51 
53 {
54  // Make it always visible
55  BOX2I bbox;
56  bbox.SetMaximum();
57 
58  return bbox;
59 }
60 
61 
62 void RATSNEST_VIEWITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
63 {
64  std::unique_lock<std::mutex> lock( m_data->GetLock(), std::try_to_lock );
65 
66  if( !lock )
67  return;
68 
69  constexpr int CROSS_SIZE = 200000;
70 
71  auto gal = aView->GetGAL();
72  gal->SetIsStroke( true );
73  gal->SetIsFill( false );
74  gal->SetLineWidth( 1.0 );
75  auto rs = static_cast<PCB_RENDER_SETTINGS*>(aView->GetPainter()->GetSettings());
76  auto color = rs->GetColor( NULL, LAYER_RATSNEST );
77 
78  int highlightedNet = rs->GetHighlightNetCode();
79 
80  gal->SetStrokeColor( color.Brightened(0.5) );
81 
82  const bool curved_ratsnest = rs->GetCurvedRatsnestLinesEnabled();
83 
84  // Draw the "dynamic" ratsnest (i.e. for objects that may be currently being moved)
85  for( const auto& l : m_data->GetDynamicRatsnest() )
86  {
87  if ( l.a == l.b )
88  {
89  gal->DrawLine( VECTOR2I( l.a.x - CROSS_SIZE, l.a.y - CROSS_SIZE ),
90  VECTOR2I( l.b.x + CROSS_SIZE, l.b.y + CROSS_SIZE ) );
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  }
94  else
95  {
96  if( curved_ratsnest )
97  {
98  auto dx = l.b.x - l.a.x;
99  auto dy = l.b.y - l.a.y;
100  const auto center = VECTOR2I( l.a.x + 0.5 * dx - 0.1 * dy,
101  l.a.y + 0.5 * dy + 0.1 * dx );
102  gal->DrawCurve( l.a, center, center, l.b );
103  }
104  else
105  {
106  gal->DrawLine( l.a, l.b );
107  }
108  }
109  }
110 
111  for( int i = 1 /* skip "No Net" at [0] */; i < m_data->GetNetCount(); ++i )
112  {
113  RN_NET* net = m_data->GetRatsnestForNet( i );
114 
115  if( !net )
116  continue;
117 
118  // Draw the "static" ratsnest
119  if( i != highlightedNet )
120  gal->SetStrokeColor( color ); // using the default ratsnest color for not highlighted
121  else
122  gal->SetStrokeColor( color.Brightened(0.8) );
123 
124  for( const auto& edge : net->GetUnconnected() )
125  {
126  //if ( !edge.IsVisible() )
127  // continue;
128 
129  const auto& sourceNode = edge.GetSourceNode();
130  const auto& targetNode = edge.GetTargetNode();
131  const VECTOR2I source( sourceNode->Pos() );
132  const VECTOR2I target( targetNode->Pos() );
133 
134  if( !sourceNode->Valid() || !targetNode->Valid() )
135  continue;
136 
137  bool enable = !sourceNode->GetNoLine() && !targetNode->GetNoLine();
138  bool show;
139 
140  // If the global ratsnest is currently enabled, the local ratsnest
141  // should be easy to turn off, so either element can disable it
142  // If the global ratsnest is disabled, the local ratsnest should be easy to turn on
143  // so either element can enable it.
144  if( rs->GetGlobalRatsnestLinesEnabled() )
145  show = sourceNode->Parent()->GetLocalRatsnestVisible() &&
146  targetNode->Parent()->GetLocalRatsnestVisible();
147  else
148  show = sourceNode->Parent()->GetLocalRatsnestVisible() ||
149  targetNode->Parent()->GetLocalRatsnestVisible();
150 
151  if ( enable && show )
152  {
153  if ( source == target )
154  {
155  gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y - CROSS_SIZE ),
156  VECTOR2I( source.x + CROSS_SIZE, source.y + CROSS_SIZE ) );
157  gal->DrawLine( VECTOR2I( source.x - CROSS_SIZE, source.y + CROSS_SIZE ),
158  VECTOR2I( source.x + CROSS_SIZE, source.y - CROSS_SIZE ) );
159  }
160  else
161  {
162  if( curved_ratsnest )
163  {
164  auto dx = target.x - source.x;
165  auto dy = target.y - source.y;
166  const auto center = VECTOR2I( source.x + 0.5 * dx - 0.1 * dy,
167  source.y + 0.5 * dy + 0.1 * dx );
168  gal->DrawCurve( source, center, center, target );
169  }
170  else
171  {
172  gal->DrawLine( source, target );
173  }
174  }
175  }
176  }
177  }
178 }
179 
180 
181 void RATSNEST_VIEWITEM::ViewGetLayers( int aLayers[], int& aCount ) const
182 {
183  aCount = 1;
184  aLayers[0] = LAYER_RATSNEST;
185 }
186 
187 }
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.
Definition: ratsnest_data.h:98
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
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:55
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 GetSettings 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:163
RN_NET Describes ratsnest for a single net.
Definition: ratsnest_data.h:59
VIEW.
Definition: view.h:61
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.