You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Created on: 11 Mar 2016, author John Beard
  5. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file array_creator.cpp
  26. */
  27. #include "array_creator.h"
  28. #include <board_commit.h>
  29. #include <dialogs/dialog_create_array.h>
  30. void ARRAY_CREATOR::Invoke()
  31. {
  32. const int numItems = getNumberOfItemsToArray();
  33. // bail out if no items
  34. if( numItems == 0 )
  35. return;
  36. MODULE* const module = getModule();
  37. const bool isModuleEditor = module != NULL;
  38. const bool enableArrayNumbering = isModuleEditor;
  39. const wxPoint rotPoint = getRotationCentre();
  40. DIALOG_CREATE_ARRAY dialog( &m_parent, enableArrayNumbering, rotPoint );
  41. int ret = dialog.ShowModal();
  42. DIALOG_CREATE_ARRAY::ARRAY_OPTIONS* const array_opts = dialog.GetArrayOptions();
  43. if( ret != wxID_OK || array_opts == NULL )
  44. return;
  45. BOARD_COMMIT commit( &m_parent );
  46. for ( int i = 0; i < numItems; ++i )
  47. {
  48. BOARD_ITEM* item = getNthItemToArray( i );
  49. if( item->Type() == PCB_PAD_T && !isModuleEditor )
  50. {
  51. // If it is not the module editor, then duplicate the parent module instead
  52. item = static_cast<MODULE*>( item )->GetParent();
  53. }
  54. // The first item in list is the original item. We do not modify it
  55. for( int ptN = 1; ptN < array_opts->GetArraySize(); ptN++ )
  56. {
  57. BOARD_ITEM* new_item;
  58. if( isModuleEditor )
  59. {
  60. // increment pad numbers if do any renumbering
  61. // (we will number again later according to the numbering scheme if set)
  62. new_item = module->Duplicate( item, array_opts->ShouldNumberItems() );
  63. }
  64. else
  65. {
  66. // PCB items keep the same numbering
  67. new_item = getBoard()->Duplicate( item );
  68. // @TODO: we should merge zones. This is a bit tricky, because
  69. // the undo command needs saving old area, if it is merged.
  70. }
  71. if( new_item )
  72. {
  73. array_opts->TransformItem( ptN, new_item, rotPoint );
  74. prePushAction( new_item );
  75. commit.Add( new_item );
  76. postPushAction( new_item );
  77. }
  78. // attempt to renumber items if the array parameters define
  79. // a complete numbering scheme to number by (as opposed to
  80. // implicit numbering by incrementing the items during creation
  81. if( new_item && array_opts->NumberingStartIsSpecified() )
  82. {
  83. // Renumber pads. Only new pad number renumbering has meaning,
  84. // in the footprint editor.
  85. if( new_item->Type() == PCB_PAD_T )
  86. {
  87. const wxString padName = array_opts->GetItemNumber( ptN );
  88. static_cast<D_PAD*>( new_item )->SetName( padName );
  89. }
  90. }
  91. }
  92. }
  93. commit.Push( _( "Create an array" ) );
  94. finalise();
  95. }