KiCad PCB EDA Suite
zones_functions_for_undo_redo.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2009 Jean-Pierre Charras <jp.charras@wanadoo.fr>
9  * Copyright (C) 2007-2015 KiCad Developers, see change_log.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 
30 /* These functions are relative to undo redo function, when zones are involved.
31  * When a zone outline is modified (or created) this zone, or others zones on the same layer
32  * and with the same netcode can change or can be deleted
33  * This is due to the fact overlapping zones are merged
34  * Also, when a zone outline is modified by adding a cutout area,
35  * this zone can be converted to more than one area, if the outline is break to 2 or more outlines
36  * and therefore new zones are created
37  *
38  * Due to the complexity of potential changes, and the fact there are only few zones
39  * in a board, and a zone has only few segments outlines, the more easy way to
40  * undo redo changes is to make a copy of all zones that can be changed
41  * and see after zone edition or creation what zones that are really modified,
42  * and ones they are modified (changes, deletion or addition)
43  */
44 
45 #include <fctsys.h>
46 #include <pgm_base.h>
47 #include <class_drawpanel.h>
48 #include <wxPcbStruct.h>
49 
50 #include <class_board.h>
51 #include <class_zone.h>
52 
53 #include <pcbnew.h>
54 #include <zones.h>
56 
64 bool ZONE_CONTAINER::IsSame( const ZONE_CONTAINER& aZoneToCompare )
65 {
66  // compare basic parameters:
67  if( GetLayer() != aZoneToCompare.GetLayer() )
68  return false;
69 
70  if( GetNetCode() != aZoneToCompare.GetNetCode() )
71  return false;
72 
73  if( GetPriority() != aZoneToCompare.GetPriority() )
74  return false;
75 
76  // Compare zone specific parameters
77  if( GetIsKeepout() != aZoneToCompare.GetIsKeepout() )
78  return false;
79 
80  if( GetIsKeepout() )
81  {
82  if( GetDoNotAllowCopperPour() != aZoneToCompare.GetDoNotAllowCopperPour() )
83  return false;
84 
85  if( GetDoNotAllowVias() != aZoneToCompare.GetDoNotAllowVias() )
86  return false;
87 
88  if( GetDoNotAllowTracks() != aZoneToCompare.GetDoNotAllowTracks() )
89  return false;
90  }
91 
92  if( m_ArcToSegmentsCount != aZoneToCompare.GetArcSegmentCount() )
93  return false;
94 
95  if( m_ZoneClearance != aZoneToCompare.m_ZoneClearance )
96  return false;
97 
98  if( m_ZoneMinThickness != aZoneToCompare.GetMinThickness() )
99  return false;
100 
101  if( m_FillMode != aZoneToCompare.GetFillMode() )
102  return false;
103 
104  if( m_PadConnection != aZoneToCompare.m_PadConnection )
105  return false;
106 
107  if( m_ThermalReliefGap != aZoneToCompare.m_ThermalReliefGap )
108  return false;
109 
111  return false;
112 
113 
114  // Compare outlines
115  wxASSERT( m_Poly ); // m_Poly == NULL Should never happen
116  wxASSERT( aZoneToCompare.Outline() );
117 
118  if( Outline() != aZoneToCompare.Outline() ) // Compare vector
119  return false;
120 
121  return true;
122 }
123 
124 
137 int SaveCopyOfZones( PICKED_ITEMS_LIST& aPickList, BOARD* aPcb, int aNetCode, LAYER_NUM aLayer )
138 {
139  int copyCount = 0;
140 
141  for( unsigned ii = 0; ; ii++ )
142  {
143  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
144 
145  if( zone == NULL ) // End of list
146  break;
147 
148  if( aNetCode >= 0 && aNetCode != zone->GetNetCode() )
149  continue;
150 
151  if( aLayer >= 0 && aLayer != zone->GetLayer() )
152  continue;
153 
154  ZONE_CONTAINER* zoneDup = new ZONE_CONTAINER( *zone );
155  zoneDup->SetParent( aPcb );
156  ITEM_PICKER picker( zone, UR_CHANGED );
157  picker.SetLink( zoneDup );
158  aPickList.PushItem( picker );
159  copyCount++;
160  }
161 
162  return copyCount;
163 }
164 
165 
203  PICKED_ITEMS_LIST& aAuxiliaryList,
204  BOARD* aPcb )
205 {
206  for( unsigned kk = 0; kk < aPickList.GetCount(); kk++ )
207  {
208  UNDO_REDO_T status = aPickList.GetPickedItemStatus( kk );
209 
210  ZONE_CONTAINER* ref = (ZONE_CONTAINER*) aPickList.GetPickedItem( kk );
211 
212  for( unsigned ii = 0; ; ii++ ) // analyse the main picked list
213  {
214  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
215 
216  if( zone == NULL )
217  {
218  /* End of list: the stored item is not found:
219  * it must be in aDeletedList:
220  * search it and restore initial values
221  * or
222  * if flagged UR_NEW: remove it definitively
223  */
224  if( status == UR_NEW )
225  {
226  delete ref;
227  ref = NULL;
228  aPickList.RemovePicker( kk );
229  kk--;
230  }
231  else
232  {
233  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
234  aPickList.SetPickedItemStatus( UR_DELETED, kk );
235 
236  wxASSERT_MSG( zcopy != NULL,
237  wxT( "UpdateCopyOfZonesList() error: link = NULL" ) );
238 
239  *ref = *zcopy;
240 
241  // the copy was deleted; the link does not exists now.
242  aPickList.SetPickedItemLink( NULL, kk );
243  delete zcopy;
244  }
245 
246  // Remove this item from aAuxiliaryList, mainly for tests purpose
247  bool notfound = true;
248 
249  for( unsigned nn = 0; nn < aAuxiliaryList.GetCount(); nn++ )
250  {
251  if( ref != NULL && aAuxiliaryList.GetPickedItem( nn ) == ref )
252  {
253  aAuxiliaryList.RemovePicker( nn );
254  notfound = false;
255  break;
256  }
257  }
258 
259  if( notfound ) // happens when the new zone overlaps an existing zone
260  // and these zones are combined
261  {
262  DBG( printf(
263  "UpdateCopyOfZonesList(): item not found in aAuxiliaryList,"
264  "combined with an other zone\n" ) );
265  }
266  break;
267  }
268 
269  if( zone == ref ) // picked zone found
270  {
271  if( aPickList.GetPickedItemStatus( kk ) != UR_NEW )
272  {
273  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
274 
275  if( zone->IsSame( *zcopy ) ) // Remove picked, because no changes
276  {
277  delete zcopy; // Delete copy
278  aPickList.RemovePicker( kk );
279  kk--;
280  }
281  }
282 
283  break;
284  }
285  }
286  }
287 
288  // Add new zones in main pick list, and remove pickers from Auxiliary List
289  for( unsigned ii = 0; ii < aAuxiliaryList.GetCount(); )
290  {
291  if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_NEW )
292  {
293  ITEM_PICKER picker = aAuxiliaryList.GetItemWrapper( ii );
294  aPickList.PushItem( picker );
295  aAuxiliaryList.RemovePicker( ii );
296  }
297  else if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_DELETED )
298  {
299  delete aAuxiliaryList.GetPickedItemLink( ii );
300  aAuxiliaryList.RemovePicker( ii );
301  }
302  else
303  ii++;
304  }
305 
306  // Should not occur:
307  wxASSERT_MSG( aAuxiliaryList.GetCount() == 0,
308  wxT( "UpdateCopyOfZonesList() error: aAuxiliaryList not empty." ) );
309 }
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:78
int m_ZoneClearance
Clearance value in internal units.
Definition: class_zone.h:747
int GetMinThickness() const
Definition: class_zone.h:209
bool GetDoNotAllowCopperPour() const
Definition: class_zone.h:655
void UpdateCopyOfZonesList(PICKED_ITEMS_LIST &aPickList, PICKED_ITEMS_LIST &aAuxiliaryList, BOARD *aPcb)
Function UpdateCopyOfZonesList check a pick list to remove zones identical to their copies and set th...
int GetArcSegmentCount() const
Definition: class_zone.h:198
Class BOARD to handle a board.
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:247
Classes to handle copper zones.
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
void SetLink(EDA_ITEM *aItem)
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
bool GetIsKeepout() const
Accessors to parameters used in Keepout zones:
Definition: class_zone.h:654
int SaveCopyOfZones(PICKED_ITEMS_LIST &aPickList, BOARD *aPcb, int aNetCode, LAYER_NUM aLayer)
Function SaveCopyOfZones creates a copy of zones having a given netcode on a given layer...
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
int m_ArcToSegmentsCount
The number of segments to convert a circle to a polygon.
Definition: class_zone.h:752
int m_ThermalReliefCopperBridge
Definition: class_zone.h:761
ZoneConnection m_PadConnection
Definition: class_zone.h:746
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:213
bool IsSame(const ZONE_CONTAINER &aZoneToCompare)
Function IsSame tests if 2 zones are equivalent: 2 zones are equivalent if they have same parameters ...
int m_FillMode
How to fill areas: 0 => use filled polygons, 1 => fill with segments.
Definition: class_zone.h:765
SHAPE_POLY_SET * m_Poly
Outline of the zone.
Definition: class_zone.h:723
bool RemovePicker(unsigned aIdx)
Function RemovePicker removes one entry (one picker) from the list of picked items.
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
unsigned GetCount() const
Function GetCount.
int GetNetCode() const
Function GetNetCode.
PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
int m_ThermalReliefGap
Width of the copper bridge in thermal reliefs.
Definition: class_zone.h:758
bool GetDoNotAllowTracks() const
Definition: class_zone.h:657
int m_ZoneMinThickness
Minimum thickness value in filled areas.
Definition: class_zone.h:748
see class PGM_BASE
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:982
int GetFillMode() const
Definition: class_zone.h:186
bool SetPickedItemStatus(UNDO_REDO_T aStatus, unsigned aIdx)
Function SetPickedItemStatus sets the type of undo/redo operation for a given picked item...
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
bool GetDoNotAllowVias() const
Definition: class_zone.h:656
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
bool SetPickedItemLink(EDA_ITEM *aLink, unsigned aIdx)
Function SetPickedItemLink set the link associated to a given picked item.
#define DBG(x)
Definition: fctsys.h:33
unsigned GetPriority() const
Function GetPriority.
Definition: class_zone.h:119
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
Function GetItemWrapper.