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