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( wxT( "Pads" ), msg, RED );
72 
73  msg.Printf( wxT( " %d" ), conn->GetNetCount() - 1 /* Don't include "No Net" in count */ );
74  AppendMsgPanel( wxT( "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  if( !connectivity->TryLock() )
101  return;
102 
104 
105  for( int i = 1 /* skip "No Net" at [0] */; i < connectivity->GetNetCount(); ++i )
106  {
107  RN_NET* net = connectivity->GetRatsnestForNet( i );
108 
109  if( !net )
110  continue;
111 
112  if( ( aNetcode <= 0 ) || ( aNetcode == i ) )
113  {
114  for( const auto& edge : net->GetEdges() )
115  {
116  auto s = edge.GetSourcePos();
117  auto d = edge.GetTargetPos();
118  auto sn = edge.GetSourceNode();
119  auto dn = edge.GetTargetNode();
120 
121  if( !sn->Valid() || !dn->Valid() )
122  continue;
123 
124  bool enable = !sn->GetNoLine() && !dn->GetNoLine();
125  bool show = sn->Parent()->GetLocalRatsnestVisible()
126  || dn->Parent()->GetLocalRatsnestVisible();
127 
128  if( enable && show )
129  GRLine( m_canvas->GetClipBox(), aDC, wxPoint( s.x, s.y ), wxPoint( d.x,
130  d.y ), 0, color );
131  }
132  }
133  }
134 
135  connectivity->Unlock();
136 }
137 
138 
140 {
141  if( DC == NULL )
142  return;
143 
145 
146  for( const auto& l : GetBoard()->GetConnectivity()->GetDynamicRatsnest() )
147  {
148  GRLine( m_canvas->GetClipBox(), DC, wxPoint( l.a.x, l.a.y ), wxPoint( l.b.x,
149  l.b.y ), 0, tmpcolor );
150  }
151 }
152 
153 
154 /*
155  * PCB_BASE_FRAME::BuildAirWiresTargetsList and
156  * PCB_BASE_FRAME::TraceAirWiresToTargets
157  * are 2 function to show the near connecting points when
158  * a new track is created, by displaying g_MaxLinksShowed airwires
159  * between the on grid mouse cursor and these connecting points
160  * during the creation of a track
161  */
162 
163 /* Buffer to store pads coordinates when creating a track.
164  * these pads are members of the net
165  * and when the mouse is moved, the g_MaxLinksShowed links to neighbors are
166  * drawn
167  */
168 
169 static wxPoint s_CursorPos; // Coordinate of the moving point (mouse cursor and
170  // end of current track segment)
171 
172 /* Function BuildAirWiresTargetsList
173  * Build a list of candidates that can be a coonection point
174  * when a track is started.
175  * This functions prepares data to show airwires to nearest connecting points (pads)
176  * from the current new track to candidates during track creation
177  */
178 
179 static BOARD_CONNECTED_ITEM* s_ref = nullptr;
180 static int s_refNet = -1;
181 
183  const wxPoint& aPosition, int aNet )
184 {
185  s_CursorPos = aPosition; // needed for sort_by_distance
186  s_ref = aItemRef;
187  s_refNet = aNet;
188 }
189 
190 
191 static MODULE movedModule( nullptr );
192 
194 {
195  auto connectivity = GetBoard()->GetConnectivity();
196 
197  movedModule = *aModule;
198  movedModule.Move( -aMoveVector );
199  connectivity->ClearDynamicRatsnest();
200  connectivity->BlockRatsnestItems( { aModule } );
201  connectivity->ComputeDynamicRatsnest( { &movedModule } );
202 }
203 
204 
206 {
207  auto connectivity = GetBoard()->GetConnectivity();
208  auto displ_opts = (PCB_DISPLAY_OPTIONS*) GetDisplayOptions();
209 
210  auto targets = connectivity->NearestUnconnectedTargets( s_ref, s_CursorPos, s_refNet );
211 
212  if( aDC == NULL )
213  return;
214 
215  GRSetDrawMode( aDC, GR_XOR );
216 
217  for( int i = 0; i < std::min( (int) displ_opts->m_MaxLinksShowed, (int) targets.size() ); i++ )
218  {
219  auto p = targets[i];
220  GRLine( m_canvas->GetClipBox(), aDC, s_CursorPos, wxPoint( p.x, p.y ), 0, YELLOW );
221  }
222 }
223 
224 
225 // Redraw in XOR mode the outlines of the module.
227  const wxPoint& aMoveVector )
228 {
229  int pad_fill_tmp;
230  D_PAD* pt_pad;
231 
232  DrawEdgesOnly( panel, DC, aMoveVector, GR_XOR );
233  auto displ_opts = (PCB_DISPLAY_OPTIONS*) ( panel->GetDisplayOptions() );
234 
235  // Show pads in sketch mode to speedu up drawings
236  pad_fill_tmp = displ_opts->m_DisplayPadFill;
237  displ_opts->m_DisplayPadFill = true;
238 
239  pt_pad = PadsList();
240 
241  for( ; pt_pad != NULL; pt_pad = pt_pad->Next() )
242  pt_pad->Draw( panel, DC, GR_XOR, aMoveVector );
243 
244  displ_opts->m_DisplayPadFill = pad_fill_tmp;
245 
246  if( displ_opts->m_Show_Module_Ratsnest )
247  {
248  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) panel->GetParent();
249  frame->build_ratsnest_module( this, aMoveVector );
250  frame->TraceModuleRatsNest( DC );
251  }
252 }
253 
254 
256 {
257  if( item && item->Type() == PCB_MODULE_T )
258  {
259  auto mod = static_cast<MODULE*> (item);
260 
261  for( auto pad : mod->Pads() )
262  {
263  pad->SetLocalRatsnestVisible( !pad->GetLocalRatsnestVisible() );
264  }
265  }
266  else
267  {
268  auto modules = GetBoard()->Modules();
269 
270  for( auto mod : modules )
271  {
272  for( auto pad : mod->Pads() )
273  {
274  pad->SetLocalRatsnestVisible( false );
275  }
276  }
277  }
278 
279  m_canvas->Refresh();
280 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
void TraceAirWiresToTargets(wxDC *aDC)
Function TraceAirWiresToTargets This functions shows airwires to nearest connecting points (pads) fro...
Definition: ratsnest.cpp:205
static wxPoint s_CursorPos
Definition: ratsnest.cpp:169
const std::vector< CN_EDGE > & GetEdges() const
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:139
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:193
BOARD * GetBoard() const
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:182
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:255
DLIST_ITERATOR_WRAPPER< MODULE > Modules()
Definition: class_board.h:254
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
D_PAD * Next() const
Definition: class_pad.h:160
Definition: colors.h:60
static int s_refNet
Definition: ratsnest.cpp:180
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
COLORS_DESIGN_SETTINGS & Colors()
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:123
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:154
The common library.
static BOARD_CONNECTED_ITEM * s_ref
Definition: ratsnest.cpp:179
Class RN_NET Describes ratsnest for a single net.
Definition: ratsnest_data.h:59
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:226
Module description (excepted pads)
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
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:296
#define mod(a, n)
Definition: greymap.cpp:24
void ClearMsgPanel(void)
Clear all messages from the message panel.
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
#define min(a, b)
Definition: auxiliary.h:85
int m_Status_Pcb
Flags used in ratsnest calculation and update.
Definition: class_board.h:240
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39