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 <colors_selection.h>
35 #include <wxBasePcbFrame.h>
36 #include <macros.h>
37 
38 #include <class_board.h>
39 #include <class_module.h>
40 #include <class_track.h>
41 
42 #include <pcbnew.h>
43 
44 #include <connectivity.h>
45 #include <ratsnest_data.h>
46 
47 #include <wxPcbStruct.h>
48 
57 void PCB_BASE_FRAME::Compile_Ratsnest( wxDC* aDC, bool aDisplayStatus )
58 {
59  GetBoard()->GetConnectivity()->RecalculateRatsnest();
60 
61  GetBoard()->m_Status_Pcb = 0; // we want a full ratsnest computation, from the scratch
62 
63  if( GetBoard()->IsElementVisible( LAYER_RATSNEST ) && aDC )
64  DrawGeneralRatsnest( aDC, 0 );
65 
66  wxString msg;
67 
68  ClearMsgPanel();
69 
70  if( aDisplayStatus )
71  {
72  msg.Printf( wxT( " %d" ), m_Pcb->GetConnectivity()->GetPadCount() );
73  AppendMsgPanel( wxT( "Pads" ), msg, RED );
74  msg.Printf( wxT( " %d" ), m_Pcb->GetConnectivity()->GetNetCount() );
75  AppendMsgPanel( wxT( "Nets" ), msg, CYAN );
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 
101 
102  for( int i = 1; i < connectivity->GetNetCount(); ++i )
103  {
104  RN_NET* net = connectivity->GetRatsnestForNet( i );
105 
106  if( ( aNetcode <= 0 ) || ( aNetcode == i ) )
107  {
108  for( const auto& edge : net->GetEdges() )
109  {
110  auto s = edge.GetSourcePos();
111  auto d = edge.GetTargetPos();
112  auto sn = edge.GetSourceNode();
113  auto dn = edge.GetTargetNode();
114 
115  bool enable = !sn->GetNoLine() && !dn->GetNoLine();
116  bool show = sn->Parent()->GetLocalRatsnestVisible()
117  || dn->Parent()->GetLocalRatsnestVisible();
118 
119  if( enable && show )
120  GRLine( m_canvas->GetClipBox(), aDC, wxPoint( s.x, s.y ), wxPoint( d.x,
121  d.y ), 0, color );
122  }
123  }
124  }
125 }
126 
127 
129 {
130  if( DC == NULL )
131  return;
132 
134 
135  for( const auto& l : GetBoard()->GetConnectivity()->GetDynamicRatsnest() )
136  {
137  GRLine( m_canvas->GetClipBox(), DC, wxPoint( l.a.x, l.a.y ), wxPoint( l.b.x,
138  l.b.y ), 0, tmpcolor );
139  }
140 }
141 
142 
143 /*
144  * PCB_BASE_FRAME::BuildAirWiresTargetsList and
145  * PCB_BASE_FRAME::TraceAirWiresToTargets
146  * are 2 function to show the near connecting points when
147  * a new track is created, by displaying g_MaxLinksShowed airwires
148  * between the on grid mouse cursor and these connecting points
149  * during the creation of a track
150  */
151 
152 /* Buffer to store pads coordinates when creating a track.
153  * these pads are members of the net
154  * and when the mouse is moved, the g_MaxLinksShowed links to neighbors are
155  * drawn
156  */
157 
158 static wxPoint s_CursorPos; // Coordinate of the moving point (mouse cursor and
159  // end of current track segment)
160 
161 /* Function BuildAirWiresTargetsList
162  * Build a list of candidates that can be a coonection point
163  * when a track is started.
164  * This functions prepares data to show airwires to nearest connecting points (pads)
165  * from the current new track to candidates during track creation
166  */
167 
168 static BOARD_CONNECTED_ITEM* s_ref = nullptr;
169 static int s_refNet = -1;
170 
172  const wxPoint& aPosition, int aNet )
173 {
174  s_CursorPos = aPosition; // needed for sort_by_distance
175  s_ref = aItemRef;
176  s_refNet = aNet;
177 }
178 
179 
180 static MODULE movedModule( nullptr );
181 
183 {
184  auto connectivity = GetBoard()->GetConnectivity();
185 
186  movedModule = *aModule;
187  movedModule.Move( -aMoveVector );
188  connectivity->ClearDynamicRatsnest();
189  connectivity->BlockRatsnestItems( { aModule } );
190  connectivity->ComputeDynamicRatsnest( { &movedModule } );
191 }
192 
193 
195 {
196  auto connectivity = GetBoard()->GetConnectivity();
198 
199  auto targets = connectivity->NearestUnconnectedTargets( s_ref, s_CursorPos, s_refNet );
200 
201  if( aDC == NULL )
202  return;
203 
204  GRSetDrawMode( aDC, GR_XOR );
205 
206  for( int i = 0; i < std::min( (int) displ_opts->m_MaxLinksShowed, (int) targets.size() ); i++ )
207  {
208  auto p = targets[i];
209  GRLine( m_canvas->GetClipBox(), aDC, s_CursorPos, wxPoint( p.x, p.y ), 0, YELLOW );
210  }
211 }
212 
213 
214 // Redraw in XOR mode the outlines of the module.
216  const wxPoint& aMoveVector )
217 {
218  int pad_fill_tmp;
219  D_PAD* pt_pad;
220 
221  DrawEdgesOnly( panel, DC, aMoveVector, GR_XOR );
222  DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*) panel->GetDisplayOptions();
223 
224  // Show pads in sketch mode to speedu up drawings
225  pad_fill_tmp = displ_opts->m_DisplayPadFill;
226  displ_opts->m_DisplayPadFill = true;
227 
228  pt_pad = PadsList();
229 
230  for( ; pt_pad != NULL; pt_pad = pt_pad->Next() )
231  pt_pad->Draw( panel, DC, GR_XOR, aMoveVector );
232 
233  displ_opts->m_DisplayPadFill = pad_fill_tmp;
234 
235  if( displ_opts->m_Show_Module_Ratsnest )
236  {
237  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) panel->GetParent();
238  frame->build_ratsnest_module( this, aMoveVector );
239  frame->TraceModuleRatsNest( DC );
240  }
241 }
242 
243 
245 {
246  if( item->Type() == PCB_MODULE_T )
247  {
248  auto mod = static_cast<MODULE*> (item);
249 
250  for( auto pad : mod->Pads() )
251  {
252  pad->SetLocalRatsnestVisible( true );
253  }
254 
255  m_canvas->Refresh();
256  }
257 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
void TraceAirWiresToTargets(wxDC *aDC)
Function TraceAirWiresToTargets This functions shows airwires to nearest connecting points (pads) fro...
Definition: ratsnest.cpp:194
static wxPoint s_CursorPos
Definition: ratsnest.cpp:158
const std::vector< CN_EDGE > & GetEdges() const
void GRSetDrawMode(wxDC *DC, GR_DRAWMODE draw_mode)
Definition: gr_basic.cpp:290
void TraceModuleRatsNest(wxDC *aDC)
Function TraceModuleRatsNest display the rats nest of a moving footprint, computed by build_ratsnest_...
Definition: ratsnest.cpp:128
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
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.
void GRLine(EDA_RECT *ClipBox, wxDC *DC, int x1, int y1, int x2, int y2, int width, COLOR4D Color)
Definition: gr_basic.cpp:352
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:182
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:171
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:101
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:244
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:773
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:106
EDA_RECT * GetClipBox()
Definition: colors.h:60
static int s_refNet
Definition: ratsnest.cpp:169
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:175
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.
COLORS_DESIGN_SETTINGS g_ColorsSettings
Definition: pcbnew.cpp:68
void Compile_Ratsnest(wxDC *aDC, bool aDisplayStatus)
Function Compile_Ratsnest Create the entire board ratsnest.
Definition: ratsnest.cpp:57
Class DISPLAY_OPTIONS handles display options like enable/disable some optional drawings.
Definition: pcbstruct.h:62
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
Definition: draw_frame.cpp:753
bool m_DisplayPadFill
Definition: pcbstruct.h:65
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:151
The common library.
int m_MaxLinksShowed
Definition: pcbstruct.h:86
static BOARD_CONNECTED_ITEM * s_ref
Definition: ratsnest.cpp:168
Class RN_NET Describes ratsnest for a single net.
Definition: ratsnest_data.h:59
DLIST< D_PAD > & PadsList()
Definition: class_module.h:134
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:215
Module description (excepted pads)
void * GetDisplayOptions()
Function GetDisplayOptions A way to pass info to draw functions.
Definition: draw_panel.cpp:182
bool m_Show_Module_Ratsnest
Definition: pcbstruct.h:87
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:764
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