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 <wxBasePcbFrame.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 <connectivity_data.h>
44 #include <ratsnest_data.h>
45 
46 #include <wxPcbStruct.h>
47 
56 void PCB_BASE_FRAME::Compile_Ratsnest( wxDC* aDC, bool aDisplayStatus )
57 {
58  GetBoard()->GetConnectivity()->RecalculateRatsnest();
59 
60  GetBoard()->m_Status_Pcb = 0; // we want a full ratsnest computation, from the scratch
61 
62  if( GetBoard()->IsElementVisible( LAYER_RATSNEST ) && aDC )
63  DrawGeneralRatsnest( aDC, 0 );
64 
65  wxString msg;
66 
67  ClearMsgPanel();
68 
69  if( aDisplayStatus )
70  {
71  msg.Printf( wxT( " %d" ), m_Pcb->GetConnectivity()->GetPadCount() );
72  AppendMsgPanel( wxT( "Pads" ), msg, RED );
73  msg.Printf( wxT( " %d" ), m_Pcb->GetConnectivity()->GetNetCount() );
74  AppendMsgPanel( wxT( "Nets" ), msg, CYAN );
75  SetMsgPanel( m_Pcb );
76  }
77 }
78 
79 
87 void PCB_BASE_FRAME::DrawGeneralRatsnest( wxDC* aDC, int aNetcode )
88 {
90  {
91  return;
92  }
93 
94  if( aDC == NULL )
95  return;
96 
97  auto connectivity = m_Pcb->GetConnectivity();
98 
99  if( !connectivity->TryLock() )
100  return;
101 
103 
104  for( int i = 1; i < connectivity->GetNetCount(); ++i )
105  {
106  RN_NET* net = connectivity->GetRatsnestForNet( i );
107 
108  if( !net )
109  continue;
110 
111  if( ( aNetcode <= 0 ) || ( aNetcode == i ) )
112  {
113  for( const auto& edge : net->GetEdges() )
114  {
115  auto s = edge.GetSourcePos();
116  auto d = edge.GetTargetPos();
117  auto sn = edge.GetSourceNode();
118  auto dn = edge.GetTargetNode();
119 
120  bool enable = !sn->GetNoLine() && !dn->GetNoLine();
121  bool show = sn->Parent()->GetLocalRatsnestVisible()
122  || dn->Parent()->GetLocalRatsnestVisible();
123 
124  if( enable && show )
125  GRLine( m_canvas->GetClipBox(), aDC, wxPoint( s.x, s.y ), wxPoint( d.x,
126  d.y ), 0, color );
127  }
128  }
129  }
130 
131  connectivity->Unlock();
132 }
133 
134 
136 {
137  if( DC == NULL )
138  return;
139 
141 
142  for( const auto& l : GetBoard()->GetConnectivity()->GetDynamicRatsnest() )
143  {
144  GRLine( m_canvas->GetClipBox(), DC, wxPoint( l.a.x, l.a.y ), wxPoint( l.b.x,
145  l.b.y ), 0, tmpcolor );
146  }
147 }
148 
149 
150 /*
151  * PCB_BASE_FRAME::BuildAirWiresTargetsList and
152  * PCB_BASE_FRAME::TraceAirWiresToTargets
153  * are 2 function to show the near connecting points when
154  * a new track is created, by displaying g_MaxLinksShowed airwires
155  * between the on grid mouse cursor and these connecting points
156  * during the creation of a track
157  */
158 
159 /* Buffer to store pads coordinates when creating a track.
160  * these pads are members of the net
161  * and when the mouse is moved, the g_MaxLinksShowed links to neighbors are
162  * drawn
163  */
164 
165 static wxPoint s_CursorPos; // Coordinate of the moving point (mouse cursor and
166  // end of current track segment)
167 
168 /* Function BuildAirWiresTargetsList
169  * Build a list of candidates that can be a coonection point
170  * when a track is started.
171  * This functions prepares data to show airwires to nearest connecting points (pads)
172  * from the current new track to candidates during track creation
173  */
174 
175 static BOARD_CONNECTED_ITEM* s_ref = nullptr;
176 static int s_refNet = -1;
177 
179  const wxPoint& aPosition, int aNet )
180 {
181  s_CursorPos = aPosition; // needed for sort_by_distance
182  s_ref = aItemRef;
183  s_refNet = aNet;
184 }
185 
186 
187 static MODULE movedModule( nullptr );
188 
190 {
191  auto connectivity = GetBoard()->GetConnectivity();
192 
193  movedModule = *aModule;
194  movedModule.Move( -aMoveVector );
195  connectivity->ClearDynamicRatsnest();
196  connectivity->BlockRatsnestItems( { aModule } );
197  connectivity->ComputeDynamicRatsnest( { &movedModule } );
198 }
199 
200 
202 {
203  auto connectivity = GetBoard()->GetConnectivity();
204  auto displ_opts = (PCB_DISPLAY_OPTIONS*) GetDisplayOptions();
205 
206  auto targets = connectivity->NearestUnconnectedTargets( s_ref, s_CursorPos, s_refNet );
207 
208  if( aDC == NULL )
209  return;
210 
211  GRSetDrawMode( aDC, GR_XOR );
212 
213  for( int i = 0; i < std::min( (int) displ_opts->m_MaxLinksShowed, (int) targets.size() ); i++ )
214  {
215  auto p = targets[i];
216  GRLine( m_canvas->GetClipBox(), aDC, s_CursorPos, wxPoint( p.x, p.y ), 0, YELLOW );
217  }
218 }
219 
220 
221 // Redraw in XOR mode the outlines of the module.
223  const wxPoint& aMoveVector )
224 {
225  int pad_fill_tmp;
226  D_PAD* pt_pad;
227 
228  DrawEdgesOnly( panel, DC, aMoveVector, GR_XOR );
229  auto displ_opts = (PCB_DISPLAY_OPTIONS*) ( panel->GetDisplayOptions() );
230 
231  // Show pads in sketch mode to speedu up drawings
232  pad_fill_tmp = displ_opts->m_DisplayPadFill;
233  displ_opts->m_DisplayPadFill = true;
234 
235  pt_pad = PadsList();
236 
237  for( ; pt_pad != NULL; pt_pad = pt_pad->Next() )
238  pt_pad->Draw( panel, DC, GR_XOR, aMoveVector );
239 
240  displ_opts->m_DisplayPadFill = pad_fill_tmp;
241 
242  if( displ_opts->m_Show_Module_Ratsnest )
243  {
244  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) panel->GetParent();
245  frame->build_ratsnest_module( this, aMoveVector );
246  frame->TraceModuleRatsNest( DC );
247  }
248 }
249 
250 
252 {
253  if( item && item->Type() == PCB_MODULE_T )
254  {
255  auto mod = static_cast<MODULE*> (item);
256 
257  for( auto pad : mod->Pads() )
258  {
259  pad->SetLocalRatsnestVisible( true );
260  }
261 
262  m_canvas->Refresh();
263  }
264 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:225
void TraceAirWiresToTargets(wxDC *aDC)
Function TraceAirWiresToTargets This functions shows airwires to nearest connecting points (pads) fro...
Definition: ratsnest.cpp:201
static wxPoint s_CursorPos
Definition: ratsnest.cpp:165
const std::vector< CN_EDGE > & GetEdges() const
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:296
void TraceModuleRatsNest(wxDC *aDC)
Function TraceModuleRatsNest display the rats nest of a moving footprint, computed by build_ratsnest_...
Definition: ratsnest.cpp:135
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
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.
void build_ratsnest_module(MODULE *aModule, wxPoint aMoveVector)
Function build_ratsnest_module Build a ratsnest relative to one footprint.
Definition: ratsnest.cpp:189
BOARD * GetBoard() const
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:178
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.
class MODULE, a footprint
Definition: typeinfo.h:89
Class PCB_DISPLAY_OPTIONS handles display options like enable/disable some optional drawings...
Classes used in Pcbnew, CvPcb and GerbView.
void Show_1_Ratsnest(EDA_ITEM *item, wxDC *DC)
Function Show_1_Ratsnest draw ratsnest.
Definition: ratsnest.cpp:251
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Function SetMsgPanel clears the message panel and populates it with the contents of aList...
Definition: draw_frame.cpp:784
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:87
Definition: colors.h:59
D_PAD * Next() const
Definition: class_pad.h:160
EDA_RECT * GetClipBox()
Definition: colors.h:60
static int s_refNet
Definition: ratsnest.cpp:176
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:174
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:358
COLORS_DESIGN_SETTINGS & Colors()
void Compile_Ratsnest(wxDC *aDC, bool aDisplayStatus)
Function Compile_Ratsnest Create the entire board ratsnest.
Definition: ratsnest.cpp:56
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
Definition: draw_frame.cpp:764
void Move(const wxPoint &aMoveVector) override
Function Move move this object.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:178
The common library.
static BOARD_CONNECTED_ITEM * s_ref
Definition: ratsnest.cpp:175
Class RN_NET Describes ratsnest for a single net.
Definition: ratsnest_data.h:59
DLIST< D_PAD > & PadsList()
Definition: class_module.h:154
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:222
Module description (excepted pads)
void * GetDisplayOptions()
Function GetDisplayOptions A way to pass info to draw functions.
Definition: draw_panel.cpp:181
static MODULE movedModule(nullptr)
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:290
#define mod(a, n)
Definition: greymap.cpp:24
void ClearMsgPanel(void)
Clear all messages from the message panel.
Definition: draw_frame.cpp:775
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:237
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39