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