KiCad PCB EDA Suite
edit_track_width.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-2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2016 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 <class_drawpanel.h>
33 #include <wxPcbStruct.h>
34 
35 #include <class_board.h>
36 #include <class_track.h>
37 
38 #include <pcbnew.h>
39 #include <drc_stuff.h>
40 
41 
43  PICKED_ITEMS_LIST* aItemsListPicker,
44  bool aUseNetclassValue )
45 {
46  /* Modify one track segment width or one via diameter and drill (using DRC control).
47  * Basic function used by other routines when editing tracks or vias
48  * aTrackItem = the track segment or via to modify
49  * aItemsListPicker = the list picker to use for an undo command (can be NULL)
50  * aUseNetclassValue = true to use NetClass value, false to use BOARD::m_designSettings value
51  * return true if done, false if no not change (due to DRC error)
52  */
53  int initial_width, new_width;
54  int initial_drill = -1,new_drill = -1;
55  bool change_ok = false;
56  NETINFO_ITEM* net = NULL;
57 
58  if( aUseNetclassValue )
59  net = aTrackItem->GetNet();
60 
61  initial_width = aTrackItem->GetWidth();
62 
63  if( net )
64  new_width = net->GetTrackWidth();
65  else
67 
68  if( aTrackItem->Type() == PCB_VIA_T )
69  {
70  const VIA *via = static_cast<const VIA *>( aTrackItem );
71 
72  // Micro vias have a size only defined in their netclass
73  // (no specific values defined by a table of specific value)
74  // Ensure the netclass is accessible:
75  if( via->GetViaType() == VIA_MICROVIA && net == NULL )
76  net = aTrackItem->GetNet();
77 
78  // Get the draill value, regardless it is default or specific
79  initial_drill = via->GetDrillValue();
80 
81  if( net )
82  {
83  new_width = net->GetViaSize();
84  new_drill = net->GetViaDrillSize();
85  }
86  else
87  {
88  new_width = GetDesignSettings().GetCurrentViaSize();
89  new_drill = GetDesignSettings().GetCurrentViaDrill();
90  }
91 
92  if( via->GetViaType() == VIA_MICROVIA )
93  {
94  if( net )
95  {
96  new_width = net->GetMicroViaSize();
97  new_drill = net->GetMicroViaDrillSize();
98  }
99  else
100  {
101  // Should not occur
102  }
103  }
104 
105  // Old versions set a drill value <= 0, when the default netclass it used
106  // but it could be better to set the drill value to the actual value
107  // to avoid issues for existing vias, if the default drill value is modified
108  // in the netclass, and not in current vias.
109  if( via->GetDrill() <= 0 ) // means default netclass drill value used
110  {
111  initial_drill = -1; // Force drill vias re-initialization
112  }
113  }
114 
115  aTrackItem->SetWidth( new_width );
116 
117  // make a DRC test because the new size is bigger than the old size
118  if( initial_width < new_width )
119  {
120  int diagdrc = OK_DRC;
121 
122  if( g_Drc_On )
123  diagdrc = m_drc->Drc( aTrackItem, GetBoard()->m_Track );
124 
125  if( diagdrc == OK_DRC )
126  change_ok = true;
127  }
128  else if( initial_width > new_width )
129  {
130  change_ok = true;
131  }
132  else if( (aTrackItem->Type() == PCB_VIA_T) )
133  {
134  // if a via has its drill value changed, force change
135  if( initial_drill != new_drill )
136  change_ok = true;
137  }
138 
139  if( change_ok )
140  {
141  OnModify();
142 
143  if( aItemsListPicker )
144  {
145  aTrackItem->SetWidth( initial_width );
146  ITEM_PICKER picker( aTrackItem, UR_CHANGED );
147  picker.SetLink( aTrackItem->Clone() );
148  aItemsListPicker->PushItem( picker );
149  aTrackItem->SetWidth( new_width );
150 
151  if( aTrackItem->Type() == PCB_VIA_T )
152  {
153  // Set new drill value. Note: currently microvias have only a default drill value
154  VIA *via = static_cast<VIA *>( aTrackItem );
155  if( new_drill > 0 )
156  via->SetDrill( new_drill );
157  else
158  via->SetDrillDefault();
159  }
160  }
161  }
162  else
163  {
164  aTrackItem->SetWidth( initial_width );
165  }
166 
167  return change_ok;
168 }
169 
170 
177 void PCB_EDIT_FRAME::Edit_TrackSegm_Width( wxDC* aDC, TRACK* aTrackItem )
178 {
179  PICKED_ITEMS_LIST itemsListPicker;
180  bool change = SetTrackSegmentWidth( aTrackItem, &itemsListPicker, false );
181 
182  if( change == 0 || aTrackItem->GetFlags() )
183  return; // No change
184 
185  // The segment has changed: redraw it and save it in undo list
186  if( aDC )
187  {
188  TRACK* oldsegm = (TRACK*) itemsListPicker.GetPickedItemLink( 0 );
189  wxASSERT( oldsegm );
190  m_canvas->CrossHairOff( aDC ); // Erase cursor shape
191  oldsegm->Draw( m_canvas, aDC, GR_XOR ); // Erase old track shape
192  aTrackItem->Draw( m_canvas, aDC, GR_OR ); // Display new track shape
193  m_canvas->CrossHairOn( aDC ); // Display cursor shape
194  }
195 
196  SaveCopyInUndoList( itemsListPicker, UR_CHANGED );
197 }
198 
199 
200 void PCB_EDIT_FRAME::Edit_Track_Width( wxDC* aDC, TRACK* aTrackSegment )
201 {
202  /* Modify a full track (a trace) width (using DRC control).
203  * a full track is the set of track segments between 2 nodes: pads or a node that has
204  * more than 2 segments connected
205  * aDC = the curred device context (can be NULL)
206  * aTrackSegment = a via or a track belonging to the trace to change
207  */
208  TRACK* pt_track;
209  int nb_segm;
210 
211  if( aTrackSegment == NULL )
212  return;
213 
214  pt_track = GetBoard()->MarkTrace( aTrackSegment, &nb_segm, NULL, NULL, true );
215 
216  PICKED_ITEMS_LIST itemsListPicker;
217  bool change = false;
218 
219  for( int ii = 0; ii < nb_segm; ii++, pt_track = pt_track->Next() )
220  {
221  pt_track->SetState( BUSY, false );
222 
223  if( SetTrackSegmentWidth( pt_track, &itemsListPicker, false ) )
224  change = true;
225  }
226 
227  if( !change )
228  return;
229 
230  // Some segment have changed: redraw them and save in undo list
231  if( aDC )
232  {
233  m_canvas->CrossHairOff( aDC ); // Erase cursor shape
234 
235  for( unsigned ii = 0; ii < itemsListPicker.GetCount(); ii++ )
236  {
237  TRACK* segm = (TRACK*) itemsListPicker.GetPickedItemLink( ii );
238  segm->Draw( m_canvas, aDC, GR_XOR ); // Erase old track shape
239  segm = (TRACK*) itemsListPicker.GetPickedItem( ii );
240  segm->Draw( m_canvas, aDC, GR_OR ); // Display new track shape
241 
242 // fixme: commit!
243 // segm->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
244  }
245 
246  m_canvas->CrossHairOn( aDC ); // Display cursor shape
247  }
248 
249  SaveCopyInUndoList( itemsListPicker, UR_CHANGED );
250 }
251 
252 
253 bool PCB_EDIT_FRAME::Change_Net_Tracks_And_Vias_Sizes( int aNetcode, bool aUseNetclassValue )
254 {
255  /* Reset all tracks width and vias diameters and drill
256  * to their default Netclass value or current values
257  * aNetcode : the netcode of the net to edit
258  * aUseNetclassValue = true to use netclass values, false to use current values
259  */
260  TRACK* pt_segm;
261 
262  if( aNetcode <= 0 )
263  return false;
264 
265  // Examine segments
266  PICKED_ITEMS_LIST itemsListPicker;
267  bool change = false;
268 
269  for( pt_segm = GetBoard()->m_Track; pt_segm != NULL; pt_segm = pt_segm->Next() )
270  {
271  if( aNetcode != pt_segm->GetNetCode() ) // not in net
272  continue;
273 
274  // we have found a item member of the net
275  if( SetTrackSegmentWidth( pt_segm, &itemsListPicker, aUseNetclassValue ) )
276  change = true;
277  }
278 
279  if( !change )
280  return false;
281 
282  // Some segment have changed: save them in undo list
283  SaveCopyInUndoList( itemsListPicker, UR_CHANGED );
284  return true;
285 }
286 
287 
289 {
290  TRACK* pt_segm;
291 
292  // read and edit tracks and vias if required
293  PICKED_ITEMS_LIST itemsListPicker;
294  bool change = false;
295 
296  for( pt_segm = GetBoard()->m_Track; pt_segm != NULL; pt_segm = pt_segm->Next() )
297  {
298  if( (pt_segm->Type() == PCB_VIA_T ) && aVia )
299  {
300  if( SetTrackSegmentWidth( pt_segm, &itemsListPicker, true ) )
301  change = true;
302  }
303 
304  if( (pt_segm->Type() == PCB_TRACE_T ) && aTrack )
305  {
306  if( SetTrackSegmentWidth( pt_segm, &itemsListPicker, true ) )
307  change = true;
308  }
309  }
310 
311  if( !change )
312  return false;
313 
314  // Some segment have changed: save them in undo list
315  SaveCopyInUndoList( itemsListPicker, UR_CHANGED );
316 
317  return true;
318 }
void Edit_Track_Width(wxDC *aDC, TRACK *aTrackSegment)
Function Edit_Track_Width Modify a full track width (using DRC control).
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
virtual void OnModify() override
Function OnModify must be called after a board change to set the modified flag.
Definition: pcbframe.cpp:993
bool g_Drc_On
Definition: pcbnew.cpp:70
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:255
#define OK_DRC
Definition: drc_stuff.h:35
int GetCurrentViaDrill() const
Function GetCurrentViaDrill.
Class BOARD to handle a board.
virtual EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
BOARD * GetBoard() const
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
#define BUSY
Pcbnew: flag indicating that the structure has.
Definition: base_struct.h:134
DRC * m_drc
the DRC controller, see drc.cpp
Definition: wxPcbStruct.h:96
int GetCurrentViaSize() const
Function GetCurrentViaSize.
int GetDrill() const
Function GetDrill returns the local drill setting for this VIA.
Definition: class_track.h:454
void SetLink(EDA_ITEM *aItem)
bool SetTrackSegmentWidth(TRACK *aTrackItem, PICKED_ITEMS_LIST *aItemsListPicker, bool aUseNetclassValue)
Function SetTrackSegmentWidth Modify one track segment width or one via diameter (using DRC control)...
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
Functions relatives to tracks, vias and segments used to fill zones.
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:107
void SetWidth(int aWidth)
Definition: class_track.h:114
VIATYPE_T GetViaType() const
Definition: class_track.h:439
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame...
TRACK * MarkTrace(TRACK *aTrace, int *aCount, double *aTraceLength, double *aInPackageLength, bool aReorder)
Function MarkTrace marks a chain of trace segments, connected to aTrace.
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
int GetTrackWidth()
Function GetTrackWidth returns the width of tracks used to route this net.
void SaveCopyInUndoList(BOARD_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0)) override
Function SaveCopyInUndoList Creates a new entry in undo list of commands.
Definition: undo_redo.cpp:172
void CrossHairOff(wxDC *DC)
Definition: draw_panel.cpp:253
void SetDrillDefault()
Function SetDrillDefault sets the drill value for vias to the default value UNDEFINED_DRILL_DIAMETER...
Definition: class_track.h:468
unsigned GetCount() const
Function GetCount.
Definition: gr_basic.h:42
int GetNetCode() const
Function GetNetCode.
void Draw(EDA_DRAW_PANEL *panel, wxDC *DC, GR_DRAWMODE aDrawMode, const wxPoint &aOffset=ZeroOffset) override
Function Draw BOARD_ITEMs have their own color information.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
Class NETINFO_ITEM handles the data for a net.
Definition: class_netinfo.h:69
void CrossHairOn(wxDC *DC)
Definition: draw_panel.cpp:260
TRACK * Next() const
Definition: class_track.h:98
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
int GetWidth() const
Definition: class_track.h:115
void SetDrill(int aDrill)
Function SetDrill sets the drill value for vias.
Definition: class_track.h:447
int Drc(TRACK *aRefSeg, TRACK *aList)
Function Drc tests the current segment and returns the result and displays the error in the status pa...
Definition: drc.cpp:155
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:108
int GetCurrentTrackWidth() const
Function GetCurrentTrackWidth.
bool Change_Net_Tracks_And_Vias_Sizes(int aNetcode, bool aUseNetclassValue)
Function Change_Net_Tracks_And_Vias_Sizes Reset all tracks width and vias diameters and drill to thei...
NETINFO_ITEM * GetNet() const
Function GetNet Returns NET_INFO object for a given item.
bool Reset_All_Tracks_And_Vias_To_Netclass_Values(bool aTrack, bool aVia)
Function Reset_All_Tracks_And_Vias_To_Netclass_Values Reset all tracks width and/or vias diameters an...
void Edit_TrackSegm_Width(wxDC *aDC, TRACK *aTrackItem)
Function Edit_TrackSegm_Width Modify one track segment width or one via diameter (using DRC control)...