KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_zone_properties.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 (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
6 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
7 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
23
24#include <widgets/wx_infobar.h>
26#include <grid_tricks.h>
27#include <wx/radiobut.h>
28#include <kiface_base.h>
29#include <confirm.h>
30#include <pcb_edit_frame.h>
31#include <dialog_shim.h>
32#include <pcbnew_settings.h>
33#include <zone_settings_bag.h>
34#include <wx/string.h>
35#include <widgets/unit_binder.h>
37#include <widgets/wx_grid.h>
38#include <pad.h>
40
41
42wxDEFINE_EVENT( EVT_ZONE_NAME_UPDATE, wxCommandEvent );
43wxDEFINE_EVENT( EVT_ZONE_NET_UPDATE, wxCommandEvent );
44
46 ZONE_SETTINGS_BAG& aZonesSettingsBag, bool allowNetSpec ) :
48 m_frame( aFrame ),
49 m_zonesSettingsBag( aZonesSettingsBag ),
50 m_zone( nullptr ),
61{
62 m_netSelector->SetNetInfo( &m_frame->GetBoard()->GetNetInfo() );
63
64 if( !allowNetSpec )
65 {
66 m_netLabel->Hide();
67 m_netSelector->Hide();
68 }
69
71 [&]() -> LSET
72 {
73 return m_settings->m_Layers;
74 } );
75
78 m_layerSpecificOverrides->SetSelectionMode( wxGrid::wxGridSelectRows );
79
80 wxGridCellAttr* attr = new wxGridCellAttr;
81 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
82 LSET forbiddenLayers = LSET::AllNonCuMask();
83
84 for( PCB_LAYER_ID copper : LSET::AllCuMask() )
85 {
86 if( !m_frame->GetBoard()->IsLayerEnabled( copper ) )
87 forbiddenLayers.set( copper );
88 }
89
90 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, forbiddenLayers ) );
91 m_layerSpecificOverrides->SetColAttr( 0, attr );
92 m_layerSpecificOverrides->SetupColumnAutosizer( 0 );
93
96
97 m_netSelector->Bind( FILTERED_ITEM_SELECTED, &PANEL_ZONE_PROPERTIES::onNetSelector, this );
98}
99
100
102{
103 // we passed ownership of table to grid
104 // delete m_layerPropsTable;
105
106 m_layerSpecificOverrides->PopEventHandler( true );
107
108 m_netSelector->Unbind( FILTERED_ITEM_SELECTED, &PANEL_ZONE_PROPERTIES::onNetSelector, this );
109}
110
111
113{
114 if( m_settings )
116
117 m_zone = aZone;
118 m_settings = m_zonesSettingsBag.GetZoneSettings( aZone );
120
122}
123
124
126{
127 if( !m_settings )
128 return false;
129
130 m_tcZoneName->ChangeValue( m_settings->m_Name );
131
132 if( m_netSelector->IsShown() )
133 m_netSelector->SetSelectedNetcode( std::max( 0, m_settings->m_Netcode ) );
134
135 m_cbLocked->SetValue( m_settings->m_Locked );
136 m_cornerSmoothingChoice->SetSelection( m_settings->GetCornerSmoothingType() );
137 m_cornerRadius.SetValue( m_settings->GetCornerRadius() );
138
139 if( m_isTeardrop ) // outlines are never smoothed: they have already the right shape
140 {
141 m_cornerSmoothingChoice->SetSelection( 0 );
142 m_cornerSmoothingChoice->Enable( false );
143 m_cornerRadius.Show( false );
144 }
145
146 switch( m_settings->m_ZoneBorderDisplayStyle )
147 {
148 case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: m_OutlineDisplayCtrl->SetSelection( 0 ); break;
152 }
153
154 m_outlineHatchPitch.SetValue( m_settings->m_BorderHatchPitch );
155
156 m_clearance.SetValue( m_settings->m_ZoneClearance );
157 m_minWidth.SetValue( m_settings->m_ZoneMinThickness );
158
159 switch( m_settings->GetPadConnection() )
160 {
161 default:
162 case ZONE_CONNECTION::THERMAL: m_PadInZoneOpt->SetSelection( 1 ); break;
163 case ZONE_CONNECTION::THT_THERMAL: m_PadInZoneOpt->SetSelection( 2 ); break;
164 case ZONE_CONNECTION::NONE: m_PadInZoneOpt->SetSelection( 3 ); break;
165 case ZONE_CONNECTION::FULL: m_PadInZoneOpt->SetSelection( 0 ); break;
166 }
167
168 if( m_isTeardrop )
169 {
170 m_PadInZoneOpt->SetSelection( 0 );
171 m_PadInZoneOpt->Enable( false );
172 m_antipadClearance.Enable( false );
173 m_spokeWidth.Enable( false );
174 }
175
176 // Do not enable/disable antipad clearance and spoke width. They might be needed if
177 // a footprint or pad overrides the zone to specify a thermal connection.
178 m_antipadClearance.SetValue( m_settings->m_ThermalReliefGap );
179 m_spokeWidth.SetValue( m_settings->m_ThermalReliefSpokeWidth );
180
182 m_islandThreshold.SetDoubleValue( static_cast<double>( m_settings->GetMinIslandArea() ) );
183
184 m_cbRemoveIslands->SetSelection( static_cast<int>( m_settings->GetIslandRemovalMode() ) );
185
187
189 m_gridStyleRotation.SetAngleValue( m_settings->m_HatchOrientation );
190 m_gridStyleThickness.SetValue( m_settings->m_HatchThickness );
191 m_gridStyleGap.SetValue( m_settings->m_HatchGap );
192
193 m_spinCtrlSmoothLevel->SetValue( m_settings->m_HatchSmoothingLevel );
194 m_spinCtrlSmoothValue->SetValue( m_settings->m_HatchSmoothingValue );
195
196 m_layerPropsTable->Clear();
197
198 for( PCB_LAYER_ID layer : LSET::AllCuMask().UIOrder() )
199 {
200 if( m_settings->m_LayerProperties.contains( layer ) )
201 {
202 ZONE_LAYER_PROPERTIES& props = m_settings->m_LayerProperties[layer];
203
204 if( props.hatching_offset.has_value() )
205 m_layerPropsTable->AddItem( layer, props );
206 }
207 }
208
209 // Enable/Disable some widgets
210 wxCommandEvent aEvent;
211 onNetSelector( aEvent );
213 OnRemoveIslandsSelection( aEvent );
214 onHatched( aEvent );
215
216 return true;
217}
218
219
221{
222 m_islandThreshold.Show( m_cbRemoveIslands->GetSelection() == 2 );
223
224 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
225 dlg->Layout();
226}
227
228
230{
231 int selection = m_cornerSmoothingChoice->GetSelection();
232
233 if( selection == ZONE_SETTINGS::SMOOTHING_CHAMFER
234 || selection == ZONE_SETTINGS::SMOOTHING_FILLET )
235 {
236 m_cornerRadius.Show( true );
237 }
238 else
239 {
240 m_cornerRadius.Show( false );
241 }
242
243 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
244 dlg->Layout();
245}
246
247
248void PANEL_ZONE_PROPERTIES::OnZoneNameChanged( wxCommandEvent& aEvent )
249{
250 // Propagate all the way out so that the MODEL_ZONES_OVERVIEW can pick it up
251 m_settings->m_Name = m_tcZoneName->GetValue();
252 m_zonesSettingsBag.GetZoneSettings( m_zone )->m_Name = m_settings->m_Name;
253
254 if( m_zone )
255 m_zone->SetZoneName( m_settings->m_Name );
256
257 wxCommandEvent* evt = new wxCommandEvent( EVT_ZONE_NAME_UPDATE );
258 wxQueueEvent( m_parent, evt );
259}
260
261
262void PANEL_ZONE_PROPERTIES::onNetSelector( wxCommandEvent& aEvent )
263{
264 // There is nothing to do if there is no zone selected which can happen when there are no zones on the board.
265 if( !m_netSelector || !m_netSelector->IsShown() )
266 return;
267
269
270 // Zones with no net never have islands removed
271 if( m_netSelector->GetSelectedNetcode() == INVALID_NET_CODE )
272 {
273 if( m_cbRemoveIslands && m_cbRemoveIslands->IsEnabled() && m_settings )
274 m_settings->SetIslandRemovalMode( (ISLAND_REMOVAL_MODE) m_cbRemoveIslands->GetSelection() );
275
276 m_cbRemoveIslands->SetSelection( 1 );
277 m_removeIslandsLabel->Enable( false );
278 m_cbRemoveIslands->Enable( false );
279 }
280 else if( !m_cbRemoveIslands->IsEnabled() )
281 {
283 m_cbRemoveIslands->SetSelection( static_cast<int>( m_settings->GetIslandRemovalMode() ) );
284
285 m_removeIslandsLabel->Enable( true );
286 m_cbRemoveIslands->Enable( true );
287 }
288
289 // Propagate all the way out so that the MODEL_ZONES_OVERVIEW can pick it up
290 if( m_settings )
291 m_settings->m_Netcode = m_netSelector->GetSelectedNetcode();
292
293 if( m_settings && m_zone )
294 {
295 m_zonesSettingsBag.GetZoneSettings( m_zone )->m_Netcode = m_settings->m_Netcode;
296 m_zone->SetNetCode( m_settings->m_Netcode, true );
297 }
298
299 wxCommandEvent* evt = new wxCommandEvent( EVT_ZONE_NET_UPDATE );
300 wxQueueEvent( m_parent, evt );
301}
302
303
305{
306 if( !CommitPendingChanges() )
307 return false;
308
309 if( !m_settings )
310 return false;
311
312 if( !AcceptOptions() )
313 return false;
314
315 return true;
316}
317
318
320{
321 return m_layerSpecificOverrides->CommitPendingChanges();
322}
323
324
325bool PANEL_ZONE_PROPERTIES::AcceptOptions( bool aUseExportableSetupOnly )
326{
327 if( !m_clearance.Validate( 0, pcbIUScale.mmToIU( ZONE_CLEARANCE_MAX_VALUE_MM ) ) )
328 return false;
329
330 if( !m_minWidth.Validate( pcbIUScale.mmToIU( ZONE_THICKNESS_MIN_VALUE_MM ), INT_MAX ) )
331 return false;
332
333 if( !m_cornerRadius.Validate( 0, INT_MAX ) )
334 return false;
335
336 if( !m_spokeWidth.Validate( 0, INT_MAX ) )
337 return false;
338
340
341 if( m_settings->m_FillMode == ZONE_FILL_MODE::HATCH_PATTERN )
342 {
343 int minThickness = m_minWidth.GetIntValue();
344
345 if( !m_gridStyleThickness.Validate( minThickness, INT_MAX ) )
346 return false;
347
348 if( !m_gridStyleGap.Validate( minThickness, INT_MAX ) )
349 return false;
350 }
351
352 switch( m_PadInZoneOpt->GetSelection() )
353 {
354 case 3: m_settings->SetPadConnection( ZONE_CONNECTION::NONE ); break;
355 case 2: m_settings->SetPadConnection( ZONE_CONNECTION::THT_THERMAL ); break;
356 case 1: m_settings->SetPadConnection( ZONE_CONNECTION::THERMAL ); break;
357 case 0: m_settings->SetPadConnection( ZONE_CONNECTION::FULL ); break;
358 }
359
360 switch( m_OutlineDisplayCtrl->GetSelection() )
361 {
362 case 0: m_settings->m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::NO_HATCH; break;
363 case 1: m_settings->m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE; break;
364 case 2: m_settings->m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL; break;
365 }
366
369 {
370 return false;
371 }
372
373 m_settings->m_BorderHatchPitch = m_outlineHatchPitch.GetIntValue();
374
375 m_settings->m_ZoneClearance = m_clearance.GetIntValue();
376 m_settings->m_ZoneMinThickness = m_minWidth.GetIntValue();
377
378 m_settings->SetCornerSmoothingType( m_cornerSmoothingChoice->GetSelection() );
379
380 if( m_settings->GetCornerSmoothingType() == ZONE_SETTINGS::SMOOTHING_NONE )
381 m_settings->SetCornerRadius( 0 );
382 else
383 m_settings->SetCornerRadius( m_cornerRadius.GetIntValue() );
384
385 m_settings->m_Locked = m_cbLocked->GetValue();
386
387 m_settings->m_ThermalReliefGap = m_antipadClearance.GetValue();
388 m_settings->m_ThermalReliefSpokeWidth = m_spokeWidth.GetValue();
389
390 if( m_settings->m_ThermalReliefSpokeWidth < m_settings->m_ZoneMinThickness )
391 {
392 DisplayErrorMessage( this, _( "Thermal spoke width cannot be smaller than the minimum width." ) );
393 return false;
394 }
395
396 m_settings->SetIslandRemovalMode( (ISLAND_REMOVAL_MODE) m_cbRemoveIslands->GetSelection() );
397 m_settings->SetMinIslandArea( m_islandThreshold.GetValue() );
398
399 // If we use only exportable to others zones parameters, exit here:
400 if( aUseExportableSetupOnly )
401 return true;
402
403 if( m_netSelector->IsShown() )
404 m_settings->m_Netcode = m_netSelector->GetSelectedNetcode();
405
406 m_settings->m_Name = m_tcZoneName->GetValue();
407
408 // Avoid clobbering theiving zone properties
410 {
411 m_settings->m_FillMode = m_cbHatched->GetValue() ? ZONE_FILL_MODE::HATCH_PATTERN
413 }
414 m_settings->m_HatchOrientation = m_gridStyleRotation.GetAngleValue();
415 m_settings->m_HatchThickness = m_gridStyleThickness.GetIntValue();
416 m_settings->m_HatchGap = m_gridStyleGap.GetIntValue();
417 m_settings->m_HatchSmoothingLevel = m_spinCtrlSmoothLevel->GetValue();
418 m_settings->m_HatchSmoothingValue = m_spinCtrlSmoothValue->GetValue();
419
420 for( auto& [layer, props] : m_settings->m_LayerProperties )
421 props.hatching_offset = std::nullopt;
422
423 for( const auto& [layer, props] : m_layerPropsTable->GetItems() )
424 m_settings->m_LayerProperties[layer] = props;
425
426 return true;
427}
428
429
430void PANEL_ZONE_PROPERTIES::onHatched( wxCommandEvent& event )
431{
432 bool enable = m_cbHatched->GetValue();
433 m_gridStyleThickness.Enable( enable );
434 m_gridStyleGap.Enable( enable );
435 m_gridStyleRotation.Enable( enable );
436 m_staticTextGridSmoothingLevel->Enable( enable );
437 m_spinCtrlSmoothLevel->Enable( enable );
438 m_staticTextGridSmootingVal->Enable( enable );
439 m_spinCtrlSmoothValue->Enable( enable );
440 m_offsetOverridesLabel->Enable( enable );
441 m_layerSpecificOverrides->Enable( enable );
442 m_bpAddCustomLayer->Enable( enable );
443 m_bpDeleteCustomLayer->Enable( enable );
444}
445
446
447void PANEL_ZONE_PROPERTIES::OnAddLayerItem( wxCommandEvent& event )
448{
449 m_layerSpecificOverrides->OnAddRow(
450 [&]() -> std::pair<int, int>
451 {
452 if( !m_layerSpecificOverrides->GetTable()->AppendRows( 1 ) )
453 {
454 m_copperZoneInfoBar->ShowMessageFor( _( "All zone layers already overridden." ), 8000,
455 wxICON_WARNING );
456 }
457
458 return { m_layerSpecificOverrides->GetNumberRows() - 1, -1 };
459 } );
460}
461
462
463void PANEL_ZONE_PROPERTIES::OnDeleteLayerItem( wxCommandEvent& event )
464{
465 m_layerSpecificOverrides->OnDeleteRows(
466 [&]( int row )
467 {
468 m_layerSpecificOverrides->GetTable()->DeleteRows( row, 1 );
469 } );
470}
471
472
474{
475 if( m_netSelector->GetSelectedNetcode() <= INVALID_NET_CODE && !m_copperZoneInfoBar->IsShown() )
476 {
477 m_copperZoneInfoBar->ShowMessage( _( "<no net> will result in an isolated copper island." ), wxICON_WARNING );
478 }
479 else if( m_copperZoneInfoBar->IsShown() )
480 {
481 m_copperZoneInfoBar->Dismiss();
482 }
483}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
BASE_SET & set(size_t pos)
Definition base_set.h:116
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:65
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:57
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static LSET AllNonCuMask()
Return a mask holding all layer minus CU layers.
Definition lset.cpp:623
static LSET AllCuMask(int aCuLayerCount)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition lset.cpp:595
PANEL_ZONE_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
void OnAddLayerItem(wxCommandEvent &event) override
static constexpr int INVALID_NET_CODE
void OnZoneNameChanged(wxCommandEvent &event) override
void onNetSelector(wxCommandEvent &aEvent)
bool AcceptOptions(bool aUseExportableSetupOnly=false)
PANEL_ZONE_PROPERTIES(wxWindow *aParent, PCB_BASE_FRAME *aFrame, ZONE_SETTINGS_BAG &aZonesSettingsBag, bool allowNetSpec=true)
void onHatched(wxCommandEvent &event) override
void OnRemoveIslandsSelection(wxCommandEvent &event) override
ZONE_SETTINGS_BAG & m_zonesSettingsBag
void OnDeleteLayerItem(wxCommandEvent &event) override
std::shared_ptr< ZONE_SETTINGS > m_settings
void OnCornerSmoothingSelection(wxCommandEvent &event) override
LAYER_PROPERTIES_GRID_TABLE * m_layerPropsTable
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
wxDEFINE_EVENT(EVT_ZONE_NAME_UPDATE, wxCommandEvent)
std::optional< VECTOR2I > hatching_offset
T NormalizeAngle180(T Angle)
Normalize angle to be in the -180.0 .
Definition trigo.h:192
ISLAND_REMOVAL_MODE
Whether or not to remove isolated islands from a zone.
#define ZONE_CLEARANCE_MAX_VALUE_MM
Definition zones.h:33
@ THERMAL
Use thermal relief for pads.
Definition zones.h:46
@ THT_THERMAL
Thermal relief only for THT pads.
Definition zones.h:48
@ NONE
Pads are not covered.
Definition zones.h:45
@ FULL
pads are covered by copper
Definition zones.h:47
#define ZONE_BORDER_HATCH_MINDIST_MM
Definition zones.h:35
#define ZONE_THICKNESS_MIN_VALUE_MM
Definition zones.h:31
#define ZONE_BORDER_HATCH_MAXDIST_MM
Definition zones.h:36