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
22#include <bitmaps.h>
23#include <magic_enum.hpp>
25
26#include <widgets/wx_infobar.h>
28#include <grid_tricks.h>
29#include <wx/radiobut.h>
30#include <kiface_base.h>
31#include <confirm.h>
32#include <pcb_edit_frame.h>
33#include <dialog_shim.h>
34#include <pcbnew_settings.h>
35#include <zone_settings_bag.h>
36#include <wx/string.h>
37#include <widgets/unit_binder.h>
39#include <widgets/wx_grid.h>
40#include <pad.h>
42
43
44wxDEFINE_EVENT( EVT_ZONE_NAME_UPDATE, wxCommandEvent );
45wxDEFINE_EVENT( EVT_ZONE_NET_UPDATE, wxCommandEvent );
46
48 ZONE_SETTINGS_BAG& aZonesSettingsBag, bool allowNetSpec ) :
50 m_frame( aFrame ),
51 m_zonesSettingsBag( aZonesSettingsBag ),
52 m_zone( nullptr ),
63{
64 m_netSelector->SetNetInfo( &m_frame->GetBoard()->GetNetInfo() );
65
66 if( !allowNetSpec )
67 {
68 m_netLabel->Hide();
69 m_netSelector->Hide();
70 }
71
73 [&]() -> LSET
74 {
75 return m_settings->m_Layers;
76 } );
77
79 m_layerSpecificOverrides->SetUnitsProvider( m_frame );
80 m_layerSpecificOverrides->SetAutoEvalCols( { 1, 2 } );
82 m_layerSpecificOverrides->SetSelectionMode( wxGrid::wxGridSelectRows );
83
84 wxGridCellAttr* attr = new wxGridCellAttr;
85 attr->SetRenderer( new GRID_CELL_LAYER_RENDERER( nullptr ) );
86 LSET forbiddenLayers = LSET::AllNonCuMask();
87
88 for( PCB_LAYER_ID copper : LSET::AllCuMask() )
89 {
90 if( !m_frame->GetBoard()->IsLayerEnabled( copper ) )
91 forbiddenLayers.set( copper );
92 }
93
94 attr->SetEditor( new GRID_CELL_LAYER_SELECTOR( nullptr, forbiddenLayers ) );
95 m_layerSpecificOverrides->SetColAttr( 0, attr );
96 m_layerSpecificOverrides->SetupColumnAutosizer( 0 );
97
100
101 m_netSelector->Bind( FILTERED_ITEM_SELECTED, &PANEL_ZONE_PROPERTIES::onNetSelector, this );
102}
103
104
106{
107 // we passed ownership of table to grid
108 // delete m_layerPropsTable;
109
110 m_layerSpecificOverrides->PopEventHandler( true );
111
112 m_netSelector->Unbind( FILTERED_ITEM_SELECTED, &PANEL_ZONE_PROPERTIES::onNetSelector, this );
113}
114
115
117{
118 if( m_settings )
120
121 m_zone = aZone;
122 m_settings = m_zonesSettingsBag.GetZoneSettings( aZone );
123
124 if( !m_settings )
125 {
126 m_isTeardrop = false;
127 m_tcZoneName->ChangeValue( wxEmptyString );
128 Enable( false );
129 return;
130 }
131
132 Enable( true );
134
136}
137
138
140{
141 if( !m_settings )
142 return false;
143
144 m_tcZoneName->ChangeValue( m_settings->m_Name );
145
146 if( m_netSelector->IsShown() )
147 m_netSelector->SetSelectedNetcode( std::max( 0, m_settings->m_Netcode ) );
148
149 m_cbLocked->SetValue( m_settings->m_Locked );
150 m_cornerSmoothingChoice->SetSelection( static_cast<int>( m_settings->GetCornerSmoothingType() ) );
151 m_cornerRadius.SetValue( m_settings->GetCornerRadius() );
152
153 if( m_isTeardrop ) // outlines are never smoothed: they have already the right shape
154 {
155 m_cornerSmoothingChoice->SetSelection( 0 );
156 m_cornerSmoothingChoice->Enable( false );
157 m_cornerRadius.Show( false );
158 }
159
160 switch( m_settings->m_ZoneBorderDisplayStyle )
161 {
162 case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: m_OutlineDisplayCtrl->SetSelection( 0 ); break;
166 }
167
168 m_outlineHatchPitch.SetValue( m_settings->m_BorderHatchPitch );
169
170 m_clearance.SetValue( m_settings->m_ZoneClearance );
171 m_minWidth.SetValue( m_settings->m_ZoneMinThickness );
172
173 switch( m_settings->GetPadConnection() )
174 {
175 default:
176 case ZONE_CONNECTION::THERMAL: m_PadInZoneOpt->SetSelection( 1 ); break;
177 case ZONE_CONNECTION::THT_THERMAL: m_PadInZoneOpt->SetSelection( 2 ); break;
178 case ZONE_CONNECTION::NONE: m_PadInZoneOpt->SetSelection( 3 ); break;
179 case ZONE_CONNECTION::FULL: m_PadInZoneOpt->SetSelection( 0 ); break;
180 }
181
182 if( m_isTeardrop )
183 {
184 m_PadInZoneOpt->SetSelection( 0 );
185 m_PadInZoneOpt->Enable( false );
186 m_antipadClearance.Enable( false );
187 m_spokeWidth.Enable( false );
188 }
189
190 // Do not enable/disable antipad clearance and spoke width. They might be needed if
191 // a footprint or pad overrides the zone to specify a thermal connection.
192 m_antipadClearance.SetValue( m_settings->m_ThermalReliefGap );
193 m_spokeWidth.SetValue( m_settings->m_ThermalReliefSpokeWidth );
194
196 m_islandThreshold.SetDoubleValue( static_cast<double>( m_settings->GetMinIslandArea() ) );
197
198 m_cbRemoveIslands->SetSelection( static_cast<int>( m_settings->GetIslandRemovalMode() ) );
199
201
203 m_gridStyleRotation.SetAngleValue( m_settings->m_HatchOrientation );
204 m_gridStyleThickness.SetValue( m_settings->m_HatchThickness );
205 m_gridStyleGap.SetValue( m_settings->m_HatchGap );
206
207 m_spinCtrlSmoothLevel->SetValue( m_settings->m_HatchSmoothingLevel );
208 m_spinCtrlSmoothValue->SetValue( m_settings->m_HatchSmoothingValue );
209
210 m_layerPropsTable->Clear();
211
212 for( PCB_LAYER_ID layer : LSET::AllCuMask().UIOrder() )
213 {
214 if( m_settings->m_LayerProperties.contains( layer ) )
215 {
216 ZONE_LAYER_PROPERTIES& props = m_settings->m_LayerProperties[layer];
217
218 if( props.hatching_offset.has_value() )
219 m_layerPropsTable->AddItem( layer, props );
220 }
221 }
222
223 // Enable/Disable some widgets
224 wxCommandEvent aEvent;
225 onNetSelector( aEvent );
227 OnRemoveIslandsSelection( aEvent );
228 onHatched( aEvent );
229
230 return true;
231}
232
233
235{
236 m_islandThreshold.Show( m_cbRemoveIslands->GetSelection() == 2 );
237
238 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
239 dlg->Layout();
240}
241
242
244{
246 magic_enum::enum_cast<ZONE_SETTINGS::CORNER_SMOOTHING>( m_cornerSmoothingChoice->GetSelection() )
248
251 {
252 m_cornerRadius.Show( true );
253 }
254 else
255 {
256 m_cornerRadius.Show( false );
257 }
258
259 if( DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( wxGetTopLevelParent( this ) ) )
260 dlg->Layout();
261}
262
263
264void PANEL_ZONE_PROPERTIES::OnZoneNameChanged( wxCommandEvent& aEvent )
265{
266 if( !m_settings )
267 return;
268
269 // Propagate all the way out so that the MODEL_ZONES_OVERVIEW can pick it up
270 m_settings->m_Name = m_tcZoneName->GetValue();
271
272 if( m_zone )
273 {
274 m_zonesSettingsBag.GetZoneSettings( m_zone )->m_Name = m_settings->m_Name;
275 m_zone->SetZoneName( m_settings->m_Name );
276 }
277
278 wxCommandEvent* evt = new wxCommandEvent( EVT_ZONE_NAME_UPDATE );
279 wxQueueEvent( m_parent, evt );
280}
281
282
283void PANEL_ZONE_PROPERTIES::onNetSelector( wxCommandEvent& aEvent )
284{
285 // There is nothing to do if there is no zone selected which can happen when there are no zones on the board.
286 if( !m_netSelector || !m_netSelector->IsShown() )
287 return;
288
290
291 // Zones with no net never have islands removed
292 if( m_netSelector->GetSelectedNetcode() == INVALID_NET_CODE )
293 {
294 if( m_cbRemoveIslands && m_cbRemoveIslands->IsEnabled() && m_settings )
295 m_settings->SetIslandRemovalMode( (ISLAND_REMOVAL_MODE) m_cbRemoveIslands->GetSelection() );
296
297 m_cbRemoveIslands->SetSelection( 1 );
298 m_removeIslandsLabel->Enable( false );
299 m_cbRemoveIslands->Enable( false );
300 }
301 else if( !m_cbRemoveIslands->IsEnabled() )
302 {
304 m_cbRemoveIslands->SetSelection( static_cast<int>( m_settings->GetIslandRemovalMode() ) );
305
306 m_removeIslandsLabel->Enable( true );
307 m_cbRemoveIslands->Enable( true );
308 }
309
310 // Propagate all the way out so that the MODEL_ZONES_OVERVIEW can pick it up
311 if( m_settings )
312 m_settings->m_Netcode = m_netSelector->GetSelectedNetcode();
313
314 if( m_settings && m_zone )
315 {
316 m_zonesSettingsBag.GetZoneSettings( m_zone )->m_Netcode = m_settings->m_Netcode;
317 m_zone->SetNetCode( m_settings->m_Netcode, true );
318 }
319
320 wxCommandEvent* evt = new wxCommandEvent( EVT_ZONE_NET_UPDATE );
321 wxQueueEvent( m_parent, evt );
322}
323
324
326{
327 if( !CommitPendingChanges() )
328 return false;
329
330 if( !m_settings )
331 return false;
332
333 if( !AcceptOptions() )
334 return false;
335
336 return true;
337}
338
339
341{
342 return m_layerSpecificOverrides->CommitPendingChanges();
343}
344
345
346bool PANEL_ZONE_PROPERTIES::AcceptOptions( bool aUseExportableSetupOnly )
347{
348 if( !m_clearance.Validate( 0, pcbIUScale.mmToIU( ZONE_CLEARANCE_MAX_VALUE_MM ) ) )
349 return false;
350
351 if( !m_minWidth.Validate( pcbIUScale.mmToIU( ZONE_THICKNESS_MIN_VALUE_MM ), INT_MAX ) )
352 return false;
353
354 if( !m_cornerRadius.Validate( 0, INT_MAX ) )
355 return false;
356
357 if( !m_spokeWidth.Validate( 0, INT_MAX ) )
358 return false;
359
361
362 if( m_settings->m_FillMode == ZONE_FILL_MODE::HATCH_PATTERN )
363 {
364 int minThickness = m_minWidth.GetIntValue();
365
366 if( !m_gridStyleThickness.Validate( minThickness, INT_MAX ) )
367 return false;
368
369 if( !m_gridStyleGap.Validate( minThickness, INT_MAX ) )
370 return false;
371 }
372
373 switch( m_PadInZoneOpt->GetSelection() )
374 {
375 case 3: m_settings->SetPadConnection( ZONE_CONNECTION::NONE ); break;
376 case 2: m_settings->SetPadConnection( ZONE_CONNECTION::THT_THERMAL ); break;
377 case 1: m_settings->SetPadConnection( ZONE_CONNECTION::THERMAL ); break;
378 case 0: m_settings->SetPadConnection( ZONE_CONNECTION::FULL ); break;
379 }
380
381 switch( m_OutlineDisplayCtrl->GetSelection() )
382 {
383 case 0: m_settings->m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::NO_HATCH; break;
384 case 1: m_settings->m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE; break;
385 case 2: m_settings->m_ZoneBorderDisplayStyle = ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL; break;
386 }
387
390 {
391 return false;
392 }
393
394 m_settings->m_BorderHatchPitch = m_outlineHatchPitch.GetIntValue();
395
396 m_settings->m_ZoneClearance = m_clearance.GetIntValue();
397 m_settings->m_ZoneMinThickness = m_minWidth.GetIntValue();
398
400 magic_enum::enum_cast<ZONE_SETTINGS::CORNER_SMOOTHING>( m_cornerSmoothingChoice->GetSelection() )
402
403 m_settings->SetCornerSmoothingType( smoothing );
404
405 if( m_settings->GetCornerSmoothingType() == ZONE_SETTINGS::CORNER_SMOOTHING::NO_SMOOTHING )
406 m_settings->SetCornerRadius( 0 );
407 else
408 m_settings->SetCornerRadius( m_cornerRadius.GetIntValue() );
409
410 m_settings->m_Locked = m_cbLocked->GetValue();
411
412 m_settings->m_ThermalReliefGap = m_antipadClearance.GetValue();
413 m_settings->m_ThermalReliefSpokeWidth = m_spokeWidth.GetValue();
414
415 if( m_settings->m_ThermalReliefSpokeWidth < m_settings->m_ZoneMinThickness )
416 {
417 DisplayErrorMessage( this, _( "Thermal spoke width cannot be smaller than the minimum width." ) );
418 return false;
419 }
420
421 m_settings->SetIslandRemovalMode( (ISLAND_REMOVAL_MODE) m_cbRemoveIslands->GetSelection() );
422 m_settings->SetMinIslandArea( m_islandThreshold.GetValue() );
423
424 // If we use only exportable to others zones parameters, exit here:
425 if( aUseExportableSetupOnly )
426 return true;
427
428 if( m_netSelector->IsShown() )
429 m_settings->m_Netcode = m_netSelector->GetSelectedNetcode();
430
431 m_settings->m_Name = m_tcZoneName->GetValue();
432
433 if( BOARD* board = m_frame->GetBoard() )
434 m_settings->m_Name = m_zonesSettingsBag.GetUniqueZoneName( *board, m_settings->m_Name, m_zone );
435
436 // Avoid clobbering theiving zone properties
438 {
439 m_settings->m_FillMode = m_cbHatched->GetValue() ? ZONE_FILL_MODE::HATCH_PATTERN
441 }
442 m_settings->m_HatchOrientation = m_gridStyleRotation.GetAngleValue();
443 m_settings->m_HatchThickness = m_gridStyleThickness.GetIntValue();
444 m_settings->m_HatchGap = m_gridStyleGap.GetIntValue();
445 m_settings->m_HatchSmoothingLevel = m_spinCtrlSmoothLevel->GetValue();
446 m_settings->m_HatchSmoothingValue = m_spinCtrlSmoothValue->GetValue();
447
448 for( auto& [layer, props] : m_settings->m_LayerProperties )
449 props.hatching_offset = std::nullopt;
450
451 for( const auto& [layer, props] : m_layerPropsTable->GetItems() )
452 m_settings->m_LayerProperties[layer] = props;
453
454 return true;
455}
456
457
458void PANEL_ZONE_PROPERTIES::onHatched( wxCommandEvent& event )
459{
460 bool enable = m_cbHatched->GetValue();
461 m_gridStyleThickness.Enable( enable );
462 m_gridStyleGap.Enable( enable );
463 m_gridStyleRotation.Enable( enable );
464 m_staticTextGridSmoothingLevel->Enable( enable );
465 m_spinCtrlSmoothLevel->Enable( enable );
466 m_staticTextGridSmootingVal->Enable( enable );
467 m_spinCtrlSmoothValue->Enable( enable );
468 m_offsetOverridesLabel->Enable( enable );
469 m_layerSpecificOverrides->Enable( enable );
470 m_bpAddCustomLayer->Enable( enable );
471 m_bpDeleteCustomLayer->Enable( enable );
472}
473
474
475void PANEL_ZONE_PROPERTIES::OnAddLayerItem( wxCommandEvent& event )
476{
477 m_layerSpecificOverrides->OnAddRow(
478 [&]() -> std::pair<int, int>
479 {
480 if( !m_layerSpecificOverrides->GetTable()->AppendRows( 1 ) )
481 {
482 m_copperZoneInfoBar->ShowMessageFor( _( "All zone layers already overridden." ), 8000,
483 wxICON_WARNING );
484 }
485
486 return { m_layerSpecificOverrides->GetNumberRows() - 1, -1 };
487 } );
488}
489
490
491void PANEL_ZONE_PROPERTIES::OnDeleteLayerItem( wxCommandEvent& event )
492{
493 m_layerSpecificOverrides->OnDeleteRows(
494 [&]( int row )
495 {
496 m_layerSpecificOverrides->GetTable()->DeleteRows( row, 1 );
497 } );
498}
499
500
502{
503 if( m_netSelector->GetSelectedNetcode() <= INVALID_NET_CODE && !m_copperZoneInfoBar->IsShown() )
504 {
505 m_copperZoneInfoBar->ShowMessage( _( "<no net> will result in an isolated copper island." ), wxICON_WARNING );
506 }
507 else if( m_copperZoneInfoBar->IsShown() )
508 {
509 m_copperZoneInfoBar->Dismiss();
510 }
511}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
BASE_SET & set(size_t pos)
Definition base_set.h:126
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:80
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:208
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