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