KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
array_tool.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 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "tools/array_tool.h"
25
26#include <array_options.h>
29#include <pad.h>
30#include <pcb_group.h>
33
34
42static void TransformItem( const ARRAY_OPTIONS& aArrOpts, int aIndex, BOARD_ITEM& aItem )
43{
44 const ARRAY_OPTIONS::TRANSFORM transform = aArrOpts.GetTransform( aIndex, aItem.GetPosition() );
45
46 aItem.Move( transform.m_offset );
47 aItem.Rotate( aItem.GetPosition(), transform.m_rotation );
48}
49
50
52 m_dialog( nullptr )
53{
54}
55
56
58{
59}
60
61
63{
64}
65
66
68{
69 return true;
70}
71
72
74{
76
77 // Be sure that there is at least one item that we can modify
78 const PCB_SELECTION& selection = selectionTool->RequestSelection(
79 []( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
80 {
81 sTool->FilterCollectorForMarkers( aCollector );
82 sTool->FilterCollectorForHierarchy( aCollector, true );
83 } );
84
85 if( selection.Empty() )
86 return 0;
87
88 m_selection = std::make_unique<PCB_SELECTION>( selection );
89
90 // we have a selection to work on now, so start the tool process
91 PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
92
93 const bool enableArrayNumbering = m_isFootprintEditor;
94 VECTOR2I origin;
95
96 if( m_selection->Size() == 1 )
97 origin = m_selection->Items()[0]->GetPosition();
98 else
99 origin = m_selection->GetCenter();
100
101 m_array_opts.reset();
102 m_dialog = new DIALOG_CREATE_ARRAY( editFrame, m_array_opts, enableArrayNumbering, origin );
103
104 m_dialog->Bind( wxEVT_CLOSE_WINDOW, &ARRAY_TOOL::onDialogClosed, this );
105
106 // Show the dialog, but it's not modal - the user might ask to select a point, in which case
107 // we'll exit and do to that.
108 m_dialog->Show( true );
109
110 return 0;
111}
112
113void ARRAY_TOOL::onDialogClosed( wxCloseEvent& aEvent )
114{
115 // Now that the dialog has served it's purpose, we can get rid of it
116 m_dialog->Destroy();
117
118 // This means the dialog failed somehow
119 if( m_array_opts == nullptr )
120 return;
121
122 wxCHECK( m_selection, /* void */ );
123
125
126 PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
127
128 BOARD_COMMIT commit( editFrame );
129
130 FOOTPRINT* const fp =
131 m_isFootprintEditor ? editFrame->GetBoard()->GetFirstFootprint() : nullptr;
132
133 ARRAY_PAD_NUMBER_PROVIDER pad_number_provider( fp, *m_array_opts );
134
135 EDA_ITEMS all_added_items;
136
137 int arraySize = m_array_opts->GetArraySize();
138
139 if( m_array_opts->ShouldArrangeSelection() )
140 {
141 std::set<FOOTPRINT*> fpDeDupe;
142
144 int selectionIndex = 0;
145
146 BOARD_ITEM* firstItem = nullptr;
147
148 for( int arrayIndex = 0; arrayIndex < arraySize; ++arrayIndex )
149 {
150 BOARD_ITEM* item = nullptr;
151
152 // Get the next valid item to arrange
153 for( ; selectionIndex < (int) sortedSelection.size(); selectionIndex++ )
154 {
155 item = nullptr;
156
157 if( !sortedSelection[selectionIndex]->IsBOARD_ITEM() )
158 continue;
159
160 item = static_cast<BOARD_ITEM*>( sortedSelection[selectionIndex] );
161
162 FOOTPRINT* parentFootprint = item->GetParentFootprint();
163
164 // If it is not the footprint editor, then move the parent footprint instead.
165 if( !m_isFootprintEditor && parentFootprint )
166 {
167 // It is possible to select multiple footprint child objects in the board editor.
168 // Do not create multiple copies of the same footprint when this occurs.
169 if( fpDeDupe.count( parentFootprint ) == 0 )
170 {
171 fpDeDupe.emplace( parentFootprint );
172 item = parentFootprint;
173 }
174 else
175 {
176 item = nullptr;
177 continue;
178 }
179 }
180
181 // Found a valid item
182 selectionIndex++;
183 break;
184 }
185
186 // Must be out of items to arrange, we're done
187 if( item == nullptr )
188 break;
189
190 commit.Modify( item );
191
192 // Transform is a relative move, so when arranging the transform needs to start from
193 // the same point for each item, e.g. the first item's position
194 if( firstItem == nullptr )
195 firstItem = item;
196 else
197 item->SetPosition( firstItem->GetPosition() );
198
199 TransformItem( *m_array_opts, arrayIndex, *item );
200 }
201
202 // Make sure we did something...
203 if( firstItem != nullptr )
204 commit.Push( _( "Arrange selection" ) );
205
206 return;
207 }
208
209 // Iterate in reverse so the original items go last, and we can
210 // use them for the positions of the clones.
211 for( int ptN = arraySize - 1; ptN >= 0; --ptN )
212 {
213 PCB_SELECTION items_for_this_block;
214 std::set<FOOTPRINT*> fpDeDupe;
215
216 for( int i = 0; i < m_selection->Size(); ++i )
217 {
218 if( !selection[i]->IsBOARD_ITEM() )
219 continue;
220
221 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( selection[i] );
222
223 FOOTPRINT* parentFootprint = item->GetParentFootprint();
224
225 // If it is not the footprint editor, then duplicate the parent footprint instead.
226 // This check assumes that the footprint child objects are correctly parented, if
227 // they are not, this will segfault.
228 if( !m_isFootprintEditor && parentFootprint )
229 {
230 // It is possible to select multiple footprint child objects in the board editor.
231 // Do not create multiple copies of the same footprint when this occurs.
232 if( fpDeDupe.count( parentFootprint ) == 0 )
233 {
234 fpDeDupe.emplace( parentFootprint );
235 item = parentFootprint;
236 }
237 else
238 {
239 continue;
240 }
241 }
242
243 BOARD_ITEM* this_item = nullptr;
244
245 if( ptN == 0 )
246 {
247 // the first point: we don't own this or add it, but
248 // we might still modify it (position or label)
249 this_item = item;
250
251 commit.Modify( this_item );
252
253 TransformItem( *m_array_opts, arraySize - 1, *this_item );
254 }
255 else
256 {
258 {
259 // Fields cannot be duplicated, especially mandatory fields.
260 // A given field is unique for the footprint
261 if( item->Type() == PCB_FIELD_T )
262 continue;
263
264 // Don't bother incrementing pads: the footprint won't update until commit,
265 // so we can only do this once
266 this_item = fp->DuplicateItem( item );
267 }
268 else
269 {
270 switch( item->Type() )
271 {
272 case PCB_FOOTPRINT_T:
273 case PCB_SHAPE_T:
275 case PCB_GENERATOR_T:
276 case PCB_TEXT_T:
277 case PCB_TEXTBOX_T:
278 case PCB_TABLE_T:
279 case PCB_TRACE_T:
280 case PCB_ARC_T:
281 case PCB_VIA_T:
283 case PCB_DIM_CENTER_T:
284 case PCB_DIM_RADIAL_T:
286 case PCB_DIM_LEADER_T:
287 case PCB_TARGET_T:
288 case PCB_ZONE_T: this_item = item->Duplicate(); break;
289
290 case PCB_GROUP_T:
291 this_item = static_cast<PCB_GROUP*>( item )->DeepDuplicate();
292 break;
293
294 default:
295 // Silently drop other items (such as footprint texts) from duplication
296 break;
297 }
298 }
299
300 // Add new items to selection (footprints in the selection will be reannotated)
301 items_for_this_block.Add( this_item );
302
303 if( this_item )
304 {
305 // Because aItem is/can be created from a selected item, and inherits from
306 // it this state, reset the selected stated of aItem:
307 this_item->ClearSelected();
308
309 this_item->RunOnChildren(
310 []( BOARD_ITEM* aItem )
311 {
312 aItem->ClearSelected();
313 },
314 RECURSE_MODE::RECURSE );
315
316 // We're iterating backwards, so the first item is the last in the array
317 TransformItem( *m_array_opts, arraySize - ptN - 1, *this_item );
318
319 // If a group is duplicated, add also created members to the board
320 if( this_item->Type() == PCB_GROUP_T )
321 {
322 this_item->RunOnChildren(
323 [&]( BOARD_ITEM* aItem )
324 {
325 commit.Add( aItem );
326 },
327 RECURSE_MODE::RECURSE );
328 }
329
330 commit.Add( this_item );
331 }
332 }
333
334 // attempt to renumber items if the array parameters define
335 // a complete numbering scheme to number by (as opposed to
336 // implicit numbering by incrementing the items during creation
337 if( this_item && m_array_opts->ShouldNumberItems() )
338 {
339 // Renumber non-aperture pads.
340 if( this_item->Type() == PCB_PAD_T )
341 {
342 PAD& pad = static_cast<PAD&>( *this_item );
343
344 if( pad.CanHaveNumber() )
345 {
346 wxString newNumber = pad_number_provider.GetNextPadNumber();
347 pad.SetNumber( newNumber );
348 }
349 }
350 }
351 }
352
353 if( !m_isFootprintEditor && m_array_opts->ShouldReannotateFootprints() )
354 {
355 m_toolMgr->GetTool<BOARD_REANNOTATE_TOOL>()->ReannotateDuplicates( items_for_this_block,
356 all_added_items );
357 }
358
359 for( EDA_ITEM* item : items_for_this_block )
360 all_added_items.push_back( item );
361 }
362
363 // Make sure original items are selected (e.g. interactive point select may clear it)
364 for( int i = 0; i < m_selection->Size(); ++i )
365 {
366 all_added_items.push_back( selection[i] );
367 }
368
370 m_toolMgr->RunAction<EDA_ITEMS*>( ACTIONS::selectItems, &all_added_items );
371
372 commit.Push( _( "Create Array" ) );
373
374 m_selection.reset();
375}
376
377
379{
380 // clang-format off
382 // clang-format on
383}
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.
Definition: array_tool.cpp:42
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: actions.h:220
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition: actions.h:228
Options that govern the setup of an "array" of multiple item.
Definition: array_options.h:39
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.
void onDialogClosed(wxCloseEvent &aEvent)
Definition: array_tool.cpp:113
int CreateArray(const TOOL_EVENT &aEvent)
Invoke a dialog box to allow positioning of the item relative to another by an exact amount.
Definition: array_tool.cpp:73
bool Init() override
Init() is called once upon a registration of the tool.
Definition: array_tool.cpp:67
std::unique_ptr< ARRAY_OPTIONS > m_array_opts
Definition: array_tool.h:76
DIALOG_CREATE_ARRAY * m_dialog
Definition: array_tool.h:75
std::unique_ptr< PCB_SELECTION > m_selection
Definition: array_tool.h:77
void setTransitions() override
Position the m_position_relative_selection selection relative to anchor position using the given tran...
Definition: array_tool.cpp:378
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: array_tool.cpp:62
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:78
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:334
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:299
virtual void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const
Invoke a function on all children.
Definition: board_item.h:205
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:463
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:108
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition: commit.h:80
bool Show(bool show) override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:96
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:252
virtual void SetPosition(const VECTOR2I &aPos)
Definition: eda_item.h:253
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:108
void ClearSelected()
Definition: eda_item.h:130
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:2565
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:202
Definition: pad.h:54
static TOOL_ACTION createArray
Tool for creating an array of objects.
Definition: pcb_actions.h:489
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
BOARD * GetBoard() const
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:53
The selection tool: currently supports:
void FilterCollectorForMarkers(GENERAL_COLLECTOR &aCollector) const
Drop any PCB_MARKERs from the collector.
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter, bool aConfirmLockedItems=false)
Return the current selection, filtered according to aClientFilter.
void FilterCollectorForHierarchy(GENERAL_COLLECTOR &aCollector, bool aMultiselect) const
In general we don't want to select both a parent and any of it's children.
bool m_isFootprintEditor
const PCB_SELECTION & selection() const
virtual void Add(EDA_ITEM *aItem)
Definition: selection.cpp:42
std::vector< EDA_ITEM * > GetItemsSortedBySelectionOrder() const
Definition: selection.cpp:263
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:110
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
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:548
Class to handle a set of BOARD_ITEMs.
Transform applied to an object by this array.
Definition: array_options.h:62
@ 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