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