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, UnS, PwrI, PwrO, OC, OE, NC */
43 /* I */ { OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
44 /* O */ { OK, ERR, OK, WAR, OK, WAR, OK, ERR, ERR, ERR, ERR },
45 /* Bi*/ { OK, OK, OK, OK, OK, WAR, OK, WAR, OK, WAR, ERR },
46 /* 3S*/ { OK, WAR, OK, OK, OK, WAR, WAR, ERR, WAR, WAR, ERR },
47 /*Pas*/ { OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
48 /*UnS */ { WAR, WAR, WAR, WAR, WAR, WAR, WAR, WAR, WAR, WAR, ERR },
49 /*PwrI*/ { OK, OK, OK, WAR, OK, WAR, OK, OK, OK, OK, ERR },
50 /*PwrO*/ { OK, ERR, WAR, ERR, OK, WAR, OK, ERR, ERR, ERR, ERR },
51 /* OC */ { OK, ERR, OK, WAR, OK, WAR, OK, ERR, OK, OK, ERR },
52 /* OE */ { OK, ERR, WAR, WAR, OK, WAR, OK, ERR, OK, OK, ERR },
53 /* NC */ { ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR }
54  };
55 
56 
68  {
69 /* In Out, Bi, 3S, Pas, UnS, PwrI,PwrO,OC, OE, NC */
70 /* In*/ { NOD, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
71 /*Out*/ { DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, NPI },
72 /* Bi*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
73 /* 3S*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
74 /*Pas*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
75 /*UnS*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
76 /*PwrI*/ { NOD, DRV, NOD, NOD, NOD, NOD, NOD, DRV, NOD, NOD, NPI },
77 /*PwrO*/ { DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, NPI },
78 /* OC*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
79 /* OE*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
80 /* NC*/ { NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI }
81  };
82 
83 
84 ERC_SETTINGS::ERC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
85  NESTED_SETTINGS( "erc", ercSettingsSchemaVersion, aParent, aPath )
86 {
87  ResetPinMap();
88 
89  for( int i = ERCE_FIRST; i <= ERCE_LAST; ++i )
91 
92  // Error is the default setting so set non-error priorities here.
102 
103  m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "rule_severities",
104  [&]() -> nlohmann::json
105  {
106  nlohmann::json ret = {};
107 
108  for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
109  {
110  int code = item.GetErrorCode();
111 
112  if( !m_Severities.count( code ) )
113  continue;
114 
115  wxString name = item.GetSettingsKey();
116 
117  ret[std::string( name.ToUTF8() )] =
118  SeverityToString( static_cast<SEVERITY>( 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 int 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, int aSeverity )
258 {
259  m_Severities[ aErrorCode ] = aSeverity;
260 }
261 
262 
264 {
265  memcpy( m_PinMap, m_defaultPinMap, sizeof( m_PinMap ) );
266 }
267 
268 
270 {
271  m_severities = aSeverities;
272 
273  m_filteredMarkers.clear();
274 
275  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
276  ERC_SETTINGS& settings = m_schematic->ErcSettings();
277 
278  for( unsigned i = 0; i < sheetList.size(); i++ )
279  {
280  for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
281  {
282  SCH_MARKER* marker = static_cast<SCH_MARKER*>( aItem );
283  int markerSeverity;
284 
285  if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
286  continue;
287 
288  if( marker->IsExcluded() )
289  markerSeverity = RPT_SEVERITY_EXCLUSION;
290  else
291  markerSeverity = settings.GetSeverity( marker->GetRCItem()->GetErrorCode() );
292 
293  if( markerSeverity & m_severities )
294  m_filteredMarkers.push_back( marker );
295  }
296  }
297 }
298 
299 
301 {
302  if( aSeverity < 0 )
303  return m_filteredMarkers.size();
304 
305  int count = 0;
306 
307  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
308  ERC_SETTINGS& settings = m_schematic->ErcSettings();
309 
310  for( unsigned i = 0; i < sheetList.size(); i++ )
311  {
312  for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
313  {
314  SCH_MARKER* marker = static_cast<SCH_MARKER*>( aItem );
315  int markerSeverity;
316 
317  if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
318  continue;
319 
320  if( marker->IsExcluded() )
321  markerSeverity = RPT_SEVERITY_EXCLUSION;
322  else
323  markerSeverity = settings.GetSeverity( marker->GetRCItem()->GetErrorCode() );
324 
325  if( markerSeverity == aSeverity )
326  count++;
327  }
328  }
329 
330  return count;
331 }
332 
333 
334 std::shared_ptr<ERC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetERCItem( int aIndex )
335 {
336  SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
337 
338  return marker ? std::static_pointer_cast<ERC_ITEM>( marker->GetRCItem() ) : nullptr;
339 }
340 
341 
342 std::shared_ptr<RC_ITEM> SHEETLIST_ERC_ITEMS_PROVIDER::GetItem( int aIndex )
343 {
344  return GetERCItem( aIndex );
345 }
346 
347 
348 void SHEETLIST_ERC_ITEMS_PROVIDER::DeleteItem( int aIndex, bool aDeep )
349 {
350  SCH_MARKER* marker = m_filteredMarkers[ aIndex ];
351  m_filteredMarkers.erase( m_filteredMarkers.begin() + aIndex );
352 
353  if( aDeep )
354  {
355  SCH_SCREENS screens( m_schematic->Root() );
356  screens.DeleteMarker( marker );
357  }
358 }
359 
360 
361 void SHEETLIST_ERC_ITEMS_PROVIDER::DeleteAllItems( bool aIncludeExclusions, bool aDeep )
362 {
363  // Filtered list was already handled through DeleteItem() by the tree control
364 
365  if( aDeep )
366  {
367  SCH_SCREENS screens( m_schematic->Root() );
368  screens.DeleteAllMarkers( MARKER_BASE::MARKER_ERC, aIncludeExclusions );
369  }
370 }
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:162
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:97
#define NOD
Definition: erc_settings.h:88
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:76
nlohmann::json json
Definition: gerbview.cpp:39
Library symbol changed from current symbol in schematic or the library symbol link no longer valid.
Definition: erc_settings.h:65
std::set< wxString > m_ErcExclusions
Definition: erc_settings.h:158
#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:166
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.
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
const int ercSettingsSchemaVersion
void SetSeverity(int aErrorCode, int aSeverity)
std::vector< SCH_MARKER * > m_filteredMarkers
Definition: erc_settings.h:178
int GetSeverity(int aErrorCode) const
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:49
int GetCount(int aSeverity=-1) override
PIN_ERROR m_PinMap[ELECTRICAL_PINTYPES_TOTAL][ELECTRICAL_PINTYPES_TOTAL]
Definition: erc_settings.h:160
bool IsExcluded() const
Definition: marker_base.h:109
std::map< int, int > m_Severities
Definition: erc_settings.h:157
Container for ERC settings.
Definition: erc_settings.h:97
A wire connected to a bus doesn't match the bus.
Definition: erc_settings.h:56
void DeleteMarker(SCH_MARKER *aMarker)
Delete a specific marker.
A no connect symbol is not connected to anything.
Definition: erc_settings.h:47
#define NPI
Types of drive on a net (used for legacy ERC)
Definition: erc_settings.h:85
A global label is unique.
Definition: erc_settings.h:62
A no connect symbol is connected to more than 1 pin.
Definition: erc_settings.h:46
ERC_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
SCH_SHEET & Root() const
Definition: schematic.h:102
wxString SeverityToString(const SEVERITY &aSeverity)
Definition: ui_common.cpp:46
const char * name
Definition: DXF_plotter.cpp:59
enum TYPEMARKER GetMarkerType() const
Definition: marker_base.h:107
SEVERITY SeverityFromString(const wxString &aSeverity)
Definition: ui_common.cpp:35
void SetPinMapValue(int aFirstType, int aSecondType, PIN_ERROR aValue)
Definition: erc_settings.h:137
PIN_ERROR GetPinMapValue(int aFirstType, int aSecondType) const
Definition: erc_settings.h:125
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:51
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:55
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:194
std::shared_ptr< RC_ITEM > GetRCItem() const
Function GetReporter returns the DRC_ITEM held within this MARKER so that its interface may be used.
Definition: marker_base.h:120
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:521
void SetSeverities(int aSeverities) override
#define DRV
Definition: erc_settings.h:86