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.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 
100 
101  for( int i = 1; i < connectivity->GetNetCount(); ++i )
102  {
103  RN_NET* net = connectivity->GetRatsnestForNet( i );
104 
105  if( !net )
106  continue;
107 
108  if( ( aNetcode <= 0 ) || ( aNetcode == i ) )
109  {
110  for( const auto& edge : net->GetEdges() )
111  {
112  auto s = edge.GetSourcePos();
113  auto d = edge.GetTargetPos();
114  auto sn = edge.GetSourceNode();
115  auto dn = edge.GetTargetNode();
116 
117  bool enable = !sn->GetNoLine() && !dn->GetNoLine();
118  bool show = sn->Parent()->GetLocalRatsnestVisible()
119  || dn->Parent()->GetLocalRatsnestVisible();
120 
121  if( enable && show )
122  GRLine( m_canvas->GetClipBox(), aDC, wxPoint( s.x, s.y ), wxPoint( d.x,
123  d.y ), 0, color );
124  }
125  }
126  }
127 }
128 
129 
131 {
132  if( DC == NULL )
133  return;
134 
136 
137  for( const auto& l : GetBoard()->GetConnectivity()->GetDynamicRatsnest() )
138  {
139  GRLine( m_canvas->GetClipBox(), DC, wxPoint( l.a.x, l.a.y ), wxPoint( l.b.x,
140  l.b.y ), 0, tmpcolor );
141  }
142 }
143 
144 
145 /*
146  * PCB_BASE_FRAME::BuildAirWiresTargetsList and
147  * PCB_BASE_FRAME::TraceAirWiresToTargets
148  * are 2 function to show the near connecting points when
149  * a new track is created, by displaying g_MaxLinksShowed airwires
150  * between the on grid mouse cursor and these connecting points
151  * during the creation of a track
152  */
153 
154 /* Buffer to store pads coordinates when creating a track.
155  * these pads are members of the net
156  * and when the mouse is moved, the g_MaxLinksShowed links to neighbors are
157  * drawn
158  */
159 
160 static wxPoint s_CursorPos; // Coordinate of the moving point (mouse cursor and
161  // end of current track segment)
162 
163 /* Function BuildAirWiresTargetsList
164  * Build a list of candidates that can be a coonection point
165  * when a track is started.
166  * This functions prepares data to show airwires to nearest connecting points (pads)
167  * from the current new track to candidates during track creation
168  */
169 
170 static BOARD_CONNECTED_ITEM* s_ref = nullptr;
171 static int s_refNet = -1;
172 
174  const wxPoint& aPosition, int aNet )
175 {
176  s_CursorPos = aPosition; // needed for sort_by_distance
177  s_ref = aItemRef;
178  s_refNet = aNet;
179 }
180 
181 
182 static MODULE movedModule( nullptr );
183 
185 {
186  auto connectivity = GetBoard()->GetConnectivity();
187 
188  movedModule = *aModule;
189  movedModule.Move( -aMoveVector );
190  connectivity->ClearDynamicRatsnest();
191  connectivity->BlockRatsnestItems( { aModule } );
192  connectivity->ComputeDynamicRatsnest( { &movedModule } );
193 }
194 
195 
197 {
198  auto connectivity = GetBoard()->GetConnectivity();
200 
201  auto targets = connectivity->NearestUnconnectedTargets( s_ref, s_CursorPos, s_refNet );
202 
203  if( aDC == NULL )
204  return;
205 
206  GRSetDrawMode( aDC, GR_XOR );
207 
208  for( int i = 0; i < std::min( (int) displ_opts->m_MaxLinksShowed, (int) targets.size() ); i++ )
209  {
210  auto p = targets[i];
211  GRLine( m_canvas->GetClipBox(), aDC, s_CursorPos, wxPoint( p.x, p.y ), 0, YELLOW );
212  }
213 }
214 
215 
216 // Redraw in XOR mode the outlines of the module.
218  const wxPoint& aMoveVector )
219 {
220  int pad_fill_tmp;
221  D_PAD* pt_pad;
222 
223  DrawEdgesOnly( panel, DC, aMoveVector, GR_XOR );
224  DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*) panel->GetDisplayOptions();
225 
226  // Show pads in sketch mode to speedu up drawings
227  pad_fill_tmp = displ_opts->m_DisplayPadFill;
228  displ_opts->m_DisplayPadFill = true;
229 
230  pt_pad = PadsList();
231 
232  for( ; pt_pad != NULL; pt_pad = pt_pad->Next() )
233  pt_pad->Draw( panel, DC, GR_XOR, aMoveVector );
234 
235  displ_opts->m_DisplayPadFill = pad_fill_tmp;
236 
237  if( displ_opts->m_Show_Module_Ratsnest )
238  {
239  PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) panel->GetParent();
240  frame->build_ratsnest_module( this, aMoveVector );
241  frame->TraceModuleRatsNest( DC );
242  }
243 }
244 
245 
247 {
248  if( item && item->Type() == PCB_MODULE_T )
249  {
250  auto mod = static_cast<MODULE*> (item);
251 
252  for( auto pad : mod->Pads() )
253  {
254  pad->SetLocalRatsnestVisible( true );
255  }
256 
257  m_canvas->Refresh();
258  }
259 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
void TraceAirWiresToTargets(wxDC *aDC)
Function TraceAirWiresToTargets This functions shows airwires to nearest connecting points (pads) fro...
Definition: ratsnest.cpp:196
static wxPoint s_CursorPos
Definition: ratsnest.cpp:160
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:130
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.
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:184
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:173
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:246
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:171
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.
COLORS_DESIGN_SETTINGS & Colors()
void Compile_Ratsnest(wxDC *aDC, bool aDisplayStatus)
Function Compile_Ratsnest Create the entire board ratsnest.
Definition: ratsnest.cpp:56
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: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
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:165
The common library.
int m_MaxLinksShowed
Definition: pcbstruct.h:86
static BOARD_CONNECTED_ITEM * s_ref
Definition: ratsnest.cpp:170
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:217
Module description (excepted pads)
void * GetDisplayOptions()
Function GetDisplayOptions A way to pass info to draw functions.
Definition: draw_panel.cpp:181
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: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