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.

185 lines
6.5 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-2020 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. #include "array_creator.h"
  25. #include <array_pad_name_provider.h>
  26. #include <board_commit.h>
  27. #include <pad_naming.h>
  28. #include <dialogs/dialog_create_array.h>
  29. /**
  30. * Transform a #BOARD_ITEM from the given #ARRAY_OPTIONS and an index into the array.
  31. *
  32. * @param aArrOpts The array options that describe the array
  33. * @param aIndex The index in the array of this item
  34. * @param aItem The item to transform
  35. */
  36. static void TransformItem( const ARRAY_OPTIONS& aArrOpts, int aIndex, BOARD_ITEM& aItem )
  37. {
  38. const ARRAY_OPTIONS::TRANSFORM transform = aArrOpts.GetTransform( aIndex, aItem.GetPosition() );
  39. aItem.Move( (wxPoint) transform.m_offset );
  40. aItem.Rotate( aItem.GetPosition(), transform.m_rotation * 10 );
  41. }
  42. void ARRAY_CREATOR::Invoke()
  43. {
  44. // bail out if no items
  45. if( m_selection.Size() == 0 )
  46. return;
  47. MODULE* const module = m_editModules ? m_parent.GetBoard()->GetFirstModule() : nullptr;
  48. const bool enableArrayNumbering = m_editModules;
  49. const wxPoint rotPoint = (wxPoint) m_selection.GetCenter();
  50. std::unique_ptr<ARRAY_OPTIONS> array_opts;
  51. DIALOG_CREATE_ARRAY dialog( &m_parent, array_opts, enableArrayNumbering, rotPoint );
  52. int ret = dialog.ShowModal();
  53. if( ret != wxID_OK || array_opts == NULL )
  54. return;
  55. BOARD_COMMIT commit( &m_parent );
  56. ARRAY_PAD_NAME_PROVIDER pad_name_provider( module, *array_opts );
  57. for ( int i = 0; i < m_selection.Size(); ++i )
  58. {
  59. BOARD_ITEM* item = static_cast<BOARD_ITEM*>( m_selection[ i ] );
  60. if( item->Type() == PCB_PAD_T && !m_editModules )
  61. {
  62. // If it is not the module editor, then duplicate the parent module instead
  63. item = static_cast<MODULE*>( item )->GetParent();
  64. }
  65. // The first item in list is the original item. We do not modify it
  66. for( int ptN = 0; ptN < array_opts->GetArraySize(); ptN++ )
  67. {
  68. BOARD_ITEM* this_item;
  69. if( ptN == 0 )
  70. {
  71. // the first point: we don't own this or add it, but
  72. // we might still modify it (position or label)
  73. this_item = item;
  74. }
  75. else
  76. {
  77. // Need to create a new item
  78. BOARD_ITEM* new_item = nullptr;
  79. if( m_editModules )
  80. {
  81. // Don't bother incrementing pads: the module won't update
  82. // until commit, so we can only do this once
  83. new_item = module->DuplicateItem( item );
  84. }
  85. else
  86. {
  87. switch( item->Type() )
  88. {
  89. case PCB_MODULE_T:
  90. case PCB_TEXT_T:
  91. case PCB_LINE_T:
  92. case PCB_TRACE_T:
  93. case PCB_VIA_T:
  94. case PCB_ZONE_AREA_T:
  95. case PCB_TARGET_T:
  96. case PCB_DIMENSION_T:
  97. new_item = item->Duplicate();
  98. break;
  99. default:
  100. // Silently drop other items (such as footprint texts) from duplication
  101. break;
  102. }
  103. // PCB items keep the same numbering
  104. // @TODO: renumber modules if asked. This needs UI to enable.
  105. // something like this, but needs a "block offset" to prevent
  106. // multiple selections overlapping.
  107. // if( new_item->Type() == PCB_MODULE_T )
  108. // static_cast<MODULE&>( *new_item ).IncrementReference( ptN );
  109. // @TODO: we should merge zones. This is a bit tricky, because
  110. // the undo command needs saving old area, if it is merged.
  111. }
  112. this_item = new_item;
  113. if( new_item )
  114. {
  115. // Because aItem is/can be created from a selected item, and inherits from
  116. // it this state, reset the selected stated of aItem:
  117. this_item->ClearSelected();
  118. if( this_item->Type() == PCB_MODULE_T )
  119. {
  120. static_cast<MODULE*>( this_item )->RunOnChildren( [&] ( BOARD_ITEM* aItem )
  121. {
  122. aItem->ClearSelected();
  123. });
  124. }
  125. commit.Add( new_item );
  126. }
  127. }
  128. // always transform the item
  129. if( this_item )
  130. {
  131. commit.Modify( this_item );
  132. TransformItem( *array_opts, ptN, *this_item );
  133. }
  134. // attempt to renumber items if the array parameters define
  135. // a complete numbering scheme to number by (as opposed to
  136. // implicit numbering by incrementing the items during creation
  137. if( this_item && array_opts->ShouldNumberItems() )
  138. {
  139. // Renumber non-aperture pads.
  140. if( this_item->Type() == PCB_PAD_T )
  141. {
  142. auto& pad = static_cast<D_PAD&>( *this_item );
  143. if( PAD_NAMING::PadCanHaveName( pad ) )
  144. {
  145. wxString newName = pad_name_provider.GetNextPadName();
  146. pad.SetName( newName );
  147. }
  148. }
  149. }
  150. }
  151. }
  152. commit.Push( _( "Create an array" ) );
  153. }