KiCad PCB EDA Suite
Loading...
Searching...
No Matches
constraint_copy.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 <algorithm>
21
24#include <tools/pcb_selection.h>
25
26
27std::set<KIID> CollectConstraintScopeIds( const PCB_SELECTION& aSelection )
28{
29 std::set<KIID> ids;
30
31 for( EDA_ITEM* item : aSelection )
32 {
33 if( !item->IsBOARD_ITEM() )
34 continue;
35
36 BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
37 ids.insert( boardItem->m_Uuid );
38 boardItem->RunOnChildren( [&]( BOARD_ITEM* aChild ) { ids.insert( aChild->m_Uuid ); },
40 }
41
42 return ids;
43}
44
45
46bool ConstraintFullySelected( const PCB_CONSTRAINT* aConstraint, const std::set<KIID>& aScopeIds )
47{
48 const std::vector<CONSTRAINT_MEMBER>& members = aConstraint->GetMembers();
49
50 return !members.empty()
51 && std::all_of( members.begin(), members.end(),
52 [&]( const CONSTRAINT_MEMBER& aMember )
53 {
54 return aScopeIds.count( aMember.m_item ) > 0;
55 } );
56}
57
58
59std::vector<PCB_CONSTRAINT*> CloneFullySelectedConstraints( const CONSTRAINTS& aSource,
60 const std::map<KIID, KIID>& aIdMap )
61{
62 std::set<KIID> scopeIds;
63
64 for( const auto& [origId, dupeId] : aIdMap )
65 scopeIds.insert( origId );
66
67 std::vector<PCB_CONSTRAINT*> clones;
68
69 for( PCB_CONSTRAINT* constraint : aSource )
70 {
71 if( !ConstraintFullySelected( constraint, scopeIds ) )
72 continue;
73
74 PCB_CONSTRAINT* clone = static_cast<PCB_CONSTRAINT*>( constraint->Clone() );
75
76 // Clone copies the source UUID but the duplicate is a distinct object beside the
77 // original so it needs its own identity before members are repointed
78 clone->ResetUuid();
79 clone->RemapKIIDs( aIdMap );
80 clones.push_back( clone );
81 }
82
83 return clones;
84}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
virtual void RunOnChildren(const std::function< void(BOARD_ITEM *)> &aFunction, RECURSE_MODE aMode) const
Invoke a function on all children.
Definition board_item.h:233
void ResetUuid()
Definition board_item.h:249
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
const KIID m_Uuid
Definition eda_item.h:531
A geometric constraint between board items (issue #2329).
const std::vector< CONSTRAINT_MEMBER > & GetMembers() const
void RemapKIIDs(const std::map< KIID, KIID > &aIdMap) override
Remap KIIDs this item stores to reference other items (e.g.
std::set< KIID > CollectConstraintScopeIds(const PCB_SELECTION &aSelection)
Shared logic deciding which constraints follow items through copy paste or duplicate.
std::vector< PCB_CONSTRAINT * > CloneFullySelectedConstraints(const CONSTRAINTS &aSource, const std::map< KIID, KIID > &aIdMap)
Clone the constraints in aSource that a duplicate should carry.
bool ConstraintFullySelected(const PCB_CONSTRAINT *aConstraint, const std::set< KIID > &aScopeIds)
True when aConstraint has members and every one of them is present in aScopeIds, so the whole relatio...
@ RECURSE
Definition eda_item.h:49
std::deque< PCB_CONSTRAINT * > CONSTRAINTS
One participant in a constraint: a referenced board item plus the feature of that item that participa...