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_group.h>
29#include <pcb_shape.h>
30#include <tools/pcb_selection.h>
32
33
34BOOST_AUTO_TEST_SUITE( ConstraintSolverClipboard )
35
36
37namespace
38{
39constexpr int MM = 1000000;
40
41
42PCB_SHAPE* addSegment( BOARD& aBoard, const VECTOR2I& aStart, const VECTOR2I& aEnd )
43{
44 PCB_SHAPE* seg = new PCB_SHAPE( &aBoard, SHAPE_T::SEGMENT );
45 seg->SetStart( aStart );
46 seg->SetEnd( aEnd );
47 seg->SetLayer( F_SilkS );
48 seg->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
49 aBoard.Add( seg );
50 return seg;
51}
52
53
54std::unique_ptr<BOARD> roundTrip( BOARD* aBoard, const PCB_SELECTION& aSelection )
55{
56 wxString data;
57 CLIPBOARD_IO io;
58 io.SetBoard( aBoard );
59 io.SetWriter( [&]( const wxString& aData ) { data = aData; } );
60 io.SetReader( [&]() { return data; } );
61
62 io.SaveSelection( aSelection, false );
63
64 BOARD_ITEM* parsed = io.Parse();
65 BOOST_REQUIRE( parsed );
66
67 return std::unique_ptr<BOARD>( static_cast<BOARD*>( parsed ) );
68}
69}
70
71
72// A constraint copies along with its objects when every participant is in the selection, and the
73// pasted constraint's members resolve to the pasted (remapped) copies.
74BOOST_AUTO_TEST_CASE( ConstraintCopiedWhenAllMembersSelected )
75{
76 auto board = std::make_unique<BOARD>();
77 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
78
79 PCB_SHAPE* a = addSegment( *board, { 0, 0 }, { 10 * MM, 0 } );
80 PCB_SHAPE* b = addSegment( *board, { 0, 5 * MM }, { 10 * MM, 6 * MM } );
81
83 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
84 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
85 board->Add( c );
86
87 PCB_SELECTION selection;
88 selection.Add( a );
89 selection.Add( b );
90
91 std::unique_ptr<BOARD> pasted = roundTrip( board.get(), selection );
92
93 BOOST_REQUIRE_EQUAL( pasted->Constraints().size(), 1 );
94
95 PCB_CONSTRAINT* pastedConstraint = pasted->Constraints().front();
96 BOOST_CHECK( pastedConstraint->GetConstraintType() == PCB_CONSTRAINT_TYPE::PARALLEL );
97 BOOST_REQUIRE_EQUAL( pastedConstraint->GetMembers().size(), 2 );
98
99 // Every member resolves to an item actually in the pasted board (remapped, not dangling).
100 for( const CONSTRAINT_MEMBER& member : pastedConstraint->GetMembers() )
101 BOOST_CHECK( pasted->ResolveItem( member.m_item, true ) != nullptr );
102}
103
104
105// Paste prunes what the destination board has no layer for. A constraint offers no layer at all,
106// so the naive test dropped it -- and raised a "layers not present" warning about an item that was
107// never on a layer. Only an item that really claims an absent layer may be pruned.
108BOOST_AUTO_TEST_CASE( LayerPruningKeepsLayerAgnosticItems )
109{
110 BOARD board;
111 const LSET enabled( { F_SilkS } );
112 const int copperLayers = 2;
113
114 PCB_CONSTRAINT constraint( &board, PCB_CONSTRAINT_TYPE::PARALLEL );
115
116 // The empty layer set is what the layer test trips over.
117 BOOST_REQUIRE( constraint.GetLayerSet().none() );
118 BOOST_CHECK( constraint.FitsEnabledLayers( enabled, copperLayers ) );
119
120 // A group is governed by its members' layers, not its own.
121 PCB_GROUP group( &board );
122 BOOST_CHECK( group.FitsEnabledLayers( enabled, copperLayers ) );
123
124 // An item that does claim a layer is still judged on it.
125 PCB_SHAPE present( &board, SHAPE_T::SEGMENT );
126 present.SetLayer( F_SilkS );
127 BOOST_CHECK( present.FitsEnabledLayers( enabled, copperLayers ) );
128
129 PCB_SHAPE absent( &board, SHAPE_T::SEGMENT );
130 absent.SetLayer( B_SilkS );
131 BOOST_CHECK( !absent.FitsEnabledLayers( enabled, copperLayers ) );
132
133 // A footprint's children are exempt, but a footprint naming no side is malformed. The IPC
134 // CreateItems path shares this rule and used to reject such a footprint by accident.
135 FOOTPRINT mounted( &board );
136 mounted.SetLayer( F_Cu );
137 BOOST_CHECK( mounted.FitsEnabledLayers( enabled, copperLayers ) );
138
139 FOOTPRINT sideless( &board );
140 sideless.SetLayer( UNDEFINED_LAYER );
141 BOOST_CHECK( !sideless.FitsEnabledLayers( enabled, copperLayers ) );
142}
143
144
145// Pasting re-UUIDs every item (placeBoardItems), which would orphan a constraint's KIID members.
146// RemapKIIDs, fed the old -> new map built during the re-UUID, must re-point them at the copies.
147BOOST_AUTO_TEST_CASE( ConstraintMembersRemapAfterReUuid )
148{
149 auto board = std::make_unique<BOARD>();
150
151 PCB_SHAPE* a = addSegment( *board, { 0, 0 }, { 10 * MM, 0 } );
152 PCB_SHAPE* b = addSegment( *board, { 0, 5 * MM }, { 10 * MM, 6 * MM } );
153
155 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
156 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
157 board->Add( c );
158
159 // Mimic the re-UUID a paste/duplicate performs, recording old -> new exactly as placeBoardItems.
160 std::map<KIID, KIID> idMap;
161
162 for( PCB_SHAPE* shape : { a, b } )
163 {
164 KIID oldUuid = shape->m_Uuid;
165 shape->ResetUuid();
166 idMap[oldUuid] = shape->m_Uuid;
167 }
168
169 // Without the remap the members still hold the pre-paste KIIDs and no longer resolve.
170 BOOST_CHECK( board->ResolveItem( c->GetMembers()[0].m_item, true ) == nullptr );
171
172 c->RemapKIIDs( idMap );
173
174 BOOST_CHECK( c->GetMembers()[0].m_item == a->m_Uuid );
175 BOOST_CHECK( c->GetMembers()[1].m_item == b->m_Uuid );
176 BOOST_CHECK( board->ResolveItem( c->GetMembers()[0].m_item, true ) == a );
177 BOOST_CHECK( board->ResolveItem( c->GetMembers()[1].m_item, true ) == b );
178
179 // A KIID absent from the map is left untouched.
180 KIID stranger;
182 d->AddMember( stranger, CONSTRAINT_ANCHOR::WHOLE );
183 d->RemapKIIDs( idMap );
184 BOOST_CHECK( d->GetMembers()[0].m_item == stranger );
185 delete d;
186}
187
188
189// A constraint is NOT copied when only some of its participants are selected.
190BOOST_AUTO_TEST_CASE( ConstraintNotCopiedWhenPartialSelection )
191{
192 auto board = std::make_unique<BOARD>();
193 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
194
195 PCB_SHAPE* a = addSegment( *board, { 0, 0 }, { 10 * MM, 0 } );
196 PCB_SHAPE* b = addSegment( *board, { 0, 5 * MM }, { 10 * MM, 6 * MM } );
197
199 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
200 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
201 board->Add( c );
202
203 PCB_SELECTION selection;
204 selection.Add( a ); // only one of the two participants
205
206 std::unique_ptr<BOARD> pasted = roundTrip( board.get(), selection );
207
208 BOOST_CHECK( pasted->Constraints().empty() );
209}
210
211
212// The footprint editor's loose-shape copy path carries footprint constraints whose members are all
213// selected, matching the board branch.
214BOOST_AUTO_TEST_CASE( FootprintEditorCopyCarriesConstraints )
215{
216 auto board = std::make_unique<BOARD>();
217 board->SetEnabledLayers( LSET::AllCuMask() | LSET::AllTechMask() );
218
219 FOOTPRINT* fp = new FOOTPRINT( board.get() );
220 board->Add( fp );
221
222 auto addFpSegment =
223 [&]( const VECTOR2I& aStart, const VECTOR2I& aEnd ) -> PCB_SHAPE*
224 {
225 PCB_SHAPE* seg = new PCB_SHAPE( fp, SHAPE_T::SEGMENT );
226 seg->SetStart( aStart );
227 seg->SetEnd( aEnd );
228 seg->SetLayer( F_SilkS );
229 seg->SetStroke( STROKE_PARAMS( pcbIUScale.mmToIU( 0.15 ), LINE_STYLE::SOLID ) );
230 fp->Add( seg );
231 return seg;
232 };
233
234 PCB_SHAPE* a = addFpSegment( { 0, 0 }, { 10 * MM, 0 } );
235 PCB_SHAPE* b = addFpSegment( { 0, 5 * MM }, { 10 * MM, 6 * MM } );
236
238 c->AddMember( a->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
239 c->AddMember( b->m_Uuid, CONSTRAINT_ANCHOR::WHOLE );
240 fp->Add( c );
241
242 // Each save gets a fresh CLIPBOARD_IO; its formatter accumulates across SaveSelection calls.
243 auto fpRoundTrip =
244 [&]( const PCB_SELECTION& aSelection ) -> std::unique_ptr<FOOTPRINT>
245 {
246 wxString data;
247 CLIPBOARD_IO io;
248 io.SetBoard( board.get() );
249 io.SetWriter( [&]( const wxString& aData ) { data = aData; } );
250 io.SetReader( [&]() { return data; } );
251
252 io.SaveSelection( aSelection, true );
253
254 BOARD_ITEM* parsed = io.Parse();
255 BOOST_REQUIRE( parsed );
256 BOOST_REQUIRE( parsed->Type() == PCB_FOOTPRINT_T );
257
258 return std::unique_ptr<FOOTPRINT>( static_cast<FOOTPRINT*>( parsed ) );
259 };
260
261 PCB_SELECTION selection;
262 selection.Add( a );
263 selection.Add( b );
264
265 std::unique_ptr<FOOTPRINT> clip = fpRoundTrip( selection );
266
267 BOOST_REQUIRE_EQUAL( clip->Constraints().size(), 1 );
268 BOOST_CHECK( clip->Constraints().front()->GetConstraintType() == PCB_CONSTRAINT_TYPE::PARALLEL );
269
270 // A partial selection leaves the constraint behind, like the board branch.
271 PCB_SELECTION partial;
272 partial.Add( a );
273
274 BOOST_CHECK( fpRoundTrip( partial )->Constraints().empty() );
275}
276
277
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
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:315
virtual bool FitsEnabledLayers(const LSET &aEnabledLayers, int aCopperLayerCount) const
Check if a board offering aEnabledLayers can hold this item.
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.
bool FitsEnabledLayers(const LSET &aEnabledLayers, int aCopperLayerCount) const override
Check if a board offering aEnabledLayers can hold this item.
Definition footprint.h:650
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition kiid.h:46
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
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.
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.
static bool empty(const wxTextEntryBase *aCtrl)
@ SEGMENT
Definition eda_shape.h:46
@ F_SilkS
Definition layer_ids.h:96
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ B_SilkS
Definition layer_ids.h:97
@ F_Cu
Definition layer_ids.h:60
@ WHOLE
The item as a whole (a segment as a line, a circle).
@ PARALLEL
Two segments are parallel.
Class to handle a set of BOARD_ITEMs.
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