KiCad PCB EDA Suite
Loading...
Searching...
No Matches
import_net_map.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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 <import_net_map.h>
21#include <reporter.h>
22#include <wx/intl.h>
23#include <ostream>
24#include <set>
25
26
27std::ostream& operator<<( std::ostream& aStream, IMPORT_NET_STATUS aStatus )
28{
29 switch( aStatus )
30 {
31 case IMPORT_NET_STATUS::RESOLVED: return aStream << "resolved";
32 case IMPORT_NET_STATUS::SPLIT: return aStream << "split";
33 case IMPORT_NET_STATUS::BUS: return aStream << "bus";
34 case IMPORT_NET_STATUS::NO_CONNECT: return aStream << "no_connect";
35 case IMPORT_NET_STATUS::UNCONNECTED: return aStream << "unconnected";
36 }
37
38 return aStream << "?";
39}
40
41
42std::map<wxString, wxString> GetBoardNetNameMap( const IMPORT_NET_MAP& aMap, REPORTER& aReporter )
43{
44 std::map<wxString, std::set<wxString>> candidates;
45
46 for( const IMPORT_NET_MAP_ENTRY& entry : aMap.entries )
47 {
48 if( entry.status == IMPORT_NET_STATUS::RESOLVED && !entry.generatedName.IsEmpty()
49 && !entry.nameAtImport.IsEmpty() )
50 {
51 candidates[entry.generatedName].insert( entry.nameAtImport );
52 }
53 }
54
55 std::map<wxString, wxString> result;
56
57 for( const auto& [source, names] : candidates )
58 {
59 if( names.size() == 1 )
60 {
61 result.emplace( source, *names.begin() );
62 }
63 else
64 {
65 aReporter.Report( wxString::Format( _( "Ambiguous imported net name '%s'; board net rename "
66 "omitted." ), source ), RPT_SEVERITY_WARNING );
67 }
68 }
69
70 // Two source nets that merged into one KiCad net would rename two board nets to the same name.
71 // The applier rejects such a batch outright, taking the whole board import down with it.
72 std::map<wxString, int> claims;
73
74 for( const auto& [source, target] : result )
75 claims[target]++;
76
77 for( auto it = result.begin(); it != result.end(); )
78 {
79 if( claims[it->second] > 1 )
80 {
81 aReporter.Report( wxString::Format( _( "Imported net name '%s' is claimed by more than one "
82 "source net; board net rename omitted." ), it->second ),
84 it = result.erase( it );
85 }
86 else
87 {
88 ++it;
89 }
90 }
91
92 return result;
93}
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
#define _(s)
std::ostream & operator<<(std::ostream &aStream, IMPORT_NET_STATUS aStatus)
Name the status, so test diagnostics read as a status rather than an ordinal.
std::map< wxString, wxString > GetBoardNetNameMap(const IMPORT_NET_MAP &aMap, REPORTER &aReporter)
Reduce the map to the board net renames it justifies.
IMPORT_NET_STATUS
How completely a source net survived conversion.
@ BUS
the source net is carried by a bus
@ NO_CONNECT
the source marks the net as deliberately unconnected
@ RESOLVED
exactly one physical net carries the source net
@ SPLIT
the source net reaches more than one physical net
@ UNCONNECTED
the source net reaches no physical net
@ RPT_SEVERITY_WARNING
wxString generatedName
wxString nameAtImport
IMPORT_NET_STATUS status
Import provenance, held only for the lifetime of the importing SCHEMATIC.
std::vector< IMPORT_NET_MAP_ENTRY > entries
wxString result
Test unit parsing edge cases and error handling.