KiCad PCB EDA Suite
erc_settings.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 (C) 2020 CERN
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <erc_item.h>
22#include <erc_settings.h>
23#include <schematic.h>
24#include <sch_marker.h>
25#include <sch_screen.h>
27#include <settings/parameters.h>
28
29
31
32
33
34#define OK PIN_ERROR::OK
35#define ERR PIN_ERROR::PP_ERROR
36#define WAR PIN_ERROR::WARNING
37
42{
43/* I, O, Bi, 3S, Pas, NIC, UnS, PwrI, PwrO, OC, OE, NC */
44/* I */ { OK, OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
45/* O */ { OK, ERR, OK, WAR, OK, OK, WAR, OK, ERR, ERR, ERR, ERR },
46/* Bi */ { OK, OK, OK, OK, OK, OK, WAR, OK, WAR, OK, WAR, ERR },
47/* 3S */ { OK, WAR, OK, OK, OK, OK, WAR, WAR, ERR, WAR, WAR, ERR },
48/*Pas */ { OK, OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
49/*NIC */ { OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, ERR },
50/*UnS */ { WAR, WAR, WAR, WAR, WAR, OK, WAR, WAR, WAR, WAR, WAR, ERR },
51/*PwrI*/ { OK, OK, OK, WAR, OK, OK, WAR, OK, OK, OK, OK, ERR },
52/*PwrO*/ { OK, ERR, WAR, ERR, OK, OK, WAR, OK, ERR, ERR, ERR, ERR },
53/* OC */ { OK, ERR, OK, WAR, OK, OK, WAR, OK, ERR, OK, OK, ERR },
54/* OE */ { OK, ERR, WAR, WAR, OK, OK, WAR, OK, ERR, OK, OK, ERR },
55/* NC */ { ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR }
56};
57
58
70{
71/* I, O, Bi, 3S, Pas, NIC, UnS, PwrI, PwrO, OC, OE, NC */
72/* I */ { NOD, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
73/* O */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, DRV, DRV, NPI },
74/* Bi */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
75/* 3S */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
76/*Pas */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
77/*NIC */ { NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NPI },
78/*UnS */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
79/*PwrI*/ { NOD, DRV, NOD, NOD, NOD, NOD, NOD, NOD, DRV, NOD, NOD, NPI },
80/*PwrO*/ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, DRV, DRV, NPI },
81/* OC */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
82/* OE */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
83/* NC */ { NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI }
84};
85
86
87ERC_SETTINGS::ERC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
88 NESTED_SETTINGS( "erc", ercSettingsSchemaVersion, aParent, aPath )
89{
91
92 for( int i = ERCE_FIRST; i <= ERCE_LAST; ++i )
94
95 // Error is the default setting so set non-error priorities here.
109
110 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "rule_severities",
111 [&]() -> nlohmann::json
112 {
113 nlohmann::json ret = {};
114
115 for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
116 {
117 wxString name = item.GetSettingsKey();
118 int code = item.GetErrorCode();
119
120 if( name.IsEmpty() || m_ERCSeverities.count( code ) == 0 )
121 continue;
122
123 ret[std::string( name.ToUTF8() )] = SeverityToString( m_ERCSeverities[code] );
124 }
125
126 return ret;
127 },
128 [&]( const nlohmann::json& aJson )
129 {
130 if( !aJson.is_object() )
131 return;
132
133 for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
134 {
135 int code = item.GetErrorCode();
136 wxString name = item.GetSettingsKey();
137
138 std::string key( name.ToUTF8() );
139
140 if( aJson.contains( key ) )
141 m_ERCSeverities[code] = SeverityFromString( aJson[key] );
142 }
143 },
144 {} ) );
145
146 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "erc_exclusions",
147 [&]() -> nlohmann::json
148 {
149 nlohmann::json js = nlohmann::json::array();
150
151 for( const auto& entry : m_ErcExclusions )
152 js.push_back( entry );
153
154 return js;
155 },
156 [&]( const nlohmann::json& aObj )
157 {
158 m_ErcExclusions.clear();
159
160 if( !aObj.is_array() )
161 return;
162
163 for( const nlohmann::json& entry : aObj )
164 {
165 if( entry.empty() )
166 continue;
167
168 m_ErcExclusions.insert( entry.get<wxString>() );
169 }
170 },
171 {} ) );
172
173 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "pin_map",
174 [&]() -> nlohmann::json
175 {
176 nlohmann::json ret = nlohmann::json::array();
177
178 for( int i = 0; i < ELECTRICAL_PINTYPES_TOTAL; i++ )
179 {
180 nlohmann::json inner = nlohmann::json::array();
181
182 for( int j = 0; j < ELECTRICAL_PINTYPES_TOTAL; j++ )
183 inner.push_back( static_cast<int>( GetPinMapValue( i, j ) ) );
184
185 ret.push_back( inner );
186 }
187
188 return ret;
189 },
190 [&]( const nlohmann::json& aJson )
191 {
192 if( !aJson.is_array() || aJson.size() != ELECTRICAL_PINTYPES_TOTAL )
193 return;
194
195 for( size_t i = 0; i < ELECTRICAL_PINTYPES_TOTAL; i++ )
196 {
197 if( i > aJson.size() - 1 )
198 break;
199
200 nlohmann::json inner = aJson[i];
201
202 if( !inner.is_array() || inner.size() != ELECTRICAL_PINTYPES_TOTAL )
203 return;
204
205 for( size_t j = 0; j < ELECTRICAL_PINTYPES_TOTAL; j++ )
206 {
207 if( inner[j].is_number_integer() )
208 {
209 int val = inner[j].get<int>();
210
211 if( val >= 0 && val <= static_cast<int>( PIN_ERROR::UNCONNECTED ) )
212 SetPinMapValue( i, j, static_cast<PIN_ERROR>( val ) );
213 }
214 }
215 }
216 },
217 {} ) );
218}
219
220
222{
223 if( m_parent )
224 {
226 m_parent = nullptr;
227 }
228}
229
230
231SEVERITY ERC_SETTINGS::GetSeverity( int aErrorCode ) const
232{
233 // Special-case pin-to-pin errors:
234 // Ignore-or-not is controlled by ERCE_PIN_TO_PIN_WARNING (for both)
235 // Warning-or-error is controlled by which errorCode it is
236 if( aErrorCode == ERCE_PIN_TO_PIN_ERROR )
237 {
238 wxASSERT( m_ERCSeverities.count( ERCE_PIN_TO_PIN_WARNING ) );
239
241 return RPT_SEVERITY_IGNORE;
242 else
243 return RPT_SEVERITY_ERROR;
244 }
245 else if( aErrorCode == ERCE_PIN_TO_PIN_WARNING )
246 {
247 wxASSERT( m_ERCSeverities.count( ERCE_PIN_TO_PIN_WARNING ) );
248
250 return RPT_SEVERITY_IGNORE;
251 else
253 }
254
255 wxCHECK_MSG( m_ERCSeverities.count( aErrorCode ), RPT_SEVERITY_IGNORE,
256 wxS( "Missing severity from map in ERC_SETTINGS!" ) );
257
258 return m_ERCSeverities.at( aErrorCode );
259}
260
261
262void ERC_SETTINGS::SetSeverity( int aErrorCode, SEVERITY aSeverity )
263{
264 m_ERCSeverities[ aErrorCode ] = aSeverity;
265}
266
267
269{
270 memcpy( m_PinMap, m_defaultPinMap, sizeof( m_PinMap ) );
271}
272
273
274void SHEETLIST_ERC_ITEMS_PROVIDER::visitMarkers( std::function<void( SCH_MARKER* )> aVisitor ) const
275{
276 SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
277
278 std::set<SCH_SCREEN*> seenScreens;
279
280 for( unsigned i = 0; i < sheetList.size(); i++ )
281 {
282 bool firstTime = seenScreens.count( sheetList[i].LastScreen() ) == 0;
283
284 if( firstTime )
285 seenScreens.insert( sheetList[i].LastScreen() );
286
287 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
288 {
289 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
290
291 if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
292 continue;
293
294 std::shared_ptr<const ERC_ITEM> ercItem =
295 std::static_pointer_cast<const ERC_ITEM>( marker->GetRCItem() );
296
297 // Only show sheet-specific markers on the owning sheet
298 if( ercItem->IsSheetSpecific() )
299 {
300 if( ercItem->GetSpecificSheetPath() != sheetList[i] )
301 continue;
302 }
303
304 // Don't show non-specific markers more than once
305 if( !firstTime && !ercItem->IsSheetSpecific() )
306 continue;
307
308 aVisitor( marker );
309 }
310 }
311}
312
313
315{
316 m_severities = aSeverities;
317
318 m_filteredMarkers.clear();
319
320 ERC_SETTINGS& settings = m_schematic->ErcSettings();
321
323 [&]( SCH_MARKER* aMarker )
324 {
325 SEVERITY markerSeverity;
326
327 if( aMarker->IsExcluded() )
328 markerSeverity = RPT_SEVERITY_EXCLUSION;
329 else
330 markerSeverity = settings.GetSeverity( aMarker->GetRCItem()->GetErrorCode() );
331
332 if( markerSeverity & m_severities )
333 m_filteredMarkers.push_back( aMarker );
334 } );
335}
336
337
339{
340 if( aSeverity < 0 )
341 return m_filteredMarkers.size();
342
343 int count = 0;
344
345 const ERC_SETTINGS& settings = m_schematic->ErcSettings();
346
348 [&]( SCH_MARKER* aMarker )
349 {
350 SEVERITY markerSeverity;
351
352 if( aMarker->IsExcluded() )
353 markerSeverity = RPT_SEVERITY_EXCLUSION;
354 else
355 markerSeverity = settings.GetSeverity( aMarker->GetRCItem()->GetErrorCode() );
356
357 if( ( markerSeverity & aSeverity ) > 0 )
358 count++;
359 } );
360
361 return count;
362}
363
364
365std::shared_ptr<ERC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetERCItem( int aIndex ) const
366{
367 SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
368
369 return marker ? std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() ) : nullptr;
370}
371
372
373std::shared_ptr<RC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetItem( int aIndex ) const
374{
375 return GetERCItem( aIndex );
376}
377
378
379void SHEETLIST_ERC_ITEMS_PROVIDER::DeleteItem( int aIndex, bool aDeep )
380{
381 SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
382 m_filteredMarkers.erase( m_filteredMarkers.begin() + aIndex );
383
384 if( aDeep )
385 {
386 SCH_SCREENS screens( m_schematic->Root() );
387 screens.DeleteMarker( marker );
388 }
389}
390
391
const char * name
Definition: DXF_plotter.cpp:56
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition: erc_item.h:76
Container for ERC settings.
Definition: erc_settings.h:114
static PIN_ERROR m_defaultPinMap[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Default Look up table which gives the ERC error level for a pair of connected pins.
Definition: erc_settings.h:182
SEVERITY GetSeverity(int aErrorCode) const
ERC_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
void ResetPinMap()
void SetSeverity(int aErrorCode, SEVERITY aSeverity)
virtual ~ERC_SETTINGS()
PIN_ERROR GetPinMapValue(int aFirstType, int aSecondType) const
Definition: erc_settings.h:141
std::map< int, SEVERITY > m_ERCSeverities
Definition: erc_settings.h:173
std::set< wxString > m_ErcExclusions
Definition: erc_settings.h:174
static int m_PinMinDrive[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Look up table which gives the minimal drive for a pair of connected pins on a net.
Definition: erc_settings.h:178
void SetPinMapValue(int aFirstType, int aSecondType, PIN_ERROR aValue)
Definition: erc_settings.h:153
PIN_ERROR m_PinMap[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Definition: erc_settings.h:176
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
bool IsExcluded() const
Definition: marker_base.h:97
std::shared_ptr< RC_ITEM > GetRCItem() const
Definition: marker_base.h:105
enum TYPEMARKER GetMarkerType() const
Definition: marker_base.h:95
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
Definition: rc_item.h:75
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:86
SCH_SHEET & Root() const
Definition: schematic.h:91
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:185
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:638
void DeleteMarker(SCH_MARKER *aMarker)
Delete a specific marker.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
int GetCount(int aSeverity=-1) const override
void SetSeverities(int aSeverities) override
void visitMarkers(std::function< void(SCH_MARKER *)> aVisitor) const
void DeleteItem(int aIndex, bool aDeep) override
Remove (and optionally deletes) the indexed item from the list.
std::shared_ptr< ERC_ITEM > GetERCItem(int aIndex) const
std::vector< SCH_MARKER * > m_filteredMarkers
Definition: erc_settings.h:195
std::shared_ptr< RC_ITEM > GetItem(int aIndex) const override
Retrieve a RC_ITEM by index.
const int ercSettingsSchemaVersion
#define ERR
#define WAR
#define OK
#define DRV
Definition: erc_settings.h:102
@ ERCE_UNSPECIFIED
Definition: erc_settings.h:38
@ ERCE_DRIVER_CONFLICT
Conflicting drivers (labels, etc) on a subgraph.
Definition: erc_settings.h:61
@ ERCE_SIMILAR_LABELS
2 labels are equal for case insensitive comparisons.
Definition: erc_settings.h:51
@ ERCE_FIRST
Definition: erc_settings.h:39
@ ERCE_ENDPOINT_OFF_GRID
Pin or wire-end off grid.
Definition: erc_settings.h:41
@ ERCE_LAST
Definition: erc_settings.h:81
@ ERCE_BUS_ENTRY_CONFLICT
A wire connected to a bus doesn't match the bus.
Definition: erc_settings.h:62
@ ERCE_GLOBLABEL
A global label is unique.
Definition: erc_settings.h:69
@ ERCE_NOCONNECT_NOT_CONNECTED
A no connect symbol is not connected to anything.
Definition: erc_settings.h:49
@ ERCE_NOCONNECT_CONNECTED
A no connect symbol is connected to more than 1 pin.
Definition: erc_settings.h:48
@ ERCE_PIN_TO_PIN_WARNING
Definition: erc_settings.h:85
@ ERCE_MISSING_INPUT_PIN
Symbol has input pins that are not placed.
Definition: erc_settings.h:55
@ ERCE_MISSING_UNIT
Symbol has units that are not placed on the schematic.
Definition: erc_settings.h:57
@ ERCE_MISSING_BIDI_PIN
Symbol has bi-directional pins that are not placed.
Definition: erc_settings.h:56
@ ERCE_LIB_SYMBOL_ISSUES
Library symbol changed from current symbol in schematic or the library symbol link no longer valid.
Definition: erc_settings.h:73
@ ERCE_PIN_TO_PIN_ERROR
Definition: erc_settings.h:86
PIN_ERROR
The values a pin-to-pin entry in the pin matrix can take on.
Definition: erc_settings.h:93
#define NOD
Definition: erc_settings.h:104
#define NPI
Types of drive on a net (used for legacy ERC)
Definition: erc_settings.h:101
nlohmann::json json
Definition: gerbview.cpp:44
#define ELECTRICAL_PINTYPES_TOTAL
Definition: pin_type.h:53
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
@ SCH_MARKER_T
Definition: typeinfo.h:141
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:59