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  if( GetDoNotAllowPads() != aZoneToCompare.GetDoNotAllowPads() )
90  return false;
91 
92  if( GetDoNotAllowFootprints() != aZoneToCompare.GetDoNotAllowFootprints() )
93  return false;
94  }
95 
96  if( m_ZoneClearance != aZoneToCompare.m_ZoneClearance )
97  return false;
98 
99  if( m_ZoneMinThickness != aZoneToCompare.GetMinThickness() )
100  return false;
101 
102  if( m_FillMode != aZoneToCompare.GetFillMode() )
103  return false;
104 
105  if( m_PadConnection != aZoneToCompare.m_PadConnection )
106  return false;
107 
108  if( m_ThermalReliefGap != aZoneToCompare.m_ThermalReliefGap )
109  return false;
110 
112  return false;
113 
114 
115  // Compare outlines
116  wxASSERT( m_Poly ); // m_Poly == NULL Should never happen
117  wxASSERT( aZoneToCompare.Outline() );
118 
119  if( Outline() != aZoneToCompare.Outline() ) // Compare vector
120  return false;
121 
122  return true;
123 }
124 
125 
138 int SaveCopyOfZones( PICKED_ITEMS_LIST& aPickList, BOARD* aPcb, int aNetCode, LAYER_NUM aLayer )
139 {
140  int copyCount = 0;
141 
142  for( unsigned ii = 0; ; ii++ )
143  {
144  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
145 
146  if( zone == NULL ) // End of list
147  break;
148 
149  if( aNetCode >= 0 && aNetCode != zone->GetNetCode() )
150  continue;
151 
152  if( aLayer >= 0 && aLayer != zone->GetLayer() )
153  continue;
154 
155  ZONE_CONTAINER* zoneDup = new ZONE_CONTAINER( *zone );
156  zoneDup->SetParent( aPcb );
157  ITEM_PICKER picker( zone, UR_CHANGED );
158  picker.SetLink( zoneDup );
159  aPickList.PushItem( picker );
160  copyCount++;
161  }
162 
163  return copyCount;
164 }
165 
166 
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 another 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 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
bool GetDoNotAllowPads() const
Definition: class_zone.h:665
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
int m_ZoneClearance
Clearance value in internal units.
Definition: class_zone.h:789
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:219
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:252
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.
virtual void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:200
bool GetDoNotAllowVias() const
Definition: class_zone.h:663
ZONE_CONNECTION m_PadConnection
Definition: class_zone.h:788
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:806
#define NULL
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:143
SHAPE_POLY_SET * m_Poly
Outline of the zone.
Definition: class_zone.h:762
bool GetDoNotAllowCopperPour() const
Definition: class_zone.h:662
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.
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:661
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:803
int m_ZoneMinThickness
Minimum thickness value in filled areas.
Definition: class_zone.h:790
ZONE_FILL_MODE m_FillMode
How to fill areas: ZONE_FILL_MODE::POLYGONS => use solid polygons ZONE_FILL_MODE::HATCH_PATTERN => us...
Definition: class_zone.h:813
unsigned GetPriority() const
Function GetPriority.
Definition: class_zone.h:100
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.
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:181
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
int GetMinThickness() const
Definition: class_zone.h:191
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:664
bool GetDoNotAllowFootprints() const
Definition: class_zone.h:666
#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:892