KiCad PCB EDA Suite
ratsnest.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) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <common.h>
33 #include <class_drawpanel.h>
34 #include <pcb_edit_frame.h>
35 #include <macros.h>
36 
37 #include <class_board.h>
38 #include <class_module.h>
39 #include <class_track.h>
40 
41 #include <pcbnew.h>
42 
43 #include <ratsnest_data.h>
44 
53 void PCB_BASE_FRAME::Compile_Ratsnest( wxDC* aDC, bool aDisplayStatus )
54 {
55  GetBoard()->GetConnectivity()->RecalculateRatsnest();
56 
57  GetBoard()->m_Status_Pcb = 0; // we want a full ratsnest computation, from the scratch
58 
59  if( GetBoard()->IsElementVisible( LAYER_RATSNEST ) && aDC )
60  DrawGeneralRatsnest( aDC, 0 );
61 
62  wxString msg;
63 
64  ClearMsgPanel();
65 
66  if( aDisplayStatus )
67  {
68  std::shared_ptr<CONNECTIVITY_DATA> conn = m_Pcb->GetConnectivity();
69 
70  msg.Printf( wxT( " %d" ), conn->GetPadCount() );
71  AppendMsgPanel( _( "Pads" ), msg, RED );
72 
73  msg.Printf( wxT( " %d" ), conn->GetNetCount() - 1 /* Don't include "No Net" in count */ );
74  AppendMsgPanel( _( "Nets" ), msg, CYAN );
75 
76  SetMsgPanel( m_Pcb );
77  }
78 }
79 
80 
88 void PCB_BASE_FRAME::DrawGeneralRatsnest( wxDC* aDC, int aNetcode )
89 {
91  {
92  return;
93  }
94 
95  if( aDC == NULL )
96  return;
97 
98  auto connectivity = m_Pcb->GetConnectivity();
99 
100  std::unique_lock<std::mutex> lock( connectivity->GetLock(), std::try_to_lock );
101 
102  if( !lock )
103  return;
104 
106 
107  auto displ_opts = (PCB_DISPLAY_OPTIONS*) GetDisplayOptions();
108 
109  const bool curved_ratsnest = displ_opts->m_DisplayRatsnestLinesCurved;
110 
111  for( int i = 1 /* skip "No Net" at [0] */; i < connectivity->GetNetCount(); ++i )
112  {
113  RN_NET* net = connectivity->GetRatsnestForNet( i );
114 
115  if( !net )
116  continue;
117 
118  if( ( aNetcode <= 0 ) || ( aNetcode == i ) )
119  {
120  for( const auto& edge : net->GetEdges() )
121  {
122  auto s = edge.GetSourcePos();
123  auto d = edge.GetTargetPos();
124  auto sn = edge.GetSourceNode();
125  auto dn = edge.GetTargetNode();
126 
127  if( !sn->Valid() || !dn->Valid() )
128  continue;
129 
130  bool enable = !sn->GetNoLine() && !dn->GetNoLine();
131  bool show = sn->Parent()->GetLocalRatsnestVisible()
132  || dn->Parent()->GetLocalRatsnestVisible();
133 
134  if( enable && show )
135  {
136  if (curved_ratsnest) {
137  auto dx = d.x - s.x;
138  auto dy = d.y - s.y;
139  auto cx = s.x + 0.5 * dx + 1.2 * dy;
140  auto cy = s.y + 0.5 * dy - 1.2 * dx;
141  GRArc1( m_canvas->GetClipBox(), aDC, s.x, s.y, d.x, d.y, cx, cy, 0, color);
142  } else {
143  GRLine( m_canvas->GetClipBox(), aDC, s.x, s.y, d.x, d.y, 0, color );
144  }
145  }
146  }
147  }
148  }
149 }
150 
151 
153 {
154  if( DC == NULL )
155  return;
156 
158 
159  for( const auto& l : GetBoard()->GetConnectivity()->GetDynamicRatsnest() )
160  {
161  GRLine( m_canvas->GetClipBox(), DC, wxPoint( l.a.x, l.a.y ), wxPoint( l.b.x,
162  l.b.y ), 0, tmpcolor );
163  }
164 }
165 
166 
167 /*
168  * PCB_BASE_FRAME::BuildAirWiresTargetsList and
169  * PCB_BASE_FRAME::TraceAirWiresToTargets
170  * are 2 function to show the near connecting points when
171  * a new track is created, by displaying g_MaxLinksShowed airwires
172  * between the on grid mouse cursor and these connecting points
173  * during the creation of a track
174  */
175 
176 /* Buffer to store pads coordinates when creating a track.
177  * these pads are members of the net
178  * and when the mouse is moved, the g_MaxLinksShowed links to neighbors are
179  * drawn
180  */
181 
182 static wxPoint s_CursorPos; // Coordinate of the moving point (mouse cursor and
183  // end of current track segment)
184 
185 /* Function BuildAirWiresTargetsList
186  * Build a list of candidates that can be a coonection point
187  * when a track is started.
188  * This functions prepares data to show airwires to nearest connecting points (pads)
189  * from the current new track to candidates during track creation
190  */
191 
192 static BOARD_CONNECTED_ITEM* s_ref = nullptr;
193 static int s_refNet = -1;
194 
196  const wxPoint& aPosition, int aNet )
197 {
198  s_CursorPos = aPosition; // needed for sort_by_distance
199  s_ref = aItemRef;
200  s_refNet = aNet;
201 }
202 
203 
204 static MODULE movedModule( nullptr );
205 
206 void PCB_BASE_FRAME::build_ratsnest_module( MODULE* aModule, wxPoint aMoveVector )
207 {
208  auto connectivity = GetBoard()->GetConnectivity();
209 
210  movedModule = *aModule;
211  movedModule.Move( -aMoveVector );
212  connectivity->ClearDynamicRatsnest();
213  connectivity->BlockRatsnestItems( { aModule } );
214  connectivity->ComputeDynamicRatsnest( { &movedModule } );
215 }
216 
217 
219 {
220  auto connectivity = GetBoard()->GetConnectivity();
221  auto displ_opts = (PCB_DISPLAY_OPTIONS*) GetDisplayOptions();
222 
223  auto targets = connectivity->NearestUnconnectedTargets( s_ref, s_CursorPos, s_refNet );
224 
225  if( aDC == NULL )
226  return;
227 
228  GRSetDrawMode( aDC, GR_XOR );
229 
230  for( int i = 0; i < std::min( (int) displ_opts->m_MaxLinksShowed, (int) targets.size() ); i++ )
231  {
232  auto p = targets[i];
233  GRLine( m_canvas->GetClipBox(), aDC, s_CursorPos, wxPoint( p.x, p.y ), 0, YELLOW );
234  }
235 }
236 
237 
238 // Redraw in XOR mode the outlines of the module.
240  const wxPoint& aMoveVector )
241 {
242  int pad_fill_tmp;
243  D_PAD* pt_pad;
244 
245  DrawEdgesOnly( panel, DC, aMoveVector, GR_XOR );
246  auto displ_opts = (PCB_DISPLAY_OPTIONS*) ( panel->GetDisplayOptions() );
247 
248  // Show pads in sketch mode to speedu up drawings
249  pad_fill_tmp = displ_opts->m_DisplayPadFill;
250  displ_opts->m_DisplayPadFill = true;
251 
252  pt_pad = PadsList();
253 
254  for( ; pt_pad != NULL; pt_pad = pt_pad->Next() )
255  pt_pad->Draw( panel, DC, GR_XOR, aMoveVector );
256 
257  displ_opts->m_DisplayPadFill = pad_fill_tmp;
258 
259  if( displ_opts->m_Show_Module_Ratsnest )
260  {
261  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) panel->GetParent();
262  frame->build_ratsnest_module( this, aMoveVector );
263  frame->TraceModuleRatsNest( DC );
264  }
265 }
266 
267 
269 {
270  if( item && item->Type() == PCB_MODULE_T )
271  {
272  auto mod = static_cast<MODULE*> (item);
273 
274  for( auto pad : mod->Pads() )
275  {
276  pad->SetLocalRatsnestVisible( !pad->GetLocalRatsnestVisible() );
277  }
278  }
279  else
280  {
281  auto modules = GetBoard()->Modules();
282 
283  for( auto mod : modules )
284  {
285  for( auto pad : mod->Pads() )
286  {
287  pad->SetLocalRatsnestVisible( false );
288  }
289  }
290  }
291 
292  m_canvas->Refresh();
293 }
D_PAD * Next() const
Definition: class_pad.h:162
void TraceAirWiresToTargets(wxDC *aDC)
Function TraceAirWiresToTargets This functions shows airwires to nearest connecting points (pads) fro...
Definition: ratsnest.cpp:218
static wxPoint s_CursorPos
Definition: ratsnest.cpp:182
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:223
void TraceModuleRatsNest(wxDC *aDC)
Function TraceModuleRatsNest display the rats nest of a moving footprint, computed by build_ratsnest_...
Definition: ratsnest.cpp:152
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, GR_DRAWMODE aDrawMode, const wxPoint &aOffset=ZeroOffset) override
Function Draw BOARD_ITEMs have their own color information.
Class BOARD to handle a board.
Class that computes missing connections on a PCB.
int color
Definition: DXF_plotter.cpp:62
void build_ratsnest_module(MODULE *aModule, wxPoint aMoveVector)
Function build_ratsnest_module Build a ratsnest relative to one footprint.
Definition: ratsnest.cpp:206
virtual EDA_RECT * GetClipBox()
void BuildAirWiresTargetsList(BOARD_CONNECTED_ITEM *aItemRef, const wxPoint &aPosition, int aNet)
Function BuildAirWiresTargetsList Build a list of candidates that can be a coonection point when a tr...
Definition: ratsnest.cpp:195
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
Functions relatives to tracks, vias and segments used to fill zones.
void * GetDisplayOptions() override
Function GetDisplayOptions returns the display options current in use Display options are relative to...
This file contains miscellaneous commonly used macros and functions.
COLOR4D GetItemColor(int aItemIdx) const
Function GetItemColor.
virtual EDA_DRAW_FRAME * GetParent() const =0
class MODULE, a footprint
Definition: typeinfo.h:89
Class PCB_DISPLAY_OPTIONS handles display options like enable/disable some optional drawings.
void Show_1_Ratsnest(EDA_ITEM *item, wxDC *DC)
Function Show_1_Ratsnest draw ratsnest.
Definition: ratsnest.cpp:268
DLIST_ITERATOR_WRAPPER< MODULE > Modules()
Definition: class_board.h:252
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
PCB_GENERAL_SETTINGS & Settings()
void DrawGeneralRatsnest(wxDC *aDC, int aNetcode=0)
function Displays the general ratsnest Only ratsnest with the status bit CH_VISIBLE is set are displa...
Definition: ratsnest.cpp:88
Definition: colors.h:59
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:292
Definition: colors.h:60
static int s_refNet
Definition: ratsnest.cpp:193
void DrawEdgesOnly(EDA_DRAW_PANEL *panel, wxDC *DC, const wxPoint &offset, GR_DRAWMODE draw_mode)
Function DrawEdgesOnly Draws the footprint edges only to the current Device Context.
void GRLine(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color, wxPenStyle aStyle)
Definition: gr_basic.cpp:285
virtual void ClearMsgPanel()
Clear all messages from the message panel.
COLORS_DESIGN_SETTINGS & Colors()
const std::vector< CN_EDGE > & GetEdges() const
void Compile_Ratsnest(wxDC *aDC, bool aDisplayStatus)
Function Compile_Ratsnest Create the entire board ratsnest.
Definition: ratsnest.cpp:53
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:128
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
void Move(const wxPoint &aMoveVector) override
Function Move move this object.
size_t i
Definition: json11.cpp:597
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:157
The common library.
static BOARD_CONNECTED_ITEM * s_ref
Definition: ratsnest.cpp:192
Class RN_NET Describes ratsnest for a single net.
Definition: ratsnest_data.h:59
DLIST< D_PAD > & PadsList()
Definition: class_module.h:162
void DrawOutlinesWhenMoving(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aMoveVector)
Function DrawOutlinesWhenMoving draws in XOR mode the footprint when moving it to the aDC.
Definition: ratsnest.cpp:239
Module description (excepted pads)
BOARD * GetBoard() const
virtual void * GetDisplayOptions()
Function GetDisplayOptions A way to pass info to draw functions.
static MODULE movedModule(nullptr)
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL)
Definition: colors.h:68
#define mod(a, n)
Definition: greymap.cpp:24
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204
#define min(a, b)
Definition: auxiliary.h:85
int m_Status_Pcb
Flags used in ratsnest calculation and update.
Definition: class_board.h:240
void GRArc1(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int xc, int yc, COLOR4D Color)
Definition: gr_basic.cpp:799
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39