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