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  *
32  * When a zone outline is modified (or created) this zone, or others zones on the same layer
33  * and with the same netcode can change or can be deleted due to the fact overlapping zones are
34  * merged. Also, when a zone outline is modified by adding a cutout area, this zone can be
35  * converted to more than one area, if the outline is break to 2 or more outlines and therefore
36  * new zones are created
37  *
38  * Due to the complexity of potential changes, and the fact there are only few zones in a board,
39  * and a zone has only few segments outlines, the more easy way to undo redo changes is to make
40  * a copy of all zones that can be changed and see after zone editing or creation what zones that
41  * are really modified, and ones they are modified (changes, deletion or addition)
42  */
43 
44 #include <fctsys.h>
45 #include <pgm_base.h>
46 #include <pcb_edit_frame.h>
47 
48 #include <class_board.h>
49 #include <class_zone.h>
50 
51 #include <pcbnew.h>
52 #include <zones.h>
54 
62 bool ZONE_CONTAINER::IsSame( const ZONE_CONTAINER& aZoneToCompare )
63 {
64  // compare basic parameters:
65  if( GetLayer() != aZoneToCompare.GetLayer() )
66  return false;
67 
68  if( GetNetCode() != aZoneToCompare.GetNetCode() )
69  return false;
70 
71  if( GetPriority() != aZoneToCompare.GetPriority() )
72  return false;
73 
74  // Compare zone specific parameters
75  if( GetIsKeepout() != aZoneToCompare.GetIsKeepout() )
76  return false;
77 
78  if( GetIsKeepout() )
79  {
80  if( GetDoNotAllowCopperPour() != aZoneToCompare.GetDoNotAllowCopperPour() )
81  return false;
82 
83  if( GetDoNotAllowVias() != aZoneToCompare.GetDoNotAllowVias() )
84  return false;
85 
86  if( GetDoNotAllowTracks() != aZoneToCompare.GetDoNotAllowTracks() )
87  return false;
88  }
89 
90  if( m_ZoneClearance != aZoneToCompare.m_ZoneClearance )
91  return false;
92 
93  if( m_ZoneMinThickness != aZoneToCompare.GetMinThickness() )
94  return false;
95 
96  if( m_FillMode != aZoneToCompare.GetFillMode() )
97  return false;
98 
99  if( m_PadConnection != aZoneToCompare.m_PadConnection )
100  return false;
101 
102  if( m_ThermalReliefGap != aZoneToCompare.m_ThermalReliefGap )
103  return false;
104 
106  return false;
107 
108 
109  // Compare outlines
110  wxASSERT( m_Poly ); // m_Poly == NULL Should never happen
111  wxASSERT( aZoneToCompare.Outline() );
112 
113  if( Outline() != aZoneToCompare.Outline() ) // Compare vector
114  return false;
115 
116  return true;
117 }
118 
119 
132 int SaveCopyOfZones( PICKED_ITEMS_LIST& aPickList, BOARD* aPcb, int aNetCode, LAYER_NUM aLayer )
133 {
134  int copyCount = 0;
135 
136  for( unsigned ii = 0; ; ii++ )
137  {
138  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
139 
140  if( zone == NULL ) // End of list
141  break;
142 
143  if( aNetCode >= 0 && aNetCode != zone->GetNetCode() )
144  continue;
145 
146  if( aLayer >= 0 && aLayer != zone->GetLayer() )
147  continue;
148 
149  ZONE_CONTAINER* zoneDup = new ZONE_CONTAINER( *zone );
150  zoneDup->SetParent( aPcb );
151  ITEM_PICKER picker( zone, UR_CHANGED );
152  picker.SetLink( zoneDup );
153  aPickList.PushItem( picker );
154  copyCount++;
155  }
156 
157  return copyCount;
158 }
159 
160 
197  PICKED_ITEMS_LIST& aAuxiliaryList,
198  BOARD* aPcb )
199 {
200  for( unsigned kk = 0; kk < aPickList.GetCount(); kk++ )
201  {
202  UNDO_REDO_T status = aPickList.GetPickedItemStatus( kk );
203 
204  ZONE_CONTAINER* ref = (ZONE_CONTAINER*) aPickList.GetPickedItem( kk );
205 
206  for( unsigned ii = 0; ; ii++ ) // analyse the main picked list
207  {
208  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
209 
210  if( zone == NULL )
211  {
212  /* End of list: the stored item is not found:
213  * it must be in aDeletedList:
214  * search it and restore initial values
215  * or
216  * if flagged UR_NEW: remove it definitively
217  */
218  if( status == UR_NEW )
219  {
220  delete ref;
221  ref = NULL;
222  aPickList.RemovePicker( kk );
223  kk--;
224  }
225  else
226  {
227  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
228  aPickList.SetPickedItemStatus( UR_DELETED, kk );
229 
230  wxASSERT_MSG( zcopy != NULL,
231  wxT( "UpdateCopyOfZonesList() error: link = NULL" ) );
232 
233  *ref = *zcopy;
234 
235  // the copy was deleted; the link does not exists now.
236  aPickList.SetPickedItemLink( NULL, kk );
237  delete zcopy;
238  }
239 
240  // Remove this item from aAuxiliaryList, mainly for tests purpose
241  bool notfound = true;
242 
243  for( unsigned nn = 0; nn < aAuxiliaryList.GetCount(); nn++ )
244  {
245  if( ref != NULL && aAuxiliaryList.GetPickedItem( nn ) == ref )
246  {
247  aAuxiliaryList.RemovePicker( nn );
248  notfound = false;
249  break;
250  }
251  }
252 
253  if( notfound ) // happens when the new zone overlaps an existing zone
254  // and these zones are combined
255  {
256  DBG( printf(
257  "UpdateCopyOfZonesList(): item not found in aAuxiliaryList,"
258  "combined with another zone\n" ) );
259  }
260  break;
261  }
262 
263  if( zone == ref ) // picked zone found
264  {
265  if( aPickList.GetPickedItemStatus( kk ) != UR_NEW )
266  {
267  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
268 
269  if( zone->IsSame( *zcopy ) ) // Remove picked, because no changes
270  {
271  delete zcopy; // Delete copy
272  aPickList.RemovePicker( kk );
273  kk--;
274  }
275  }
276 
277  break;
278  }
279  }
280  }
281 
282  // Add new zones in main pick list, and remove pickers from Auxiliary List
283  for( unsigned ii = 0; ii < aAuxiliaryList.GetCount(); )
284  {
285  if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_NEW )
286  {
287  ITEM_PICKER picker = aAuxiliaryList.GetItemWrapper( ii );
288  aPickList.PushItem( picker );
289  aAuxiliaryList.RemovePicker( ii );
290  }
291  else if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_DELETED )
292  {
293  delete aAuxiliaryList.GetPickedItemLink( ii );
294  aAuxiliaryList.RemovePicker( ii );
295  }
296  else
297  ii++;
298  }
299 
300  // Should not occur:
301  wxASSERT_MSG( aAuxiliaryList.GetCount() == 0,
302  wxT( "UpdateCopyOfZonesList() error: aAuxiliaryList not empty." ) );
303 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:57
int m_ZoneClearance
Clearance value in internal units.
Definition: class_zone.h:729
int GetNetCode() const
Function GetNetCode.
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...
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
virtual PCB_LAYER_ID GetLayer() const override
Function GetLayer returns the primary layer this item is on.
Definition: class_zone.cpp:190
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:237
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
void SetLink(EDA_ITEM *aItem)
unsigned GetCount() const
Function GetCount.
bool GetDoNotAllowVias() const
Definition: class_zone.h:623
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,...
int m_ThermalReliefCopperBridge
Definition: class_zone.h:746
ZoneConnection m_PadConnection
Definition: class_zone.h:728
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:225
bool IsSame(const ZONE_CONTAINER &aZoneToCompare)
Function IsSame tests if 2 zones are equivalent: 2 zones are equivalent if they have same parameters ...
ZONE_FILL_MODE GetFillMode() const
Definition: class_zone.h:150
SHAPE_POLY_SET * m_Poly
Outline of the zone.
Definition: class_zone.h:704
bool GetDoNotAllowCopperPour() const
Definition: class_zone.h:622
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
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.
bool GetIsKeepout() const
Accessors to parameters used in Keepout zones:
Definition: class_zone.h:621
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
Function GetItemWrapper.
int m_ThermalReliefGap
Width of the copper bridge in thermal reliefs.
Definition: class_zone.h:743
int m_ZoneMinThickness
Minimum thickness value in filled areas.
Definition: class_zone.h:730
ZONE_FILL_MODE m_FillMode
How to fill areas: ZFM_POLYGONS => use solid polygons ZFM_HATCH_PATTERN => use a grid pattern as shap...
Definition: class_zone.h:753
unsigned GetPriority() const
Function GetPriority.
Definition: class_zone.h:94
see class PGM_BASE
bool SetPickedItemStatus(UNDO_REDO_T aStatus, unsigned aIdx)
Function SetPickedItemStatus sets the type of undo/redo operation for a given picked item.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
int GetMinThickness() const
Definition: class_zone.h:176
bool SetPickedItemLink(EDA_ITEM *aLink, unsigned aIdx)
Function SetPickedItemLink set the link associated to a given picked item.
bool GetDoNotAllowTracks() const
Definition: class_zone.h:624
#define DBG(x)
Definition: fctsys.h:33
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:926