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( GetLayerSet() != aZoneToCompare.GetLayerSet() )
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  if( m_zoneName != aZoneToCompare.m_zoneName )
115  return false;
116 
117  if( m_islandRemovalMode != aZoneToCompare.m_islandRemovalMode )
118  return false;
119 
120  if( m_minIslandArea != aZoneToCompare.m_minIslandArea )
121  return false;
122 
123 
124  // Compare outlines
125  wxASSERT( m_Poly ); // m_Poly == NULL Should never happen
126  wxASSERT( aZoneToCompare.Outline() );
127 
128  if( Outline() != aZoneToCompare.Outline() ) // Compare vector
129  return false;
130 
131  return true;
132 }
133 
134 
147 int SaveCopyOfZones( PICKED_ITEMS_LIST& aPickList, BOARD* aPcb, int aNetCode, LAYER_NUM aLayer )
148 {
149  int copyCount = 0;
150 
151  for( unsigned ii = 0; ; ii++ )
152  {
153  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
154 
155  if( zone == NULL ) // End of list
156  break;
157 
158  if( aNetCode >= 0 && aNetCode != zone->GetNetCode() )
159  continue;
160 
161  if( aLayer >= 0 && !zone->GetLayerSet().test( aLayer ) )
162  continue;
163 
164  ZONE_CONTAINER* zoneDup = new ZONE_CONTAINER( *zone );
165  zoneDup->SetParent( aPcb );
166  ITEM_PICKER picker( nullptr, zone, UR_CHANGED );
167  picker.SetLink( zoneDup );
168  aPickList.PushItem( picker );
169  copyCount++;
170  }
171 
172  return copyCount;
173 }
174 
175 
212  PICKED_ITEMS_LIST& aAuxiliaryList,
213  BOARD* aPcb )
214 {
215  for( unsigned kk = 0; kk < aPickList.GetCount(); kk++ )
216  {
217  UNDO_REDO_T status = aPickList.GetPickedItemStatus( kk );
218 
219  ZONE_CONTAINER* ref = (ZONE_CONTAINER*) aPickList.GetPickedItem( kk );
220 
221  for( unsigned ii = 0; ; ii++ ) // analyse the main picked list
222  {
223  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
224 
225  if( zone == NULL )
226  {
227  /* End of list: the stored item is not found:
228  * it must be in aDeletedList:
229  * search it and restore initial values
230  * or
231  * if flagged UR_NEW: remove it definitively
232  */
233  if( status == UR_NEW )
234  {
235  delete ref;
236  ref = NULL;
237  aPickList.RemovePicker( kk );
238  kk--;
239  }
240  else
241  {
242  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
243  aPickList.SetPickedItemStatus( UR_DELETED, kk );
244 
245  wxASSERT_MSG( zcopy != NULL,
246  wxT( "UpdateCopyOfZonesList() error: link = NULL" ) );
247 
248  *ref = *zcopy;
249 
250  // the copy was deleted; the link does not exists now.
251  aPickList.SetPickedItemLink( NULL, kk );
252  delete zcopy;
253  }
254 
255  // Remove this item from aAuxiliaryList, mainly for tests purpose
256  bool notfound = true;
257 
258  for( unsigned nn = 0; nn < aAuxiliaryList.GetCount(); nn++ )
259  {
260  if( ref != NULL && aAuxiliaryList.GetPickedItem( nn ) == ref )
261  {
262  aAuxiliaryList.RemovePicker( nn );
263  notfound = false;
264  break;
265  }
266  }
267 
268  if( notfound ) // happens when the new zone overlaps an existing zone
269  // and these zones are combined
270  {
271  DBG( printf(
272  "UpdateCopyOfZonesList(): item not found in aAuxiliaryList,"
273  "combined with another zone\n" ) );
274  }
275  break;
276  }
277 
278  if( zone == ref ) // picked zone found
279  {
280  if( aPickList.GetPickedItemStatus( kk ) != UR_NEW )
281  {
282  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
283 
284  if( zone->IsSame( *zcopy ) ) // Remove picked, because no changes
285  {
286  delete zcopy; // Delete copy
287  aPickList.RemovePicker( kk );
288  kk--;
289  }
290  }
291 
292  break;
293  }
294  }
295  }
296 
297  // Add new zones in main pick list, and remove pickers from Auxiliary List
298  for( unsigned ii = 0; ii < aAuxiliaryList.GetCount(); )
299  {
300  if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_NEW )
301  {
302  ITEM_PICKER picker = aAuxiliaryList.GetItemWrapper( ii );
303  aPickList.PushItem( picker );
304  aAuxiliaryList.RemovePicker( ii );
305  }
306  else if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_DELETED )
307  {
308  delete aAuxiliaryList.GetPickedItemLink( ii );
309  aAuxiliaryList.RemovePicker( ii );
310  }
311  else
312  ii++;
313  }
314 
315  // Should not occur:
316  wxASSERT_MSG( aAuxiliaryList.GetCount() == 0,
317  wxT( "UpdateCopyOfZonesList() error: aAuxiliaryList not empty." ) );
318 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
bool GetDoNotAllowPads() const
Definition: class_zone.h:717
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:61
int m_ZoneClearance
Clearance value in internal units.
Definition: class_zone.h:857
int GetNetCode() const
Function GetNetCode.
ISLAND_REMOVAL_MODE m_islandRemovalMode
Definition: class_zone.h:861
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...
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:276
long long int m_minIslandArea
When island removal mode is set to AREA, islands below this area will be removed.
Definition: class_zone.h:867
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:196
bool GetDoNotAllowVias() const
Definition: class_zone.h:715
ZONE_CONNECTION m_PadConnection
Definition: class_zone.h:856
virtual LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_zone.cpp:284
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,...
wxString m_zoneName
An optional unique name for this zone, used for identifying it in DRC checking.
Definition: class_zone.h:832
int m_ThermalReliefCopperBridge
Definition: class_zone.h:882
#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:153
SHAPE_POLY_SET * m_Poly
Outline of the zone.
Definition: class_zone.h:827
bool GetDoNotAllowCopperPour() const
Definition: class_zone.h:714
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:713
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:879
int m_ZoneMinThickness
Minimum thickness value in filled areas.
Definition: class_zone.h:858
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:889
unsigned GetPriority() const
Function GetPriority.
Definition: class_zone.h:106
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:180
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
int GetMinThickness() const
Definition: class_zone.h:206
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:716
bool GetDoNotAllowFootprints() const
Definition: class_zone.h:718
#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:899