KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_rule.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
21#include <board.h>
22#include <board_item.h>
23#include <api/api_enums.h>
24#include <api/board/board.pb.h>
25#include <api/board/board_rules.pb.h>
26#include <base_units.h>
27#include <drc/drc_rule.h>
29
30
31bool IsComponentClassSelector( const wxString& aToken )
32{
33 return aToken.Upper().StartsWith( wxT( "CLASS:" ) );
34}
35
36
38 m_Unary( false ),
40 m_ImplicitItem( nullptr ),
41 m_LayerCondition( LSET::AllLayersMask() ),
42 m_Condition( nullptr ),
45{
46}
47
48
49DRC_RULE::DRC_RULE( const wxString& aName ) :
50 m_Unary( false ),
52 m_ImplicitItem( nullptr ),
53 m_Name( aName ),
54 m_LayerCondition( LSET::AllLayersMask() ),
55 m_Condition( nullptr ),
58{
59}
60
61
63{
64 delete m_Condition;
65}
66
67
69{
70 aConstraint.SetParentRule( this );
71 m_Constraints.push_back( aConstraint );
72}
73
74
75std::optional<DRC_CONSTRAINT> DRC_RULE::FindConstraint( DRC_CONSTRAINT_T aType )
76{
78 {
79 if( c.m_Type == aType )
80 return c;
81 }
82
83 return std::optional<DRC_CONSTRAINT>();
84}
85
86
88{
89 // Classify by the items supplied to rule evaluation, not the geometry measured by the test.
90 switch( m_Type )
91 {
106 case SKEW_CONSTRAINT:
113 return true;
114
115 default:
116 return false;
117 }
118}
119
120
121wxString DRC_RULE::FormatRuleFromProto( const kiapi::board::CustomRule& aRule, wxString* aErrorText )
122{
123 if( aErrorText )
124 *aErrorText = wxEmptyString;
125
126 auto fail =
127 [&]( const wxString& aMessage ) -> wxString
128 {
129 if( aErrorText )
130 *aErrorText = aMessage;
131
132 return wxEmptyString;
133 };
134
135 auto escapeQuotedRuleText =
136 []( const wxString& aText ) -> wxString
137 {
138 wxString escaped = aText;
139 escaped.Replace( "\\", "\\\\" );
140 escaped.Replace( "\"", "\\\"" );
141 return escaped;
142 };
143
144 auto hasConstraintOption =
145 []( const kiapi::board::CustomRuleConstraint& aConstraint,
146 const kiapi::board::CustomRuleConstraintOption aOption ) -> bool
147 {
148 for( int optionValue : aConstraint.options() )
149 {
150 if( static_cast<kiapi::board::CustomRuleConstraintOption>( optionValue ) == aOption )
151 return true;
152 }
153
154 return false;
155 };
156
157 auto getConstraintToken =
158 []( const kiapi::board::CustomRuleConstraintType aType,
159 bool* aAddWithinDiffPairs ) -> std::optional<wxString>
160 {
161 if( aAddWithinDiffPairs )
162 *aAddWithinDiffPairs = false;
163
164 switch( aType )
165 {
166 case kiapi::board::CRCT_CLEARANCE: return wxS( "clearance" );
167 case kiapi::board::CRCT_CREEPAGE: return wxS( "creepage" );
168 case kiapi::board::CRCT_HOLE_CLEARANCE: return wxS( "hole_clearance" );
169 case kiapi::board::CRCT_HOLE_TO_HOLE: return wxS( "hole_to_hole" );
170 case kiapi::board::CRCT_EDGE_CLEARANCE: return wxS( "edge_clearance" );
171 case kiapi::board::CRCT_HOLE_SIZE: return wxS( "hole_size" );
172 case kiapi::board::CRCT_COURTYARD_CLEARANCE: return wxS( "courtyard_clearance" );
173 case kiapi::board::CRCT_SILK_CLEARANCE: return wxS( "silk_clearance" );
174 case kiapi::board::CRCT_TEXT_HEIGHT: return wxS( "text_height" );
175 case kiapi::board::CRCT_TEXT_THICKNESS: return wxS( "text_thickness" );
176 case kiapi::board::CRCT_TRACK_WIDTH: return wxS( "track_width" );
177 case kiapi::board::CRCT_TRACK_SEGMENT_LENGTH: return wxS( "track_segment_length" );
178 case kiapi::board::CRCT_ANNULAR_WIDTH: return wxS( "annular_width" );
179 case kiapi::board::CRCT_ZONE_CONNECTION: return wxS( "zone_connection" );
180 case kiapi::board::CRCT_THERMAL_RELIEF_GAP: return wxS( "thermal_relief_gap" );
181 case kiapi::board::CRCT_THERMAL_SPOKE_WIDTH: return wxS( "thermal_spoke_width" );
182 case kiapi::board::CRCT_MIN_RESOLVED_SPOKES: return wxS( "min_resolved_spokes" );
183 case kiapi::board::CRCT_SOLDER_MASK_EXPANSION: return wxS( "solder_mask_expansion" );
184 case kiapi::board::CRCT_SOLDER_PASTE_ABS_MARGIN: return wxS( "solder_paste_abs_margin" );
185 case kiapi::board::CRCT_SOLDER_PASTE_REL_MARGIN: return wxS( "solder_paste_rel_margin" );
186 case kiapi::board::CRCT_DISALLOW: return wxS( "disallow" );
187 case kiapi::board::CRCT_VIA_DIAMETER: return wxS( "via_diameter" );
188 case kiapi::board::CRCT_LENGTH: return wxS( "length" );
189 case kiapi::board::CRCT_SKEW: return wxS( "skew" );
190 case kiapi::board::CRCT_DIFF_PAIR_GAP: return wxS( "diff_pair_gap" );
191 case kiapi::board::CRCT_MAX_UNCOUPLED: return wxS( "diff_pair_uncoupled" );
192 case kiapi::board::CRCT_DIFF_PAIR_INTRA_SKEW:
193 {
194 if( aAddWithinDiffPairs )
195 *aAddWithinDiffPairs = true;
196
197 return wxS( "skew" );
198 }
199 case kiapi::board::CRCT_VIA_COUNT: return wxS( "via_count" );
200 case kiapi::board::CRCT_PHYSICAL_CLEARANCE: return wxS( "physical_clearance" );
201 case kiapi::board::CRCT_PHYSICAL_HOLE_CLEARANCE: return wxS( "physical_hole_clearance" );
202 case kiapi::board::CRCT_ASSERTION: return wxS( "assertion" );
203 case kiapi::board::CRCT_CONNECTION_WIDTH: return wxS( "connection_width" );
204 case kiapi::board::CRCT_TRACK_ANGLE: return wxS( "track_angle" );
205 case kiapi::board::CRCT_VIA_DANGLING: return wxS( "via_dangling" );
206 case kiapi::board::CRCT_BRIDGED_MASK: return wxS( "bridged_mask" );
207 case kiapi::board::CRCT_SOLDER_MASK_SLIVER: return wxS( "solder_mask_sliver" );
208
209 case kiapi::board::CRCT_UNKNOWN:
210 default:
211 return std::nullopt;
212 }
213 };
214
215 auto formatNumericValueForConstraint =
216 [&]( const kiapi::board::CustomRuleConstraint& aConstraint,
217 const int aValue ) -> wxString
218 {
219 if( aConstraint.type() == kiapi::board::CRCT_TRACK_ANGLE )
220 return wxString::Format( "%ddeg", aValue );
221
222 switch( aConstraint.type() )
223 {
224 case kiapi::board::CRCT_VIA_COUNT:
225 case kiapi::board::CRCT_MIN_RESOLVED_SPOKES:
226 case kiapi::board::CRCT_VIA_DANGLING:
227 case kiapi::board::CRCT_BRIDGED_MASK:
228 return wxString::Format( "%d", aValue );
229
230 default:
231 break;
232 }
233
234 if( hasConstraintOption( aConstraint, kiapi::board::CRCO_TIME_DOMAIN ) )
235 return wxString::Format( "%dps", aValue );
236
237 wxString formatted = wxString::FromUTF8( EDA_UNIT_UTILS::FormatInternalUnits( pcbIUScale, aValue ) );
238 return formatted + "mm";
239 };
240
241 auto severityToken =
242 []( const kiapi::common::types::RuleSeverity aSeverity ) -> wxString
243 {
244 switch( aSeverity )
245 {
246 case kiapi::common::types::RuleSeverity::RS_WARNING: return wxS( "warning" );
247 case kiapi::common::types::RuleSeverity::RS_ERROR: return wxS( "error" );
248 case kiapi::common::types::RuleSeverity::RS_EXCLUSION: return wxS( "exclusion" );
249 case kiapi::common::types::RuleSeverity::RS_IGNORE: return wxS( "ignore" );
250
251 default:
252 return wxEmptyString;
253 }
254 };
255
256 if( aRule.name().empty() )
257 return fail( wxS( "Rules must have a name" ) );
258
259 wxString ruleText;
260 ruleText << "(rule \"" << escapeQuotedRuleText( wxString::FromUTF8( aRule.name() ) ) << "\"\n";
261
262 if( aRule.has_comments() && !aRule.comments().empty() )
263 {
264 wxArrayString commentLines = wxSplit( wxString::FromUTF8( aRule.comments() ), '\n', '\0' );
265
266 for( const wxString& line : commentLines )
267 ruleText << "\t# " << line << "\n";
268 }
269
270 if( !aRule.condition().empty() )
271 {
272 ruleText << "\t(condition \""
273 << escapeQuotedRuleText( wxString::FromUTF8( aRule.condition() ) )
274 << "\")\n";
275 }
276
277 if( aRule.layer_condition_case() == kiapi::board::CustomRule::LayerConditionCase::kLayerMode )
278 {
279 switch( aRule.layer_mode() )
280 {
281 case kiapi::board::CRLM_OUTER:
282 ruleText << "\t(layer outer)\n";
283 break;
284
285 case kiapi::board::CRLM_INNER:
286 ruleText << "\t(layer inner)\n";
287 break;
288
289 case kiapi::board::CRLM_UNKNOWN:
290 default:
291 break;
292 }
293 }
294 else if( aRule.layer_condition_case() == kiapi::board::CustomRule::LayerConditionCase::kSingleLayer )
295 {
296 PCB_LAYER_ID layer =
298
299 if( layer == UNDEFINED_LAYER || layer == UNSELECTED_LAYER )
300 return fail( wxS( "Invalid single_layer value in custom rule" ) );
301
302 ruleText << "\t(layer \"" << BOARD::GetStandardLayerName( layer ) << "\")\n";
303 }
304
305 for( const kiapi::board::CustomRuleConstraint& constraint : aRule.constraints() )
306 {
307 bool addWithinDiffPairs = false;
308 std::optional<wxString> token = getConstraintToken( constraint.type(), &addWithinDiffPairs );
309
310 if( !token )
311 return fail( wxS( "Unsupported custom rule constraint type" ) );
312
313 wxString text = wxS( "\t(constraint " ) + *token;
314
315 if( constraint.has_name() && !constraint.name().empty() )
316 {
317 text += wxS( " (name \"" )
318 + escapeQuotedRuleText( wxString::FromUTF8( constraint.name() ) )
319 + wxS( "\")" );
320 }
321
322 if( constraint.value_case() == kiapi::board::CustomRuleConstraint::ValueCase::kDisallow )
323 {
324 for( int disallowTypeValue : constraint.disallow().types() )
325 {
326 kiapi::board::CustomRuleDisallowType disallowType =
327 static_cast<kiapi::board::CustomRuleDisallowType>( disallowTypeValue );
328
329 switch( disallowType )
330 {
331 case kiapi::board::CRDT_THROUGH_VIAS: text += wxS( " through_via" ); break;
332 case kiapi::board::CRDT_MICRO_VIAS: text += wxS( " micro_via" ); break;
333 case kiapi::board::CRDT_BLIND_VIAS: text += wxS( " blind_via" ); break;
334 case kiapi::board::CRDT_BURIED_VIAS: text += wxS( " buried_via" ); break;
335 case kiapi::board::CRDT_TRACKS: text += wxS( " track" ); break;
336 case kiapi::board::CRDT_PADS: text += wxS( " pad" ); break;
337 case kiapi::board::CRDT_ZONES: text += wxS( " zone" ); break;
338 case kiapi::board::CRDT_TEXTS: text += wxS( " text" ); break;
339 case kiapi::board::CRDT_GRAPHICS: text += wxS( " graphic" ); break;
340 case kiapi::board::CRDT_HOLES: text += wxS( " hole" ); break;
341 case kiapi::board::CRDT_FOOTPRINTS: text += wxS( " footprint" ); break;
342
343 case kiapi::board::CRDT_UNKNOWN:
344 default:
345 break;
346 }
347 }
348 }
349 else if( constraint.value_case() == kiapi::board::CustomRuleConstraint::ValueCase::kZoneConnection )
350 {
351 switch( constraint.zone_connection() )
352 {
353 case kiapi::board::types::ZCS_FULL:
354 text += wxS( " solid" );
355 break;
356
357 case kiapi::board::types::ZCS_THERMAL:
358 text += wxS( " thermal_reliefs" );
359 break;
360
361 case kiapi::board::types::ZCS_NONE:
362 text += wxS( " none" );
363 break;
364
365 default:
366 return fail( wxS( "Unsupported zone connection style" ) );
367 }
368 }
369 else if( constraint.value_case() == kiapi::board::CustomRuleConstraint::ValueCase::kAssertionExpression )
370 {
371 text += wxS( " \"" )
372 + escapeQuotedRuleText( wxString::FromUTF8( constraint.assertion_expression() ) )
373 + wxS( "\"" );
374 }
375 else if( constraint.value_case() == kiapi::board::CustomRuleConstraint::ValueCase::kNumeric )
376 {
377 const kiapi::common::types::MinOptMax& numeric = constraint.numeric();
378
379 if( numeric.has_min() )
380 text += wxS( " (min " )
381 + formatNumericValueForConstraint( constraint, numeric.min() )
382 + wxS( ")" );
383
384 if( numeric.has_opt() )
385 text += wxS( " (opt " )
386 + formatNumericValueForConstraint( constraint, numeric.opt() )
387 + wxS( ")" );
388
389 if( numeric.has_max() )
390 text += wxS( " (max " )
391 + formatNumericValueForConstraint( constraint, numeric.max() )
392 + wxS( ")" );
393 }
394
395 if( addWithinDiffPairs
396 || hasConstraintOption( constraint, kiapi::board::CRCO_SKEW_WITHIN_DIFF_PAIRS ) )
397 {
398 text += wxS( " (within_diff_pairs)" );
399 }
400
401 text += wxS( ")" );
402 ruleText << text << "\n";
403 }
404
405 wxString severity = severityToken( aRule.severity() );
406
407 if( !severity.IsEmpty() )
408 ruleText << "\t(severity " << severity << ")\n";
409
410 ruleText << ")\n";
411 return ruleText;
412}
413
414
415void DRC_CONSTRAINT::ToProto( kiapi::board::CustomRuleConstraint& aProto ) const
416{
418
419 if( !m_name.IsEmpty() )
420 aProto.set_name( m_name.ToUTF8() );
421
422 for( size_t ii = 0; ii < static_cast<size_t>( OPTIONS::NUM_OPTIONS ); ++ii )
423 {
424 OPTIONS option = static_cast<OPTIONS>( ii );
425
426 if( GetOption( option ) )
428 }
429
431 {
432 kiapi::board::CustomRuleDisallowSettings* disallow = aProto.mutable_disallow();
433
435 {
436 if( m_DisallowFlags & flag )
437 {
439 static_cast<DRC_DISALLOW_T>( flag ) ) );
440 }
441 }
442 }
444 {
445 aProto.set_zone_connection(
447 }
448 else if( m_Type == ASSERTION_CONSTRAINT )
449 {
450 if( m_Test )
451 aProto.set_assertion_expression( m_Test->GetExpression().ToUTF8() );
452 }
453 else
454 {
455 kiapi::common::types::MinOptMax* numeric = aProto.mutable_numeric();
456
457 if( m_Value.HasMin() )
458 numeric->set_min( m_Value.Min() );
459
460 if( m_Value.HasOpt() )
461 numeric->set_opt( m_Value.Opt() );
462
463 if( m_Value.HasMax() )
464 numeric->set_max( m_Value.Max() );
465 }
466}
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:55
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition board.h:1112
DRC_RULE_CONDITION * m_Test
Definition drc_rule.h:247
void ToProto(kiapi::board::CustomRuleConstraint &aProto) const
Definition drc_rule.cpp:415
int m_DisallowFlags
Definition drc_rule.h:245
void SetParentRule(DRC_RULE *aParentRule)
Definition drc_rule.h:203
ZONE_CONNECTION m_ZoneConnection
Definition drc_rule.h:246
MINOPTMAX< int > m_Value
Definition drc_rule.h:244
DRC_CONSTRAINT_T m_Type
Definition drc_rule.h:243
bool IsUnary() const
Definition drc_rule.cpp:87
wxString m_name
Definition drc_rule.h:261
bool GetOption(OPTIONS option) const
Definition drc_rule.h:233
SEVERITY m_Severity
Definition drc_rule.h:160
bool m_Unary
Definition drc_rule.h:152
DRC_RULE_CONDITION * m_Condition
Definition drc_rule.h:158
LSET m_LayerCondition
Definition drc_rule.h:157
static wxString FormatRuleFromProto(const kiapi::board::CustomRule &aRule, wxString *aErrorText=nullptr)
Definition drc_rule.cpp:121
KIID m_ImplicitItemId
Definition drc_rule.h:153
std::vector< DRC_CONSTRAINT > m_Constraints
Definition drc_rule.h:159
BOARD_ITEM * m_ImplicitItem
Definition drc_rule.h:154
wxString m_Name
Definition drc_rule.h:155
void AddConstraint(DRC_CONSTRAINT &aConstraint)
Definition drc_rule.cpp:68
virtual ~DRC_RULE()
Definition drc_rule.cpp:62
std::optional< DRC_CONSTRAINT > FindConstraint(DRC_CONSTRAINT_T aType)
Definition drc_rule.cpp:75
DRC_IMPLICIT_SOURCE m_implicitSource
Definition drc_rule.h:163
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
bool IsComponentClassSelector(const wxString &aToken)
${Class:X} inside a DRC rule is a component-class selector consumed by testFootprintSelector(),...
Definition drc_rule.cpp:31
DRC_IMPLICIT_SOURCE
Definition drc_rule.h:112
DRC_DISALLOW_T
Definition drc_rule.h:96
@ DRC_DISALLOW_THROUGH_VIAS
Definition drc_rule.h:97
@ DRC_DISALLOW_FOOTPRINTS
Definition drc_rule.h:107
DRC_CONSTRAINT_T
Definition drc_rule.h:49
@ ANNULAR_WIDTH_CONSTRAINT
Definition drc_rule.h:63
@ BRIDGED_MASK_CONSTRAINT
Definition drc_rule.h:88
@ VIA_DIAMETER_CONSTRAINT
Definition drc_rule.h:72
@ ZONE_CONNECTION_CONSTRAINT
Definition drc_rule.h:64
@ NET_CHAIN_LENGTH_CONSTRAINT
Definition drc_rule.h:74
@ VIA_DANGLING_CONSTRAINT
Definition drc_rule.h:87
@ NET_CHAIN_STUB_LENGTH_CONSTRAINT
Definition drc_rule.h:75
@ DISALLOW_CONSTRAINT
Definition drc_rule.h:71
@ TRACK_SEGMENT_LENGTH_CONSTRAINT
Definition drc_rule.h:62
@ TEXT_THICKNESS_CONSTRAINT
Definition drc_rule.h:60
@ LENGTH_CONSTRAINT
Definition drc_rule.h:73
@ VIA_COUNT_CONSTRAINT
Definition drc_rule.h:81
@ NET_CHAIN_RETURN_PATH_CONSTRAINT
Definition drc_rule.h:76
@ MICROVIA_ASPECT_RATIO_CONSTRAINT
Definition drc_rule.h:91
@ ASSERTION_CONSTRAINT
Definition drc_rule.h:84
@ SKEW_CONSTRAINT
Definition drc_rule.h:77
@ MICROVIA_STACK_DEPTH_CONSTRAINT
Definition drc_rule.h:90
@ SOLDER_PASTE_ABS_MARGIN_CONSTRAINT
Definition drc_rule.h:69
@ SOLDER_MASK_EXPANSION_CONSTRAINT
Definition drc_rule.h:68
@ HOLE_SIZE_CONSTRAINT
Definition drc_rule.h:56
@ TEXT_HEIGHT_CONSTRAINT
Definition drc_rule.h:59
@ SOLDER_PASTE_REL_MARGIN_CONSTRAINT
Definition drc_rule.h:70
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNSELECTED_LAYER
Definition layer_ids.h:58
@ UNDEFINED_LAYER
Definition layer_ids.h:57
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
Converts aValue from internal units to a string appropriate for writing to file.
@ RPT_SEVERITY_UNDEFINED