KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_constraint_clipboard.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 <memory>
21
23
24#include <board.h>
25#include <footprint.h>
26#include <kicad_clipboard.h>
27#include <lset.h>
28#include <pcb_shape.h>
29#include <tools/pcb_selection.h>
31
32
33BOOST_AUTO_TEST_SUITE( ConstraintSolverClipboard )
34
35
36namespace
37{
38constexpr int MM = 1000000;
39
40
41PCB_SHAPE* addSegment( BOARD& aBoard, const VECTOR2I& aStart, const VECTOR2I& aEnd )
42{
43 PCB_SHAPE* seg = new PCB_SHAPE( &aBoard, SHAPE_T::SEGMENT );
44 seg->SetStart( aStart );
45 seg->SetEnd( aEnd );
46 seg->SetLayer( F_SilkS );
47 seg->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
48 aBoard.Add( seg );
49 return seg;
50}
51
52
53std::unique_ptr<BOARD> roundTrip( BOARD* aBoard, const PCB_SELECTION& aSelection )
54{
55 wxString data;
56 CLIPBOARD_IO io;
57 io.SetBoard( aBoard );
58 io.SetWriter( [&]( const wxString& aData ) { data = aData; } );
59 io.SetReader( [&]() { return data; } );
60
61 io.SaveSelection( aSelection, false );
62
63 BOARD_ITEM* parsed = io.Parse();
64 BOOST_REQUIRE( parsed );
65
66 return std::unique_ptr<BOARD>( static_cast<BOARD*>( parsed ) );
67}
68}
69
70
71// A constraint copies along with its objects when every participant is in the selection, and the
72// pasted constraint's members resolve to the pasted (remapped) copies.
73BOOST_AUTO_TEST_CASE( ConstraintCopiedWhenAllMembersSelected )
74{
75 auto board = std::make_unique<BOARD>();
76 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
77
78 PCB_SHAPE* a = addSegment( *board, { 0, 0 }, { 10 * MM, 0 } );
79 PCB_SHAPE* b = addSegment( *board, { 0, 5 * MM }, { 10 * MM, 6 * MM } );
80
82 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
83 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
84 board->Add( c );
85
86 PCB_SELECTION selection;
87 selection.Add( a );
88 selection.Add( b );
89
90 std::unique_ptr<BOARD> pasted = roundTrip( board.get(), selection );
91
92 BOOST_REQUIRE_EQUAL( pasted->Constraints().size(), 1 );
93
94 PCB_CONSTRAINT* pastedConstraint = pasted->Constraints().front();
95 BOOST_CHECK( pastedConstraint->GetConstraintType() == PCB_CONSTRAINT_TYPE::PARALLEL );
96 BOOST_REQUIRE_EQUAL( pastedConstraint->GetMembers().size(), 2 );
97
98 // Every member resolves to an item actually in the pasted board (remapped, not dangling).
99 for( const CONSTRAINT_MEMBER& member : pastedConstraint->GetMembers() )
100 BOOST_CHECK( pasted->ResolveItem( member.m_item, true ) != nullptr );
101}
102
103
104// Pasting re-UUIDs every item (placeBoardItems), which would orphan a constraint's KIID members.
105// RemapKIIDs, fed the old -> new map built during the re-UUID, must re-point them at the copies.
106BOOST_AUTO_TEST_CASE( ConstraintMembersRemapAfterReUuid )
107{
108 auto board = std::make_unique<BOARD>();
109
110 PCB_SHAPE* a = addSegment( *board, { 0, 0 }, { 10 * MM, 0 } );
111 PCB_SHAPE* b = addSegment( *board, { 0, 5 * MM }, { 10 * MM, 6 * MM } );
112
114 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
115 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
116 board->Add( c );
117
118 // Mimic the re-UUID a paste/duplicate performs, recording old -> new exactly as placeBoardItems.
119 std::map<KIID, KIID> idMap;
120
121 for( PCB_SHAPE* shape : { a, b } )
122 {
123 KIID oldUuid = shape->m_Uuid;
124 shape->ResetUuid();
125 idMap[oldUuid] = shape->m_Uuid;
126 }
127
128 // Without the remap the members still hold the pre-paste KIIDs and no longer resolve.
129 BOOST_CHECK( board->ResolveItem( c->GetMembers()[0].m_item, true ) == nullptr );
130
131 c->RemapKIIDs( idMap );
132
133 BOOST_CHECK( c->GetMembers()[0].m_item == a->m_Uuid );
134 BOOST_CHECK( c->GetMembers()[1].m_item == b->m_Uuid );
135 BOOST_CHECK( board->ResolveItem( c->GetMembers()[0].m_item, true ) == a );
136 BOOST_CHECK( board->ResolveItem( c->GetMembers()[1].m_item, true ) == b );
137
138 // A KIID absent from the map is left untouched.
139 KIID stranger;
141 d->AddMember( stranger, CONSTRAINT_ANCHOR::WHOLE );
142 d->RemapKIIDs( idMap );
143 BOOST_CHECK( d->GetMembers()[0].m_item == stranger );
144 delete d;
145}
146
147
148// A constraint is NOT copied when only some of its participants are selected.
149BOOST_AUTO_TEST_CASE( ConstraintNotCopiedWhenPartialSelection )
150{
151 auto board = std::make_unique<BOARD>();
152 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
153
154 PCB_SHAPE* a = addSegment( *board, { 0, 0 }, { 10 * MM, 0 } );
155 PCB_SHAPE* b = addSegment( *board, { 0, 5 * MM }, { 10 * MM, 6 * MM } );
156
158 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
159 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
160 board->Add( c );
161
162 PCB_SELECTION selection;
163 selection.Add( a ); // only one of the two participants
164
165 std::unique_ptr<BOARD> pasted = roundTrip( board.get(), selection );
166
167 BOOST_CHECK( pasted->Constraints().empty() );
168}
169
170
171// The footprint editor's loose-shape copy path carries footprint constraints whose members are all
172// selected, matching the board branch.
173BOOST_AUTO_TEST_CASE( FootprintEditorCopyCarriesConstraints )
174{
175 auto board = std::make_unique<BOARD>();
176 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
177
178 FOOTPRINT* fp = new FOOTPRINT( board.get() );
179 board->Add( fp );
180
181 auto addFpSegment =
182 [&]( const VECTOR2I& aStart, const VECTOR2I& aEnd ) -> PCB_SHAPE*
183 {
184 PCB_SHAPE* seg = new PCB_SHAPE( fp, SHAPE_T::SEGMENT );
185 seg->SetStart( aStart );
186 seg->SetEnd( aEnd );
187 seg->SetLayer( F_SilkS );
188 seg->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
189 fp->Add( seg );
190 return seg;
191 };
192
193 PCB_SHAPE* a = addFpSegment( { 0, 0 }, { 10 * MM, 0 } );
194 PCB_SHAPE* b = addFpSegment( { 0, 5 * MM }, { 10 * MM, 6 * MM } );
195
197 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
198 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
199 fp->Add( c );
200
201 // Each save gets a fresh CLIPBOARD_IO; its formatter accumulates across SaveSelection calls.
202 auto fpRoundTrip =
203 [&]( const PCB_SELECTION& aSelection ) -> std::unique_ptr<FOOTPRINT>
204 {
205 wxString data;
206 CLIPBOARD_IO io;
207 io.SetBoard( board.get() );
208 io.SetWriter( [&]( const wxString& aData ) { data = aData; } );
209 io.SetReader( [&]() { return data; } );
210
211 io.SaveSelection( aSelection, true );
212
213 BOARD_ITEM* parsed = io.Parse();
214 BOOST_REQUIRE( parsed );
215 BOOST_REQUIRE( parsed->Type() == PCB_FOOTPRINT_T );
216
217 return std::unique_ptr<FOOTPRINT>( static_cast<FOOTPRINT*>( parsed ) );
218 };
219
220 PCB_SELECTION selection;
221 selection.Add( a );
222 selection.Add( b );
223
224 std::unique_ptr<FOOTPRINT> clip = fpRoundTrip( selection );
225
226 BOOST_REQUIRE_EQUAL( clip->Constraints().size(), 1 );
227 BOOST_CHECK( clip->Constraints().front()->GetConstraintType() == PCB_CONSTRAINT_TYPE::PARALLEL );
228
229 // A partial selection leaves the constraint behind, like the board branch.
230 PCB_SELECTION partial;
231 partial.Add( a );
232
233 BOOST_CHECK( fpRoundTrip( partial )->Constraints().empty() );
234}
235
236
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:83
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1355
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)
const KIID m_Uuid
Definition eda_item.h:531
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition kiid.h:44
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 geometric constraint between board items (issue #2329).
const std::vector< CONSTRAINT_MEMBER > & GetMembers() const
PCB_CONSTRAINT_TYPE GetConstraintType() const
void AddMember(const KIID &aItem, CONSTRAINT_ANCHOR aAnchor=CONSTRAINT_ANCHOR::WHOLE, int aIndex=-1)
void RemapKIIDs(const std::map< KIID, KIID > &aIdMap) override
Remap KIIDs this item stores to reference other items (e.g.
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.
static bool empty(const wxTextEntryBase *aCtrl)
@ SEGMENT
Definition eda_shape.h:46
@ F_SilkS
Definition layer_ids.h:96
@ WHOLE
The item as a whole (a segment as a line, a circle).
@ PARALLEL
Two segments are parallel.
static bool addSegment(VRML_LAYER &model, IDF_SEGMENT *seg, int icont, int iseg)
One participant in a constraint: a referenced board item plus the feature of that item that participa...
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_CASE(ConstraintCopiedWhenAllMembersSelected)
static void roundTrip(const DIFF_VALUE &aValue)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
static const long long MM
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:79
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683