KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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) 2016-2024 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.h>
32#include <tool/tool_manager.h>
35
43static void TransformItem( const ARRAY_OPTIONS& aArrOpts, int aIndex, BOARD_ITEM& aItem )
44{
45 const ARRAY_OPTIONS::TRANSFORM transform = aArrOpts.GetTransform( aIndex, aItem.GetPosition() );
46
47 aItem.Move( transform.m_offset );
48 aItem.Rotate( aItem.GetPosition(), transform.m_rotation );
49}
50
51
53{
54 // bail out if no items
55 if( m_selection.Size() == 0 )
56 return;
57
59
60 const bool enableArrayNumbering = m_isFootprintEditor;
61 VECTOR2I origin;
62
63 if( m_selection.Size() == 1 )
64 origin = m_selection.Items()[0]->GetPosition();
65 else
66 origin = m_selection.GetCenter();
67
68 std::unique_ptr<ARRAY_OPTIONS> array_opts;
69
70 DIALOG_CREATE_ARRAY dialog( &m_parent, array_opts, enableArrayNumbering, origin );
71
72 int ret = dialog.ShowModal();
73
74 if( ret != wxID_OK || array_opts == nullptr )
75 return;
76
77 BOARD_COMMIT commit( &m_parent );
78
79 ARRAY_PAD_NUMBER_PROVIDER pad_number_provider( fp, *array_opts );
80
81 EDA_ITEMS all_added_items;
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 PCB_SELECTION items_for_this_block;
87 std::set<FOOTPRINT*> fpDeDupe;
88
89 for ( int i = 0; i < m_selection.Size(); ++i )
90 {
91 if( !m_selection[i]->IsBOARD_ITEM() )
92 continue;
93
94 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( m_selection[ i ] );
95
96 FOOTPRINT* parentFootprint = item->GetParentFootprint();
97
98 // If it is not the footprint editor, then duplicate the parent footprint instead.
99 // This check assumes that the footprint child objects are correctly parented, if
100 // they are not, this will segfault.
101 if( !m_isFootprintEditor && parentFootprint )
102 {
103 // It is possible to select multiple footprint child objects in the board editor.
104 // Do not create multiple copies of the same footprint when this occurs.
105 if( fpDeDupe.count( parentFootprint ) == 0 )
106 {
107 fpDeDupe.emplace( parentFootprint );
108 item = parentFootprint;
109 }
110 else
111 {
112 continue;
113 }
114 }
115
116 BOARD_ITEM* this_item = nullptr;
117
118 if( ptN == 0 )
119 {
120 // the first point: we don't own this or add it, but
121 // we might still modify it (position or label)
122 this_item = item;
123
124 commit.Modify( this_item );
125 TransformItem( *array_opts, ptN, *this_item );
126 }
127 else
128 {
130 {
131 // Fields cannot be duplicated, especially mandatory fields.
132 // A given field is unique for the footprint
133 if( item->Type() == PCB_FIELD_T )
134 continue;
135
136 // Don't bother incrementing pads: the footprint won't update until commit,
137 // so we can only do this once
138 this_item = fp->DuplicateItem( item );
139 }
140 else
141 {
142 switch( item->Type() )
143 {
144 case PCB_FOOTPRINT_T:
145 case PCB_SHAPE_T:
147 case PCB_GENERATOR_T:
148 case PCB_TEXT_T:
149 case PCB_TEXTBOX_T:
150 case PCB_TABLE_T:
151 case PCB_TRACE_T:
152 case PCB_ARC_T:
153 case PCB_VIA_T:
155 case PCB_DIM_CENTER_T:
156 case PCB_DIM_RADIAL_T:
158 case PCB_DIM_LEADER_T:
159 case PCB_TARGET_T:
160 case PCB_ZONE_T:
161 this_item = item->Duplicate();
162 break;
163
164 case PCB_GROUP_T:
165 this_item = static_cast<PCB_GROUP*>( item )->DeepDuplicate();
166 break;
167
168 default:
169 // Silently drop other items (such as footprint texts) from duplication
170 break;
171 }
172 }
173
174 // Add new items to selection (footprints in the selection will be reannotated)
175 items_for_this_block.Add( this_item );
176
177 if( this_item )
178 {
179 // Because aItem is/can be created from a selected item, and inherits from
180 // it this state, reset the selected stated of aItem:
181 this_item->ClearSelected();
182
183 this_item->RunOnDescendants(
184 []( BOARD_ITEM* aItem )
185 {
186 aItem->ClearSelected();
187 } );
188
189 TransformItem( *array_opts, ptN, *this_item );
190
191 // If a group is duplicated, add also created members to the board
192 if( this_item->Type() == PCB_GROUP_T )
193 {
194 this_item->RunOnDescendants(
195 [&]( BOARD_ITEM* aItem )
196 {
197 commit.Add( aItem );
198 } );
199 }
200
201 commit.Add( this_item );
202 }
203 }
204
205 // attempt to renumber items if the array parameters define
206 // a complete numbering scheme to number by (as opposed to
207 // implicit numbering by incrementing the items during creation
208 if( this_item && array_opts->ShouldNumberItems() )
209 {
210 // Renumber non-aperture pads.
211 if( this_item->Type() == PCB_PAD_T )
212 {
213 PAD& pad = static_cast<PAD&>( *this_item );
214
215 if( pad.CanHaveNumber() )
216 {
217 wxString newNumber = pad_number_provider.GetNextPadNumber();
218 pad.SetNumber( newNumber );
219 }
220 }
221 }
222 }
223
224 if( !m_isFootprintEditor && array_opts->ShouldReannotateFootprints() )
225 {
226 m_toolMgr->GetTool<BOARD_REANNOTATE_TOOL>()->ReannotateDuplicates( items_for_this_block,
227 all_added_items );
228 }
229
230 for( EDA_ITEM* item : items_for_this_block )
231 all_added_items.push_back( item );
232 }
233
236
237 commit.Push( _( "Create Array" ) );
238}
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.
bool m_isFootprintEditor
Definition: array_creator.h:61
TOOL_MANAGER * m_toolMgr
Definition: array_creator.h:63
const PCB_SELECTION m_selection
Definition: array_creator.h:62
PCB_BASE_FRAME & m_parent
Definition: array_creator.h:60
Options that govern the setup of an "array" of multiple item.
Definition: array_options.h:38
virtual TRANSFORM GetTransform(int aN, const VECTOR2I &aPos) const =0
Get the transform of the n-th point in the array.
Simple class that sequentially provides numbers from an ARRAY_OPTIONS object, making sure that they d...
wxString GetNextPadNumber()
Get the next available pad name.
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
virtual void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Rotate this object.
Definition: board_item.cpp:356
virtual BOARD_ITEM * Duplicate() const
Create a copy of this BOARD_ITEM.
Definition: board_item.cpp:244
virtual void Move(const VECTOR2I &aMoveVector)
Move this object.
Definition: board_item.h:343
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:299
virtual void RunOnDescendants(const std::function< void(BOARD_ITEM *)> &aFunction, int aDepth=0) const
Invoke a function on all descendants.
Definition: board_item.h:213
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:448
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been added.
Definition: commit.h:80
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:243
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
void ClearSelected()
Definition: eda_item.h:122
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:2443
Definition: pad.h:54
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:68
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition: pcb_actions.h:76
BOARD * GetBoard() const
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:52
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:42
virtual VECTOR2I GetCenter() const
Returns the center point of the selection area bounding box.
Definition: selection.cpp:93
int Size() const
Returns the number of selected parts.
Definition: selection.h:116
std::deque< EDA_ITEM * > & Items()
Definition: selection.h:177
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
#define _(s)
std::vector< EDA_ITEM * > EDA_ITEMS
Define list of drawing items for screens.
Definition: eda_item.h:536
Class to handle a set of BOARD_ITEMs.
Transform applied to an object by this array.
Definition: array_options.h:60
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition: typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:107
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:106
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104