KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_clipboard_group.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
30
32
33#include <board.h>
34#include <footprint.h>
35#include <kicad_clipboard.h>
36#include <lset.h>
37#include <pad.h>
38#include <pcb_group.h>
39#include <pcb_shape.h>
40#include <tools/pcb_selection.h>
41
42
43namespace
44{
45
50std::unique_ptr<BOARD> roundTripSelection( BOARD* aSourceBoard, const PCB_SELECTION& aSelection )
51{
52 wxString clipboardData;
53
54 CLIPBOARD_IO io;
55 io.SetBoard( aSourceBoard );
56 io.SetWriter( [&]( const wxString& aData ) { clipboardData = aData; } );
57 io.SetReader( [&]() { return clipboardData; } );
58
59 io.SaveSelection( aSelection, false );
60
61 BOARD_ITEM* parsed = io.Parse();
62 BOOST_REQUIRE( parsed );
63 BOOST_REQUIRE_EQUAL( parsed->Type(), PCB_T );
64
65 return std::unique_ptr<BOARD>( static_cast<BOARD*>( parsed ) );
66}
67
68} // anonymous namespace
69
70
71BOOST_AUTO_TEST_SUITE( ClipboardGroup )
72
73
74
78BOOST_AUTO_TEST_CASE( CopyPasteGroupPreservesMembership )
79{
80 auto board = std::make_unique<BOARD>();
81 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
82
83 PCB_SHAPE* line1 = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
84 line1->SetStart( VECTOR2I( 0, 0 ) );
85 line1->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 5 ), 0 ) );
86 line1->SetLayer( F_SilkS );
87 line1->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
88 board->Add( line1 );
89
90 PCB_SHAPE* rect = new PCB_SHAPE( board.get(), SHAPE_T::RECTANGLE );
91 rect->SetStart( VECTOR2I( 0, 0 ) );
92 rect->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 5 ), pcbIUScale.mmToIU( 5 ) ) );
93 rect->SetLayer( F_SilkS );
94 rect->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
95 board->Add( rect );
96
97 PCB_GROUP* group = new PCB_GROUP( board.get() );
98 group->SetName( wxT( "TestGroup" ) );
99 group->AddItem( line1 );
100 group->AddItem( rect );
101 board->Add( group );
102
103 PCB_SELECTION selection;
104 selection.Add( group );
105
106 std::unique_ptr<BOARD> clipBoard = roundTripSelection( board.get(), selection );
107
108 BOOST_REQUIRE_EQUAL( clipBoard->Groups().size(), 1u );
109
110 PCB_GROUP* pastedGroup = clipBoard->Groups().front();
111 BOOST_CHECK_EQUAL( pastedGroup->GetName(), wxT( "TestGroup" ) );
112 BOOST_CHECK_EQUAL( pastedGroup->GetItems().size(), 2u );
113
114 // Each pasted child must point back at the pasted group, not be orphaned.
115 for( EDA_ITEM* member : pastedGroup->GetItems() )
116 {
117 BOOST_REQUIRE( member );
118 BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( member );
119 BOOST_REQUIRE( boardItem );
120 BOOST_CHECK_EQUAL( boardItem->GetParentGroup(), pastedGroup );
121 }
122}
123
124
129BOOST_AUTO_TEST_CASE( CopyPasteNestedGroupPreservesMembership )
130{
131 auto board = std::make_unique<BOARD>();
132 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
133
134 PCB_SHAPE* innerShape1 = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
135 innerShape1->SetStart( VECTOR2I( 0, 0 ) );
136 innerShape1->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 1 ), 0 ) );
137 innerShape1->SetLayer( F_SilkS );
138 innerShape1->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.1 ), LINE_STYLE::SOLID ) );
139 board->Add( innerShape1 );
140
141 PCB_SHAPE* innerShape2 = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
142 innerShape2->SetStart( VECTOR2I( 0, 0 ) );
143 innerShape2->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 2 ), 0 ) );
144 innerShape2->SetLayer( F_SilkS );
145 innerShape2->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.1 ), LINE_STYLE::SOLID ) );
146 board->Add( innerShape2 );
147
148 PCB_GROUP* innerGroup = new PCB_GROUP( board.get() );
149 innerGroup->SetName( wxT( "InnerGroup" ) );
150 innerGroup->AddItem( innerShape1 );
151 innerGroup->AddItem( innerShape2 );
152 board->Add( innerGroup );
153
154 PCB_SHAPE* outerShape = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
155 outerShape->SetStart( VECTOR2I( 0, 0 ) );
156 outerShape->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 3 ), 0 ) );
157 outerShape->SetLayer( F_SilkS );
158 outerShape->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.1 ), LINE_STYLE::SOLID ) );
159 board->Add( outerShape );
160
161 PCB_GROUP* outerGroup = new PCB_GROUP( board.get() );
162 outerGroup->SetName( wxT( "OuterGroup" ) );
163 outerGroup->AddItem( innerGroup );
164 outerGroup->AddItem( outerShape );
165 board->Add( outerGroup );
166
167 PCB_SELECTION selection;
168 selection.Add( outerGroup );
169
170 std::unique_ptr<BOARD> clipBoard = roundTripSelection( board.get(), selection );
171
172 BOOST_REQUIRE_EQUAL( clipBoard->Groups().size(), 2u );
173
174 // Find the outer group by name (round-trip should preserve names).
175 PCB_GROUP* pastedOuter = nullptr;
176 PCB_GROUP* pastedInner = nullptr;
177
178 for( PCB_GROUP* g : clipBoard->Groups() )
179 {
180 if( g->GetName() == wxT( "OuterGroup" ) )
181 pastedOuter = g;
182 else if( g->GetName() == wxT( "InnerGroup" ) )
183 pastedInner = g;
184 }
185
186 BOOST_REQUIRE( pastedOuter );
187 BOOST_REQUIRE( pastedInner );
188
189 BOOST_CHECK_EQUAL( pastedOuter->GetItems().size(), 2u );
190 BOOST_CHECK_EQUAL( pastedInner->GetItems().size(), 2u );
191 BOOST_CHECK_EQUAL( pastedInner->GetParentGroup(), pastedOuter );
192}
193
194
196
197
198BOOST_AUTO_TEST_SUITE( ClipboardFootprint )
199
200
205BOOST_AUTO_TEST_CASE( CopyPasteLoneFootprintKeepsBody )
206{
207 auto board = std::make_unique<BOARD>();
208 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
209
210 FOOTPRINT* source = new FOOTPRINT( board.get() );
211 source->SetFPID( LIB_ID( wxT( "TestLib" ), wxT( "TestFootprint" ) ) );
212 source->SetLayer( F_Cu );
213 source->SetPosition( VECTOR2I( pcbIUScale.mmToIU( 50 ), pcbIUScale.mmToIU( 50 ) ) );
214
216 silk->SetStart( source->GetPosition() );
217 silk->SetEnd( source->GetPosition() + VECTOR2I( pcbIUScale.mmToIU( 2 ), 0 ) );
218 silk->SetLayer( F_SilkS );
219 silk->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.12 ), LINE_STYLE::SOLID ) );
220 source->Add( silk );
221
222 PAD* pad = new PAD( source );
223 pad->SetNumber( wxT( "1" ) );
224 pad->SetPosition( source->GetPosition() );
225 pad->SetSize( PADSTACK::ALL_LAYERS, VECTOR2I( pcbIUScale.mmToIU( 1 ), pcbIUScale.mmToIU( 1 ) ) );
226 pad->SetAttribute( PAD_ATTRIB::SMD );
227 pad->SetLayerSet( LSET( { F_Cu, F_Mask, F_Paste } ) );
228 source->Add( pad );
229
230 board->Add( source );
231
232 PCB_SELECTION selection;
233 selection.Add( source );
234
235 std::unique_ptr<BOARD> clipBoard = roundTripSelection( board.get(), selection );
236
237 BOOST_REQUIRE_EQUAL( clipBoard->Footprints().size(), 1u );
238
239 const FOOTPRINT* pasted = clipBoard->Footprints().front();
240
241 BOOST_CHECK_EQUAL( pasted->Pads().size(), 1u );
242 BOOST_CHECK_EQUAL( pasted->GraphicalItems().size(), 1u );
243}
244
245
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
void SaveSelection(const PCB_SELECTION &selected, bool isFootprintEditor)
void SetWriter(std::function< void(const wxString &)> aWriter)
BOARD_ITEM * Parse()
void SetReader(std::function< wxString()> aReader)
void SetBoard(BOARD *aBoard)
std::unordered_set< EDA_ITEM * > & GetItems()
Definition eda_group.h:78
wxString GetName() const
Definition eda_group.h:75
void AddItem(EDA_ITEM *aItem)
Add item to group.
Definition eda_group.cpp:58
void SetName(const wxString &aName)
Definition eda_group.h:76
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:116
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
std::deque< PAD * > & Pads()
Definition footprint.h:355
DRAWINGS & GraphicalItems()
Definition footprint.h:358
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & AllTechMask()
Return a mask holding all technical layers (no CU layer) on both side.
Definition lset.cpp:672
static LSET AllCuMask(int aCuLayerCount)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition lset.cpp:595
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
Definition padstack.h:184
Definition pad.h:61
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:51
void SetEnd(const VECTOR2I &aEnd) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStart(const VECTOR2I &aStart) override
void SetStroke(const STROKE_PARAMS &aStroke) override
virtual void Add(EDA_ITEM *aItem)
A null aItem is ignored; the selection never holds null members.
Definition selection.cpp:38
Simple container to manage line stroke parameters.
@ SEGMENT
Definition eda_shape.h:57
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:58
std::string source
@ F_Paste
Definition layer_ids.h:100
@ F_Mask
Definition layer_ids.h:93
@ F_SilkS
Definition layer_ids.h:96
@ F_Cu
Definition layer_ids.h:60
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:100
Class to handle a set of BOARD_ITEMs.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(CopyPasteGroupPreservesMembership)
Copy a group containing two PCB_SHAPE objects to the clipboard, parse it back, and verify the round-t...
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
@ PCB_T
Definition typeinfo.h:74
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)