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 <pcb_group.h>
30 #include <pad_naming.h>
31 
33 
41 static void TransformItem( const ARRAY_OPTIONS& aArrOpts, int aIndex, BOARD_ITEM& aItem )
42 {
43  const ARRAY_OPTIONS::TRANSFORM transform = aArrOpts.GetTransform( aIndex, aItem.GetPosition() );
44 
45  aItem.Move( (wxPoint) transform.m_offset );
46  aItem.Rotate( aItem.GetPosition(), transform.m_rotation * 10 );
47 }
48 
49 
51 {
52  // bail out if no items
53  if( m_selection.Size() == 0 )
54  return;
55 
57 
58  const bool enableArrayNumbering = m_isFootprintEditor;
59  const wxPoint rotPoint = (wxPoint) m_selection.GetCenter();
60 
61  std::unique_ptr<ARRAY_OPTIONS> array_opts;
62 
63  DIALOG_CREATE_ARRAY dialog( &m_parent, array_opts, enableArrayNumbering, rotPoint );
64 
65  int ret = dialog.ShowModal();
66 
67  if( ret != wxID_OK || array_opts == NULL )
68  return;
69 
70  BOARD_COMMIT commit( &m_parent );
71 
72  ARRAY_PAD_NAME_PROVIDER pad_name_provider( fp, *array_opts );
73 
74  for ( int i = 0; i < m_selection.Size(); ++i )
75  {
76  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( m_selection[ i ] );
77 
78  if( item->Type() == PCB_PAD_T && !m_isFootprintEditor )
79  {
80  // If it is not the footprint editor, then duplicate the parent footprint instead
81  item = static_cast<FOOTPRINT*>( item )->GetParent();
82  }
83 
84  // The first item in list is the original item. We do not modify it
85  for( int ptN = 0; ptN < array_opts->GetArraySize(); ptN++ )
86  {
87  BOARD_ITEM* this_item = nullptr;
88 
89  if( ptN == 0 )
90  {
91  // the first point: we don't own this or add it, but
92  // we might still modify it (position or label)
93  this_item = item;
94  }
95  else
96  {
98  {
99  // Don't bother incrementing pads: the footprint won't update until commit,
100  // so we can only do this once
101  this_item = fp->DuplicateItem( item );
102  }
103  else
104  {
105  switch( item->Type() )
106  {
107  case PCB_FOOTPRINT_T:
108  case PCB_SHAPE_T:
109  case PCB_TEXT_T:
110  case PCB_TRACE_T:
111  case PCB_VIA_T:
112  case PCB_DIM_ALIGNED_T:
113  case PCB_DIM_CENTER_T:
115  case PCB_DIM_LEADER_T:
116  case PCB_TARGET_T:
117  case PCB_ZONE_T:
118  this_item = item->Duplicate();
119  break;
120 
121  case PCB_GROUP_T:
122  this_item = static_cast<PCB_GROUP*>( item )->DeepDuplicate();
123  break;
124 
125  default:
126  // Silently drop other items (such as footprint texts) from duplication
127  break;
128  }
129 
130  // PCB items keep the same numbering
131 
132  // @TODO: renumber footprints if asked. This needs UI to enable.
133  // something like this, but needs a "block offset" to prevent
134  // multiple selections overlapping.
135  // if( this_item->Type() == PCB_FOOTPRINT_T )
136  // static_cast<FOOTPRINT&>( *new_item ).IncrementReference( ptN );
137 
138  // @TODO: we should merge zones. This is a bit tricky, because
139  // the undo command needs saving old area, if it is merged.
140  }
141 
142  if( this_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_GROUP_T )
149  {
150  static_cast<PCB_GROUP*>( this_item )->RunOnDescendants(
151  [&]( BOARD_ITEM* aItem )
152  {
153  aItem->ClearSelected();
154  commit.Add( aItem );
155  });
156  }
157  else if( this_item->Type() == PCB_FOOTPRINT_T )
158  {
159  static_cast<FOOTPRINT*>( this_item )->RunOnChildren(
160  [&]( BOARD_ITEM* aItem )
161  {
162  aItem->ClearSelected();
163  });
164  }
165 
166  commit.Add( this_item );
167  }
168  }
169 
170  // always transform the item
171  if( this_item )
172  {
173  commit.Modify( this_item );
174  TransformItem( *array_opts, ptN, *this_item );
175  }
176 
177  // attempt to renumber items if the array parameters define
178  // a complete numbering scheme to number by (as opposed to
179  // implicit numbering by incrementing the items during creation
180  if( this_item && array_opts->ShouldNumberItems() )
181  {
182  // Renumber non-aperture pads.
183  if( this_item->Type() == PCB_PAD_T )
184  {
185  PAD& pad = static_cast<PAD&>( *this_item );
186 
188  {
189  wxString newName = pad_name_provider.GetNextPadName();
190  pad.SetName( newName );
191  }
192  }
193  }
194  }
195  }
196 
197  commit.Push( _( "Create an array" ) );
198 }
BOARD_ITEM * DuplicateItem(const BOARD_ITEM *aItem, bool aAddToFootprint=false)
Duplicate a given item within the footprint, optionally adding it to the board.
Definition: footprint.cpp:1629
Transform applied to an object by this array.
Definition: array_options.h:55
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:100
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:70
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:101
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
virtual void Rotate(const wxPoint &aRotCentre, double aAngle)
Rotate this object.
Definition: board_item.cpp:167
void ClearSelected()
Definition: eda_item.h:132
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:102
PCB_BASE_FRAME & m_parent
Definition: array_creator.h:57
const PCB_SELECTION & m_selection
Definition: array_creator.h:59
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
class PAD, a pad in a footprint
Definition: typeinfo.h:89
virtual wxPoint GetPosition() const
Definition: eda_item.h:252
wxString GetNextPadName()
Get the next available pad name.
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
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
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:317
#define NULL
bool m_isFootprintEditor
Definition: array_creator.h:58
virtual void Move(const wxPoint &aMoveVector)
Move this object.
Definition: board_item.h:275
bool PadCanHaveName(const PAD &aPad)
Check if a pad should be named.
Definition: pad_naming.cpp:26
#define _(s)
class ZONE, a copper pour area
Definition: typeinfo.h:105
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:104
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
virtual TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const =0
Get the transform of the n-th point in the array.
int Size() const
Returns the number of selected parts.
Definition: selection.h:103
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.h:200
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:103
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
BOARD * GetBoard() const
Definition: pad.h:57
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:166
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113