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 <jon@craftyjon.com>
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>
26 #include <settings/parameters.h>
27 
28 
30 
31 
32 
33 #define OK PIN_ERROR::OK
34 #define ERR PIN_ERROR::PP_ERROR
35 #define WAR PIN_ERROR::WARNING
36 
41 {
42 /* I, O, Bi, 3S, Pas, NIC, UnS, PwrI, PwrO, OC, OE, NC */
43 /* I */ { OK, OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
44 /* O */ { OK, ERR, OK, WAR, OK, OK, WAR, OK, ERR, ERR, ERR, ERR },
45 /* Bi */ { OK, OK, OK, OK, OK, OK, WAR, OK, WAR, OK, WAR, ERR },
46 /* 3S */ { OK, WAR, OK, OK, OK, OK, WAR, WAR, ERR, WAR, WAR, ERR },
47 /*Pas */ { OK, OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
48 /*NIC */ { OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, OK, ERR },
49 /*UnS */ { WAR, WAR, WAR, WAR, WAR, OK, WAR, WAR, WAR, WAR, WAR, ERR },
50 /*PwrI*/ { OK, OK, OK, WAR, OK, OK, WAR, OK, OK, OK, OK, ERR },
51 /*PwrO*/ { OK, ERR, WAR, ERR, OK, OK, WAR, OK, ERR, ERR, ERR, ERR },
52 /* OC */ { OK, ERR, OK, WAR, OK, OK, WAR, OK, ERR, OK, OK, ERR },
53 /* OE */ { OK, ERR, WAR, WAR, OK, OK, WAR, OK, ERR, OK, OK, ERR },
54 /* NC */ { ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR }
55 };
56 
57 
69 {
70 /* I, O, Bi, 3S, Pas, NIC, UnS, PwrI, PwrO, OC, OE, NC */
71 /* I */ { NOD, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
72 /* O */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, DRV, DRV, NPI },
73 /* Bi */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
74 /* 3S */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
75 /*Pas */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
76 /*NIC */ { NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NOD, NPI },
77 /*UnS */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
78 /*PwrI*/ { NOD, DRV, NOD, NOD, NOD, NOD, NOD, NOD, DRV, NOD, NOD, NPI },
79 /*PwrO*/ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, DRV, DRV, NPI },
80 /* OC */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
81 /* OE */ { DRV, DRV, DRV, DRV, DRV, NOD, DRV, NOD, DRV, DRV, DRV, NPI },
82 /* NC */ { NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI }
83 };
84 
85 
86 ERC_SETTINGS::ERC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
87  NESTED_SETTINGS( "erc", ercSettingsSchemaVersion, aParent, aPath )
88 {
89  ResetPinMap();
90 
91  for( int i = ERCE_FIRST; i <= ERCE_LAST; ++i )
93 
94  // Error is the default setting so set non-error priorities here.
104 
105  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "rule_severities",
106  [&]() -> nlohmann::json
107  {
108  nlohmann::json ret = {};
109 
110  for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
111  {
112  wxString name = item.GetSettingsKey();
113  int code = item.GetErrorCode();
114 
115  if( name.IsEmpty() || m_Severities.count( code ) == 0 )
116  continue;
117 
118  ret[std::string( name.ToUTF8() )] = SeverityToString( m_Severities[code] );
119  }
120 
121  return ret;
122  },
123  [&]( const nlohmann::json& aJson )
124  {
125  if( !aJson.is_object() )
126  return;
127 
128  for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
129  {
130  int code = item.GetErrorCode();
131  wxString name = item.GetSettingsKey();
132 
133  std::string key( name.ToUTF8() );
134 
135  if( aJson.contains( key ) )
136  m_Severities[code] = SeverityFromString( aJson[key] );
137  }
138  },
139  {} ) );
140 
141  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "erc_exclusions",
142  [&]() -> nlohmann::json
143  {
144  nlohmann::json js = nlohmann::json::array();
145 
146  for( const auto& entry : m_ErcExclusions )
147  js.push_back( entry );
148 
149  return js;
150  },
151  [&]( const nlohmann::json& aObj )
152  {
153  m_ErcExclusions.clear();
154 
155  if( !aObj.is_array() )
156  return;
157 
158  for( const nlohmann::json& entry : aObj )
159  {
160  if( entry.empty() )
161  continue;
162 
163  m_ErcExclusions.insert( entry.get<wxString>() );
164  }
165  },
166  {} ) );
167 
168  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "pin_map",
169  [&]() -> nlohmann::json
170  {
171  nlohmann::json ret = nlohmann::json::array();
172 
173  for( int i = 0; i < ELECTRICAL_PINTYPES_TOTAL; i++ )
174  {
175  nlohmann::json inner = nlohmann::json::array();
176 
177  for( int j = 0; j < ELECTRICAL_PINTYPES_TOTAL; j++ )
178  inner.push_back( static_cast<int>( GetPinMapValue( i, j ) ) );
179 
180  ret.push_back( inner );
181  }
182 
183  return ret;
184  },
185  [&]( const nlohmann::json& aJson )
186  {
187  if( !aJson.is_array() || aJson.size() != ELECTRICAL_PINTYPES_TOTAL )
188  return;
189 
190  for( size_t i = 0; i < ELECTRICAL_PINTYPES_TOTAL; i++ )
191  {
192  if( i > aJson.size() - 1 )
193  break;
194 
195  nlohmann::json inner = aJson[i];
196 
197  if( !inner.is_array() || inner.size() != ELECTRICAL_PINTYPES_TOTAL )
198  return;
199 
200  for( size_t j = 0; j < ELECTRICAL_PINTYPES_TOTAL; j++ )
201  {
202  if( inner[j].is_number_integer() )
203  {
204  int val = inner[j].get<int>();
205 
206  if( val >= 0 && val <= static_cast<int>( PIN_ERROR::UNCONNECTED ) )
207  SetPinMapValue( i, j, static_cast<PIN_ERROR>( val ) );
208  }
209  }
210  }
211  },
212  {} ) );
213 }
214 
215 
217 {
218  if( m_parent )
219  {
221  m_parent = nullptr;
222  }
223 }
224 
225 
226 SEVERITY ERC_SETTINGS::GetSeverity( int aErrorCode ) const
227 {
228  // Special-case pin-to-pin errors:
229  // Ignore-or-not is controlled by ERCE_PIN_TO_PIN_WARNING (for both)
230  // Warning-or-error is controlled by which errorCode it is
231  if( aErrorCode == ERCE_PIN_TO_PIN_ERROR )
232  {
233  wxASSERT( m_Severities.count( ERCE_PIN_TO_PIN_WARNING ) );
234 
236  return RPT_SEVERITY_IGNORE;
237  else
238  return RPT_SEVERITY_ERROR;
239  }
240  else if( aErrorCode == ERCE_PIN_TO_PIN_WARNING )
241  {
242  wxASSERT( m_Severities.count( ERCE_PIN_TO_PIN_WARNING ) );
243 
245  return RPT_SEVERITY_IGNORE;
246  else
247  return RPT_SEVERITY_WARNING;
248  }
249 
250  wxCHECK_MSG( m_Severities.count( aErrorCode ), RPT_SEVERITY_IGNORE,
251  "Missing severity from map in ERC_SETTINGS!" );
252 
253  return m_Severities.at( aErrorCode );
254 }
255 
256 
257 void ERC_SETTINGS::SetSeverity( int aErrorCode, SEVERITY aSeverity )
258 {
259  m_Severities[ aErrorCode ] = aSeverity;
260 }
261 
262 
264 {
265  memcpy( m_PinMap, m_defaultPinMap, sizeof( m_PinMap ) );
266 }
267 
268 
269 void SHEETLIST_ERC_ITEMS_PROVIDER::visitMarkers( std::function<void( SCH_MARKER* )> aVisitor )
270 {
271  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
272 
273  std::set<SCH_SCREEN*> seenScreens;
274 
275  for( unsigned i = 0; i < sheetList.size(); i++ )
276  {
277  bool firstTime = seenScreens.count( sheetList[i].LastScreen() ) == 0;
278 
279  if( firstTime )
280  seenScreens.insert( sheetList[i].LastScreen() );
281 
282  for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
283  {
284  SCH_MARKER* marker = static_cast<SCH_MARKER*>( aItem );
285 
286  if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
287  continue;
288 
289  // Don't show non-specific markers more than once
290  if( !firstTime &&
291  !static_cast<ERC_ITEM*>( marker->GetRCItem().get() )->IsSheetSpecific() )
292  {
293  continue;
294  }
295 
296  aVisitor( marker );
297  }
298  }
299 }
300 
301 
303 {
304  m_severities = aSeverities;
305 
306  m_filteredMarkers.clear();
307 
308  ERC_SETTINGS& settings = m_schematic->ErcSettings();
309 
310  visitMarkers(
311  [&]( SCH_MARKER* aMarker )
312  {
313  SEVERITY markerSeverity;
314 
315  if( aMarker->IsExcluded() )
316  markerSeverity = RPT_SEVERITY_EXCLUSION;
317  else
318  markerSeverity = settings.GetSeverity( aMarker->GetRCItem()->GetErrorCode() );
319 
320  if( markerSeverity & m_severities )
321  m_filteredMarkers.push_back( aMarker );
322  } );
323 }
324 
325 
327 {
328  if( aSeverity < 0 )
329  return m_filteredMarkers.size();
330 
331  int count = 0;
332 
333  ERC_SETTINGS& settings = m_schematic->ErcSettings();
334 
335  visitMarkers(
336  [&]( SCH_MARKER* aMarker )
337  {
338  SEVERITY markerSeverity;
339 
340  if( aMarker->IsExcluded() )
341  markerSeverity = RPT_SEVERITY_EXCLUSION;
342  else
343  markerSeverity = settings.GetSeverity( aMarker->GetRCItem()->GetErrorCode() );
344 
345  if( markerSeverity == aSeverity )
346  count++;
347  } );
348 
349  return count;
350 }
351 
352 
353 std::shared_ptr<ERC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetERCItem( int aIndex )
354 {
355  SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
356 
357  return marker ? std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() ) : nullptr;
358 }
359 
360 
361 std::shared_ptr<RC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetItem( int aIndex )
362 {
363  return GetERCItem( aIndex );
364 }
365 
366 
367 void SHEETLIST_ERC_ITEMS_PROVIDER::DeleteItem( int aIndex, bool aDeep )
368 {
369  SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
370  m_filteredMarkers.erase( m_filteredMarkers.begin() + aIndex );
371 
372  if( aDeep )
373  {
374  SCH_SCREENS screens( m_schematic->Root() );
375  screens.DeleteMarker( marker );
376  }
377 }
378 
379 
380 void SHEETLIST_ERC_ITEMS_PROVIDER::DeleteAllItems( bool aIncludeExclusions, bool aDeep )
381 {
382  // Filtered list was already handled through DeleteItem() by the tree control
383 
384  if( aDeep )
385  {
386  SCH_SCREENS screens( m_schematic->Root() );
387  screens.DeleteAllMarkers( MARKER_BASE::MARKER_ERC, aIncludeExclusions );
388  }
389 }
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
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:170
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
#define NOD
Definition: erc_settings.h:96
SEVERITY
Definition: ui_common.h:83
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition: erc_item.h:50
#define WAR
A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
Definition: rc_item.h:72
#define OK
PIN_ERROR
The values a pin-to-pin entry in the pin matrix can take on.
Definition: erc_settings.h:84
void SetSeverity(int aErrorCode, SEVERITY aSeverity)
nlohmann::json json
Definition: gerbview.cpp:40
Library symbol changed from current symbol in schematic or the library symbol link no longer valid.
Definition: erc_settings.h:66
std::set< wxString > m_ErcExclusions
Definition: erc_settings.h:166
#define ERR
void ResetPinMap()
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:174
std::shared_ptr< RC_ITEM > GetItem(int aIndex) override
Retrieve a RC_ITEM by index.
void DeleteItem(int aIndex, bool aDeep) override
Remove (and optionally deletes) the indexed item from the list.
void visitMarkers(std::function< void(SCH_MARKER *)> aVisitor)
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
const int ercSettingsSchemaVersion
std::vector< SCH_MARKER * > m_filteredMarkers
Definition: erc_settings.h:187
void DeleteAllMarkers(enum MARKER_BASE::TYPEMARKER aMarkerType, bool aIncludeExclusions)
Delete all electronic rules check markers of aMarkerType from all the screens in the list.
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
2 labels are equal for case insensitive comparisons.
Definition: erc_settings.h:50
int GetCount(int aSeverity=-1) override
PIN_ERROR m_PinMap[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Definition: erc_settings.h:168
bool IsExcluded() const
Definition: marker_base.h:94
Container for ERC settings.
Definition: erc_settings.h:105
A wire connected to a bus doesn't match the bus.
Definition: erc_settings.h:57
void DeleteMarker(SCH_MARKER *aMarker)
Delete a specific marker.
A no connect symbol is not connected to anything.
Definition: erc_settings.h:48
#define NPI
Types of drive on a net (used for legacy ERC)
Definition: erc_settings.h:93
A global label is unique.
Definition: erc_settings.h:63
std::map< int, SEVERITY > m_Severities
Definition: erc_settings.h:165
A no connect symbol is connected to more than 1 pin.
Definition: erc_settings.h:47
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:111
ERC_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
SCH_SHEET & Root() const
Definition: schematic.h:116
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:47
const char * name
Definition: DXF_plotter.cpp:59
enum TYPEMARKER GetMarkerType() const
Definition: marker_base.h:92
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition: ui_common.cpp:36
void SetPinMapValue(int aFirstType, int aSecondType, PIN_ERROR aValue)
Definition: erc_settings.h:145
PIN_ERROR GetPinMapValue(int aFirstType, int aSecondType) const
Definition: erc_settings.h:133
void DeleteAllItems(bool aIncludeExclusions, bool aDeep) override
virtual ~ERC_SETTINGS()
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:132
#define ELECTRICAL_PINTYPES_TOTAL
Definition: pin_type.h:52
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
Conflicting drivers (labels, etc) on a subgraph.
Definition: erc_settings.h:56
std::shared_ptr< ERC_ITEM > GetERCItem(int aIndex)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:196
std::shared_ptr< RC_ITEM > GetRCItem() const
Definition: marker_base.h:100
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:534
void SetSeverities(int aSeverities) override
SEVERITY GetSeverity(int aErrorCode) const
#define DRV
Definition: erc_settings.h:94