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