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 (C) 1992-2024 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 <base_units.h>
28#include <footprint.h>
29#include <pcb_edit_frame.h>
30#include <wx/msgdlg.h>
31
32#include <boost/algorithm/string/join.hpp>
33
38{
44 m_OptionsSet( true ),
45 m_GridNx( 5 ),
46 m_GridNy( 5 ),
47 m_GridDx( pcbIUScale.mmToIU( 2.54 ) ),
48 m_GridDy( pcbIUScale.mmToIU( 2.54 ) ),
49 m_GridOffsetX( 0 ),
50 m_GridOffsetY( 0 ),
51 m_GridStagger( 1 ),
52 m_GridStaggerRows( true ),
53 m_GridNumberingAxis( 0 ), // h then v
54 m_GridNumReverseAlt( false ),
55 m_GridNumStartSet( 1 ), // use specified start
56 m_Grid2dArrayNumbering( 0 ), // linear numbering
57 m_GridPrimaryAxisScheme( 0 ), // numeric
58 m_GridSecondaryAxisScheme( 0 ), // numeric
59 m_GridPrimaryNumOffset( wxT( "1" ) ), // numeric
60 m_GridSecondaryNumOffset( wxT( "1" ) ), // numeric
63 m_CircCentreX( 0 ),
64 m_CircCentreY( 0 ),
65 m_CircCount( 4 ),
66 m_CircNumStartSet( 1 ), // use specified start
68 m_CircNumberingOffset( wxT( "1" ) ),
70 m_CircRotatationStep( false ),
71 m_ArrayTypeTab( 0 ), // start on grid view
73 m_FootprintReannotate( true ) // Assign unique by default
74 {
75 }
76
78
97
110};
111
112// Persistent options settings
114
119{
121 wxString m_label;
122};
123
128static const std::vector<NUMBERING_LIST_DATA> numberingTypeData {
129 {
131 _( "Numerals (0,1,2,...,9,10)" ),
132 },
133 {
135 _( "Hexadecimal (0,1,...,F,10,...)" ),
136 },
137 {
139 _( "Alphabet, minus IOSQXZ" ),
140 },
141 {
143 _( "Alphabet, full 26 characters" ),
144 },
145};
146
148 std::unique_ptr<ARRAY_OPTIONS>& aSettings,
149 bool aIsFootprintEditor, const VECTOR2I& aOrigPos ) :
150 DIALOG_CREATE_ARRAY_BASE( aParent ),
151 m_settings( aSettings ),
152 m_originalItemPosition( aOrigPos ),
153 m_isFootprintEditor( aIsFootprintEditor ),
154 m_hSpacing( aParent, m_labelDx, m_entryDx, m_unitLabelDx ),
155 m_vSpacing( aParent, m_labelDy, m_entryDy, m_unitLabelDy ),
156 m_hOffset( aParent, m_labelOffsetX, m_entryOffsetX, m_unitLabelOffsetX ),
157 m_vOffset( aParent, m_labelOffsetY, m_entryOffsetY, m_unitLabelOffsetY ),
158 m_refPosX( aParent, m_stRefPosXTxt, m_tcRefPosX, m_stRefPosXUnit ),
159 m_refPosY( aParent, m_stRefPosYTxt, m_tcRefPosY, m_stRefPosYUnit ),
160 m_hCentre( aParent, m_labelCentreX, m_entryCentreX, m_unitLabelCentreX ),
161 m_vCentre( aParent, m_labelCentreY, m_entryCentreY, m_unitLabelCentreY ),
162 m_circRadius( aParent, m_labelCircRadius, m_tcValueCircRadius, m_unitLabelCircRadius ),
163 m_circCenterAngle( aParent, m_labelCircCenterAngle, m_tcValueCircCenterAngle, m_unitLabelCircCenterAngle ),
164 m_circAngle( aParent, m_labelCircAngle, m_entryCircAngle, m_unitLabelCircAngle ),
165 m_cfg_persister( pcbIUScale, s_arrayOptions.m_OptionsSet )
166{
167 // Configure display origin transforms
174
175 // Set up numbering scheme drop downs character set strings
176 for( const auto& numData : numberingTypeData )
177 {
178 const wxString label = wxGetTranslation( numData.m_label );
179 void* clientData = (void*) &numData;
180
181 m_choicePriAxisNumbering->Append( label, clientData );
182 m_choiceSecAxisNumbering->Append( label, clientData );
183 m_choiceCircNumbering->Append( label, clientData );
184 }
185
186 m_choicePriAxisNumbering->SetSelection( 0 );
187 m_choiceSecAxisNumbering->SetSelection( 0 );
188 m_choiceCircNumbering->SetSelection( 0 );
189
190 m_circCenterAngle.SetUnits( EDA_UNITS::DEGREES );
191 m_circAngle.SetUnits( EDA_UNITS::DEGREES );
192
193 // bind grid options to persister
198
202
204
207
212
217
218 // bind circular options to persister
224
229
231
234
236
237 // Run the callbacks once to process the dialog contents
241
243 Fit();
244 SetMinSize( GetSize() );
245}
246
247
248void DIALOG_CREATE_ARRAY::OnButtonPosition( wxCommandEvent& event )
249{
251}
252
253
254void DIALOG_CREATE_ARRAY::OnButtonRadius( wxCommandEvent& event )
255{
257}
258
259
261{
262 if( m_radioBtnSetByRadius->GetValue() )
263 {
264 m_entryCentreX->Disable();
265 m_entryCentreY->Disable();
266 m_tcValueCircRadius->Enable();
267 m_tcValueCircCenterAngle->Enable();
268 }
269 else
270 {
271 m_entryCentreX->Enable();
272 m_entryCentreY->Enable();
273 m_tcValueCircRadius->Disable();
274 m_tcValueCircCenterAngle->Disable();
275 }
276}
277
278
279void DIALOG_CREATE_ARRAY::OnParameterChanged( wxCommandEvent& event )
280{
282
283 if( m_radioBtnSetByPos->GetValue() )
284 {
287 }
288}
289
290void DIALOG_CREATE_ARRAY::OnRadiusChanged( wxCommandEvent& event )
291{
293
294 if( m_radioBtnSetByRadius->GetValue() )
295 {
298 }
299}
300
301
311static bool validateLongEntry( const wxTextEntry& entry, long& dest, const wxString& description,
312 wxArrayString& errors )
313{
314 bool ok = true;
315
316 if( !entry.GetValue().ToLong( &dest ) )
317 {
318 wxString err;
319 err.Printf( _( "Bad numeric value for %s: %s" ), description, entry.GetValue() );
320 errors.Add( err );
321 ok = false;
322 }
323
324 return ok;
325}
326
327
338static bool validateAxisOptions( const wxTextCtrl& offsetEntry, const wxChoice& typeEntry,
339 const wxTextCtrl& aStepEntry, ARRAY_AXIS& aAxis,
340 wxArrayString& errors )
341{
342 void* clientData = typeEntry.GetClientData( typeEntry.GetSelection() );
343 const NUMBERING_LIST_DATA* numberingData = static_cast<NUMBERING_LIST_DATA*>( clientData );
344
345 wxCHECK_MSG( numberingData, false, wxT( "Failed to get client data from list control." ) );
346
347 aAxis.SetAxisType( numberingData->m_numbering_type );
348
349 const wxString text = offsetEntry.GetValue();
350
351 bool ok = aAxis.SetOffset( text );
352
353 if( !ok )
354 {
355 errors.Add( wxString::Format( _( "Could not determine numbering start from '%s': "
356 "expected value consistent with alphabet '%s'." ),
357 text,
358 aAxis.GetAlphabet() ) );
359 return false;
360 }
361
362 long step;
363 ok = validateLongEntry( aStepEntry, step, _( "step value" ), errors );
364
365 if( ok )
366 aAxis.SetStep( step );
367
368 return ok;
369}
370
371
373{
374 std::unique_ptr<ARRAY_OPTIONS> newSettings;
375
376 wxArrayString errors;
377 const wxWindow* page = m_gridTypeNotebook->GetCurrentPage();
378
379 if( page == m_gridPanel )
380 {
381 auto newGrid = std::make_unique<ARRAY_GRID_OPTIONS>();
382 bool ok = true;
383
384 // ints
385 ok &= validateLongEntry(*m_entryNx, newGrid->m_nx, _("horizontal count"), errors);
386 ok &= validateLongEntry(*m_entryNy, newGrid->m_ny, _("vertical count"), errors);
387
388 newGrid->m_delta.x = m_hSpacing.GetIntValue();
389 newGrid->m_delta.y = m_vSpacing.GetIntValue();
390
391 newGrid->m_offset.x = m_hOffset.GetIntValue();
392 newGrid->m_offset.y = m_vOffset.GetIntValue();
393
394 ok &= validateLongEntry(*m_entryStagger, newGrid->m_stagger, _("stagger"), errors);
395
396 newGrid->m_stagger_rows = m_staggerRows->GetValue();
397
398 newGrid->m_horizontalThenVertical = m_radioBoxGridNumberingAxis->GetSelection() == 0;
399 newGrid->m_reverseNumberingAlternate = m_checkBoxGridReverseNumbering->GetValue();
400
401 newGrid->SetShouldNumber( m_isFootprintEditor );
402
404 {
405 newGrid->SetNumberingStartIsSpecified( m_rbGridStartNumberingOpt->GetSelection() == 1 );
406
407 if( newGrid->GetNumberingStartIsSpecified() )
408 {
409 newGrid->m_2dArrayNumbering = m_radioBoxGridNumberingScheme->GetSelection() != 0;
410
411 // validate from the input fields
415 newGrid->m_pri_axis, errors );
416
417 if( newGrid->m_2dArrayNumbering )
418 {
422 newGrid->m_sec_axis, errors );
423 }
424
425 ok &= numOk;
426 }
427 else
428 {
429 // artificial linear numeric scheme from 1
430 newGrid->m_2dArrayNumbering = false;
431 newGrid->m_pri_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
432 newGrid->m_pri_axis.SetOffset( 1 );
433 }
434 }
435
436 // Only use settings if all values are good
437 if( ok )
438 newSettings = std::move( newGrid );
439 }
440 else if( page == m_circularPanel )
441 {
442 auto newCirc = std::make_unique<ARRAY_CIRCULAR_OPTIONS>();
443 bool ok = true;
445
446 newCirc->m_centre.x = m_hCentre.GetIntValue();
447 newCirc->m_centre.y = m_vCentre.GetIntValue();
448 newCirc->m_angle = EDA_ANGLE( angle, DEGREES_T );
449
450 ok = validateLongEntry(*m_entryCircCount, newCirc->m_nPts, _("point count"), errors);
451
452 newCirc->m_rotateItems = m_entryRotateItemsCb->GetValue();
453 newCirc->SetShouldNumber( m_isFootprintEditor );
454
456 {
457 newCirc->SetNumberingStartIsSpecified( m_rbCircStartNumberingOpt->GetSelection() == 1 );
458
459 if( newCirc->GetNumberingStartIsSpecified() )
460 {
462 *m_entryCircNumberingStep, newCirc->m_axis, errors );
463 }
464 else
465 {
466 // artificial linear numeric scheme from 1
467 newCirc->m_axis.SetAxisType( ARRAY_AXIS::NUMBERING_TYPE::NUMBERING_NUMERIC );
468 newCirc->m_axis.SetOffset( 1 ); // Start at "1"
469 }
470 }
471
472 // Only use settings if all values are good
473 if( ok )
474 newSettings = std::move( newCirc );
475 }
476
477 // If we got good settings, send them out and finish
478 if( newSettings )
479 {
480 // assign pointer and ownership here
481 m_settings = std::move( newSettings );
482
483 m_settings->SetSShouldReannotateFootprints( m_radioBtnUniqueRefs->GetValue() );
484
485 // persist the control state for next time
487
488 return true;
489 }
490 else
491 {
492 wxString errorStr;
493
494 if( errors.IsEmpty() )
495 errorStr = _("Bad parameters");
496 else
497 errorStr = boost::algorithm::join( errors, wxT( "\n" ) );
498
499 wxMessageBox( errorStr );
500 return false;
501 }
502}
503
504
506{
508 {
509 m_footprintReannotatePanel->Show( false );
510
511 m_gridPadNumberingPanel->Show( true );
512 m_circularPadNumberingPanel->Show( true );
513
514 // If we set the start number, we can set the other options,
515 // otherwise it's a hardcoded linear array
516 const bool use_set_start_grid = m_rbGridStartNumberingOpt->GetSelection() == 1;
517
518 m_radioBoxGridNumberingScheme->Enable( use_set_start_grid );
519 m_labelPriAxisNumbering->Enable( use_set_start_grid );
520 m_choicePriAxisNumbering->Enable( use_set_start_grid );
521
522 // Disable the secondary axis numbering option if the
523 // numbering scheme doesn't have two axes
524 const bool num2d = m_radioBoxGridNumberingScheme->GetSelection() != 0;
525
526 m_labelSecAxisNumbering->Enable( use_set_start_grid && num2d );
527 m_choiceSecAxisNumbering->Enable( use_set_start_grid && num2d );
528
529 // We can only set an offset if we're setting the start number
530 m_labelGridNumberingOffset->Enable( use_set_start_grid );
531 m_entryGridPriNumberingOffset->Enable( use_set_start_grid );
532 m_entryGridSecNumberingOffset->Enable( use_set_start_grid && num2d );
533 m_entryGridSecNumberingStep->Enable( use_set_start_grid && num2d );
534
535 // disable the circular number offset in the same way
536 const bool use_set_start_circ = m_rbCircStartNumberingOpt->GetSelection() == 1;
537 m_entryCircNumberingStart->Enable( use_set_start_circ );
538 }
539 else
540 {
541 // grid
542 m_rbGridStartNumberingOpt->Enable( false );
543 m_radioBoxGridNumberingScheme->Enable( false );
544
545 m_labelPriAxisNumbering->Enable( false );
546 m_labelSecAxisNumbering->Enable( false );
547
548 m_choiceSecAxisNumbering->Enable( false );
549 m_choicePriAxisNumbering->Enable( false );
550
551 m_labelGridNumberingOffset->Enable( false );
552 m_entryGridPriNumberingOffset->Enable( false );
553 m_entryGridSecNumberingOffset->Enable( false );
554
555 m_gridPadNumberingPanel->Show( false );
556
557 // circular
558 m_rbCircStartNumberingOpt->Enable( false );
559 m_entryCircNumberingStart->Enable( false );
560
561 m_circularPadNumberingPanel->Show( false );
562
563 m_footprintReannotatePanel->Show( true );
564 }
565}
566
567
569{
570 if( m_radioBtnSetByPos->GetValue() )
571 {
573
574 // Find the radius, etc of the circle
575 centre -= m_originalItemPosition;
576 EDA_ANGLE angle( centre );
577
578 m_circRadius.SetValue( int( centre.EuclideanNorm() ) );
580
583 }
584 else
585 {
588
589 double radius = m_circRadius.GetIntValue();
591
592 m_hCentre.SetValue( m_originalItemPosition.x + radius * angle.Cos() );
593 m_vCentre.SetValue( m_originalItemPosition.y + radius * angle.Sin() );
594 }
595}
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:102
const wxString & GetAlphabet() const
Get the alphabet for the current numbering scheme.
Definition: array_axis.cpp:43
void SetAxisType(NUMBERING_TYPE aType)
Set the axis numbering type.
Definition: array_axis.cpp:96
@ 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
Definition: array_axis.h:46
@ NUMBERING_ALPHA_FULL
Full 26-character alphabet.
Definition: array_axis.h:52
void SetStep(int aStep)
Set the skip between consecutive numbers (useful when doing a partial array, e.g.
Definition: array_axis.cpp:127
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.
void OnButtonRadius(wxCommandEvent &event) override
bool TransferDataFromWindow() override
void OnButtonPosition(wxCommandEvent &event) override
void OnParameterChanged(wxCommandEvent &event) override
void OnRadiusChanged(wxCommandEvent &event) override
const VECTOR2I m_originalItemPosition
std::unique_ptr< ARRAY_OPTIONS > & m_settings
The settings to re-seat on dialog OK.
void SetupStandardButtons(std::map< int, wxString > aLabels={})
double Sin() const
Definition: eda_angle.h:212
EDA_ANGLE Round(int digits) const
Definition: eda_angle.h:318
double Cos() const
Definition: eda_angle.h:227
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
int GetIntValue()
Definition: unit_binder.h:127
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 EDA_ANGLE GetAngleValue()
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:188
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:265
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)
@ 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)
Function DoubleValueFromString converts aTextValue to a double.
Definition: eda_units.cpp:576
Struct containing the last-entered values for the dialog.
CREATE_ARRAY_DIALOG_ENTRIES()
Construct with some sensible defaults.
Local mapping for list-box <-> numbering type.
ARRAY_AXIS::NUMBERING_TYPE m_numbering_type