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}
63
64
66{
67 return true;
68}
69
70
72{
73 PCB_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
74
75 // Be sure that there is at least one item that we can modify
76 const PCB_SELECTION& selection = selectionTool->RequestSelection(
77 []( const VECTOR2I&, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
78 {
79 sTool->FilterCollectorForMarkers( aCollector );
80 sTool->FilterCollectorForHierarchy( aCollector, true );
81 } );
82
83 if( selection.Empty() )
84 return 0;
85
86 m_selection = std::make_unique<PCB_SELECTION>( selection );
87
88 // we have a selection to work on now, so start the tool process
90
91 const bool enableArrayNumbering = m_isFootprintEditor;
92 VECTOR2I origin;
93
94 if( m_selection->Size() == 1 )
95 origin = m_selection->Items()[0]->GetPosition();
96 else
97 origin = m_selection->GetCenter();
98
99 m_array_opts.reset();
100 m_dialog = new DIALOG_CREATE_ARRAY( editFrame, m_array_opts, enableArrayNumbering, origin );
101
102 m_dialog->Bind( wxEVT_CLOSE_WINDOW, &ARRAY_TOOL::onDialogClosed, this );
103
104 // Show the dialog, but it's not modal - the user might ask to select a point, in which case
105 // we'll exit and do to that.
106 m_dialog->Show( true );
107
108 return 0;
109}
110
111void ARRAY_TOOL::onDialogClosed( wxCloseEvent& aEvent )
112{
113 // Now that the dialog has served it's purpose, we can get rid of it
114 m_dialog->Destroy();
115
116 // This means the dialog failed somehow
117 if( m_array_opts == nullptr )
118 return;
119
120 wxCHECK( m_selection, /* void */ );
121
124 BOARD_COMMIT commit( editFrame );
125
126 const int arraySize = m_array_opts->GetArraySize();
127
128 if( m_array_opts->ShouldArrangeSelection() )
129 {
130 std::set<FOOTPRINT*> fpDeDupe;
131
132 EDA_ITEMS sortedSelection = selection.GetItemsSortedBySelectionOrder();
133 int selectionIndex = 0;
134
135 BOARD_ITEM* firstItem = nullptr;
136
137 for( int arrayIndex = 0; arrayIndex < arraySize; ++arrayIndex )
138 {
139 BOARD_ITEM* item = nullptr;
140
141 // Get the next valid item to arrange
142 for( ; selectionIndex < (int) sortedSelection.size(); selectionIndex++ )
143 {
144 item = nullptr;
145
146 if( !sortedSelection[selectionIndex]->IsBOARD_ITEM() )
147 continue;
148
149 item = static_cast<BOARD_ITEM*>( sortedSelection[selectionIndex] );
150
151 FOOTPRINT* parentFootprint = item->GetParentFootprint();
152
153 // If it is not the footprint editor, then move the parent footprint instead.
154 if( !m_isFootprintEditor && parentFootprint )
155 {
156 // It is possible to select multiple footprint child objects in the board editor.
157 // Do not create multiple copies of the same footprint when this occurs.
158 if( fpDeDupe.count( parentFootprint ) == 0 )
159 {
160 fpDeDupe.emplace( parentFootprint );
161 item = parentFootprint;
162 }
163 else
164 {
165 item = nullptr;
166 continue;
167 }
168 }
169
170 // Found a valid item
171 selectionIndex++;
172 break;
173 }
174
175 // Must be out of items to arrange, we're done
176 if( item == nullptr )
177 break;
178
179 commit.Modify( item, nullptr, RECURSE_MODE::RECURSE );
180
181 // Transform is a relative move, so when arranging the transform needs to start from
182 // the same point for each item, e.g. the first item's position
183 if( firstItem == nullptr )
184 firstItem = item;
185 else
186 item->SetPosition( firstItem->GetPosition() );
187
188 TransformItem( *m_array_opts, arrayIndex, *item );
189 }
190
191 // Make sure we did something...
192 if( firstItem != nullptr )
193 commit.Push( _( "Arrange selection" ) );
194
195 return;
196 }
197
198 FOOTPRINT* const fp = m_isFootprintEditor ? editFrame->GetBoard()->GetFirstFootprint()
199 : nullptr;
200
201 // Collect a list of pad numbers that will _not_ be counted as "used"
202 // when finding the next pad numbers.
203 // Things that are selected are fair game, as they'll give up their numbers.
204 // Keeps numbers used by both selected and unselected pads as "reserved".
205 std::set<wxString> unchangingPadNumbers;
206 if( fp )
207 {
208 for( PAD* pad : fp->Pads() )
209 {
210 if( !pad->IsSelected() )
211 unchangingPadNumbers.insert( pad->GetNumber() );
212 }
213 }
214
215 ARRAY_PAD_NUMBER_PROVIDER pad_number_provider( unchangingPadNumbers, *m_array_opts );
216
217 const bool will_reannotate = !m_isFootprintEditor && m_array_opts->ShouldReannotateFootprints();
218 EDA_ITEMS all_added_items;
219
220 // Iterate in reverse so the original items go last, and we can
221 // use them for the positions of the clones.
222 for( int ptN = arraySize - 1; ptN >= 0; --ptN )
223 {
224 PCB_SELECTION items_for_this_block;
225 std::set<FOOTPRINT*> fpDeDupe;
226
227 for( EDA_ITEM* eda_item : selection )
228
229 {
230 if( !eda_item->IsBOARD_ITEM() )
231 continue;
232
233 BOARD_ITEM* item = static_cast<BOARD_ITEM*>( eda_item );
234
235 FOOTPRINT* parentFootprint = item->GetParentFootprint();
236
237 // If it is not the footprint editor, then duplicate the parent footprint instead.
238 // This check assumes that the footprint child objects are correctly parented, if
239 // they are not, this will segfault.
240 if( !m_isFootprintEditor && parentFootprint )
241 {
242 // It is possible to select multiple footprint child objects in the board editor.
243 // Do not create multiple copies of the same footprint when this occurs.
244 if( fpDeDupe.count( parentFootprint ) == 0 )
245 {
246 fpDeDupe.emplace( parentFootprint );
247 item = parentFootprint;
248 }
249 else
250 {
251 continue;
252 }
253 }
254
255 BOARD_ITEM* this_item = nullptr;
256
257 if( ptN == 0 )
258 {
259 // the first point: we don't own this or add it, but
260 // we might still modify it (position or label)
261 this_item = item;
262
263 commit.Modify( this_item, nullptr, RECURSE_MODE::RECURSE );
264
265 TransformItem( *m_array_opts, arraySize - 1, *this_item );
266 }
267 else
268 {
270 {
271 // Fields cannot be duplicated, especially mandatory fields.
272 // A given field is unique for the footprint
273 if( item->Type() == PCB_FIELD_T )
274 continue;
275
276 // Don't bother incrementing pads: the footprint won't update until commit,
277 // so we can only do this once
278 this_item = fp->DuplicateItem( true, &commit, item );
279 }
280 else
281 {
282 switch( item->Type() )
283 {
284 case PCB_FOOTPRINT_T:
285 case PCB_SHAPE_T:
286 case PCB_BARCODE_T:
288 case PCB_TEXT_T:
289 case PCB_TEXTBOX_T:
290 case PCB_TABLE_T:
291 case PCB_TRACE_T:
292 case PCB_ARC_T:
293 case PCB_VIA_T:
295 case PCB_DIM_CENTER_T:
296 case PCB_DIM_RADIAL_T:
298 case PCB_DIM_LEADER_T:
299 case PCB_POINT_T:
300 case PCB_TARGET_T:
301 case PCB_ZONE_T:
302 this_item = item->Duplicate( true, &commit );
303 break;
304
305 case PCB_GENERATOR_T:
306 case PCB_GROUP_T:
307 // Not DeepClone(): array copies must get fresh UUIDs, not the originals'.
308 this_item = static_cast<PCB_GROUP*>( item )->DeepDuplicate( true, &commit );
309 break;
310
311 default:
312 // Silently drop other items (such as footprint texts) from duplication
313 break;
314 }
315 }
316
317 if( this_item )
318 {
319 // Because aItem is/can be created from a selected item, and inherits from
320 // it this state, reset the selected stated of aItem:
321 this_item->ClearSelected();
322
323 this_item->RunOnChildren(
324 []( BOARD_ITEM* aItem )
325 {
326 aItem->ClearSelected();
327 },
329
330 // We're iterating backwards, so the first item is the last in the array
331 TransformItem( *m_array_opts, arraySize - ptN - 1, *this_item );
332
333 // If a group is duplicated, add also created members to the board
334 if( this_item->Type() == PCB_GROUP_T ||
335 this_item->Type() == PCB_GENERATOR_T )
336 {
337 this_item->RunOnChildren(
338 [&]( BOARD_ITEM* aItem )
339 {
340 commit.Add( aItem );
341 },
343 }
344
345 commit.Add( this_item );
346 }
347 }
348
349 // Add new items to selection (footprints in the selection will be reannotated)
350 if( this_item )
351 items_for_this_block.Add( this_item );
352
353 // attempt to renumber items if the array parameters define
354 // a complete numbering scheme to number by (as opposed to
355 // implicit numbering by incrementing the items during creation
356 if( this_item && m_array_opts->ShouldNumberItems() )
357 {
358 // Renumber non-aperture pads.
359 if( this_item->Type() == PCB_PAD_T )
360 {
361 PAD& pad = static_cast<PAD&>( *this_item );
362
363 if( pad.CanHaveNumber() )
364 {
365 wxString newNumber = pad_number_provider.GetNextPadNumber();
366 pad.SetNumber( newNumber );
367 }
368 }
369 }
370 }
371
372 // Do not reannotate the first item, or it will skip its own numbering and
373 // the array annotations will shift by one cell.
374 if( will_reannotate && ptN != arraySize - 1 )
375 {
376 m_toolMgr->GetTool<BOARD_REANNOTATE_TOOL>()->ReannotateDuplicates( items_for_this_block,
377 all_added_items );
378 }
379
380 for( EDA_ITEM* item : items_for_this_block )
381 all_added_items.push_back( item );
382 }
383
384 // Make sure original items are selected (e.g. interactive point select may clear it)
385 for( EDA_ITEM* eda_item : selection )
386 {
387 all_added_items.push_back( eda_item );
388 }
389
391 m_toolMgr->RunAction<EDA_ITEMS*>( ACTIONS::selectItems, &all_added_items );
392
393 commit.Push( _( "Create Array" ) );
394
395 m_selection.reset();
396}
397
398
400{
401 // clang-format off
403 // clang-format on
404}
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.
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:81
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:372
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:229
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition board.h:587
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:96
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
virtual void SetPosition(const VECTOR2I &aPos)
Definition eda_item.h:283
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
void ClearSelected()
Definition eda_item.h:147
std::deque< PAD * > & Pads()
Definition footprint.h:375
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:49
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)
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
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:49
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:81
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:99
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:96
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:84
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:90
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:97
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:104
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:86
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:101
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:85
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:82
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:83
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:94
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition typeinfo.h:100
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:79
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:95
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:80
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:91
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition typeinfo.h:87
@ PCB_POINT_T
class PCB_POINT, a 0-dimensional point
Definition typeinfo.h:106
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:89
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:98
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683