KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "tools/array_tool.h"
21
22#include <array_options.h>
25#include <footprint.h>
26#include <pad.h>
27#include <pcb_generator.h>
28#include <pcb_group.h>
31
32
40static 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( transform.m_offset );
45 aItem.Rotate( aItem.GetPosition(), transform.m_rotation );
46}
47
48
50 m_dialog( nullptr )
51{
52}
53
54
58
59
61{
62 if( aReason == MODEL_RELOAD || aReason == SUPERMODEL_RELOAD || aReason == SHUTDOWN )
63 {
64 if( m_dialog )
65 {
66 m_dialog->Unbind( wxEVT_CLOSE_WINDOW, &ARRAY_TOOL::onDialogClosed, this );
67 m_dialog->Destroy();
68 m_dialog = nullptr;
69 }
70
71 m_array_opts.reset();
72 m_selection.reset();
73 }
74}
75
76
78{
79 return true;
80}
81
82
84{
85 PCB_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
86
87 // Be sure that there is at least one item that we can modify
88 const PCB_SELECTION& selection = selectionTool->RequestSelection(
89 []( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
90 {
91 sTool->FilterCollectorForMarkers( aCollector );
92 sTool->FilterCollectorForHierarchy( aCollector, true );
93 } );
94
95 if( selection.Empty() )
96 return 0;
97
98 m_selection = std::make_unique<PCB_SELECTION>( selection );
99
100 // we have a selection to work on now, so start the tool process
102
103 const bool enableArrayNumbering = m_isFootprintEditor;
104 VECTOR2I origin;
105
106 if( m_selection->Size() == 1 )
107 origin = m_selection->Items()[0]->GetPosition();
108 else
109 origin = m_selection->GetCenter();
110
111 m_array_opts.reset();
112 m_dialog = new DIALOG_CREATE_ARRAY( editFrame, m_array_opts, enableArrayNumbering, origin );
113
114 m_dialog->Bind( wxEVT_CLOSE_WINDOW, &ARRAY_TOOL::onDialogClosed, this );
115
116 // Show the dialog, but it's not modal - the user might ask to select a point, in which case
117 // we'll exit and do to that.
118 m_dialog->Show( true );
119
120 return 0;
121}
122
123void ARRAY_TOOL::onDialogClosed( wxCloseEvent& aEvent )
124{
125 // Now that the dialog has served it's purpose, we can get rid of it
126 m_dialog->Destroy();
127 m_dialog = nullptr;
128
129 // This means the dialog failed somehow
130 if( m_array_opts == nullptr )
131 {
132 m_selection.reset();
133 return;
134 }
135
136 wxCHECK( m_selection, /* void */ );
137
140 BOARD_COMMIT commit( editFrame );
141
142 const int arraySize = m_array_opts->GetArraySize();
143
144 if( m_array_opts->ShouldArrangeSelection() )
145 {
146 std::set<FOOTPRINT*> fpDeDupe;
147
148 EDA_ITEMS sortedSelection = selection.GetItemsSortedBySelectionOrder();
149 int selectionIndex = 0;
150
151 BOARD_ITEM* firstItem = nullptr;
152
153 for( int arrayIndex = 0; arrayIndex < arraySize; ++arrayIndex )
154 {
155 BOARD_ITEM* item = nullptr;
156
157 // Get the next valid item to arrange
158 for( ; selectionIndex < (int) sortedSelection.size(); selectionIndex++ )
159 {
160 item = nullptr;
161
162 if( !sortedSelection[selectionIndex]->IsBOARD_ITEM() )
163 continue;
164
165 item = static_cast<BOARD_ITEM*>( sortedSelection[selectionIndex] );
166
167 FOOTPRINT* parentFootprint = item->GetParentFootprint();
168
169 // If it is not the footprint editor, then move the parent footprint instead.
170 if( !m_isFootprintEditor && parentFootprint )
171 {
172 // It is possible to select multiple footprint child objects in the board editor.
173 // Do not create multiple copies of the same footprint when this occurs.
174 if( fpDeDupe.count( parentFootprint ) == 0 )
175 {
176 fpDeDupe.emplace( parentFootprint );
177 item = parentFootprint;
178 }
179 else
180 {
181 item = nullptr;
182 continue;
183 }
184 }
185
186 // Found a valid item
187 selectionIndex++;
188 break;
189 }
190
191 // Must be out of items to arrange, we're done
192 if( item == nullptr )
193 break;
194
195 commit.Modify( item, nullptr, RECURSE_MODE::RECURSE );
196
197 // Transform is a relative move, so when arranging the transform needs to start from
198 // the same point for each item, e.g. the first item's position
199 if( firstItem == nullptr )
200 firstItem = item;
201 else
202 item->SetPosition( firstItem->GetPosition() );
203
204 TransformItem( *m_array_opts, arrayIndex, *item );
205 }
206
207 // Make sure we did something...
208 if( firstItem != nullptr )
209 commit.Push( _( "Arrange selection" ) );
210
211 return;
212 }
213
214 FOOTPRINT* const fp = m_isFootprintEditor ? editFrame->GetBoard()->GetFirstFootprint()
215 : nullptr;
216
217 // Collect a list of pad numbers that will _not_ be counted as "used"
218 // when finding the next pad numbers.
219 // Things that are selected are fair game, as they'll give up their numbers.
220 // Keeps numbers used by both selected and unselected pads as "reserved".
221 std::set<wxString> unchangingPadNumbers;
222 if( fp )
223 {
224 for( PAD* pad : fp->Pads() )
225 {
226 if( !pad->IsSelected() )
227 unchangingPadNumbers.insert( pad->GetNumber() );
228 }
229 }
230
231 ARRAY_PAD_NUMBER_PROVIDER pad_number_provider( unchangingPadNumbers, *m_array_opts );
232
233 const bool will_reannotate = !m_isFootprintEditor && m_array_opts->ShouldReannotateFootprints();
234 EDA_ITEMS all_added_items;
235
236 // Iterate in reverse so the original items go last, and we can
237 // use them for the positions of the clones.
238 for( int ptN = arraySize - 1; ptN >= 0; --ptN )
239 {
240 PCB_SELECTION items_for_this_block;
241 std::set<FOOTPRINT*> fpDeDupe;
242
243 for( EDA_ITEM* eda_item : selection )
244
245 {
246 if( !eda_item->IsBOARD_ITEM() )
247 continue;
248
249 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( eda_item );
250
251 FOOTPRINT* parentFootprint = item->GetParentFootprint();
252
253 // If it is not the footprint editor, then duplicate the parent footprint instead.
254 // This check assumes that the footprint child objects are correctly parented, if
255 // they are not, this will segfault.
256 if( !m_isFootprintEditor && parentFootprint )
257 {
258 // It is possible to select multiple footprint child objects in the board editor.
259 // Do not create multiple copies of the same footprint when this occurs.
260 if( fpDeDupe.count( parentFootprint ) == 0 )
261 {
262 fpDeDupe.emplace( parentFootprint );
263 item = parentFootprint;
264 }
265 else
266 {
267 continue;
268 }
269 }
270
271 BOARD_ITEM* this_item = nullptr;
272
273 if( ptN == 0 )
274 {
275 // the first point: we don't own this or add it, but
276 // we might still modify it (position or label)
277 this_item = item;
278
279 commit.Modify( this_item, nullptr, RECURSE_MODE::RECURSE );
280
281 TransformItem( *m_array_opts, arraySize - 1, *this_item );
282 }
283 else
284 {
286 {
287 // Fields cannot be duplicated, especially mandatory fields.
288 // A given field is unique for the footprint
289 if( item->Type() == PCB_FIELD_T )
290 continue;
291
292 // Don't bother incrementing pads: the footprint won't update until commit,
293 // so we can only do this once
294 this_item = fp->DuplicateItem( true, &commit, item );
295 }
296 else
297 {
298 switch( item->Type() )
299 {
300 case PCB_FOOTPRINT_T:
301 case PCB_SHAPE_T:
302 case PCB_BARCODE_T:
304 case PCB_TEXT_T:
305 case PCB_TEXTBOX_T:
306 case PCB_TABLE_T:
308 case PCB_TRACE_T:
309 case PCB_ARC_T:
310 case PCB_VIA_T:
312 case PCB_DIM_CENTER_T:
313 case PCB_DIM_RADIAL_T:
315 case PCB_DIM_LEADER_T:
316 case PCB_POINT_T:
317 case PCB_TARGET_T:
318 case PCB_ZONE_T:
319 this_item = item->Duplicate( true, &commit );
320 break;
321
322 case PCB_GENERATOR_T:
323 case PCB_GROUP_T:
324 // Not DeepClone(): array copies must get fresh UUIDs, not the originals'.
325 this_item = static_cast<PCB_GROUP*>( item )->DeepDuplicate( true, &commit );
326 break;
327
328 default:
329 // Silently drop other items (such as footprint texts) from duplication
330 break;
331 }
332 }
333
334 if( this_item )
335 {
336 // Because aItem is/can be created from a selected item, and inherits from
337 // it this state, reset the selected stated of aItem:
338 this_item->ClearSelected();
339
340 this_item->RunOnChildren(
341 []( BOARD_ITEM* aItem )
342 {
343 aItem->ClearSelected();
344 },
346
347 // We're iterating backwards, so the first item is the last in the array
348 TransformItem( *m_array_opts, arraySize - ptN - 1, *this_item );
349
350 // If a group is duplicated, add also created members to the board
351 if( this_item->Type() == PCB_GROUP_T ||
352 this_item->Type() == PCB_GENERATOR_T )
353 {
354 this_item->RunOnChildren(
355 [&]( BOARD_ITEM* aItem )
356 {
357 commit.Add( aItem );
358 },
360 }
361
362 commit.Add( this_item );
363 }
364 }
365
366 // Add new items to selection (footprints in the selection will be reannotated)
367 if( this_item )
368 items_for_this_block.Add( this_item );
369
370 // attempt to renumber items if the array parameters define
371 // a complete numbering scheme to number by (as opposed to
372 // implicit numbering by incrementing the items during creation
373 if( this_item && m_array_opts->ShouldNumberItems() )
374 {
375 // Renumber non-aperture pads.
376 if( this_item->Type() == PCB_PAD_T )
377 {
378 PAD& pad = static_cast<PAD&>( *this_item );
379
380 if( pad.CanHaveNumber() )
381 {
382 wxString newNumber = pad_number_provider.GetNextPadNumber();
383 pad.SetNumber( newNumber );
384 }
385 }
386 }
387 }
388
389 // Do not reannotate the first item, or it will skip its own numbering and
390 // the array annotations will shift by one cell.
391 if( will_reannotate && ptN != arraySize - 1 )
392 {
393 m_toolMgr->GetTool<BOARD_REANNOTATE_TOOL>()->ReannotateDuplicates( items_for_this_block,
394 all_added_items );
395 }
396
397 for( EDA_ITEM* item : items_for_this_block )
398 all_added_items.push_back( item );
399 }
400
401 // Make sure original items are selected (e.g. interactive point select may clear it)
402 for( EDA_ITEM* eda_item : selection )
403 {
404 all_added_items.push_back( eda_item );
405 }
406
408 m_toolMgr->RunAction<EDA_ITEMS*>( ACTIONS::selectItems, &all_added_items );
409
410 commit.Push( _( "Create Array" ) );
411
412 m_selection.reset();
413}
414
415
417{
418 // clang-format off
420 // clang-format on
421}
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.
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.
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)
int CreateArray(const TOOL_EVENT &aEvent)
Invoke a dialog box to allow positioning of the item relative to another by an exact amount.
bool Init() override
Init() is called once upon a registration of the tool.
std::unique_ptr< ARRAY_OPTIONS > m_array_opts
Definition array_tool.h:72
DIALOG_CREATE_ARRAY * m_dialog
Definition array_tool.h:71
std::unique_ptr< PCB_SELECTION > m_selection
Definition array_tool.h:73
void setTransitions() override
Position the m_position_relative_selection selection relative to anchor position using the given tran...
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
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:85
virtual BOARD_ITEM * Duplicate(bool addToParentGroup, BOARD_COMMIT *aCommit=nullptr) const
Create a copy of this BOARD_ITEM.
virtual void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Rotate this object.
virtual void Move(const VECTOR2I &aMoveVector)
Move this object.
Definition board_item.h:445
FOOTPRINT * GetParentFootprint() const
virtual void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const
Invoke a function on all children.
Definition board_item.h:265
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition board.h:713
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
virtual void SetPosition(const VECTOR2I &aPos)
Definition eda_item.h:349
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
void ClearSelected()
Definition eda_item.h:153
std::deque< PAD * > & Pads()
Definition footprint.h:355
BOARD_ITEM * DuplicateItem(bool addToParentGroup, BOARD_COMMIT *aCommit, const BOARD_ITEM *aItem, bool addToFootprint=false)
Duplicate a given item within the footprint, optionally adding it to the board.
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:203
Definition pad.h:61
static TOOL_ACTION createArray
Tool for creating an array of objects.
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:51
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)
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.
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
const PCB_SELECTION & selection() const
virtual void Add(EDA_ITEM *aItem)
A null aItem is ignored; the selection never holds null members.
Definition selection.cpp:38
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:182
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
@ SHUTDOWN
Tool is being shut down.
Definition tool_base.h:80
@ MODEL_RELOAD
Model changes (the sheet for a schematic)
Definition tool_base.h:76
@ SUPERMODEL_RELOAD
For schematics, the entire schematic changed, not just the sheet.
Definition tool_base.h:77
Generic, UI-independent tool event.
Definition tool_event.h:167
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).
#define _(s)
@ RECURSE
Definition eda_item.h:51
Class to handle a set of BOARD_ITEMs.
std::vector< EDA_ITEM * > EDA_ITEMS
Transform applied to an object by this array.
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:98
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:95
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:83
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:96
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:103
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:81
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:82
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:93
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition typeinfo.h:99
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:94
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:86
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:105
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:97
@ PCB_DRILL_CHART_T
class PCB_DRILL_CHART, a live drill chart derived from PCB_TABLE
Definition typeinfo.h:239
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708