KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ref_suffix_split.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 "ref_suffix_split.h"
21
22#include <set>
23#include <sstream>
24
25#include <footprint.h>
26#include <mail_type.h>
27
28
29std::optional<SHEET_REF_SUFFIXES> ParseRefSuffixes( const std::string& aPayload )
30{
31 std::istringstream stream( aPayload );
32 std::string line;
33
34 if( !std::getline( stream, line ) || line != MAIL_SCH_REF_SUFFIXES_TAG )
35 return std::nullopt;
36
37 SHEET_REF_SUFFIXES suffixes;
38 size_t rank = 0;
39
40 while( std::getline( stream, line ) )
41 {
42 size_t tab = line.find( '\t' );
43
44 if( tab == std::string::npos )
45 continue;
46
47 SHEET_REF_SUFFIX& entry = suffixes[wxString::FromUTF8( line.substr( 0, tab ) )];
48 entry.Rank = rank++;
49 entry.Suffix = wxString::FromUTF8( line.substr( tab + 1 ) );
50 }
51
52 return suffixes;
53}
54
55
56std::map<KIID, FOOTPRINT_REF_SPLIT> SplitRefSuffixes( const FOOTPRINTS& aFootprints,
57 const SHEET_REF_SUFFIXES* aSuffixes, bool aSuffixMode )
58{
59 struct GROUP
60 {
61 size_t Rank = 0;
62 KIID Representative;
63 bool Suffixed = false;
64 };
65
66 std::map<KIID, FOOTPRINT_REF_SPLIT> splits;
67 std::map<std::pair<KIID, wxString>, GROUP> groups;
68 std::set<KIID> grouped;
69
70 auto groupKey =
71 []( const FOOTPRINT* aFootprint, const FOOTPRINT_REF_SPLIT& aSplit )
72 {
73 return std::make_pair( aFootprint->GetPath().back(), aSplit.Base );
74 };
75
76 for( const FOOTPRINT* footprint : aFootprints )
77 {
78 FOOTPRINT_REF_SPLIT& split = splits[footprint->m_Uuid];
79 const wxString ref = footprint->GetReference();
80 KIID_PATH sheet = footprint->GetPath();
81 const SHEET_REF_SUFFIX* entry = nullptr;
82
83 split.Base = ref;
84 split.Representative = footprint->m_Uuid;
85
86 if( aSuffixes && !sheet.empty() )
87 {
88 // A package spanning sheets names its own suffix, which can differ from its sheet's
89 if( auto it = aSuffixes->find( sheet.AsString() ); it != aSuffixes->end() )
90 {
91 entry = &it->second;
92 }
93 else
94 {
95 sheet.pop_back();
96
97 if( it = aSuffixes->find( sheet.AsString() + wxS( "/" ) ); it != aSuffixes->end() )
98 entry = &it->second;
99 }
100 }
101
102 if( !aSuffixes && aSuffixMode && footprint->GetPath().size() > 1 )
103 {
104 // With no schematic to ask, a numeric suffix cannot be told from part of the number
105 split.Unknown = true;
106 continue;
107 }
108
109 const wxString suffix = entry ? entry->Suffix : wxString();
110
111 if( !suffix.IsEmpty() && ( ref.length() <= suffix.length() || !ref.EndsWith( suffix ) ) )
112 {
113 split.Unknown = true;
114 continue;
115 }
116
117 const wxString base = ref.Left( ref.length() - suffix.length() );
118 const size_t firstDigit = base.find_first_of( wxS( "0123456789" ) );
119
120 // Unannotated and empty references are not renumbered, so they need no split
121 if( firstDigit == wxString::npos )
122 continue;
123
124 // Text after the number that no suffix accounts for would be dropped by renumbering
125 if( base.find_first_not_of( wxS( "0123456789" ), firstDigit ) != wxString::npos )
126 {
127 split.Unknown = true;
128 continue;
129 }
130
131 if( !entry )
132 continue;
133
134 split.Base = base;
135 split.Suffix = suffix;
136
137 grouped.insert( footprint->m_Uuid );
138
139 auto [it, added] = groups.try_emplace( groupKey( footprint, split ) );
140 GROUP& group = it->second;
141
142 if( added || entry->Rank < group.Rank )
143 {
144 group.Rank = entry->Rank;
145 group.Representative = footprint->m_Uuid;
146 }
147
148 group.Suffixed |= !suffix.IsEmpty();
149 }
150
151 // Copies share a base only where a suffix tells them apart, since equal bare references are duplicates
152 for( const FOOTPRINT* footprint : aFootprints )
153 {
154 FOOTPRINT_REF_SPLIT& split = splits[footprint->m_Uuid];
155
156 if( !grouped.count( footprint->m_Uuid ) )
157 continue;
158
159 if( auto it = groups.find( groupKey( footprint, split ) ); it != groups.end() && it->second.Suffixed )
160 {
161 split.Representative = it->second.Representative;
162 split.Shared = true;
163 }
164 }
165
166 return splits;
167}
const KIID_PATH & GetPath() const
Definition footprint.h:455
wxString AsString() const
Definition kiid.cpp:423
Definition kiid.h:46
#define MAIL_SCH_REF_SUFFIXES_TAG
First line of a MAIL_SCH_GET_REF_SUFFIXES reply, so a schematic with no sheets still answers.
Definition mail_type.h:85
std::deque< FOOTPRINT * > FOOTPRINTS
std::optional< SHEET_REF_SUFFIXES > ParseRefSuffixes(const std::string &aPayload)
std::map< KIID, FOOTPRINT_REF_SPLIT > SplitRefSuffixes(const FOOTPRINTS &aFootprints, const SHEET_REF_SUFFIXES *aSuffixes, bool aSuffixMode)
Split each footprint reference into its base and sheet suffix.
std::map< wxString, SHEET_REF_SUFFIX > SHEET_REF_SUFFIXES
Sheet suffixes keyed by the sheet path string that starts the paths of its footprints,...
static std::vector< std::string > split(const std::string &aStr, const std::string &aDelim)
Split the input string into a vector of output strings.
A schematic sheet path's composed reference suffix and its position in page order.