KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_create_array.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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <wx/msgdlg.h>
27
28#include <base_units.h>
29#include <footprint.h>
30#include <pcb_edit_frame.h>
31#include <tools/pcb_actions.h>
33#include <tool/tool_manager.h>
35
36
41{
47
48 bool m_OptionsSet = true;
49
50 long m_GridNx = 5;
51 long m_GridNy = 5;
52 long m_GridDx = pcbIUScale.mmToIU( 2.54 );
53 long m_GridDy = pcbIUScale.mmToIU( 2.54 );
54 long m_GridOffsetX = 0;
55 long m_GridOffsetY = 0;
56 long m_GridStagger = 1;
57 bool m_GridStaggerRows = true;
59 long m_GridNumberingAxis = 0; // h then v
60 bool m_GridNumReverseAlt = false;
61 long m_GridNumStartSet = 1; // use specified start
62 long m_Grid2dArrayNumbering = 0; // linear numbering
63 long m_GridPrimaryAxisScheme = 0; // numeric
64 long m_GridSecondaryAxisScheme = 0; // numeric
65 wxString m_GridPrimaryNumOffset = wxT( "1" ); // numeric
66 wxString m_GridSecondaryNumOffset = wxT( "1" ); // numeric
69
70 long m_CircCentreX = 0;
71 long m_CircCentreY = 0;
73 long m_CircCount = 4;
75 long m_CircNumStartSet = 1; // use specified start
77 wxString m_CircNumberingOffset = wxT("1");
80 long m_ArrayTypeTab = 0; // start on grid view
82 bool m_FootprintReannotate = true; // Assign unique by default
83};
84
85// Persistent options settings
87
92{
94 wxString m_label;
95};
96
101static const std::vector<NUMBERING_LIST_DATA> numberingTypeData {
102 {
104 _( "Numerals (0,1,2,...,9,10)" ),
105 },
106 {
108 _( "Hexadecimal (0,1,...,F,10,...)" ),
109 },
110 {
112 _( "Alphabet, minus IOSQXZ" ),
113 },
114 {
116 _( "Alphabet, full 26 characters" ),
117 },
118};
119
121 std::unique_ptr<ARRAY_OPTIONS>& aSettings,
122 bool aIsFootprintEditor, const VECTOR2I& aOrigPos ) :
123 DIALOG_CREATE_ARRAY_BASE( aParent ),
124 m_frame( aParent ),
125 m_settings( aSettings ),
126 m_originalItemPosition( aOrigPos ), m_isFootprintEditor( aIsFootprintEditor ),
127 m_hSpacing( aParent, m_labelDx, m_entryDx, m_unitLabelDx ),
128 m_vSpacing( aParent, m_labelDy, m_entryDy, m_unitLabelDy ),
129 m_hOffset( aParent, m_labelOffsetX, m_entryOffsetX, m_unitLabelOffsetX ),
130 m_vOffset( aParent, m_labelOffsetY, m_entryOffsetY, m_unitLabelOffsetY ),
131 m_hCentre( aParent, m_labelCentreX, m_entryCentreX, m_unitLabelCentreX ),
132 m_vCentre( aParent, m_labelCentreY, m_entryCentreY, m_unitLabelCentreY ),
133 m_circAngle( aParent, m_labelCircAngle, m_entryCircAngle, m_unitLabelCircAngle ),
134 m_cfg_persister( pcbIUScale, s_arrayOptions.m_OptionsSet )
135{
136 // Configure display origin transforms
143
144 // Set up numbering scheme drop downs character set strings
145 for( const auto& numData : numberingTypeData )
146 {
147 const wxString label = wxGetTranslation( numData.m_label );
148 void* clientData = (void*) &numData;
149
150 m_choicePriAxisNumbering->Append( label, clientData );
151 m_choiceSecAxisNumbering->Append( label, clientData );
152 m_choiceCircNumbering->Append( label, clientData );
153 }
154
155 m_choicePriAxisNumbering->SetSelection( 0 );
156 m_choiceSecAxisNumbering->SetSelection( 0 );
157 m_choiceCircNumbering->SetSelection( 0 );
158
159 m_circAngle.SetUnits( EDA_UNITS::DEGREES );
160
161 // bind grid options to persister
166
170
172
174
177
182
187
188 // bind circular options to persister
191
196
201
203
206
208
209 // Run the callbacks once to process the dialog contents
212
214 Fit();
215 SetMinSize( GetSize() );
216}
217
218
220{
221}
222
223
224void DIALOG_CREATE_ARRAY::OnParameterChanged( wxCommandEvent& event )
225{
226 if( m_checkBoxFullCircle->GetValue() && m_entryCircAngle == event.GetEventObject() )
227 {
228 return;
229 }
230
233}
234
235
236void DIALOG_CREATE_ARRAY::OnSelectCenterButton( wxCommandEvent& event )
237{
238 event.Skip();
239
241 wxCHECK( pickerTool, /* void */ );
242
243 if( event.GetEventObject() == m_btnSelectCenterItem )
244 {
247 PCB_PICKER_TOOL::INTERACTIVE_PARAMS{ this, _( "Select center item..." ) } );
248 }
249 else if( event.GetEventObject() == m_btnSelectCenterPoint )
250 {
253 PCB_PICKER_TOOL::INTERACTIVE_PARAMS{ this, _( "Select center point..." ) } );
254 }
255 else
256 {
257 wxFAIL_MSG( "Unknown event source" );
258 }
259
260 // Hide, but do not close, the dialog
261 Hide();
262}
263
264
265void DIALOG_CREATE_ARRAY::OnAxisNumberingChange( wxCommandEvent& aEvent )
266{
267 // On an alphabet change, make sure the offset control is valid by default.
268
269 const unsigned newAlphabet = aEvent.GetSelection();
270
271 wxCHECK( newAlphabet >= 0 && newAlphabet < numberingTypeData.size(), /* void */ );
272
273 const ARRAY_AXIS::NUMBERING_TYPE numberingType =
274 numberingTypeData[newAlphabet].m_numbering_type;
275
276 wxTextCtrl* matchingTextCtrl = nullptr;
277
278 if( aEvent.GetEventObject() == m_choicePriAxisNumbering )
279 matchingTextCtrl = m_entryGridPriNumberingOffset;
280 else if( aEvent.GetEventObject() == m_choiceSecAxisNumbering )
281 matchingTextCtrl = m_entryGridSecNumberingOffset;
282 else if( aEvent.GetEventObject() == m_choiceCircNumbering )
283 matchingTextCtrl = m_entryCircNumberingStart;
284
285 wxCHECK( matchingTextCtrl, /* void */ );
286
287 ARRAY_AXIS dummyAxis;
288 dummyAxis.SetAxisType( numberingType );
289
290 // If the text control has a valid value for the new alphabet, keep it
291 // else reset to the first value in the new alphabet.
292
293 const bool isAlreadyOK = dummyAxis.SetOffset( matchingTextCtrl->GetValue() );
294
295 if( !isAlreadyOK )
296 {
297 dummyAxis.SetOffset( ARRAY_AXIS::TypeIsNumeric( numberingType ) ? 1 : 0 );
298 matchingTextCtrl->SetValue( dummyAxis.GetItemNumber( 0 ) );
299 }
300}
301
302
303// Implement the RECEIVER interface for the callback from the TOOL
305{
306 if( aItem )
307 {
308 m_hCentre.SetValue( aItem->GetPosition().x );
309 m_vCentre.SetValue( aItem->GetPosition().y );
310 }
311
312 Show( true );
313}
314
315
316void DIALOG_CREATE_ARRAY::UpdatePickedPoint( const std::optional<VECTOR2I>& aPoint )
317{
318 if( aPoint )
319 {
320 m_hCentre.SetValue( aPoint->x );
321 m_vCentre.SetValue( aPoint->y );
322 }
323
324 Show( true );
325}
326
327
337static bool validateLongEntry( const wxTextEntry& entry, long& dest, const wxString& description,
338 wxArrayString& errors )
339{
340 bool ok = true;
341
342 if( !entry.GetValue().ToLong( &dest ) )
343 {
344 wxString err;
345 err.Printf( _( "Bad numeric value for %s: %s" ), description, entry.GetValue() );
346 errors.Add( err );
347 ok = false;
348 }
349
350 return ok;
351}
352
353
364static bool validateAxisOptions( const wxTextCtrl& offsetEntry, const wxChoice& typeEntry,
365 const wxTextCtrl& aStepEntry, ARRAY_AXIS& aAxis,
366 wxArrayString& errors )
367{
368 void* clientData = typeEntry.GetClientData( typeEntry.GetSelection() );
369 const NUMBERING_LIST_DATA* numberingData = static_cast<NUMBERING_LIST_DATA*>( clientData );
370
371 wxCHECK_MSG( numberingData, false, wxT( "Failed to get client data from list control." ) );
372
373 aAxis.SetAxisType( numberingData->m_numbering_type );
374
375 const wxString text = offsetEntry.GetValue();
376
377 bool ok = aAxis.SetOffset( text );
378
379 if( !ok )
380 {
381 errors.Add( wxString::Format( _( "Could not determine numbering start from '%s': "
382 "expected value consistent with alphabet '%s'." ),
383 text,
384 aAxis.GetAlphabet() ) );
385 return false;
386 }
387
388 long step;
389 ok = validateLongEntry( aStepEntry, step, _( "step value" ), errors );
390
391 if( ok )
392 aAxis.SetStep( step );
393
394 return ok;
395}
396
397
399{
400 std::unique_ptr<ARRAY_OPTIONS> newSettings;
401
402 wxArrayString errors;
403 const wxWindow* page = m_gridTypeNotebook->GetCurrentPage();
404
405 if( page == m_gridPanel )
406 {
407 auto newGrid = std::make_unique<ARRAY_GRID_OPTIONS>();
408 bool ok = true;
409
410 // ints
411 ok &= validateLongEntry(*m_entryNx, newGrid->m_nx, _("horizontal count"), errors);
412 ok &= validateLongEntry(*m_entryNy, newGrid->m_ny, _("vertical count"), errors);
413
414 newGrid->m_delta.x = m_hSpacing.GetIntValue();
415 newGrid->m_delta.y = m_vSpacing.GetIntValue();
416
417 newGrid->m_offset.x = m_hOffset.GetIntValue();
418 newGrid->m_offset.y = m_vOffset.GetIntValue();
419
420 newGrid->m_centred = m_rbCentreOnSource->GetValue();
421
422 ok &= validateLongEntry(*m_entryStagger, newGrid->m_stagger, _("stagger"), errors);
423
424 newGrid->m_stagger_rows = m_staggerRows->GetValue();
425
426 newGrid->m_horizontalThenVertical = m_radioBoxGridNumberingAxis->GetSelection() == 0;
427 newGrid->m_reverseNumberingAlternate = m_checkBoxGridReverseNumbering->GetValue();
428
429 newGrid->SetShouldNumber( m_isFootprintEditor );
430
432 {
433 newGrid->SetNumberingStartIsSpecified( m_rbGridStartNumberingOpt->GetSelection() == 1 );
434
435 if( newGrid->GetNumberingStartIsSpecified() )
436 {
437 newGrid->m_2dArrayNumbering = m_radioBoxGridNumberingScheme->GetSelection() != 0;
438
439 // validate from the input fields
443 newGrid->m_pri_axis, errors );
444
445 if( newGrid->m_2dArrayNumbering )
446 {
450 newGrid->m_sec_axis, errors );
451 }
452
453 ok &= numOk;
454 }
455 else
456 {
457 // artificial linear numeric scheme from 1
458 newGrid->m_2dArrayNumbering = false;
459 newGrid->m_pri_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
460 newGrid->m_pri_axis.SetOffset( 1 );
461 }
462 }
463
464 // Only use settings if all values are good
465 if( ok )
466 newSettings = std::move( newGrid );
467 }
468 else if( page == m_circularPanel )
469 {
470 auto newCirc = std::make_unique<ARRAY_CIRCULAR_OPTIONS>();
471 bool ok = true;
473
474 newCirc->m_centre.x = m_hCentre.GetIntValue();
475 newCirc->m_centre.y = m_vCentre.GetIntValue();
476 newCirc->m_angle = EDA_ANGLE( angle, DEGREES_T );
477
478 ok = validateLongEntry(*m_entryCircCount, newCirc->m_nPts, _("point count"), errors);
479
480 newCirc->m_rotateItems = m_entryRotateItemsCb->GetValue();
481 newCirc->SetShouldNumber( m_isFootprintEditor );
482
484 {
485 newCirc->SetNumberingStartIsSpecified( m_rbCircStartNumberingOpt->GetSelection() == 1 );
486
487 if( newCirc->GetNumberingStartIsSpecified() )
488 {
490 *m_entryCircNumberingStep, newCirc->m_axis, errors );
491 }
492 else
493 {
494 // artificial linear numeric scheme from 1
495 newCirc->m_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
496 newCirc->m_axis.SetOffset( 1 ); // Start at "1"
497 }
498 }
499
500 // Only use settings if all values are good
501 if( ok )
502 newSettings = std::move( newCirc );
503 }
504
505 bool ret = false;
506
507 // If we got good settings, send them out and finish
508 if( newSettings )
509 {
510 // assign pointer and ownership here
511 m_settings = std::move( newSettings );
512
513 m_settings->SetSShouldReannotateFootprints( m_radioBtnUniqueRefs->GetValue() );
514
515 // persist the control state for next time
517
518 ret = true;
519 }
520 else
521 {
522 wxString errorStr;
523
524 if( errors.IsEmpty() )
525 errorStr = _("Bad parameters");
526 else
527 errorStr = wxJoin( errors, '\n' );
528
529 wxMessageBox( errorStr );
530 ret = false;
531 }
532
533 // This dialog is not modal, so close it now if successful
534 if( ret )
535 Close();
536
537 return ret;
538}
539
540
542{
543 if( m_checkBoxFullCircle->GetValue() )
544 {
545 m_entryCircAngle->Disable();
546 }
547 else
548 {
549 m_entryCircAngle->Enable();
550 }
551
553 {
554 m_footprintReannotatePanel->Show( false );
555
556 m_gridPadNumberingPanel->Show( true );
557 m_circularPadNumberingPanel->Show( true );
558
559 // If we set the start number, we can set the other options,
560 // otherwise it's a hardcoded linear array
561 const bool use_set_start_grid = m_rbGridStartNumberingOpt->GetSelection() == 1;
562
563 m_radioBoxGridNumberingScheme->Enable( use_set_start_grid );
564 m_labelPriAxisNumbering->Enable( use_set_start_grid );
565 m_choicePriAxisNumbering->Enable( use_set_start_grid );
566
567 // Disable the secondary axis numbering option if the
568 // numbering scheme doesn't have two axes
569 const bool num2d = m_radioBoxGridNumberingScheme->GetSelection() != 0;
570
571 m_labelSecAxisNumbering->Enable( use_set_start_grid && num2d );
572 m_choiceSecAxisNumbering->Enable( use_set_start_grid && num2d );
573
574 // We can only set an offset if we're setting the start number
575 m_labelGridNumberingOffset->Enable( use_set_start_grid );
576 m_entryGridPriNumberingOffset->Enable( use_set_start_grid );
577 m_entryGridSecNumberingOffset->Enable( use_set_start_grid && num2d );
578 m_entryGridSecNumberingStep->Enable( use_set_start_grid && num2d );
579
580 // disable the circular number offset in the same way
581 const bool use_set_start_circ = m_rbCircStartNumberingOpt->GetSelection() == 1;
582 m_entryCircNumberingStart->Enable( use_set_start_circ );
583 }
584 else
585 {
586 // grid
587 m_rbGridStartNumberingOpt->Enable( false );
588 m_radioBoxGridNumberingScheme->Enable( false );
589
590 m_labelPriAxisNumbering->Enable( false );
591 m_labelSecAxisNumbering->Enable( false );
592
593 m_choiceSecAxisNumbering->Enable( false );
594 m_choicePriAxisNumbering->Enable( false );
595
596 m_labelGridNumberingOffset->Enable( false );
597 m_entryGridPriNumberingOffset->Enable( false );
598 m_entryGridSecNumberingOffset->Enable( false );
599
600 m_gridPadNumberingPanel->Show( false );
601
602 // circular
603 m_rbCircStartNumberingOpt->Enable( false );
604 m_entryCircNumberingStart->Enable( false );
605
606 m_circularPadNumberingPanel->Show( false );
607
608 m_footprintReannotatePanel->Show( true );
609 }
610}
611
612
614{
615 // In full circle mode, the division angle is computed from the number of points
616 if( m_checkBoxFullCircle->GetValue() )
617 {
618 long nPts;
619 if( m_entryCircCount->GetValue().ToLong( &nPts ) )
620 {
621 EDA_ANGLE division = EDA_ANGLE( 360, DEGREES_T ) / nPts;
622 m_circAngle.SetAngleValue( division );
623 }
624 }
625}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
Class that contains information about a single array axis and the numbering of items along that axis.
Definition: array_axis.h:40
bool SetOffset(const wxString &aOffsetName)
Set the axis start (as a string, which should decode to a valid index in the alphabet),...
Definition: array_axis.cpp:104
wxString GetItemNumber(int n) const
Get the position number (name) for the n'th axis point.
Definition: array_axis.cpp:135
static bool TypeIsNumeric(NUMBERING_TYPE type)
Check if a numbering type is a numeric type.
Definition: array_axis.h:60
const wxString & GetAlphabet() const
Get the alphabet for the current numbering scheme.
Definition: array_axis.cpp:45
void SetAxisType(NUMBERING_TYPE aType)
Set the axis numbering type.
Definition: array_axis.cpp:98
@ NUMBERING_NUMERIC
Arabic numerals: 0,1,2,3,4,5,6,7,8,9,10,11...
Definition: array_axis.h:44
@ NUMBERING_HEX
Definition: array_axis.h:45
@ NUMBERING_ALPHA_NO_IOSQXZ
Alphabet, excluding IOSQXZ.
Definition: array_axis.h:53
@ NUMBERING_ALPHA_FULL
Full 26-character alphabet.
Definition: array_axis.h:54
void SetStep(int aStep)
Set the skip between consecutive numbers (useful when doing a partial array, e.g.
Definition: array_axis.cpp:129
Class DIALOG_CREATE_ARRAY_BASE.
WIDGET_SAVE_RESTORE m_cfg_persister
DIALOG_CREATE_ARRAY(PCB_BASE_FRAME *aParent, std::unique_ptr< ARRAY_OPTIONS > &aOptions, bool enableNumbering, const VECTOR2I &aOrigPos)
Construct a new dialog.
bool TransferDataFromWindow() override
PCB_BASE_FRAME * m_frame
void OnSelectCenterButton(wxCommandEvent &aEvent) override
std::unique_ptr< ARRAY_OPTIONS > & m_settings
The settings to re-seat on dialog OK.
void OnParameterChanged(wxCommandEvent &aEvent) override
void UpdatePickedItem(const EDA_ITEM *aItem) override
void UpdatePickedPoint(const std::optional< VECTOR2I > &aPoint) override
void OnAxisNumberingChange(wxCommandEvent &aEvent) override
bool Show(bool show) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:244
static TOOL_ACTION selectPointInteractively
Definition: pcb_actions.h:332
static TOOL_ACTION selectItemInteractively
Selection of reference points/items.
Definition: pcb_actions.h:331
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
Generic tool for picking an item.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
int GetIntValue()
Definition: unit_binder.h:129
virtual void SetUnits(EDA_UNITS aUnits)
Normally not needed (as the UNIT_BINDER inherits from the parent frame), but can be used to set to DE...
virtual void SetAngleValue(const EDA_ANGLE &aValue)
virtual void SetValue(long long int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
void SetCoordType(ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType)
Set the current origin transform mode.
Definition: unit_binder.h:200
void Add(wxRadioBox &ctrl, long &dest)
Bind a radiobox to a choice.
void RestoreConfigToControls()
Restore the values from the internally-stored references to the underlying data to each bound control...
void ReadConfigFromControls()
Read values of all bound controls into the internally-stored references to the underlying data.
static const std::vector< NUMBERING_LIST_DATA > numberingTypeData
List of type <--> name mappings (in order) for the numbering type list boxes.
static bool validateAxisOptions(const wxTextCtrl &offsetEntry, const wxChoice &typeEntry, const wxTextCtrl &aStepEntry, ARRAY_AXIS &aAxis, wxArrayString &errors)
Validates and saves (if valid) the type and offset of an array axis numbering.
static bool validateLongEntry(const wxTextEntry &entry, long &dest, const wxString &description, wxArrayString &errors)
Validate and save a long integer entry.
static CREATE_ARRAY_DIALOG_ENTRIES s_arrayOptions
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
@ DEGREES_T
Definition: eda_angle.h:31
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue to a double.
Definition: eda_units.cpp:553
Struct containing the last-entered values for the dialog.
CREATE_ARRAY_DIALOG_ENTRIES()
Construct with some sensible defaults.
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
Local mapping for list-box <-> numbering type.
ARRAY_AXIS::NUMBERING_TYPE m_numbering_type