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 int newAlphabet = aEvent.GetSelection();
270
271 wxCHECK( newAlphabet >= 0 && newAlphabet < static_cast<int>( numberingTypeData.size() ),
272 /* void */ );
273
274 const ARRAY_AXIS::NUMBERING_TYPE numberingType =
275 numberingTypeData[newAlphabet].m_numbering_type;
276
277 wxTextCtrl* matchingTextCtrl = nullptr;
278
279 if( aEvent.GetEventObject() == m_choicePriAxisNumbering )
280 matchingTextCtrl = m_entryGridPriNumberingOffset;
281 else if( aEvent.GetEventObject() == m_choiceSecAxisNumbering )
282 matchingTextCtrl = m_entryGridSecNumberingOffset;
283 else if( aEvent.GetEventObject() == m_choiceCircNumbering )
284 matchingTextCtrl = m_entryCircNumberingStart;
285
286 wxCHECK( matchingTextCtrl, /* void */ );
287
288 ARRAY_AXIS dummyAxis;
289 dummyAxis.SetAxisType( numberingType );
290
291 // If the text control has a valid value for the new alphabet, keep it
292 // else reset to the first value in the new alphabet.
293
294 const bool isAlreadyOK = dummyAxis.SetOffset( matchingTextCtrl->GetValue() );
295
296 if( !isAlreadyOK )
297 {
298 dummyAxis.SetOffset( ARRAY_AXIS::TypeIsNumeric( numberingType ) ? 1 : 0 );
299 matchingTextCtrl->SetValue( dummyAxis.GetItemNumber( 0 ) );
300 }
301}
302
303
304// Implement the RECEIVER interface for the callback from the TOOL
306{
307 if( aItem )
308 {
309 m_hCentre.SetValue( aItem->GetPosition().x );
310 m_vCentre.SetValue( aItem->GetPosition().y );
311 }
312
313 Show( true );
314}
315
316
317void DIALOG_CREATE_ARRAY::UpdatePickedPoint( const std::optional<VECTOR2I>& aPoint )
318{
319 if( aPoint )
320 {
321 m_hCentre.SetValue( aPoint->x );
322 m_vCentre.SetValue( aPoint->y );
323 }
324
325 Show( true );
326}
327
328
338static bool validateLongEntry( const wxTextEntry& entry, long& dest, const wxString& description,
339 wxArrayString& errors )
340{
341 bool ok = true;
342
343 if( !entry.GetValue().ToLong( &dest ) )
344 {
345 wxString err;
346 err.Printf( _( "Bad numeric value for %s: %s" ), description, entry.GetValue() );
347 errors.Add( err );
348 ok = false;
349 }
350
351 return ok;
352}
353
354
365static bool validateAxisOptions( const wxTextCtrl& offsetEntry, const wxChoice& typeEntry,
366 const wxTextCtrl& aStepEntry, ARRAY_AXIS& aAxis,
367 wxArrayString& errors )
368{
369 void* clientData = typeEntry.GetClientData( typeEntry.GetSelection() );
370 const NUMBERING_LIST_DATA* numberingData = static_cast<NUMBERING_LIST_DATA*>( clientData );
371
372 wxCHECK_MSG( numberingData, false, wxT( "Failed to get client data from list control." ) );
373
374 aAxis.SetAxisType( numberingData->m_numbering_type );
375
376 const wxString text = offsetEntry.GetValue();
377
378 bool ok = aAxis.SetOffset( text );
379
380 if( !ok )
381 {
382 errors.Add( wxString::Format( _( "Could not determine numbering start from '%s': "
383 "expected value consistent with alphabet '%s'." ),
384 text,
385 aAxis.GetAlphabet() ) );
386 return false;
387 }
388
389 long step;
390 ok = validateLongEntry( aStepEntry, step, _( "step value" ), errors );
391
392 if( ok )
393 aAxis.SetStep( step );
394
395 return ok;
396}
397
398
400{
401 std::unique_ptr<ARRAY_OPTIONS> newSettings;
402
403 wxArrayString errors;
404 const wxWindow* page = m_gridTypeNotebook->GetCurrentPage();
405
406 if( page == m_gridPanel )
407 {
408 auto newGrid = std::make_unique<ARRAY_GRID_OPTIONS>();
409 bool ok = true;
410
411 // ints
412 ok &= validateLongEntry(*m_entryNx, newGrid->m_nx, _("horizontal count"), errors);
413 ok &= validateLongEntry(*m_entryNy, newGrid->m_ny, _("vertical count"), errors);
414
415 newGrid->m_delta.x = m_hSpacing.GetIntValue();
416 newGrid->m_delta.y = m_vSpacing.GetIntValue();
417
418 newGrid->m_offset.x = m_hOffset.GetIntValue();
419 newGrid->m_offset.y = m_vOffset.GetIntValue();
420
421 newGrid->m_centred = m_rbCentreOnSource->GetValue();
422
423 ok &= validateLongEntry(*m_entryStagger, newGrid->m_stagger, _("stagger"), errors);
424
425 newGrid->m_stagger_rows = m_staggerRows->GetValue();
426
427 newGrid->m_horizontalThenVertical = m_radioBoxGridNumberingAxis->GetSelection() == 0;
428 newGrid->m_reverseNumberingAlternate = m_checkBoxGridReverseNumbering->GetValue();
429
430 newGrid->SetShouldNumber( m_isFootprintEditor );
431
433 {
434 newGrid->SetNumberingStartIsSpecified( m_rbGridStartNumberingOpt->GetSelection() == 1 );
435
436 if( newGrid->GetNumberingStartIsSpecified() )
437 {
438 newGrid->m_2dArrayNumbering = m_radioBoxGridNumberingScheme->GetSelection() != 0;
439
440 // validate from the input fields
444 newGrid->m_pri_axis, errors );
445
446 if( newGrid->m_2dArrayNumbering )
447 {
451 newGrid->m_sec_axis, errors );
452 }
453
454 ok &= numOk;
455 }
456 else
457 {
458 // artificial linear numeric scheme from 1
459 newGrid->m_2dArrayNumbering = false;
460 newGrid->m_pri_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
461 newGrid->m_pri_axis.SetOffset( 1 );
462 }
463 }
464
465 // Only use settings if all values are good
466 if( ok )
467 newSettings = std::move( newGrid );
468 }
469 else if( page == m_circularPanel )
470 {
471 auto newCirc = std::make_unique<ARRAY_CIRCULAR_OPTIONS>();
472 bool ok = true;
474
475 newCirc->m_centre.x = m_hCentre.GetIntValue();
476 newCirc->m_centre.y = m_vCentre.GetIntValue();
477 newCirc->m_angle = EDA_ANGLE( angle, DEGREES_T );
478
479 ok = validateLongEntry(*m_entryCircCount, newCirc->m_nPts, _("point count"), errors);
480
481 newCirc->m_rotateItems = m_entryRotateItemsCb->GetValue();
482 newCirc->SetShouldNumber( m_isFootprintEditor );
483
485 {
486 newCirc->SetNumberingStartIsSpecified( m_rbCircStartNumberingOpt->GetSelection() == 1 );
487
488 if( newCirc->GetNumberingStartIsSpecified() )
489 {
491 *m_entryCircNumberingStep, newCirc->m_axis, errors );
492 }
493 else
494 {
495 // artificial linear numeric scheme from 1
496 newCirc->m_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
497 newCirc->m_axis.SetOffset( 1 ); // Start at "1"
498 }
499 }
500
501 // Only use settings if all values are good
502 if( ok )
503 newSettings = std::move( newCirc );
504 }
505
506 bool ret = false;
507
508 // If we got good settings, send them out and finish
509 if( newSettings )
510 {
511 // assign pointer and ownership here
512 m_settings = std::move( newSettings );
513
514 m_settings->SetSShouldReannotateFootprints( m_radioBtnUniqueRefs->GetValue() );
515
516 // persist the control state for next time
518
519 ret = true;
520 }
521 else
522 {
523 wxString errorStr;
524
525 if( errors.IsEmpty() )
526 errorStr = _("Bad parameters");
527 else
528 errorStr = wxJoin( errors, '\n' );
529
530 wxMessageBox( errorStr );
531 ret = false;
532 }
533
534 // This dialog is not modal, so close it now if successful
535 if( ret )
536 Close();
537
538 return ret;
539}
540
541
543{
544 if( m_checkBoxFullCircle->GetValue() )
545 {
546 m_entryCircAngle->Disable();
547 }
548 else
549 {
550 m_entryCircAngle->Enable();
551 }
552
554 {
555 m_footprintReannotatePanel->Show( false );
556
557 m_gridPadNumberingPanel->Show( true );
558 m_circularPadNumberingPanel->Show( true );
559
560 // If we set the start number, we can set the other options,
561 // otherwise it's a hardcoded linear array
562 const bool use_set_start_grid = m_rbGridStartNumberingOpt->GetSelection() == 1;
563
564 m_radioBoxGridNumberingScheme->Enable( use_set_start_grid );
565 m_labelPriAxisNumbering->Enable( use_set_start_grid );
566 m_choicePriAxisNumbering->Enable( use_set_start_grid );
567
568 // Disable the secondary axis numbering option if the
569 // numbering scheme doesn't have two axes
570 const bool num2d = m_radioBoxGridNumberingScheme->GetSelection() != 0;
571
572 m_labelSecAxisNumbering->Enable( use_set_start_grid && num2d );
573 m_choiceSecAxisNumbering->Enable( use_set_start_grid && num2d );
574
575 // We can only set an offset if we're setting the start number
576 m_labelGridNumberingOffset->Enable( use_set_start_grid );
577 m_entryGridPriNumberingOffset->Enable( use_set_start_grid );
578 m_entryGridSecNumberingOffset->Enable( use_set_start_grid && num2d );
579 m_entryGridSecNumberingStep->Enable( use_set_start_grid && num2d );
580
581 // disable the circular number offset in the same way
582 const bool use_set_start_circ = m_rbCircStartNumberingOpt->GetSelection() == 1;
583 m_entryCircNumberingStart->Enable( use_set_start_circ );
584 }
585 else
586 {
587 // grid
588 m_rbGridStartNumberingOpt->Enable( false );
589 m_radioBoxGridNumberingScheme->Enable( false );
590
591 m_labelPriAxisNumbering->Enable( false );
592 m_labelSecAxisNumbering->Enable( false );
593
594 m_choiceSecAxisNumbering->Enable( false );
595 m_choicePriAxisNumbering->Enable( false );
596
597 m_labelGridNumberingOffset->Enable( false );
598 m_entryGridPriNumberingOffset->Enable( false );
599 m_entryGridSecNumberingOffset->Enable( false );
600
601 m_gridPadNumberingPanel->Show( false );
602
603 // circular
604 m_rbCircStartNumberingOpt->Enable( false );
605 m_entryCircNumberingStart->Enable( false );
606
607 m_circularPadNumberingPanel->Show( false );
608
609 m_footprintReannotatePanel->Show( true );
610 }
611}
612
613
615{
616 // In full circle mode, the division angle is computed from the number of points
617 if( m_checkBoxFullCircle->GetValue() )
618 {
619 long nPts;
620 if( m_entryCircCount->GetValue().ToLong( &nPts ) )
621 {
622 EDA_ANGLE division = EDA_ANGLE( 360, DEGREES_T ) / nPts;
623 m_circAngle.SetAngleValue( division );
624 }
625 }
626}
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:242
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