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 <class_drawpanel.h>
47 #include <pcb_edit_frame.h>
48 
49 #include <class_board.h>
50 #include <class_zone.h>
51 
52 #include <pcbnew.h>
53 #include <zones.h>
55 
63 bool ZONE_CONTAINER::IsSame( const ZONE_CONTAINER& aZoneToCompare )
64 {
65  // compare basic parameters:
66  if( GetLayer() != aZoneToCompare.GetLayer() )
67  return false;
68 
69  if( GetNetCode() != aZoneToCompare.GetNetCode() )
70  return false;
71 
72  if( GetPriority() != aZoneToCompare.GetPriority() )
73  return false;
74 
75  // Compare zone specific parameters
76  if( GetIsKeepout() != aZoneToCompare.GetIsKeepout() )
77  return false;
78 
79  if( GetIsKeepout() )
80  {
81  if( GetDoNotAllowCopperPour() != aZoneToCompare.GetDoNotAllowCopperPour() )
82  return false;
83 
84  if( GetDoNotAllowVias() != aZoneToCompare.GetDoNotAllowVias() )
85  return false;
86 
87  if( GetDoNotAllowTracks() != aZoneToCompare.GetDoNotAllowTracks() )
88  return false;
89  }
90 
91  if( m_ArcToSegmentsCount != aZoneToCompare.GetArcSegmentCount() )
92  return false;
93 
94  if( m_ZoneClearance != aZoneToCompare.m_ZoneClearance )
95  return false;
96 
97  if( m_ZoneMinThickness != aZoneToCompare.GetMinThickness() )
98  return false;
99 
100  if( m_FillMode != aZoneToCompare.GetFillMode() )
101  return false;
102 
103  if( m_PadConnection != aZoneToCompare.m_PadConnection )
104  return false;
105 
106  if( m_ThermalReliefGap != aZoneToCompare.m_ThermalReliefGap )
107  return false;
108 
110  return false;
111 
112 
113  // Compare outlines
114  wxASSERT( m_Poly ); // m_Poly == NULL Should never happen
115  wxASSERT( aZoneToCompare.Outline() );
116 
117  if( Outline() != aZoneToCompare.Outline() ) // Compare vector
118  return false;
119 
120  return true;
121 }
122 
123 
136 int SaveCopyOfZones( PICKED_ITEMS_LIST& aPickList, BOARD* aPcb, int aNetCode, LAYER_NUM aLayer )
137 {
138  int copyCount = 0;
139 
140  for( unsigned ii = 0; ; ii++ )
141  {
142  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
143 
144  if( zone == NULL ) // End of list
145  break;
146 
147  if( aNetCode >= 0 && aNetCode != zone->GetNetCode() )
148  continue;
149 
150  if( aLayer >= 0 && aLayer != zone->GetLayer() )
151  continue;
152 
153  ZONE_CONTAINER* zoneDup = new ZONE_CONTAINER( *zone );
154  zoneDup->SetParent( aPcb );
155  ITEM_PICKER picker( zone, UR_CHANGED );
156  picker.SetLink( zoneDup );
157  aPickList.PushItem( picker );
158  copyCount++;
159  }
160 
161  return copyCount;
162 }
163 
164 
201  PICKED_ITEMS_LIST& aAuxiliaryList,
202  BOARD* aPcb )
203 {
204  for( unsigned kk = 0; kk < aPickList.GetCount(); kk++ )
205  {
206  UNDO_REDO_T status = aPickList.GetPickedItemStatus( kk );
207 
208  ZONE_CONTAINER* ref = (ZONE_CONTAINER*) aPickList.GetPickedItem( kk );
209 
210  for( unsigned ii = 0; ; ii++ ) // analyse the main picked list
211  {
212  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
213 
214  if( zone == NULL )
215  {
216  /* End of list: the stored item is not found:
217  * it must be in aDeletedList:
218  * search it and restore initial values
219  * or
220  * if flagged UR_NEW: remove it definitively
221  */
222  if( status == UR_NEW )
223  {
224  delete ref;
225  ref = NULL;
226  aPickList.RemovePicker( kk );
227  kk--;
228  }
229  else
230  {
231  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
232  aPickList.SetPickedItemStatus( UR_DELETED, kk );
233 
234  wxASSERT_MSG( zcopy != NULL,
235  wxT( "UpdateCopyOfZonesList() error: link = NULL" ) );
236 
237  *ref = *zcopy;
238 
239  // the copy was deleted; the link does not exists now.
240  aPickList.SetPickedItemLink( NULL, kk );
241  delete zcopy;
242  }
243 
244  // Remove this item from aAuxiliaryList, mainly for tests purpose
245  bool notfound = true;
246 
247  for( unsigned nn = 0; nn < aAuxiliaryList.GetCount(); nn++ )
248  {
249  if( ref != NULL && aAuxiliaryList.GetPickedItem( nn ) == ref )
250  {
251  aAuxiliaryList.RemovePicker( nn );
252  notfound = false;
253  break;
254  }
255  }
256 
257  if( notfound ) // happens when the new zone overlaps an existing zone
258  // and these zones are combined
259  {
260  DBG( printf(
261  "UpdateCopyOfZonesList(): item not found in aAuxiliaryList,"
262  "combined with another zone\n" ) );
263  }
264  break;
265  }
266 
267  if( zone == ref ) // picked zone found
268  {
269  if( aPickList.GetPickedItemStatus( kk ) != UR_NEW )
270  {
271  ZONE_CONTAINER* zcopy = (ZONE_CONTAINER*) aPickList.GetPickedItemLink( kk );
272 
273  if( zone->IsSame( *zcopy ) ) // Remove picked, because no changes
274  {
275  delete zcopy; // Delete copy
276  aPickList.RemovePicker( kk );
277  kk--;
278  }
279  }
280 
281  break;
282  }
283  }
284  }
285 
286  // Add new zones in main pick list, and remove pickers from Auxiliary List
287  for( unsigned ii = 0; ii < aAuxiliaryList.GetCount(); )
288  {
289  if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_NEW )
290  {
291  ITEM_PICKER picker = aAuxiliaryList.GetItemWrapper( ii );
292  aPickList.PushItem( picker );
293  aAuxiliaryList.RemovePicker( ii );
294  }
295  else if( aAuxiliaryList.GetPickedItemStatus( ii ) == UR_DELETED )
296  {
297  delete aAuxiliaryList.GetPickedItemLink( ii );
298  aAuxiliaryList.RemovePicker( ii );
299  }
300  else
301  ii++;
302  }
303 
304  // Should not occur:
305  wxASSERT_MSG( aAuxiliaryList.GetCount() == 0,
306  wxT( "UpdateCopyOfZonesList() error: aAuxiliaryList not empty." ) );
307 }
Class 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:718
int GetMinThickness() const
Definition: class_zone.h:204
bool GetDoNotAllowCopperPour() const
Definition: class_zone.h:624
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:193
Class BOARD to handle a board.
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:175
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:242
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: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_ArcToSegmentsCount
The number of segments to convert a circle to a polygon.
Definition: class_zone.h:723
int m_ThermalReliefCopperBridge
Definition: class_zone.h:732
ZoneConnection m_PadConnection
Definition: class_zone.h:717
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:216
bool IsSame(const ZONE_CONTAINER &aZoneToCompare)
Function IsSame tests if 2 zones are equivalent: 2 zones are equivalent if they have same parameters ...
SHAPE_POLY_SET * m_Poly
Outline of the zone.
Definition: class_zone.h:693
ZONE_CONTAINER(BOARD *parent)
Definition: class_zone.cpp:52
ZONE_FILL_MODE GetFillMode() const
Definition: class_zone.h:181
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.
int m_ThermalReliefGap
Width of the copper bridge in thermal reliefs.
Definition: class_zone.h:729
bool GetDoNotAllowTracks() const
Definition: class_zone.h:626
int m_ZoneMinThickness
Minimum thickness value in filled areas.
Definition: class_zone.h:719
ZONE_FILL_MODE m_FillMode
How to fill areas: ZFM_POLYGONS => use filled polygons, ZFM_SEGMENTS => fill with segments...
Definition: class_zone.h:736
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:991
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:625
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
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:107
ITEM_PICKER GetItemWrapper(unsigned int aIdx) const
Function GetItemWrapper.