KiCad PCB EDA Suite
Loading...
Searching...
No Matches
zone_settings_bag.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Ethan Chien <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "zone_settings_bag.h"
22
23#include <zone.h>
24#include <memory>
25#include <algorithm>
26
27
29{
30 for( ZONE* zone : aBoard->Zones() )
31 {
32 if( !zone->GetIsRuleArea() && !zone->IsTeardropArea() && !zone->IsCopperThieving()
33 && zone->IsOnCopperLayer() )
34 {
35 auto zone_clone = std::shared_ptr<ZONE>( static_cast<ZONE*>( zone->Clone() ) );
36 m_zonesCloneMap.try_emplace( zone, zone_clone );
37 m_clonedZoneList.push_back( zone_clone.get() );
38 }
39 }
40
41 for( ZONE* zone : m_clonedZoneList )
42 {
43 m_zoneSettings[zone] = std::make_shared<ZONE_SETTINGS>();
44 *m_zoneSettings[zone] << *zone;
45 }
46
47 std::vector<ZONE*> sortedClonedZones = m_clonedZoneList;
48
49 std::sort( sortedClonedZones.begin(), sortedClonedZones.end(),
50 []( ZONE* const& l, ZONE* const& r )
51 {
52 return l->HigherPriority( r );
53 } );
54
55 unsigned currentPriority = sortedClonedZones.size() - 1;
56
57 for( ZONE* zone : sortedClonedZones )
58 {
59 m_zonePriorities[zone] = std::make_pair<>( currentPriority, currentPriority );
60 --currentPriority;
61 }
62}
63
64
66{
67 m_zoneSettings[aZone] = std::make_shared<ZONE_SETTINGS>( *aSettings );
68}
69
70
71std::shared_ptr<ZONE_SETTINGS> ZONE_SETTINGS_BAG::GetZoneSettings( ZONE* aZone )
72{
73 return m_zoneSettings[aZone];
74}
75
76
78{
79 return m_zonePriorities[aZone].second;
80}
81
82
83void ZONE_SETTINGS_BAG::SwapPriority( ZONE* aZone, ZONE* otherZone )
84{
85 std::swap( m_zonePriorities[aZone].second, m_zonePriorities[otherZone].second );
86}
87
88
89wxString ZONE_SETTINGS_BAG::GetUniqueZoneName( const BOARD& aBoard, const wxString& aBaseName, const ZONE* aZone ) const
90{
91 auto inUse = [&]( const wxString& aName )
92 {
93 for( ZONE* zone : aBoard.Zones() )
94 {
95 ZONE* working = zone;
96
97 if( auto clone = m_zonesCloneMap.find( zone ); clone != m_zonesCloneMap.end() )
98 working = clone->second.get();
99
100 if( working == aZone )
101 continue;
102
103 auto settings = m_zoneSettings.find( working );
104 wxString name = working->GetZoneName();
105
106 if( settings != m_zoneSettings.end() && settings->second )
107 name = settings->second->m_Name;
108
109 if( name == aName )
110 return true;
111 }
112
113 return false;
114 };
115
116 return BOARD::MakeUniqueZoneName( aBaseName, inUse );
117}
118
119
120void ZONE_SETTINGS_BAG::SetZonePriority( ZONE* aClone, unsigned aPriority )
121{
122 m_zonePriorities[aClone].second = aPriority;
123
124 if( m_zoneSettings.contains( aClone ) )
125 m_zoneSettings[aClone]->m_ZonePriority = aPriority;
126}
127
128
130{
131 auto it = m_zonesCloneMap.find( aOriginalZone );
132
133 if( it != m_zonesCloneMap.end() )
134 {
135 ZONE* clone = it->second.get();
136
137 // Remove from cloned list
138 m_clonedZoneList.erase( std::remove( m_clonedZoneList.begin(), m_clonedZoneList.end(), clone ),
139 m_clonedZoneList.end() );
140
141
142 // Remove from zone settings and priorities maps
143 m_zoneSettings.erase( clone );
144 m_zonePriorities.erase( clone );
145
146 // Remove from clone map
147 m_zonesCloneMap.erase( it );
148 }
149}
150
151
153{
154 for( ZONE* zone : m_clonedZoneList )
155 {
156 if( m_zoneSettings.contains( zone ) )
157 m_zoneSettings[zone]->ExportSetting( *zone );
158 }
159
160 // Prevent version-control churn by not updating potentially sparse priorities if their
161 // order didn't change.
162 bool priorityChanged = false;
163
164 for( ZONE* zone : m_clonedZoneList )
165 {
166 if( m_zonePriorities[zone].first != m_zonePriorities[zone].second )
167 {
168 priorityChanged = true;
169 break;
170 }
171 }
172
173 if( priorityChanged )
174 {
175 for( ZONE* zone : m_clonedZoneList )
176 zone->SetAssignedPriority( m_zonePriorities[zone].second );
177 }
178}
const char * name
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:410
const ZONES & Zones() const
Definition board.h:468
static wxString MakeUniqueZoneName(const wxString &aBaseName, const std::function< bool(const wxString &)> &aInUse)
Return a name based on aBaseName for which aInUse returns false, adding or incrementing a _<number> s...
Definition board.cpp:1487
void SetZonePriority(ZONE *aClone, unsigned aPriority)
Update the tracked priority for a cloned zone.
unsigned GetZonePriority(ZONE *aZone)
ZONE_SETTINGS_BAG()=default
std::unordered_map< ZONE *, std::shared_ptr< ZONE > > m_zonesCloneMap
void RemoveZone(ZONE *aOriginalZone)
Remove a zone from the bag (used when marking for deletion)
void UpdateClonedZones()
The cloned list is the working storage.
std::vector< ZONE * > m_clonedZoneList
wxString GetUniqueZoneName(const BOARD &aBoard, const wxString &aBaseName, const ZONE *aZone) const
Return a name based on aBaseName that no other zone on aBoard uses.
void SwapPriority(ZONE *aZOne, ZONE *otherZone)
std::unordered_map< ZONE *, std::shared_ptr< ZONE_SETTINGS > > m_zoneSettings
std::shared_ptr< ZONE_SETTINGS > GetZoneSettings(ZONE *aZone)
std::unordered_map< ZONE *, std::pair< unsigned, unsigned > > m_zonePriorities
Handle zones parameters.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
const wxString & GetZoneName() const
Definition zone.h:160