KiCad PCB EDA Suite
array_creator.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  * 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 
25 #include "array_creator.h"
26 
28 #include <board_commit.h>
29 #include <pad_naming.h>
30 
32 
40 static void TransformItem( const ARRAY_OPTIONS& aArrOpts, int aIndex, BOARD_ITEM& aItem )
41 {
42  const ARRAY_OPTIONS::TRANSFORM transform = aArrOpts.GetTransform( aIndex, aItem.GetPosition() );
43 
44  aItem.Move( (wxPoint) transform.m_offset );
45  aItem.Rotate( aItem.GetPosition(), transform.m_rotation * 10 );
46 }
47 
48 
50 {
51  // bail out if no items
52  if( m_selection.Size() == 0 )
53  return;
54 
55  MODULE* const module = m_editModules ? m_parent.GetBoard()->GetFirstModule() : nullptr;
56 
57  const bool enableArrayNumbering = m_editModules;
58  const wxPoint rotPoint = (wxPoint) m_selection.GetCenter();
59 
60  std::unique_ptr<ARRAY_OPTIONS> array_opts;
61 
62  DIALOG_CREATE_ARRAY dialog( &m_parent, array_opts, enableArrayNumbering, rotPoint );
63 
64  int ret = dialog.ShowModal();
65 
66  if( ret != wxID_OK || array_opts == NULL )
67  return;
68 
69  BOARD_COMMIT commit( &m_parent );
70 
71  ARRAY_PAD_NAME_PROVIDER pad_name_provider( module, *array_opts );
72 
73  for ( int i = 0; i < m_selection.Size(); ++i )
74  {
75  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( m_selection[ i ] );
76 
77  if( item->Type() == PCB_PAD_T && !m_editModules )
78  {
79  // If it is not the module editor, then duplicate the parent module instead
80  item = static_cast<MODULE*>( item )->GetParent();
81  }
82 
83  // The first item in list is the original item. We do not modify it
84  for( int ptN = 0; ptN < array_opts->GetArraySize(); ptN++ )
85  {
86  BOARD_ITEM* this_item;
87 
88  if( ptN == 0 )
89  {
90  // the first point: we don't own this or add it, but
91  // we might still modify it (position or label)
92  this_item = item;
93  }
94  else
95  {
96  // Need to create a new item
97  BOARD_ITEM* new_item = nullptr;
98 
99  if( m_editModules )
100  {
101  // Don't bother incrementing pads: the module won't update
102  // until commit, so we can only do this once
103  new_item = module->DuplicateItem( item );
104  }
105  else
106  {
107  switch( item->Type() )
108  {
109  case PCB_MODULE_T:
110  case PCB_TEXT_T:
111  case PCB_SHAPE_T:
112  case PCB_TRACE_T:
113  case PCB_VIA_T:
114  case PCB_ZONE_AREA_T:
115  case PCB_TARGET_T:
116  case PCB_DIM_ALIGNED_T:
117  case PCB_DIM_CENTER_T:
119  case PCB_DIM_LEADER_T:
120  new_item = item->Duplicate();
121  break;
122 
123  default:
124  // Silently drop other items (such as footprint texts) from duplication
125  break;
126  }
127 
128  // PCB items keep the same numbering
129 
130  // @TODO: renumber footprints if asked. This needs UI to enable.
131  // something like this, but needs a "block offset" to prevent
132  // multiple selections overlapping.
133  // if( new_item->Type() == PCB_MODULE_T )
134  // static_cast<MODULE&>( *new_item ).IncrementReference( ptN );
135 
136  // @TODO: we should merge zones. This is a bit tricky, because
137  // the undo command needs saving old area, if it is merged.
138  }
139 
140  this_item = new_item;
141 
142  if( new_item )
143  {
144  // Because aItem is/can be created from a selected item, and inherits from
145  // it this state, reset the selected stated of aItem:
146  this_item->ClearSelected();
147 
148  if( this_item->Type() == PCB_MODULE_T )
149  {
150  static_cast<MODULE*>( this_item )->RunOnChildren(
151  [&]( BOARD_ITEM* aItem )
152  {
153  aItem->ClearSelected();
154  });
155  }
156 
157  commit.Add( new_item );
158  }
159  }
160 
161  // always transform the item
162  if( this_item )
163  {
164  commit.Modify( this_item );
165  TransformItem( *array_opts, ptN, *this_item );
166  }
167 
168  // attempt to renumber items if the array parameters define
169  // a complete numbering scheme to number by (as opposed to
170  // implicit numbering by incrementing the items during creation
171  if( this_item && array_opts->ShouldNumberItems() )
172  {
173  // Renumber non-aperture pads.
174  if( this_item->Type() == PCB_PAD_T )
175  {
176  auto& pad = static_cast<D_PAD&>( *this_item );
177 
178  if( PAD_NAMING::PadCanHaveName( pad ) )
179  {
180  wxString newName = pad_name_provider.GetNextPadName();
181  pad.SetName( newName );
182  }
183  }
184  }
185  }
186  }
187 
188  commit.Push( _( "Create an array" ) );
189 }
Transform applied to an object by this array.
Definition: array_options.h:55
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
class ALIGNED_DIMENSION, a linear dimension (graphic item)
Definition: typeinfo.h:101
class LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
void ClearSelected()
Definition: eda_item.h:200
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
class CENTER_DIMENSION, a center point marking (graphic item)
Definition: typeinfo.h:103
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.h:137
PCB_BASE_FRAME & m_parent
Definition: array_creator.h:56
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:106
bool PadCanHaveName(const D_PAD &aPad)
Check if a pad should be named.
Definition: pad_naming.cpp:26
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
virtual wxPoint GetPosition() const
Definition: eda_item.h:326
wxString GetNextPadName()
Get the next available pad name.
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
Simple class that sequentially provides names from an ARRAY_OPTIONS object, making sure that they do ...
Options that govern the setup of an "array" of multiple item.
Definition: array_options.h:36
class MODULE, a footprint
Definition: typeinfo.h:89
#define NULL
virtual void Move(const wxPoint &aMoveVector)
Function Move move this object.
MODULE * GetFirstModule() const
Gets the first module in the list (used in footprint viewer/editor) or NULL if none.
Definition: class_board.h:347
virtual void Rotate(const wxPoint &aRotCentre, double aAngle)
Function Rotate Rotate this object.
BOARD_ITEM * DuplicateItem(const BOARD_ITEM *aItem, bool aAddToModule=false)
Function DuplicateItem Duplicate a given item within the module, optionally adding it to the board.
static void TransformItem(const ARRAY_OPTIONS &aArrOpts, int aIndex, BOARD_ITEM &aItem)
Transform a BOARD_ITEM from the given ARRAY_OPTIONS and an index into the array.
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:105
virtual TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const =0
Get the transform of the n-th point in the array.
#define _(s)
Definition: 3d_actions.cpp:33
int Size() const
Returns the number of selected parts.
Definition: selection.h:126
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
virtual BOARD_ITEM * Duplicate() const
Function Duplicate creates a copy of a BOARD_ITEM.
class ORTHOGONAL_DIMENSION, a linear dimension constrained to x/y
Definition: typeinfo.h:104
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
BOARD * GetBoard() const
BOARD_ITEM_CONTAINER * GetParent() const
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:91
KICAD_T Type() const
Function Type()
Definition: eda_item.h:182
const PCBNEW_SELECTION & m_selection
Definition: array_creator.h:58