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 <kicad_clipboard.h>
35#include <lset.h>
36#include <pcb_group.h>
37#include <pcb_shape.h>
38#include <tools/pcb_selection.h>
39
40
41namespace
42{
43
48std::unique_ptr<BOARD> roundTripSelection( BOARD* aSourceBoard, const PCB_SELECTION& aSelection )
49{
50 wxString clipboardData;
51
52 CLIPBOARD_IO io;
53 io.SetBoard( aSourceBoard );
54 io.SetWriter( [&]( const wxString& aData ) { clipboardData = aData; } );
55 io.SetReader( [&]() { return clipboardData; } );
56
57 io.SaveSelection( aSelection, false );
58
59 BOARD_ITEM* parsed = io.Parse();
60 BOOST_REQUIRE( parsed );
61 BOOST_REQUIRE_EQUAL( parsed->Type(), PCB_T );
62
63 return std::unique_ptr<BOARD>( static_cast<BOARD*>( parsed ) );
64}
65
66} // anonymous namespace
67
68
69BOOST_AUTO_TEST_SUITE( ClipboardGroup )
70
71
72
76BOOST_AUTO_TEST_CASE( CopyPasteGroupPreservesMembership )
77{
78 auto board = std::make_unique<BOARD>();
79 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
80
81 PCB_SHAPE* line1 = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
82 line1->SetStart( VECTOR2I( 0, 0 ) );
83 line1->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 5 ), 0 ) );
84 line1->SetLayer( F_SilkS );
85 line1->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
86 board->Add( line1 );
87
88 PCB_SHAPE* rect = new PCB_SHAPE( board.get(), SHAPE_T::RECTANGLE );
89 rect->SetStart( VECTOR2I( 0, 0 ) );
90 rect->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 5 ), pcbIUScale.mmToIU( 5 ) ) );
91 rect->SetLayer( F_SilkS );
92 rect->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
93 board->Add( rect );
94
95 PCB_GROUP* group = new PCB_GROUP( board.get() );
96 group->SetName( wxT( "TestGroup" ) );
97 group->AddItem( line1 );
98 group->AddItem( rect );
99 board->Add( group );
100
101 PCB_SELECTION selection;
102 selection.Add( group );
103
104 std::unique_ptr<BOARD> clipBoard = roundTripSelection( board.get(), selection );
105
106 BOOST_REQUIRE_EQUAL( clipBoard->Groups().size(), 1u );
107
108 PCB_GROUP* pastedGroup = clipBoard->Groups().front();
109 BOOST_CHECK_EQUAL( pastedGroup->GetName(), wxT( "TestGroup" ) );
110 BOOST_CHECK_EQUAL( pastedGroup->GetItems().size(), 2u );
111
112 // Each pasted child must point back at the pasted group, not be orphaned.
113 for( EDA_ITEM* member : pastedGroup->GetItems() )
114 {
115 BOOST_REQUIRE( member );
116 BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( member );
117 BOOST_REQUIRE( boardItem );
118 BOOST_CHECK_EQUAL( boardItem->GetParentGroup(), pastedGroup );
119 }
120}
121
122
127BOOST_AUTO_TEST_CASE( CopyPasteNestedGroupPreservesMembership )
128{
129 auto board = std::make_unique<BOARD>();
130 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
131
132 PCB_SHAPE* innerShape1 = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
133 innerShape1->SetStart( VECTOR2I( 0, 0 ) );
134 innerShape1->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 1 ), 0 ) );
135 innerShape1->SetLayer( F_SilkS );
136 innerShape1->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.1 ), LINE_STYLE::SOLID ) );
137 board->Add( innerShape1 );
138
139 PCB_SHAPE* innerShape2 = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
140 innerShape2->SetStart( VECTOR2I( 0, 0 ) );
141 innerShape2->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 2 ), 0 ) );
142 innerShape2->SetLayer( F_SilkS );
143 innerShape2->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.1 ), LINE_STYLE::SOLID ) );
144 board->Add( innerShape2 );
145
146 PCB_GROUP* innerGroup = new PCB_GROUP( board.get() );
147 innerGroup->SetName( wxT( "InnerGroup" ) );
148 innerGroup->AddItem( innerShape1 );
149 innerGroup->AddItem( innerShape2 );
150 board->Add( innerGroup );
151
152 PCB_SHAPE* outerShape = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
153 outerShape->SetStart( VECTOR2I( 0, 0 ) );
154 outerShape->SetEnd( VECTOR2I( pcbIUScale.mmToIU( 3 ), 0 ) );
155 outerShape->SetLayer( F_SilkS );
156 outerShape->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.1 ), LINE_STYLE::SOLID ) );
157 board->Add( outerShape );
158
159 PCB_GROUP* outerGroup = new PCB_GROUP( board.get() );
160 outerGroup->SetName( wxT( "OuterGroup" ) );
161 outerGroup->AddItem( innerGroup );
162 outerGroup->AddItem( outerShape );
163 board->Add( outerGroup );
164
165 PCB_SELECTION selection;
166 selection.Add( outerGroup );
167
168 std::unique_ptr<BOARD> clipBoard = roundTripSelection( board.get(), selection );
169
170 BOOST_REQUIRE_EQUAL( clipBoard->Groups().size(), 2u );
171
172 // Find the outer group by name (round-trip should preserve names).
173 PCB_GROUP* pastedOuter = nullptr;
174 PCB_GROUP* pastedInner = nullptr;
175
176 for( PCB_GROUP* g : clipBoard->Groups() )
177 {
178 if( g->GetName() == wxT( "OuterGroup" ) )
179 pastedOuter = g;
180 else if( g->GetName() == wxT( "InnerGroup" ) )
181 pastedInner = g;
182 }
183
184 BOOST_REQUIRE( pastedOuter );
185 BOOST_REQUIRE( pastedInner );
186
187 BOOST_CHECK_EQUAL( pastedOuter->GetItems().size(), 2u );
188 BOOST_CHECK_EQUAL( pastedInner->GetItems().size(), 2u );
189 BOOST_CHECK_EQUAL( pastedInner->GetParentGroup(), pastedOuter );
190}
191
192
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
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:50
wxString GetName() const
Definition eda_group.h:47
void AddItem(EDA_ITEM *aItem)
Add item to group.
Definition eda_group.cpp:58
void SetName(const wxString &aName)
Definition eda_group.h:48
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:114
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
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
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:49
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)
Definition selection.cpp:38
Simple container to manage line stroke parameters.
@ SEGMENT
Definition eda_shape.h:46
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:47
@ F_SilkS
Definition layer_ids.h:96
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(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_T
Definition typeinfo.h:75
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683