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-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 
29 #include "array_creator.h"
30 
31 #include <board_commit.h>
32 
34 
35 
37 {
38  const int numItems = getNumberOfItemsToArray();
39 
40  // bail out if no items
41  if( numItems == 0 )
42  return;
43 
44  BOARD_COMMIT commit( &m_parent );
45  MODULE* const module = getModule();
46  const bool isModuleEditor = module != NULL;
47 
48  const bool enableArrayNumbering = isModuleEditor;
49  const wxPoint rotPoint = getRotationCentre();
50 
51  DIALOG_CREATE_ARRAY dialog( &m_parent, enableArrayNumbering, rotPoint );
52  int ret = dialog.ShowModal();
53 
54  DIALOG_CREATE_ARRAY::ARRAY_OPTIONS* const array_opts = dialog.GetArrayOptions();
55 
56  if( ret != wxID_OK || array_opts == NULL )
57  return;
58 
59  for ( int i = 0; i < numItems; ++i )
60  {
61  BOARD_ITEM* item = getNthItemToArray( i );
62 
63  if( item->Type() == PCB_PAD_T && !isModuleEditor )
64  {
65  // If it is not the module editor, then duplicate the parent module instead
66  item = static_cast<MODULE*>( item )->GetParent();
67  }
68 
69  // The first item in list is the original item. We do not modify it
70  for( int ptN = 1; ptN < array_opts->GetArraySize(); ptN++ )
71  {
72  BOARD_ITEM* new_item;
73 
74  if( isModuleEditor )
75  {
76  // increment pad numbers if do any renumbering
77  // (we will number again later according to the numbering scheme if set)
78  new_item = module->Duplicate( item, array_opts->ShouldNumberItems() );
79  }
80  else
81  {
82  // PCB items keep the same numbering
83  new_item = getBoard()->Duplicate( item );
84 
85  // @TODO: we should merge zones. This is a bit tricky, because
86  // the undo command needs saving old area, if it is merged.
87  }
88 
89  if( new_item )
90  {
91  array_opts->TransformItem( ptN, new_item, rotPoint );
92  prePushAction( new_item );
93  commit.Add( new_item );
94  postPushAction( new_item );
95  }
96 
97  // attempt to renumber items if the array parameters define
98  // a complete numbering scheme to number by (as opposed to
99  // implicit numbering by incrementing the items during creation
100  if( new_item && array_opts->NumberingStartIsSpecified() )
101  {
102  // Renumber pads. Only new pad number renumbering has meaning,
103  // in the footprint editor.
104  if( new_item->Type() == PCB_PAD_T )
105  {
106  const wxString padName = array_opts->GetItemNumber( ptN );
107  static_cast<D_PAD*>( new_item )->SetPadName( padName );
108  }
109  }
110  }
111  }
112 
113  commit.Push( _( "Create an array" ) );
114  finalise();
115 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
virtual wxString GetItemNumber(int n) const =0
ARRAY_OPTIONS * GetArrayOptions() const
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
BOARD_ITEM * Duplicate(const BOARD_ITEM *aItem, bool aIncrementPadNumbers, bool aAddToModule=false)
Function Duplicate Duplicate a given item within the module, without adding to the board...
virtual void postPushAction(BOARD_ITEM *new_item)
PCB_BASE_FRAME & m_parent
Definition: array_creator.h:61
class D_PAD, a pad in a footprint
Definition: typeinfo.h:102
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true) override
Executes the changes.
virtual MODULE * getModule() const =0
virtual void TransformItem(int n, BOARD_ITEM *item, const wxPoint &rotPoint) const =0
virtual wxPoint getRotationCentre() const =0
virtual BOARD * getBoard() const =0
virtual int getNumberOfItemsToArray() const =0
virtual void finalise()=0
virtual BOARD_ITEM * getNthItemToArray(int n) const =0
virtual int GetArraySize() const =0
virtual void prePushAction(BOARD_ITEM *new_item)
Definition: array_creator.h:96
BOARD_ITEM * Duplicate(const BOARD_ITEM *aItem, bool aAddToBoard=false)