KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_fp_edit_pad_table.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/display.h>
23#include <wx/dcclient.h>
24#include <wx/filedlg.h>
25#include <wx/msgdlg.h>
26#include <wx/settings.h>
27
28#include <column_formatter.h>
29#include <pcb_shape.h>
30#include <widgets/wx_grid.h>
34#include <base_units.h>
35#include <bitmaps.h>
36#include <units_provider.h>
38#include <board.h>
39#include <footprint.h>
41#include <grid_tricks.h>
42#include <kiplatform/ui.h>
43#include <pin_numbers.h>
44#include <board_commit.h>
45#include <pad_utils.h>
46#include <reporter.h>
47#include <table_io.h>
49
50
52
53// Helper to map shape string to PAD_SHAPE
54static PAD_SHAPE ShapeFromString( const wxString& shape )
55{
56 if( shape == _( "Oval" ) ) return PAD_SHAPE::OVAL;
57 if( shape == _( "Rectangle" ) ) return PAD_SHAPE::RECTANGLE;
58 if( shape == _( "Trapezoid" ) ) return PAD_SHAPE::TRAPEZOID;
59 if( shape == _( "Rounded rectangle" ) ) return PAD_SHAPE::ROUNDRECT;
60 if( shape == _( "Chamfered rectangle" ) ) return PAD_SHAPE::CHAMFERED_RECT;
61 if( shape == _( "Custom shape" ) ) return PAD_SHAPE::CUSTOM;
62
63 return PAD_SHAPE::CIRCLE;
64}
65
66
67static wxString ShapeToString( PAD_SHAPE shape )
68{
69 switch( shape )
70 {
71 case PAD_SHAPE::CIRCLE: return _( "Circle" );
72 case PAD_SHAPE::OVAL: return _( "Oval" );
73 case PAD_SHAPE::RECTANGLE: return _( "Rectangle" );
74 case PAD_SHAPE::TRAPEZOID: return _( "Trapezoid" );
75 case PAD_SHAPE::ROUNDRECT: return _( "Rounded rectangle" );
76 case PAD_SHAPE::CHAMFERED_RECT: return _( "Chamfered rectangle" );
77 case PAD_SHAPE::CUSTOM: return _( "Custom shape" );
78 default:
79 wxFAIL_MSG( wxT( "Invalid pad shape" ) );
80 return wxEmptyString;
81 }
82}
83
84
85static wxString GetPadTypeString( const PAD& aPad )
86{
87 if( aPad.IsAperturePad() )
88 return _( "Aperture" );
89
90 const PAD_ATTRIB attrib = aPad.GetAttribute();
91 return PAD_UTILS::GetPadAttributeString( attrib );
92}
93
94
95static void SetPadTypeFromString( PAD& aPad, const wxString& aType )
96{
97 // Heuristic for detecting pads that look like they mean to be
98 // back-only pads.
99 const auto isBackOnlyPad =
100 []( const PAD& pad ) -> bool
101 {
102 const LSET layers = pad.GetLayerSet();
103 return ( layers & LSET::BackMask() ).any() && ( layers & LSET::FrontMask() ).none();
104 };
105
106 if( MatchTranslationOrNative( aType, _HKI( "Through-hole" ), false ) )
107 {
109 }
110 else if( MatchTranslationOrNative( aType, _HKI( "SMD" ), false ) )
111 {
112 // If the pad was already SMD, don't mess with the layers, but if it
113 // is _becoming_ SMD (including if it was an aperture pad), set
114 // default layerset
115 if( aPad.GetAttribute() != PAD_ATTRIB::SMD || aPad.IsAperturePad() )
116 {
117 LSET newLayers = PAD::SMDMask();
118
119 if( isBackOnlyPad( aPad ) )
120 newLayers = newLayers.FlipStandardLayers();
121
122 aPad.SetLayerSet( newLayers );
123 }
124
126 }
127 else if( MatchTranslationOrNative( aType, _HKI( "Connector" ), false ) )
128 {
130 }
131 else if( MatchTranslationOrNative( aType, _HKI( "Non-component" ), false ) )
132 {
134 {
135 // Take the first set copper layer as the default for non-component pads.
136 const LSET currCu = aPad.GetLayerSet() & LSET::AllCuMask();
137
138 if( !currCu.any() )
139 aPad.SetLayerSet( LSET{ F_Cu } );
140 else
141 aPad.SetLayerSet( LSET{ currCu.CuStack().front() } );
142
144 }
145 }
146 else if( MatchTranslationOrNative( aType, _HKI( "NPTH" ), false ) )
147 {
149 }
150 else if( MatchTranslationOrNative( aType, _HKI( "Aperture" ), false ) )
151 {
152 if( !aPad.IsAperturePad() )
153 {
154 // Aperture pads are SMD pads with no copper
156
157 // Unset layers except F.Paste
158 LSET apertureLayers{ F_Paste };
159
160 if( isBackOnlyPad( aPad ) )
161 apertureLayers = apertureLayers.FlipStandardLayers();
162
163 aPad.SetLayerSet( apertureLayers );
164 }
165 }
166
167 // Note, bad strings can sneak in here, e.g. via pasting into a dropdown cell.
168 // So don't assert or crash, it's not necessarily a programming error.
169}
170
171
172static bool DrillsAreEditable( const PAD& aPad )
173{
174 return aPad.GetAttribute() == PAD_ATTRIB::PTH || aPad.GetAttribute() == PAD_ATTRIB::NPTH;
175}
176
177
188static void UpdateDrillCells( WX_GRID& aGrid, UNITS_PROVIDER& aUnitsProvider, int aRowId, const PAD& aPad,
189 bool aPreserveValues )
190{
191 const bool drillIsEditable = DrillsAreEditable( aPad );
192 const wxColour drillTextColour = drillIsEditable ? aGrid.GetDefaultCellTextColour()
193 : wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT );
194
195 aGrid.SetReadOnly( aRowId, COLS::COL_DRILL_X, !drillIsEditable );
196 aGrid.SetReadOnly( aRowId, COLS::COL_DRILL_Y, !drillIsEditable );
197 aGrid.SetCellTextColour( aRowId, COLS::COL_DRILL_X, drillTextColour );
198 aGrid.SetCellTextColour( aRowId, COLS::COL_DRILL_Y, drillTextColour );
199
200 if( aPreserveValues )
201 return;
202
203 const VECTOR2I drill = aPad.GetDrillSize();
204 aGrid.SetCellValue( aRowId, COLS::COL_DRILL_X, drill.x > 0 ? aUnitsProvider.StringFromValue( drill.x, true )
205 : wxString{} );
206 aGrid.SetCellValue( aRowId, COLS::COL_DRILL_Y, drill.y > 0 ? aUnitsProvider.StringFromValue( drill.y, true )
207 : wxString{} );
208}
209
210
216static wxString GetPadTableColLabel( COLS aCol )
217{
218 switch( aCol )
219 {
220 case COLS::COL_NUMBER: return wxT( "Number" );
221 case COLS::COL_TYPE: return wxT( "Type" );
222 case COLS::COL_SHAPE: return wxT( "Shape" );
223 case COLS::COL_POS_X: return wxT( "Pos X" );
224 case COLS::COL_POS_Y: return wxT( "Pos Y" );
225 case COLS::COL_SIZE_X: return wxT( "Size X" );
226 case COLS::COL_SIZE_Y: return wxT( "Size Y" );
227 case COLS::COL_DRILL_X: return wxT( "Drill X" );
228 case COLS::COL_DRILL_Y: return wxT( "Drill Y" );
229 case COLS::COL_P2D_LENGTH: return wxT( "Pad to die length" );
230 case COLS::COL_P2D_DELAY: return wxT( "Pad to die delay" );
231 default:
232 wxFAIL_MSG( wxT( "Invalid column index" ) );
233 return wxEmptyString;
234 }
235}
236
237
238static COLS GetColTypeForString( const wxString& aStr )
239{
240 for( int i = 0; i < static_cast<int>( COLS::COL_COUNT ); i++ )
241 {
242 COLS col = static_cast<COLS>( i );
243
244 if( MatchTranslationOrNative( aStr, GetPadTableColLabel( col ), false ) )
245 return col;
246 }
247 return COLS::COL_COUNT;
248}
249
250
258{
259public:
260 PAD_INFO_FORMATTER( UNITS_PROVIDER& aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat,
261 REPORTER& aReporter ) :
262 COLUMN_FORMATTER( aUnitsProvider, aIncludeUnits, aBoolFormat, aReporter )
263 {
264 }
265
266 wxString Format( const PAD& aPin, int aFieldId ) const
267 {
268 wxCHECK_MSG( aFieldId >= 0 && aFieldId < static_cast<int>( COLS::COL_COUNT ), wxEmptyString,
269 wxT( "Invalid column index" ) );
270
271 const DIALOG_FP_EDIT_PAD_TABLE::COLS col = static_cast<DIALOG_FP_EDIT_PAD_TABLE::COLS>( aFieldId );
272
273 switch( col )
274 {
276 return aPin.GetNumber();
277
279 return GetPadTypeString( aPin );
280
283
285 return m_unitsProvider.StringFromValue( aPin.GetCenter().x, m_includeUnits );
286
288 return m_unitsProvider.StringFromValue( aPin.GetCenter().y, m_includeUnits );
289
291 return m_unitsProvider.StringFromValue( aPin.GetSize( PADSTACK::ALL_LAYERS ).x, m_includeUnits );
292
294 return m_unitsProvider.StringFromValue( aPin.GetSize( PADSTACK::ALL_LAYERS ).y, m_includeUnits );
295
297 return m_unitsProvider.StringFromValue( aPin.GetDrillSize().x, m_includeUnits );
298
300 return m_unitsProvider.StringFromValue( aPin.GetDrillSize().y, m_includeUnits );
301
303 if( aPin.GetPadToDieLength() )
304 return m_unitsProvider.StringFromValue( aPin.GetPadToDieLength(), m_includeUnits );
305
306 return wxEmptyString;
307
309 if( aPin.GetPadToDieDelay() )
310 return m_unitsProvider.StringFromValue( aPin.GetPadToDieDelay(), m_includeUnits, EDA_DATA_TYPE::TIME );
311
312 return wxEmptyString;
313
315 return wxEmptyString;
316 }
317
318 wxFAIL_MSG( "Invalid column index" );
319 return wxEmptyString;
320 }
321
328 void UpdatePad( PAD& aPad, const wxString& aValue, int aFieldId ) const
329 {
330 switch( aFieldId )
331 {
333 aPad.SetNumber( aValue );
334 break;
335
337 SetPadTypeFromString( aPad, aValue );
338 break;
339
341 // Note that this will blow away any layer-specific shapes, but this process
342 // doesn't support layer-specific editing.
344 [&]( PCB_LAYER_ID aLayer )
345 {
346 aPad.SetShape( aLayer, ShapeFromString( aValue ) );
347 } );
348 break;
349
351 {
352 VECTOR2I pos = aPad.GetPosition();
353 pos.x = m_unitsProvider.ValueFromString( aValue );
354 aPad.SetPosition( pos );
355 break;
356 }
357
359 {
360 VECTOR2I pos = aPad.GetPosition();
361 pos.y = m_unitsProvider.ValueFromString( aValue );
362 aPad.SetPosition( pos );
363 break;
364 }
365
367 // Same as shape: overwrite all layers
369 [&]( PCB_LAYER_ID aLayer )
370 {
371 VECTOR2I size = aPad.GetSize( aLayer );
372 size.x = m_unitsProvider.ValueFromString( aValue );
373 aPad.SetSize( aLayer, size );
374 } );
375 break;
376
378 // Same as shape: overwrite all layers
380 [&]( PCB_LAYER_ID aLayer )
381 {
382 VECTOR2I size = aPad.GetSize( aLayer );
383 size.y = m_unitsProvider.ValueFromString( aValue );
384 aPad.SetSize( aLayer, size );
385 } );
386 break;
387
389 {
390 VECTOR2I drill = aPad.GetDrillSize();
391 drill.x = m_unitsProvider.ValueFromString( aValue );
392 aPad.SetDrillSize( drill );
393 break;
394 }
395
397 {
398 VECTOR2I drill = aPad.GetDrillSize();
399 drill.y = m_unitsProvider.ValueFromString( aValue );
400 aPad.SetDrillSize( drill );
401 break;
402 }
403
405 if( aValue.empty() )
406 aPad.SetPadToDieLength( 0 );
407 else
408 aPad.SetPadToDieLength( m_unitsProvider.ValueFromString( aValue ) );
409
410 break;
411
413 if( aValue.empty() )
414 aPad.SetPadToDieDelay( 0 );
415 else
416 aPad.SetPadToDieDelay( m_unitsProvider.ValueFromString( aValue, EDA_DATA_TYPE::TIME ) );
417
418 break;
419
420 default:
421 wxFAIL_MSG( "Invalid column index" );
422 break;
423 }
424 }
425};
426
427
429 DIALOG_FP_EDIT_PAD_TABLE_BASE( (wxWindow*) aParent ),
430 m_frame( aParent ),
431 m_footprint( aFootprint ),
433 m_summaryDirty( true )
434{
436
437 // The base class created a single placeholder row; resize the grid to fit the pads.
438 if( m_grid->GetNumberRows() > 0 )
439 m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
440
441 if( !m_originalPads.empty() )
442 m_grid->AppendRows( static_cast<int>( m_originalPads.size() ) );
443
444 // Constrain summary label widths so they ellipsize rather than push the layout around
445 // when long pin-number summaries (or duplicate lists) are produced.
446 const int summaryW = m_pin_numbers_summary->GetCharWidth() * 30;
447
448 m_duplicate_pins->SetWindowStyleFlag( m_duplicate_pins->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
449 m_pin_numbers_summary->SetMaxSize( wxSize( summaryW, -1 ) );
450
451 m_duplicate_pins->SetWindowStyleFlag( m_duplicate_pins->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
452 m_duplicate_pins->SetMaxSize( wxSize( summaryW, -1 ) );
453
454 wxGridCellAttr* attr = nullptr;
455
456 // Type column editor (attribute)
457 attr = new wxGridCellAttr;
458 wxArrayString typeNames;
459 typeNames.push_back( _( "Through-hole" ) ); // PTH
460 typeNames.push_back( _( "SMD" ) ); // SMD
461 typeNames.push_back( _( "Connector" ) ); // CONN SMD? (use CONN?)
462 typeNames.push_back( _( "NPTH" ) ); // NPTH
463 typeNames.push_back( _( "Non-component" ) ); // NON_COMPONENT
464 typeNames.push_back( _( "Aperture" ) ); // inferred copper-less
465 attr->SetEditor( new GRID_CELL_COMBOBOX( typeNames ) );
466 m_grid->SetColAttr( COL_TYPE, attr );
467
468 attr = new wxGridCellAttr;
469 wxArrayString shapeNames;
477 attr->SetEditor( new GRID_CELL_COMBOBOX( shapeNames ) );
478 m_grid->SetColAttr( COL_SHAPE, attr );
479
480 attr = new wxGridCellAttr;
481 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
482 m_grid->SetColAttr( COL_POS_X, attr );
483
484 attr = new wxGridCellAttr;
485 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
486 m_grid->SetColAttr( COL_POS_Y, attr );
487
488 attr = new wxGridCellAttr;
489 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
490 m_grid->SetColAttr( COL_SIZE_X, attr );
491
492 attr = new wxGridCellAttr;
493 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
494 m_grid->SetColAttr( COL_SIZE_Y, attr );
495
496 // Drill X
497 attr = new wxGridCellAttr;
498 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
499 m_grid->SetColAttr( COL_DRILL_X, attr );
500
501 // Drill Y
502 attr = new wxGridCellAttr;
503 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
504 m_grid->SetColAttr( COL_DRILL_Y, attr );
505
506 // Pad->Die Length
507 m_grid->SetAutoEvalColUnits( COL_P2D_LENGTH, m_unitsProvider->GetUnitsFromType( EDA_DATA_TYPE::DISTANCE ) );
508
509 // Pad->Die Delay
510 m_grid->SetAutoEvalColUnits( COL_P2D_DELAY, m_unitsProvider->GetUnitsFromType( EDA_DATA_TYPE::TIME ) );
511
512 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_POS_X );
513 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_POS_Y );
514 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_SIZE_X );
515 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_SIZE_Y );
516 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_DRILL_X );
517 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_DRILL_Y );
518 m_grid->SetAutoEvalCols( { COL_POS_X, COL_POS_Y,
522 COL_P2D_DELAY } );
523
524 // add Cut, Copy, and Paste to wxGrid
525 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
526
529
531
532 Layout();
534
535 // Cap the initial height so the dialog does not grow off-screen for footprints
536 // with many pads. The grid grows to fill the available space via wxEXPAND.
537 // Use the parent window to find the display since this dialog isn't shown yet.
538 int displayIdx = wxDisplay::GetFromWindow( aParent );
539
540 if( displayIdx == wxNOT_FOUND )
541 displayIdx = 0;
542
543 wxRect displayArea = wxDisplay( (unsigned int) displayIdx ).GetClientArea();
544 wxSize dlgSize = GetSize();
545 int maxH = ( displayArea.height * 4 ) / 5;
546
547 if( dlgSize.y > maxH )
548 {
549 dlgSize.y = maxH;
550 SetSize( dlgSize );
551
552 // Reset minimum height so the user can resize the capped dialog freely.
553 // The minimum width from finishDialogSettings() is still honoured.
554 wxSize minSz = GetMinSize();
555 minSz.y = -1;
556 SetMinSize( minSz );
557
558 Centre();
559 }
560}
561
562
564{
565 // Roll back any session changes unless the dialog was accepted. This means
566 // rollback happens on both title-bar 'X'/Esc and Cancel.
567 if( !m_accepted )
569
570 // destroy GRID_TRICKS before m_grid.
571 m_grid->PopEventHandler( true );
572}
573
574
576{
577 m_grid->SetCellValue( aRowId, COL_NUMBER, aPad->GetNumber() );
578
579 wxString attrStr = GetPadTypeString( *aPad );
580 int size_x = aPad->GetSize( F_Cu ).x;
581 int size_y = aPad->GetSize( F_Cu ).y;
582 wxString padShape = aPad->ShowPadShape( F_Cu );
583
585 [&]( PCB_LAYER_ID aLayer )
586 {
587 if( aPad->GetSize( aLayer ).x != size_x )
588 size_x = -1;
589
590 if( aPad->GetSize( aLayer ).y != size_y )
591 size_y = -1;
592
593 if( aPad->ShowPadShape( aLayer ) != padShape )
594 padShape = INDETERMINATE_STATE;
595 } );
596
597 m_grid->SetCellValue( aRowId, COL_TYPE, attrStr );
598 m_grid->SetCellValue( aRowId, COL_SHAPE, padShape );
599 m_grid->SetCellValue( aRowId, COL_POS_X, m_unitsProvider->StringFromValue( aPad->GetPosition().x, true ) );
600 m_grid->SetCellValue( aRowId, COL_POS_Y, m_unitsProvider->StringFromValue( aPad->GetPosition().y, true ) );
601 m_grid->SetCellValue( aRowId, COL_SIZE_X, size_x >= 0 ? m_unitsProvider->StringFromValue( size_x, true )
603 m_grid->SetCellValue( aRowId, COL_SIZE_Y, size_y >= 0 ? m_unitsProvider->StringFromValue( size_y, true )
605
606 UpdateDrillCells( *m_grid, *m_unitsProvider, aRowId, *aPad, false );
607
608 // Pad to die metrics
609 if( aPad->GetPadToDieLength() )
610 m_grid->SetUnitValue( aRowId, COL_P2D_LENGTH, aPad->GetPadToDieLength() );
611 else
612 m_grid->SetCellValue( aRowId, COL_P2D_LENGTH, wxEmptyString );
613
614 if( aPad->GetPadToDieDelay() )
615 m_grid->SetUnitValue( aRowId, COL_P2D_DELAY, aPad->GetPadToDieDelay() );
616 else
617 m_grid->SetCellValue( aRowId, COL_P2D_DELAY, wxEmptyString );
618
619 setRowNullableEditors( aRowId );
620}
621
622
624{
625 if( !m_footprint )
626 return false;
627
628 int row = 0;
629
630 for( PAD* pad : m_rowPads )
631 {
632 fillGridRow( row, pad );
633 row++;
634 }
635
636 // Auto size the data columns first to get reasonable initial widths
637 m_grid->AutoSizeColumns();
638
639 // Ensure the Shape column (index 1) is wide enough for the longest translated
640 // shape text plus the dropdown arrow / padding. We compute a max text width
641 // using a device context and add a platform neutral padding.
642 {
643 wxClientDC dc( m_grid );
644 dc.SetFont( m_grid->GetFont() );
645
646 wxArrayString shapeNames;
654
655 int maxWidth = 0;
656
657 for( const wxString& str : shapeNames )
658 {
659 int w, h;
660 dc.GetTextExtent( str, &w, &h );
661 maxWidth = std::max( maxWidth, w );
662 }
663
664 // Add padding for internal cell margins + dropdown control.
665 int padding = FromDIP( 30 ); // heuristic: 2*margin + arrow button
666 m_grid->SetColSize( COL_SHAPE, maxWidth + padding );
667 }
668
669 // Record initial proportions for proportional resizing later.
671
672 // Run an initial proportional resize using current client size so columns
673 // respect proportions immediately.
674 wxSizeEvent sizeEvt( GetSize(), GetId() );
675 CallAfter(
676 [this, sizeEvt]
677 {
678 wxSizeEvent evt( sizeEvt );
679 this->OnSize( evt );
680 } );
681
682 // If pads exist, select the first row to show initial highlight
683 if( m_grid->GetNumberRows() > 0 )
684 {
685 m_grid->SetGridCursor( 0, 0 );
686
687 // Construct event with required parameters (id, type, obj, row, col,...)
688 wxGridEvent ev( m_grid->GetId(), wxEVT_GRID_SELECT_CELL, m_grid, 0, 0, -1, -1, true );
689 OnSelectCell( ev );
690 }
691
692 return true;
693}
694
695
697{
698 // Set nullable editors
699 auto setCellEditor =
700 [this, aRowId]( int aCol )
701 {
703 wxGridCellAttr* attr = m_grid->GetOrCreateCellAttr( aRowId, aCol );
704 attr->SetEditor( cellEditor );
705 attr->DecRef();
706 };
707
708 setCellEditor( COL_P2D_LENGTH );
709 setCellEditor( COL_P2D_DELAY );
710}
711
712
714{
715 m_originalPads.clear();
716 m_rowPads.clear();
717 m_removedPads.clear();
718
719 if( !m_footprint )
720 return;
721
722 for( PAD* pad : m_footprint->Pads() )
723 {
724 PAD_SNAPSHOT snap( pad );
725 snap.number = pad->GetNumber();
726 snap.position = pad->GetPosition();
727 snap.padstack = pad->Padstack();
728 snap.attribute = pad->GetAttribute();
729 snap.padToDieLength= pad->GetPadToDieLength();
730 snap.padToDieDelay = pad->GetPadToDieDelay();
731
732 m_originalPads.try_emplace( pad, std::move( snap ) );
733 m_rowPads.push_back( pad );
734 }
735
736 std::sort( m_rowPads.begin(), m_rowPads.end(), PAD_SNAPSHOT_COMPARE() );
737}
738
739
741{
742 aPad.SetNumber( aSnap.number );
743 aPad.SetPosition( aSnap.position );
744 aPad.SetPadstack( aSnap.padstack );
745 aPad.SetAttribute( aSnap.attribute );
746 aPad.SetPadToDieLength( aSnap.padToDieLength );
747 aPad.SetPadToDieDelay( aSnap.padToDieDelay );
748}
749
750
752{
753 if( !m_footprint )
754 return;
755
756 const PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
757 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
758
759 for( PAD* pad : m_footprint->Pads() )
760 {
761 // Clear brighted even if the pad isn't original
762 pad->ClearBrightened();
763
764 if( !m_originalPads.contains( pad ) )
765 continue;
766
768
769 if( canvas )
770 canvas->GetView()->Update( pad, KIGFX::REPAINT );
771 }
772}
773
774
776{
777 if( !m_footprint )
778 return;
779
781
782 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
783 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
784 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
785
786 // Remove pads added during the session; they have no snapshot entry.
787 std::vector<PAD*> livePads( m_footprint->Pads().begin(), m_footprint->Pads().end() );
788
789 for( PAD* pad : livePads )
790 {
791 if( m_originalPads.contains( pad ) )
792 continue;
793
794 m_footprint->Remove( pad );
795
796 if( view )
797 view->Remove( pad );
798
799 delete pad;
800 }
801
802 // Re-add pads removed during the session and restore their original data.
803 // Pads with no snapshot were added and removed again, so just drop them.
804 for( PAD* pad : m_removedPads )
805 {
806 auto snapIt = m_originalPads.find( pad );
807
808 if( snapIt == m_originalPads.end() )
809 {
810 delete pad;
811 continue;
812 }
813
814 restorePadFromSnapshot( *pad, snapIt->second );
815 m_footprint->Add( pad );
816
817 if( view )
818 view->Add( pad );
819 }
820
821 m_removedPads.clear();
822
823 // Rebuild the row mapping so no pointers to freed pads survive for any UI
824 // events still pending while the dialog closes.
825 m_rowPads.clear();
826
827 for( PAD* pad : m_footprint->Pads() )
828 m_rowPads.push_back( pad );
829
830 std::sort( m_rowPads.begin(), m_rowPads.end(), PAD_SNAPSHOT_COMPARE() );
831
832 if( canvas )
833 {
835 canvas->ForceRefresh();
836 }
837
838 m_summaryDirty = true;
839}
840
841
843{
844 switch( aCol )
845 {
846 case COL_NUMBER:
847 aPad.SetNumber( m_grid->GetCellValue( aRowId, aCol ) );
848 break;
849
850 case COL_TYPE:
851 SetPadTypeFromString( aPad, m_grid->GetCellValue( aRowId, aCol ) );
852 break;
853
854 case COL_SHAPE:
855 {
856 const wxString shape = m_grid->GetCellValue( aRowId, aCol );
857
858 if( shape == INDETERMINATE_STATE )
859 break;
860
861 const PAD_SHAPE newShape = ShapeFromString( shape );
862
864 [&]( PCB_LAYER_ID aLayer )
865 {
866 aPad.SetShape( aLayer, newShape );
867 } );
868 break;
869 }
870
871 case COL_POS_X:
872 case COL_POS_Y:
873 {
874 VECTOR2I pos = aPad.GetPosition();
875
876 if( aCol == COL_POS_X )
877 pos.x = m_grid->GetUnitValue( aRowId, aCol );
878 else
879 pos.y = m_grid->GetUnitValue( aRowId, aCol );
880
881 aPad.SetPosition( pos );
882 break;
883 }
884
885 case COL_SIZE_X:
886 case COL_SIZE_Y:
887 {
888 const wxString sizeValue = m_grid->GetCellValue( aRowId, aCol );
889
890 if( sizeValue == INDETERMINATE_STATE )
891 break;
892
893 const int size = m_grid->GetUnitValue( aRowId, aCol );
894
896 [&]( PCB_LAYER_ID aLayer )
897 {
898 VECTOR2I layerSize = aPad.GetSize( aLayer );
899
900 if( aCol == COL_SIZE_X )
901 layerSize.x = size;
902 else
903 layerSize.y = size;
904
905 aPad.SetSize( aLayer, layerSize );
906 } );
907 break;
908 }
909
910 case COL_DRILL_X:
911 case COL_DRILL_Y:
912 {
913 // Drill sizes (only if attribute allows)
914 if( DrillsAreEditable( aPad ) )
915 {
916 int drillX = m_grid->GetUnitValue( aRowId, COL_DRILL_X );
917 int drillY = m_grid->GetUnitValue( aRowId, COL_DRILL_Y );
918
919 if( drillX > 0 || drillY > 0 )
920 {
921 if( drillX <= 0 )
922 drillX = drillY;
923
924 if( drillY <= 0 )
925 drillY = drillX;
926
927 aPad.SetDrillSize( { drillX, drillY } );
928 }
929 }
930
931 break;
932 }
933
934 case COL_P2D_LENGTH:
935 {
936 const wxString lenStr = m_grid->GetCellValue( aRowId, aCol );
937
938 if( lenStr.IsEmpty() )
939 aPad.SetPadToDieLength( 0 );
940 else
941 aPad.SetPadToDieLength( m_grid->GetUnitValue( aRowId, aCol ) );
942
943 break;
944 }
945
946 case COL_P2D_DELAY:
947 {
948 const wxString delayStr = m_grid->GetCellValue( aRowId, aCol );
949
950 if( delayStr.IsEmpty() )
951 aPad.SetPadToDieDelay( 0 );
952 else
953 aPad.SetPadToDieDelay( m_grid->GetUnitValue( aRowId, aCol ) );
954
955 break;
956 }
957
958 default:
959 wxFAIL_MSG( wxT( "Invalid column index" ) );
960 break;
961 }
962}
963
964
966{
967 if( !m_grid->CommitPendingChanges() )
968 return false;
969
970 if( !m_footprint )
971 return true;
972
973 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
974 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
975 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
976
978
979 BOARD_COMMIT commit( m_frame );
980
981 // Pads removed during the session are re-inserted momentarily so their
982 // removal can be staged in this commit (Push performs the actual removal).
983 for( PAD* pad : m_removedPads )
984 {
985 auto snapIt = m_originalPads.find( pad );
986
987 if( snapIt == m_originalPads.end() )
988 {
989 // A pad added and removed again during the session: it never needs
990 // to appear in the commit.
991 delete pad;
992 continue;
993 }
994
995 restorePadFromSnapshot( *pad, snapIt->second );
996 m_footprint->Add( pad );
997
998 if( view )
999 view->Add( pad );
1000
1001 commit.Remove( pad );
1002 }
1003
1004 m_removedPads.clear();
1005
1006 int row = 0;
1007
1008 const auto applyRowDataToPad =
1009 [&]( PAD& aPad, int aRowId )
1010 {
1011 for( int col = 0; col < m_grid->GetNumberCols(); ++col )
1012 setPadFromGridCell( aPad, aRowId, static_cast<COLS>( col ) );
1013 };
1014
1015 for( PAD* pad : m_rowPads )
1016 {
1017 if( m_originalPads.contains( pad ) )
1018 {
1019 // Existing pad: its data was already restored to the dialog-open
1020 // state above, so the commit's undo image is correct.
1021 commit.Modify( pad );
1022 applyRowDataToPad( *pad, row );
1023 }
1024 else
1025 {
1026 // Imported pad: it was added to the footprint at import time for the
1027 // canvas preview. Take it out again so the commit can stage a clean
1028 // addition.
1029 applyRowDataToPad( *pad, row );
1030
1031 m_footprint->Remove( pad );
1032
1033 if( view )
1034 view->Remove( pad );
1035
1036 commit.Add( pad );
1037 }
1038
1039 row++;
1040 }
1041
1042 commit.Push( _( "Edit Pads" ) );
1043 m_frame->Refresh();
1044
1045 m_accepted = true;
1046
1047 return true;
1048}
1049
1050
1052{
1053 m_colProportions.clear();
1054 m_minColWidths.clear();
1055
1056 if( !m_grid )
1057 return;
1058
1059 // Only consider the actual data columns (all of them since row labels are hidden)
1060 int cols = m_grid->GetNumberCols();
1061 int total = 0;
1062 std::vector<int> widths;
1063 widths.reserve( cols );
1064
1065 for( int c = 0; c < cols; ++c )
1066 {
1067 int w = m_grid->GetColSize( c );
1068 widths.push_back( w );
1069 total += w;
1070 }
1071
1072 if( total <= 0 )
1073 return;
1074
1075 for( int w : widths )
1076 {
1077 m_colProportions.push_back( (double) w / (double) total );
1078 m_minColWidths.push_back( w );
1079 }
1080}
1081
1082
1083void DIALOG_FP_EDIT_PAD_TABLE::OnSize( wxSizeEvent& aEvent )
1084{
1085 if( m_colProportions.empty() )
1086 {
1087 aEvent.Skip();
1088 return;
1089 }
1090
1091 // Compute available total width for columns and resize keeping proportions.
1092 int cols = m_grid->GetNumberCols();
1093 int available = 0;
1094
1095 for( int c = 0; c < cols; ++c )
1096 available += m_grid->GetColSize( c );
1097
1098 // Use client size of grid minus scrollbar estimate to better distribute.
1099 int clientW = m_grid->GetClientSize().x;
1100
1101 if( clientW > 0 )
1102 available = clientW; // prefer actual client width
1103
1104 int used = 0;
1105
1106 for( int c = 0; c < cols; ++c )
1107 {
1108 int target = (int) std::round( m_colProportions[c] * available );
1109 target = std::max( target, m_minColWidths[c] );
1110
1111 // Defer last column to absorb rounding diff.
1112 if( c == cols - 1 )
1113 target = std::max( available - used, m_minColWidths[c] );
1114
1115 m_grid->SetColSize( c, target );
1116 used += target;
1117 }
1118
1119 aEvent.Skip();
1120}
1121
1122
1124{
1125 if( m_grid->IsCellEditControlShown() && m_grid->GetGridCursorCol() == COL_NUMBER )
1126 m_summaryDirty = true;
1127
1128 DIALOG_SHIM::OnCharHook( aEvent );
1129}
1130
1131
1133{
1134 int row = aEvent.GetRow();
1135 int col = aEvent.GetCol();
1136
1137 if( !m_footprint )
1138 return;
1139
1140 PAD* target = getPadForRow( row );
1141
1142 if( !target )
1143 return;
1144
1145 const bool drillsWereEditable = DrillsAreEditable( *target );
1146 bool needCanvasRefresh = true;
1147
1148 setPadFromGridCell( *target, row, static_cast<COLS>( col ) );
1149
1150 if( col == COL_TYPE )
1151 {
1152 const bool drillsAreEditable = DrillsAreEditable( *target );
1153
1154 if( drillsAreEditable )
1155 {
1156 // PAD::SetAttribute() removes drills for SMD pads. So if we roundtrip
1157 // from PTH -> SMD -> PTH, the drill size is lost. In that case, restore
1158 // from the "ghost" value in the grid if there is one.
1159 const int drillX = m_grid->GetUnitValue( row, COL_DRILL_X );
1160 const int drillY = m_grid->GetUnitValue( row, COL_DRILL_Y );
1161
1162 if( drillX > 0 || drillY > 0 )
1163 {
1164 setPadFromGridCell( *target, row, COL_DRILL_X );
1165 }
1166 else if( !drillsWereEditable )
1167 {
1168 // Use a default drill size the pad is becoming editable
1169 // And there was no "ghost" value in the grid to restore from.
1170 const int defaultDrill = pcbIUScale.mmToIU( 1.0 );
1171 target->SetDrillSize( { defaultDrill, defaultDrill } );
1172 }
1173 }
1174
1175 UpdateDrillCells( *m_grid, *m_unitsProvider, row, *target, !drillsAreEditable );
1176 }
1177 else if( col == COL_P2D_LENGTH || col == COL_P2D_DELAY )
1178 {
1179 // Pad-to-die values are not drawn on the canvas.
1180 needCanvasRefresh = false;
1181 }
1182
1183 if( col == COL_NUMBER )
1184 m_summaryDirty = true;
1185
1186 // Request redraw (simple approach)
1187 target->SetDirty();
1188
1189 if( needCanvasRefresh )
1190 {
1191 if( PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() ) )
1192 {
1193 if( KIGFX::PCB_VIEW* view = base->GetCanvas()->GetView() )
1194 {
1195 // Some changes, e.g. type change, can change the layers
1196 view->Update( target, KIGFX::REPAINT | KIGFX::LAYERS );
1197 }
1198
1199 base->GetCanvas()->ForceRefresh();
1200 }
1201 }
1202}
1203
1204
1206{
1207 int row = aEvent.GetRow();
1208
1209 if( !m_footprint )
1210 return;
1211
1212 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1213 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1214
1215 // Clear existing pad selections
1216 for( PAD* pad : m_footprint->Pads() )
1217 {
1218 if( pad->IsBrightened() )
1219 {
1220 pad->ClearBrightened();
1221
1222 if( canvas )
1223 canvas->GetView()->Update( pad, KIGFX::REPAINT );
1224 }
1225 }
1226
1227 PAD* pad = getPadForRow( row );
1228
1229 if( !pad )
1230 return;
1231
1232 pad->SetBrightened();
1233
1234 if( canvas )
1235 {
1236 canvas->GetView()->Update( pad, KIGFX::REPAINT );
1237 canvas->ForceRefresh();
1238 }
1239}
1240
1241
1242void DIALOG_FP_EDIT_PAD_TABLE::OnUpdateUI( wxUpdateUIEvent& aEvent )
1243{
1244 if( m_summaryDirty )
1245 {
1246 if( m_grid->IsCellEditControlShown() && m_grid->GetGridCursorCol() == COL_NUMBER )
1247 {
1248 int row = m_grid->GetGridCursorRow();
1249 int col = m_grid->GetGridCursorCol();
1250
1251 PAD* target = getPadForRow( row );
1252
1253 if( !target )
1254 return;
1255
1256 wxGridCellEditor* editor = m_grid->GetCellEditor( row, col );
1257
1258 if( editor )
1259 {
1260 target->SetNumber( editor->GetValue() );
1261 editor->DecRef();
1262 }
1263 }
1264
1265 updateSummary();
1266 m_summaryDirty = false;
1267 }
1268}
1269
1270
1271void DIALOG_FP_EDIT_PAD_TABLE::OnCancel( wxCommandEvent& aEvent )
1272{
1273 // The destructor rolls back everything that was not accepted.
1274 m_accepted = false;
1275 aEvent.Skip();
1276}
1277
1278
1280{
1281 PIN_NUMBERS pinNumbers;
1282
1283 for( PAD* pad : m_footprint->Pads() )
1284 {
1285 if( pad->GetNumber().Length() )
1286 pinNumbers.insert( pad->GetNumber() );
1287 }
1288
1289 const wxString summary = pinNumbers.GetSummary();
1290 const wxString duplicates = pinNumbers.GetDuplicates();
1291
1292 m_pin_numbers_summary->SetLabel( summary );
1293 m_pin_numbers_summary->SetToolTip( summary );
1294 m_pin_count->SetLabel( wxString::Format( wxT( "%u" ), (unsigned) m_footprint->Pads().size() ) );
1295 m_duplicate_pins->SetLabel( duplicates );
1296 m_duplicate_pins->SetToolTip( duplicates );
1297
1298 Layout();
1299}
1300
1301
1303{
1304 if( aRowId < 0 || static_cast<size_t>( aRowId ) >= m_rowPads.size() )
1305 return nullptr;
1306
1307 return m_rowPads[aRowId];
1308}
1309
1310
1312{
1313 bool toFile = aEvent.GetEventObject() == m_btnExportToFile;
1314
1315 wxString filePath;
1316
1317 if( toFile )
1318 {
1319 wxFileName fn( m_footprint->GetFPID().GetLibItemName() );
1320 fn.SetExt( FILEEXT::CsvFileExtension );
1321
1322 wxFileDialog dlg( this, _( "Select pad data file" ), "", fn.GetFullName(), FILEEXT::CsvTsvFileWildcard(),
1323 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1324
1326
1327 if( dlg.ShowModal() == wxID_CANCEL )
1328 return;
1329
1330 filePath = dlg.GetPath();
1331 }
1332
1333 std::vector<PAD*> padsToExport;
1334 bool complexPadstacks = false;
1335
1336 for( PAD* pad : m_rowPads )
1337 {
1338 if( pad->GetPadstackMode() != PADSTACK::MODE::NORMAL )
1339 {
1340 complexPadstacks = true;
1341 continue;
1342 }
1343
1344 padsToExport.push_back( pad );
1345 }
1346
1347 if( complexPadstacks )
1348 {
1349 if( wxMessageBox( _( "Complex padstacks cannot be exported via CSV.\n\nThey will be skipped." ),
1350 _( "Export Pad Table" ), wxOK | wxCANCEL | wxICON_WARNING, this ) != wxOK )
1351 {
1352 return;
1353 }
1354 }
1355
1356 static const std::vector<COLS> exportCols {
1357 COLS::COL_NUMBER,
1358 COLS::COL_TYPE,
1359 COLS::COL_SHAPE,
1360 COLS::COL_POS_X,
1361 COLS::COL_POS_Y,
1362 COLS::COL_SIZE_X,
1363 COLS::COL_SIZE_Y,
1364 COLS::COL_DRILL_X,
1365 COLS::COL_DRILL_Y,
1366 COLS::COL_P2D_LENGTH,
1367 COLS::COL_P2D_DELAY,
1368 };
1369
1372
1373 std::vector<std::vector<wxString>> table;
1374 table.reserve( padsToExport.size() + 1 );
1375
1376 std::vector<wxString> header;
1377 header.reserve( exportCols.size() );
1378
1379 for( COLS col : exportCols )
1380 header.emplace_back( wxGetTranslation( GetPadTableColLabel( col ) ) );
1381
1382 table.emplace_back( std::move( header ) );
1383
1384 for( PAD* pad : padsToExport )
1385 {
1386 std::vector<wxString>& row = table.emplace_back();
1387 row.reserve( exportCols.size() );
1388
1389 for( COLS col : exportCols )
1390 row.emplace_back( formatter.Format( *pad, col ) );
1391 }
1392
1394}
1395
1396
1398{
1399 bool fromFile = aEvent.GetEventObject() == m_btnImportFromFile;
1400 bool replaceAll = m_rbReplaceExisting->GetValue();
1401
1403
1405
1406 std::optional<std::vector<std::vector<wxString>>> csvData = ReadTableFromFileOrClipboard( *m_frame, fromFile );
1407
1408 // The pad table does not and cannot capture the full glory of a PAD object
1409 // (for example a custom pad's custom shape). So, when we are re-importing
1410 // pads and "replacing existing", we attempt to rematch the imported pads
1411 // to the existing ones by pad number. If a match is found, we update the
1412 // existing pad with the imported data. If no match is found, we add the
1413 // imported pad as a new pad. If "replace existing" is not selected, we
1414 // simply add the imported pads as new pads.
1415
1416 // Group the current pads by case-insensitive pad number so each imported
1417 // row can be rematched with a single map lookup rather than a linear scan.
1418 // Buckets keep the grid order, so this is stable as we'll match in grid order.
1419 std::map<wxString, std::vector<PAD*>> padsByNumber;
1420
1421 for( PAD* pad : m_rowPads )
1422 padsByNumber[pad->GetNumber()].push_back( pad );
1423
1424 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1425 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1426 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
1427
1428 std::vector<std::unique_ptr<PAD>> createdPads;
1429
1430 if( csvData && csvData->size() >= 2 )
1431 {
1432 std::vector<COLS> headerCols;
1433 wxArrayString unknownHeaders;
1434 std::optional<size_t> numberColIdx;
1435
1436 for( const wxString& label : ( *csvData )[0] )
1437 {
1438 COLS col = GetColTypeForString( label );
1439
1440 if( col >= COLS::COL_COUNT )
1441 unknownHeaders.push_back( label );
1442
1443 if( col == COLS::COL_NUMBER )
1444 numberColIdx = headerCols.size();
1445
1446 headerCols.push_back( col );
1447 }
1448
1449 if( replaceAll && !numberColIdx.has_value() )
1450 {
1451 wxString msg = _( "Imported pad data must include pad numbers." );
1452 wxMessageBox( msg, _( "Import error" ), wxOK | wxICON_ERROR, this );
1453 return;
1454 }
1455
1456 if( !unknownHeaders.IsEmpty() )
1457 {
1458 wxString msg = wxString::Format( _( "Unknown columns in data: %s. These columns will be ignored." ),
1459 AccumulateDescriptions( unknownHeaders ) );
1460 reporter.Report( msg, RPT_SEVERITY_WARNING );
1461 }
1462
1463 if( reporter.HasMessage() )
1464 {
1465 int ret = wxMessageBox( reporter.GetMessages(), _( "Errors" ), wxOK | wxCANCEL | wxICON_ERROR, this );
1466
1467 if( ret == wxCANCEL )
1468 return;
1469 }
1470
1471 for( size_t i = 1; i < csvData->size(); ++i )
1472 {
1473 const std::vector<wxString>& cols = ( *csvData )[i];
1474
1475 // Decide whether to create a new pad or update an existing one based on the
1476 // "replace existing" option and the pad number.
1477 std::unique_ptr<PAD> newPad;
1478 PAD* padToUpdate = nullptr;
1479
1480 const size_t numberCol = numberColIdx.value_or( cols.size() );
1481
1482 if( replaceAll && numberCol < cols.size() && !cols[numberCol].IsEmpty() )
1483 {
1484 auto bucketIt = padsByNumber.find( cols[numberCol] );
1485
1486 if( bucketIt != padsByNumber.end() && !bucketIt->second.empty() )
1487 {
1488 padToUpdate = bucketIt->second.front();
1489 bucketIt->second.erase( bucketIt->second.begin() );
1490 }
1491 }
1492
1493 if( !padToUpdate )
1494 {
1495 // We are adding a new pad, not updating an existing one.
1496 newPad = std::make_unique<PAD>( m_footprint );
1497 padToUpdate = newPad.get();
1498 }
1499
1500 size_t maxCol = std::min( headerCols.size(), cols.size() );
1501
1502 for( size_t j = 0; j < maxCol; ++j )
1503 {
1504 if( headerCols[j] == COLS::COL_COUNT )
1505 continue;
1506
1507 fmt.UpdatePad( *padToUpdate, cols[j], headerCols[j] );
1508 }
1509
1510 // Invalidate the pad's cached draw data and mark it for re-render
1511 // so the canvas refresh below shows the imported values.
1512 padToUpdate->SetDirty();
1513
1514 // Type changes can change the pad's layers
1515 if( view )
1516 view->Update( padToUpdate, KIGFX::REPAINT | KIGFX::LAYERS );
1517
1518 if( newPad )
1519 createdPads.push_back( std::move( newPad ) );
1520 }
1521
1522 // Bulk-load the imported pads into the view.
1523 if( !createdPads.empty() )
1524 {
1525 if( view )
1526 {
1527 std::vector<KIGFX::VIEW_ITEM*> viewPads;
1528
1529 for( const auto& pad : createdPads )
1530 viewPads.push_back( pad.get() );
1531
1532 view->AddBatch( viewPads );
1533 }
1534
1535 for( std::unique_ptr<PAD>& pad : createdPads )
1536 m_footprint->Add( pad.release(), ADD_MODE::BULK_APPEND, true );
1537 }
1538
1539 // Any pads not matched to an imported pad are removed from the footprint.
1540 // They are kept alive so they can be restored on cancel and staged as
1541 // removals when the dialog is accepted.
1542 if( replaceAll )
1543 {
1544 for( const auto& [number, pads] : padsByNumber )
1545 {
1546 for( PAD* pad : pads )
1547 {
1548 m_footprint->Remove( pad );
1549
1550 if( view )
1551 view->Remove( pad );
1552
1553 m_removedPads.push_back( pad );
1554 }
1555 }
1556 }
1557 }
1558
1559 // Commit any in-progress cell edits so the grid rebuild below does not
1560 // discard them.
1561 m_grid->CommitPendingChanges();
1562
1563 // Rebuild the row mapping from the pads now in the footprint.
1564 m_rowPads.clear();
1565
1566 for( PAD* pad : m_footprint->Pads() )
1567 m_rowPads.push_back( pad );
1568
1569 std::sort( m_rowPads.begin(), m_rowPads.end(), PAD_SNAPSHOT_COMPARE() );
1570
1571 // Rebuild the grid to match the new pad count.
1572 int currentRows = m_grid->GetNumberRows();
1573 int neededRows = static_cast<int>( m_rowPads.size() );
1574
1575 if( neededRows > currentRows )
1576 m_grid->AppendRows( neededRows - currentRows );
1577 else if( neededRows < currentRows )
1578 m_grid->DeleteRows( neededRows, currentRows - neededRows );
1579
1581 updateSummary();
1582
1583 if( canvas )
1584 canvas->ForceRefresh();
1585}
1586
1587
1588void DIALOG_FP_EDIT_PAD_TABLE::OnAddRow( wxCommandEvent& aEvent )
1589{
1590 if( !m_footprint )
1591 return;
1592
1593 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1594 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1595 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
1596
1597 m_grid->OnAddRow(
1598 [&]() -> std::pair<int, int>
1599 {
1600 PAD* newPad = nullptr;
1601
1602 // Copy the settings of the last pad onto the new pad and offset its position
1603 // by the current grid so the copy is easy to find.
1604 if( !m_rowPads.empty() )
1605 {
1606 newPad = static_cast<PAD*>( m_rowPads.back()->Duplicate( false, nullptr ) );
1607 newPad->Move( VECTOR2I( 0, KiROUND( canvas->GetGAL()->GetGridSize().y ) ) );
1608 }
1609 else
1610 {
1611 newPad = new PAD( m_footprint );
1612 }
1613
1614 if( view )
1615 view->Add( newPad );
1616
1617 m_rowPads.push_back( newPad );
1618 m_footprint->Add( newPad );
1619
1620 int row = m_grid->GetNumberRows();
1621 m_grid->AppendRows( 1 );
1622 fillGridRow( row, newPad );
1623 updateSummary();
1624
1625 if( canvas )
1626 canvas->ForceRefresh();
1627
1628 return { row, COL_NUMBER };
1629 } );
1630}
1631
1632
1633void DIALOG_FP_EDIT_PAD_TABLE::OnDeleteRow( wxCommandEvent& aEvent )
1634{
1635 if( !m_footprint )
1636 return;
1637
1638 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1639 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1640 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
1641
1642 m_grid->OnDeleteRows(
1643 [&]( int row )
1644 {
1645 if( row < 0 || static_cast<size_t>( row ) >= m_rowPads.size() )
1646 return;
1647
1648 PAD* pad = m_rowPads[row];
1649
1650 pad->ClearBrightened();
1651 m_footprint->Remove( pad );
1652
1653 if( view )
1654 view->Remove( pad );
1655
1656 // Keep the pad alive: on OK it is staged as a removal, on cancel
1657 // it is re-added to the footprint.
1658 m_removedPads.push_back( pad );
1659
1660 m_rowPads.erase( m_rowPads.begin() + row );
1661 m_grid->DeleteRows( row, 1 );
1662 } );
1663
1664 updateSummary();
1665
1666 if( canvas )
1667 canvas->ForceRefresh();
1668}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
COLUMN_FORMATTER(UNITS_PROVIDER &aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat, REPORTER &aReporter)
UNITS_PROVIDER & m_unitsProvider
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:86
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
DIALOG_FP_EDIT_PAD_TABLE_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Pad Table"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void setPadFromGridCell(PAD &aPad, int aRowId, COLS aCol)
void OnCellChanged(wxGridEvent &aEvent) override
DIALOG_FP_EDIT_PAD_TABLE(PCB_BASE_FRAME *aParent, FOOTPRINT *aFootprint)
void OnCharHook(wxKeyEvent &aEvent) override
void fillGridRow(int aRowId, PAD *aPad)
void OnSelectCell(wxGridEvent &aEvent) override
void setRowNullableEditors(int aRowId) const
void OnImportButtonClick(wxCommandEvent &aEvent) override
bool m_accepted
Set when the changes are committed on OK.
void OnExportButtonClick(wxCommandEvent &aEvent) override
std::map< PAD *, PAD_SNAPSHOT > m_originalPads
void OnDeleteRow(wxCommandEvent &aEvent) override
void OnSize(wxSizeEvent &aEvent) override
void restorePadFromSnapshot(PAD &aPad, const PAD_SNAPSHOT &aSnap) const
void OnCancel(wxCommandEvent &aEvent) override
std::vector< double > m_colProportions
std::unique_ptr< UNITS_PROVIDER > m_unitsProvider
void OnUpdateUI(wxUpdateUIEvent &aEvent) override
void OnAddRow(wxCommandEvent &aEvent) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
virtual void OnCharHook(wxKeyEvent &aEvt)
EDA_UNITS GetUserUnits() const
void ForceRefresh()
Force a redraw.
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:57
const VECTOR2D & GetGridSize() const
Return the grid size.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition pcb_view.cpp:87
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition pcb_view.cpp:53
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition pcb_view.cpp:70
void AddBatch(const std::vector< VIEW_ITEM * > &aItems)
Add a batch of items to the view, using bulk-loaded R-trees for initial population.
Definition view.cpp:414
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:729
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition lset.cpp:718
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
static const LSET & BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition lset.cpp:725
LSEQ CuStack() const
Return a sequence of copper layers in starting from the front/top and extending to the back/bottom.
Definition lset.cpp:259
LSET & FlipStandardLayers(int aCopperLayersCount=0)
Flip the layers in this set.
Definition lset.cpp:481
A singleton reporter that reports to nowhere.
Definition reporter.h:267
void ForEachUniqueLayer(const std::function< void(PCB_LAYER_ID)> &aMethod) const
Runs the given callable for each active unique copper layer in this padstack, meaning F_Cu for MODE::...
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:175
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
Definition padstack.h:184
Class that handles conversion of various pad data fields into strings for display in the UI or serial...
void UpdatePad(PAD &aPad, const wxString &aValue, int aFieldId) const
Update the pad from the given col/string.
PAD_INFO_FORMATTER(UNITS_PROVIDER &aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat, REPORTER &aReporter)
wxString Format(const PAD &aPin, int aFieldId) const
Definition pad.h:61
bool IsAperturePad() const
Definition pad.h:589
void SetAttribute(PAD_ATTRIB aAttribute)
Definition pad.cpp:1563
static wxString ShowPadShape(PAD_SHAPE aShape)
Definition pad.cpp:2524
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition pad.h:579
PAD_ATTRIB GetAttribute() const
Definition pad.h:582
const wxString & GetNumber() const
Definition pad.h:143
void SetShape(PCB_LAYER_ID aLayer, PAD_SHAPE aShape)
Set the new shape of this pad.
Definition pad.h:196
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pad.h:329
VECTOR2I GetPosition() const override
Definition pad.cpp:255
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition pad.h:1023
void SetDirty()
Definition pad.h:571
VECTOR2I GetDrillSize() const
Definition pad.h:320
void SetPadToDieDelay(int aDelay)
Definition pad.h:602
PAD_SHAPE GetShape(PCB_LAYER_ID aLayer) const
Definition pad.h:205
void SetNumber(const wxString &aNumber)
Set the pad number (note that it can be alphanumeric, such as the array reference "AA12").
Definition pad.h:142
VECTOR2I GetSize(PCB_LAYER_ID aLayer) const
Definition pad.cpp:297
int GetPadToDieDelay() const
Definition pad.h:603
void SetPadstack(const PADSTACK &aPadstack)
Definition pad.h:333
void SetPosition(const VECTOR2I &aPos) override
Definition pad.cpp:244
const PADSTACK & Padstack() const
Definition pad.h:331
void SetDrillSize(const VECTOR2I &aSize)
Definition pad.h:319
void SetSize(PCB_LAYER_ID aLayer, const VECTOR2I &aSize)
Definition pad.cpp:264
static LSET SMDMask()
layer set for a SMD pad on Front layer
Definition pad.cpp:627
void SetLayerSet(const LSET &aLayers) override
Definition pad.cpp:1930
void SetPadToDieLength(int aLength)
Definition pad.h:599
int GetPadToDieLength() const
Definition pad.h:600
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the KIGFX::VIEW instance used in the panel.
wxString GetDuplicates() const
Gets a formatted string of all the pins that have duplicate numbers.
void insert(value_type const &v)
Definition pin_numbers.h:58
wxString GetSummary() const
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
A wrapper for reporting to a wxString object.
Definition reporter.h:242
bool MatchTranslationOrNative(const wxString &aStr, const wxString &aNativeLabel, bool aCaseSensitive)
Return true if the given string matches either the translated or native version of the given label.
static void UpdateDrillCells(WX_GRID &aGrid, UNITS_PROVIDER &aUnitsProvider, int aRowId, const PAD &aPad, bool aPreserveValues)
Update the drill size cells in the pad table for a given pad.
static COLS GetColTypeForString(const wxString &aStr)
static wxString GetPadTableColLabel(COLS aCol)
Get the label for a given column in the pin table.
static wxString GetPadTypeString(const PAD &aPad)
static PAD_SHAPE ShapeFromString(const wxString &shape)
static bool DrillsAreEditable(const PAD &aPad)
static wxString ShapeToString(PAD_SHAPE shape)
static void SetPadTypeFromString(PAD &aPad, const wxString &aType)
DIALOG_FP_EDIT_PAD_TABLE::COLS COLS
static COL_ORDER GetColTypeForString(const wxString &aStr)
#define _(s)
static std::map< int, wxString > shapeNames
static const std::string CsvFileExtension
static wxString CsvTsvFileWildcard()
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_Paste
Definition layer_ids.h:100
@ F_Cu
Definition layer_ids.h:60
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:54
@ LAYERS
Layers have changed.
Definition view_item.h:52
@ TARGET_OVERLAY
Items that may change while the view stays the same (noncached)
Definition definitions.h:35
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:675
wxString GetPadAttributeString(PAD_ATTRIB aAttrib)
Get the human-readable string for a pad attribute.
Definition pad_utils.cpp:68
STL namespace.
PAD_ATTRIB
The set of pad shapes, used with PAD::{Set,Get}Attribute().
Definition padstack.h:98
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:104
@ NON_COMPONENT
Pad with no hole that does not connect to a physical component, but rather provides a connection poin...
Definition padstack.h:106
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:100
@ PTH
Plated through hole pad.
Definition padstack.h:99
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
Definition padstack.h:101
PAD_SHAPE
The set of pad shapes, used with PAD::{Set,Get}Shape()
Definition padstack.h:53
@ CHAMFERED_RECT
Definition padstack.h:61
@ ROUNDRECT
Definition padstack.h:58
@ TRAPEZOID
Definition padstack.h:57
@ RECTANGLE
Definition padstack.h:55
#define _HKI(x)
Definition page_info.cpp:40
@ RPT_SEVERITY_WARNING
void AccumulateDescriptions(wxString &aDesc, const T &aItemCollection)
Build a comma-separated list from a collection of wxStrings.
void WriteTableToFileOrClipboard(const wxString &aToFile, const std::vector< std::vector< wxString > > &aTable)
Write aTable as CSV to aToFile (if non-empty) or to the system clipboard.
Definition table_io.cpp:66
std::optional< std::vector< std::vector< wxString > > > ReadTableFromFileOrClipboard(EDA_BASE_FRAME &aFrame, bool aFromFile)
Read a CSV/TSV table from a file selected via a dialog (aFromFile) or from the system clipboard.
Definition table_io.cpp:34
const std::string table
DATABASE_CONNECTION dc(odbcBackend(cs))
IbisParser parser & reporter
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:48
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
Definition of file extensions used in Kicad.