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 <pgm_base.h>
45 #include <pcb_edit_frame.h>
46 
47 #include <class_board.h>
48 #include <class_zone.h>
49 
50 #include <pcbnew.h>
51 #include <zones.h>
53 
61 bool ZONE_CONTAINER::IsSame( const ZONE_CONTAINER& aZoneToCompare )
62 {
63  // compare basic parameters:
64  if( GetLayerSet() != aZoneToCompare.GetLayerSet() )
65  return false;
66 
67  if( GetNetCode() != aZoneToCompare.GetNetCode() )
68  return false;
69 
70  if( GetPriority() != aZoneToCompare.GetPriority() )
71  return false;
72 
73  // Compare zone specific parameters
74  if( GetIsRuleArea() != aZoneToCompare.GetIsRuleArea() )
75  return false;
76 
77  if( GetIsRuleArea() )
78  {
79  if( GetDoNotAllowCopperPour() != aZoneToCompare.GetDoNotAllowCopperPour() )
80  return false;
81 
82  if( GetDoNotAllowVias() != aZoneToCompare.GetDoNotAllowVias() )
83  return false;
84 
85  if( GetDoNotAllowTracks() != aZoneToCompare.GetDoNotAllowTracks() )
86  return false;
87 
88  if( GetDoNotAllowPads() != aZoneToCompare.GetDoNotAllowPads() )
89  return false;
90 
91  if( GetDoNotAllowFootprints() != aZoneToCompare.GetDoNotAllowFootprints() )
92  return false;
93  }
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  if( m_zoneName != aZoneToCompare.m_zoneName )
114  return false;
115 
116  if( m_islandRemovalMode != aZoneToCompare.m_islandRemovalMode )
117  return false;
118 
119  if( m_minIslandArea != aZoneToCompare.m_minIslandArea )
120  return false;
121 
122 
123  // Compare outlines
124  wxASSERT( m_Poly ); // m_Poly == NULL Should never happen
125  wxASSERT( aZoneToCompare.Outline() );
126 
127  if( Outline() != aZoneToCompare.Outline() ) // Compare vector
128  return false;
129 
130  return true;
131 }
132 
133 
146 int SaveCopyOfZones( PICKED_ITEMS_LIST& aPickList, BOARD* aPcb, int aNetCode, LAYER_NUM aLayer )
147 {
148  int copyCount = 0;
149 
150  for( unsigned ii = 0; ; ii++ )
151  {
152  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
153 
154  if( zone == NULL ) // End of list
155  break;
156 
157  if( aNetCode >= 0 && aNetCode != zone->GetNetCode() )
158  continue;
159 
160  if( aLayer >= 0 && !zone->GetLayerSet().test( aLayer ) )
161  continue;
162 
163  ZONE_CONTAINER* zoneDup = new ZONE_CONTAINER( *zone );
164  zoneDup->SetParent( aPcb );
165  ITEM_PICKER picker( nullptr, zone, UNDO_REDO::CHANGED );
166  picker.SetLink( zoneDup );
167  aPickList.PushItem( picker );
168  copyCount++;
169  }
170 
171  return copyCount;
172 }
173 
174 
211  PICKED_ITEMS_LIST& aAuxiliaryList,
212  BOARD* aPcb )
213 {
214  for( unsigned kk = 0; kk < aPickList.GetCount(); kk++ )
215  {
216  UNDO_REDO status = aPickList.GetPickedItemStatus( kk );
217 
218  ZONE_CONTAINER* ref = (ZONE_CONTAINER*) aPickList.GetPickedItem( kk );
219 
220  for( unsigned ii = 0; ; ii++ ) // analyse the main picked list
221  {
222  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
223 
224  if( zone == NULL )
225  {
226  /* End of list: the stored item is not found:
227  * it must be in aDeletedList:
228  * search it and restore initial values
229  * or
230  * if flagged NEWITEM: remove it definitively
231  */
232  if( status == UNDO_REDO::NEWITEM )
233  {
234  delete ref;
235  ref = NULL;
236  aPickList.RemovePicker( kk );
237  kk--;
238  }
239  else
240  {
241  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
242  aPickList.SetPickedItemStatus( UNDO_REDO::DELETED, kk );
243 
244  wxASSERT_MSG( zcopy != NULL,
245  wxT( "UpdateCopyOfZonesList() error: link = NULL" ) );
246 
247  *ref = *zcopy;
248 
249  // the copy was deleted; the link does not exists now.
250  aPickList.SetPickedItemLink( NULL, kk );
251  delete zcopy;
252  }
253 
254  // Remove this item from aAuxiliaryList, mainly for tests purpose
255  bool notfound = true;
256 
257  for( unsigned nn = 0; nn < aAuxiliaryList.GetCount(); nn++ )
258  {
259  if( ref != NULL && aAuxiliaryList.GetPickedItem( nn ) == ref )
260  {
261  aAuxiliaryList.RemovePicker( nn );
262  notfound = false;
263  break;
264  }
265  }
266 
267  if( notfound ) // happens when the new zone overlaps an existing zone
268  // and these zones are combined
269  {
270  #if defined(DEBUG)
271  printf( "UpdateCopyOfZonesList(): item not found in aAuxiliaryList,"
272  "combined with another zone\n" );
273  fflush(0);
274  #endif
275  }
276  break;
277  }
278 
279  if( zone == ref ) // picked zone found
280  {
281  if( aPickList.GetPickedItemStatus( kk ) != UNDO_REDO::NEWITEM )
282  {
283  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
284 
285  if( zone->IsSame( *zcopy ) ) // Remove picked, because no changes
286  {
287  delete zcopy; // Delete copy
288  aPickList.RemovePicker( kk );
289  kk--;
290  }
291  }
292 
293  break;
294  }
295  }
296  }
297 
298  // Add new zones in main pick list, and remove pickers from Auxiliary List
299  for( unsigned ii = 0; ii < aAuxiliaryList.GetCount(); )
300  {
301  if( aAuxiliaryList.GetPickedItemStatus( ii ) == UNDO_REDO::NEWITEM )
302  {
303  ITEM_PICKER picker = aAuxiliaryList.GetItemWrapper( ii );
304  aPickList.PushItem( picker );
305  aAuxiliaryList.RemovePicker( ii );
306  }
307  else if( aAuxiliaryList.GetPickedItemStatus( ii ) == UNDO_REDO::DELETED )
308  {
309  delete aAuxiliaryList.GetPickedItemLink( ii );
310  aAuxiliaryList.RemovePicker( ii );
311  }
312  else
313  ii++;
314  }
315 
316  // Should not occur:
317  wxASSERT_MSG( aAuxiliaryList.GetCount() == 0,
318  wxT( "UpdateCopyOfZonesList() error: aAuxiliaryList not empty." ) );
319 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
bool GetDoNotAllowPads() const
Definition: class_zone.h:733
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:61
int GetNetCode() const
Function GetNetCode.
ISLAND_REMOVAL_MODE m_islandRemovalMode
Definition: class_zone.h:868
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 m_thermalReliefGap
Definition: class_zone.h:885
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:300
long long int m_minIslandArea
When island removal mode is set to AREA, islands below this area will be removed.
Definition: class_zone.h:874
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
int m_thermalReliefSpokeWidth
Definition: class_zone.h:886
void SetLink(EDA_ITEM *aItem)
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
unsigned GetCount() const
Function GetCount.
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:185
bool GetDoNotAllowVias() const
Definition: class_zone.h:731
ZONE_CONNECTION m_PadConnection
Definition: class_zone.h:863
virtual LSET GetLayerSet() const override
Function GetLayerSet returns a std::bitset of all layers on which the item physically resides.
Definition: class_zone.cpp:287
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:839
#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:163
SHAPE_POLY_SET * m_Poly
Outline of the zone.
Definition: class_zone.h:834
bool GetDoNotAllowCopperPour() const
Definition: class_zone.h:730
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 GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: class_zone.h:729
int LAYER_NUM
This can be replaced with int and removed.
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
Function GetItemWrapper.
bool SetPickedItemStatus(UNDO_REDO aStatus, unsigned aIdx)
Function SetPickedItemStatus sets the type of undo/redo operation for a given picked item.
int m_ZoneMinThickness
Definition: class_zone.h:865
unsigned GetPriority() const
Function GetPriority.
Definition: class_zone.h:106
see class PGM_BASE
Information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:186
int GetMinThickness() const
Definition: class_zone.h:224
bool SetPickedItemLink(EDA_ITEM *aLink, unsigned aIdx)
Function SetPickedItemLink set the link associated to a given picked item.
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
bool GetDoNotAllowTracks() const
Definition: class_zone.h:732
bool GetDoNotAllowFootprints() const
Definition: class_zone.h:734
ZONE_CONTAINER * GetArea(int index) const
Return the Area (Zone Container) at a given index.
Definition: class_board.h:882
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:893