KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_rule_loader.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) 2024 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#include "drc_re_rule_loader.h"
21
22#include <reporter.h>
24#include <drc/drc_rule_parser.h>
26#include <wx/ffile.h>
27
42
43
47
48
49double DRC_RULE_LOADER::toMM( int aValue )
50{
51 return aValue / 1000000.0;
52}
53
54
56{
57 for( const DRC_CONSTRAINT& constraint : aRule.m_Constraints )
58 {
59 if( constraint.m_Type == aType )
60 return &constraint;
61 }
62
63 return nullptr;
64}
65
66
67static bool isSymmetricMinOptMax( const DRC_CONSTRAINT* aConstraint )
68{
69 if( !aConstraint )
70 return true;
71
72 const auto& value = aConstraint->GetValue();
73
74 if( !value.HasMin() || !value.HasOpt() || !value.HasMax() )
75 return true;
76
77 return ( value.Opt() - value.Min() ) == ( value.Max() - value.Opt() );
78}
79
80
81static std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA> makeCustomRuleData( const DRC_RULE& aRule )
82{
83 auto customData = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
84 customData->SetRuleName( aRule.m_Name );
85 return customData;
86}
87
88
89wxString DRC_RULE_LOADER::ExtractRuleBody( const wxString& aOriginalText )
90{
91 int ruleKeyword = aOriginalText.Find( wxS( "rule " ) );
92 if( ruleKeyword == wxNOT_FOUND )
93 return aOriginalText;
94
95 int bodyStart = aOriginalText.find( '(', ruleKeyword + 5 );
96 if( bodyStart == (int) wxString::npos )
97 return aOriginalText;
98
99 wxString body = aOriginalText.Mid( bodyStart );
100 body.Trim( true );
101
102 if( body.EndsWith( wxS( ")" ) ) )
103 body = body.Left( body.Length() - 1 );
104 body.Trim( true );
105
106 return body;
107}
108
109
110wxString DRC_RULE_LOADER::ExtractRuleComment( const wxString& aOriginalText )
111{
112 wxString comment;
113 wxArrayString lines = wxSplit( aOriginalText, '\n', '\0' );
114
115 for( const wxString& line : lines )
116 {
117 wxString trimmed = line;
118 trimmed.Trim( false );
119
120 if( trimmed.StartsWith( wxS( "#" ) ) )
121 {
122 wxString commentLine = trimmed.Mid( 1 );
123 commentLine.Trim( false );
124
125 if( !comment.IsEmpty() )
126 comment += wxS( "\n" );
127
128 comment += commentLine;
129 }
130 }
131
132 return comment;
133}
134
135
136wxString DRC_RULE_LOADER::cleanStrippedCondition( const wxString& aCondition )
137{
138 wxString cleaned = aCondition;
139
140 // Strip empty parentheses left over from removing conditions
141 wxString prev;
142 do
143 {
144 prev = cleaned;
145 cleaned.Replace( wxS( "()" ), wxS( "" ) );
146 } while( cleaned != prev );
147
148 cleaned.Replace( wxS( "&& &&" ), wxS( "&&" ) );
149 cleaned.Replace( wxS( "|| ||" ), wxS( "||" ) );
150 cleaned.Trim( true ).Trim( false );
151 if( cleaned.StartsWith( wxS( "&&" ) ) )
152 cleaned = cleaned.Mid( 2 ).Trim( false );
153 if( cleaned.EndsWith( wxS( "&&" ) ) )
154 cleaned = cleaned.Left( cleaned.Length() - 2 ).Trim( true );
155 if( cleaned.StartsWith( wxS( "||" ) ) )
156 cleaned = cleaned.Mid( 2 ).Trim( false );
157 if( cleaned.EndsWith( wxS( "||" ) ) )
158 cleaned = cleaned.Left( cleaned.Length() - 2 ).Trim( true );
159
160 return cleaned;
161}
162
163
164std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>
166 const DRC_RULE& aRule,
167 const std::set<DRC_CONSTRAINT_T>& aClaimedConstraints )
168{
169 switch( aPanel )
170 {
171 case VIA_STYLE:
172 {
173 auto data = std::make_shared<DRC_RE_VIA_STYLE_CONSTRAINT_DATA>();
174 data->SetRuleName( aRule.m_Name );
175 data->SetConstraintCode( "via_style" );
176
177 const DRC_CONSTRAINT* viaDia = findConstraint( aRule, VIA_DIAMETER_CONSTRAINT );
178 const DRC_CONSTRAINT* holeSize = findConstraint( aRule, HOLE_SIZE_CONSTRAINT );
179
180 if( viaDia )
181 {
182 data->SetMinViaDiameter( toMM( viaDia->GetValue().Min() ) );
183 data->SetMaxViaDiameter( toMM( viaDia->GetValue().Max() ) );
184 }
185
186 if( holeSize )
187 {
188 data->SetMinViaHoleSize( toMM( holeSize->GetValue().Min() ) );
189 data->SetMaxViaHoleSize( toMM( holeSize->GetValue().Max() ) );
190 }
191
192 if( aRule.m_Condition )
193 {
194 wxString expr = aRule.m_Condition->GetExpression();
195
196 if( expr.Contains( wxS( "'Micro'" ) ) )
197 data->SetViaType( VIA_STYLE_TYPE::MICRO );
198 else if( expr.Contains( wxS( "'Through'" ) ) )
199 data->SetViaType( VIA_STYLE_TYPE::THROUGH );
200 else if( expr.Contains( wxS( "'Blind'" ) ) )
201 data->SetViaType( VIA_STYLE_TYPE::BLIND );
202 else if( expr.Contains( wxS( "'Buried'" ) ) )
203 data->SetViaType( VIA_STYLE_TYPE::BURIED );
204
205 // Strip the via type condition so it doesn't duplicate on save
206 wxString cleanedCondition = expr;
207 cleanedCondition.Replace( wxS( "A.Via_Type == 'Micro'" ), wxS( "" ) );
208 cleanedCondition.Replace( wxS( "A.Via_Type == 'Through'" ), wxS( "" ) );
209 cleanedCondition.Replace( wxS( "A.Via_Type == 'Blind'" ), wxS( "" ) );
210 cleanedCondition.Replace( wxS( "A.Via_Type == 'Buried'" ), wxS( "" ) );
211
212 data->SetRuleCondition( cleanStrippedCondition( cleanedCondition ) );
213 }
214
215 return data;
216 }
217
219 {
220 const DRC_CONSTRAINT* trackWidth = findConstraint( aRule, TRACK_WIDTH_CONSTRAINT );
221 const DRC_CONSTRAINT* diffGap = findConstraint( aRule, DIFF_PAIR_GAP_CONSTRAINT );
222 const DRC_CONSTRAINT* uncoupled = findConstraint( aRule, MAX_UNCOUPLED_CONSTRAINT );
223
224 if( !isSymmetricMinOptMax( trackWidth ) || !isSymmetricMinOptMax( diffGap ) )
225 {
226 return makeCustomRuleData( aRule );
227 }
228
229 auto data = std::make_shared<DRC_RE_ROUTING_DIFF_PAIR_CONSTRAINT_DATA>();
230 data->SetRuleName( aRule.m_Name );
231 data->SetConstraintCode( "diff_pair_gap" );
232
233 if( trackWidth )
234 {
235 double opt = toMM( trackWidth->GetValue().PinnedOpt() );
236 double min = toMM( trackWidth->GetValue().Min() );
237
238 data->SetOptWidth( opt );
239 data->SetWidthTolerance( opt - min );
240 }
241
242 if( diffGap )
243 {
244 double opt = toMM( diffGap->GetValue().PinnedOpt() );
245 double min = toMM( diffGap->GetValue().Min() );
246
247 data->SetOptGap( opt );
248 data->SetGapTolerance( opt - min );
249 }
250
251 if( uncoupled )
252 {
253 data->SetMaxUncoupledLength( toMM( uncoupled->GetValue().Max() ) );
254 }
255
256 return data;
257 }
258
260 {
261 auto data = std::make_shared<DRC_RE_MINIMUM_TEXT_HEIGHT_THICKNESS_CONSTRAINT_DATA>();
262 data->SetRuleName( aRule.m_Name );
263 data->SetConstraintCode( "text_height" );
264
265 const DRC_CONSTRAINT* textHeight = findConstraint( aRule, TEXT_HEIGHT_CONSTRAINT );
266 const DRC_CONSTRAINT* textThickness = findConstraint( aRule, TEXT_THICKNESS_CONSTRAINT );
267
268 if( textHeight )
269 data->SetMinTextHeight( toMM( textHeight->GetValue().Min() ) );
270
271 if( textThickness )
272 data->SetMinTextThickness( toMM( textThickness->GetValue().Min() ) );
273
274 return data;
275 }
276
277 case ROUTING_WIDTH:
278 {
279 const DRC_CONSTRAINT* trackWidth = findConstraint( aRule, TRACK_WIDTH_CONSTRAINT );
280
281 if( !isSymmetricMinOptMax( trackWidth ) )
282 return makeCustomRuleData( aRule );
283
284 auto data = std::make_shared<DRC_RE_ROUTING_WIDTH_CONSTRAINT_DATA>();
285 data->SetRuleName( aRule.m_Name );
286 data->SetConstraintCode( "track_width" );
287
288 if( trackWidth )
289 {
290 double opt = toMM( trackWidth->GetValue().PinnedOpt() );
291 double min = toMM( trackWidth->GetValue().Min() );
292
293 data->SetOptWidth( opt );
294 data->SetWidthTolerance( opt - min );
295 }
296
297 return data;
298 }
299
300 case ABSOLUTE_LENGTH:
301 {
302 const DRC_CONSTRAINT* length = findConstraint( aRule, LENGTH_CONSTRAINT );
303
304 if( !isSymmetricMinOptMax( length ) )
305 return makeCustomRuleData( aRule );
306
307 auto data = std::make_shared<DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA>();
308 data->SetRuleName( aRule.m_Name );
309 data->SetConstraintCode( "length" );
310
311 if( length )
312 {
313 double minMM = toMM( length->GetValue().Min() );
314 double optMM = toMM( length->GetValue().PinnedOpt() );
315 double maxMM = toMM( length->GetValue().Max() );
316
317 data->SetOptimumLength( optMM );
318 data->SetTolerance( ( maxMM - minMM ) / 2.0 );
319 }
320
321 return data;
322 }
323
325 {
326 const DRC_CONSTRAINT* length = findConstraint( aRule, LENGTH_CONSTRAINT );
327
328 if( !isSymmetricMinOptMax( length ) )
329 return makeCustomRuleData( aRule );
330
331 auto data = std::make_shared<DRC_RE_MATCHED_LENGTH_DIFF_PAIR_CONSTRAINT_DATA>();
332 data->SetRuleName( aRule.m_Name );
333 data->SetConstraintCode( "length" );
334
335 if( length )
336 {
337 double minMM = toMM( length->GetValue().Min() );
338 double optMM = toMM( length->GetValue().PinnedOpt() );
339 double maxMM = toMM( length->GetValue().Max() );
340
341 data->SetOptimumLength( optMM );
342 data->SetTolerance( ( maxMM - minMM ) / 2.0 );
343 }
344
345 const DRC_CONSTRAINT* skew = findConstraint( aRule, SKEW_CONSTRAINT );
346
347 if( skew )
348 {
349 data->SetMaxSkew( toMM( skew->GetValue().Max() ) );
350 data->SetWithinDiffPairs( skew->GetOption( DRC_CONSTRAINT::OPTIONS::SKEW_WITHIN_DIFF_PAIRS ) );
351 }
352
353 return data;
354 }
355
356 case PERMITTED_LAYERS:
357 {
358 auto data = std::make_shared<DRC_RE_PERMITTED_LAYERS_CONSTRAINT_DATA>();
359 data->SetRuleName( aRule.m_Name );
360
361 const DRC_CONSTRAINT* constraint = findConstraint( aRule, ASSERTION_CONSTRAINT );
362
363 if( constraint && constraint->m_Test )
364 {
365 wxString expr = constraint->m_Test->GetExpression();
366
367 data->SetTopLayerEnabled( expr.Contains( wxS( "F.Cu" ) ) );
368 data->SetBottomLayerEnabled( expr.Contains( wxS( "B.Cu" ) ) );
369
370 // Check for layer references the panel can't represent
371 wxString remaining = expr;
372 remaining.Replace( wxS( "A.Layer == 'F.Cu'" ), wxS( "" ) );
373 remaining.Replace( wxS( "A.Layer == 'B.Cu'" ), wxS( "" ) );
374
375 if( remaining.Contains( wxS( "A.Layer" ) ) )
376 {
377 // Inner or non-standard layers — fall back to custom rule
378 auto customData = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
379 customData->SetRuleName( aRule.m_Name );
380 return customData;
381 }
382 }
383
384 return data;
385 }
386
388 {
389 auto data = std::make_shared<DRC_RE_ALLOWED_ORIENTATION_CONSTRAINT_DATA>();
390 data->SetRuleName( aRule.m_Name );
391 data->SetConstraintCode( wxS( "allowed_orientation" ) );
392
393 const DRC_CONSTRAINT* constraint = findConstraint( aRule, ASSERTION_CONSTRAINT );
394
395 if( constraint && constraint->m_Test )
396 {
397 wxString expr = constraint->m_Test->GetExpression();
398
399 data->SetIsZeroDegreesAllowed( expr.Contains( wxS( "== 0 deg" ) ) );
400 data->SetIsNinetyDegreesAllowed( expr.Contains( wxS( "== 90 deg" ) ) );
401 data->SetIsOneEightyDegreesAllowed( expr.Contains( wxS( "== 180 deg" ) ) );
402 data->SetIsTwoSeventyDegreesAllowed( expr.Contains( wxS( "== 270 deg" ) ) );
403
404 if( data->GetIsZeroDegreesAllowed() && data->GetIsNinetyDegreesAllowed()
405 && data->GetIsOneEightyDegreesAllowed() && data->GetIsTwoSeventyDegreesAllowed() )
406 {
407 data->SetIsAllDegreesAllowed( true );
408 return data;
409 }
410
411 if( data->GetIsZeroDegreesAllowed() || data->GetIsNinetyDegreesAllowed()
412 || data->GetIsOneEightyDegreesAllowed() || data->GetIsTwoSeventyDegreesAllowed() )
413 {
414 return data;
415 }
416
417 // Non-standard angles cannot be represented by the
418 // orientation panel, fall back to custom rule
419 auto customData = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
420 customData->SetRuleName( aRule.m_Name );
421 return customData;
422 }
423 else
424 {
425 data->SetIsAllDegreesAllowed( true );
426 }
427
428 return data;
429 }
430
431 case VIAS_UNDER_SMD:
432 {
433 auto data = std::make_shared<DRC_RE_VIAS_UNDER_SMD_CONSTRAINT_DATA>();
434 data->SetRuleName( aRule.m_Name );
435 data->SetConstraintCode( wxS( "disallow_via" ) );
436
437 const DRC_CONSTRAINT* constraint = findConstraint( aRule, DISALLOW_CONSTRAINT );
438
439 if( constraint )
440 {
441 data->SetDisallowThroughVias( ( constraint->m_DisallowFlags & DRC_DISALLOW_THROUGH_VIAS ) != 0 );
442 data->SetDisallowMicroVias( ( constraint->m_DisallowFlags & DRC_DISALLOW_MICRO_VIAS ) != 0 );
443 data->SetDisallowBlindVias( ( constraint->m_DisallowFlags & DRC_DISALLOW_BLIND_VIAS ) != 0 );
444 data->SetDisallowBuriedVias( ( constraint->m_DisallowFlags & DRC_DISALLOW_BURIED_VIAS ) != 0 );
445 }
446
447 return data;
448 }
449
450 case CUSTOM_RULE:
451 {
452 auto data = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
453 data->SetRuleName( aRule.m_Name );
454 return data;
455 }
456
457 default:
458 {
459 // For numeric input types, create a generic numeric constraint data
461 {
463 data->SetRuleName( aRule.m_Name );
464
465 wxString code = DRC_RULE_EDITOR_UTILS::GetConstraintCode( aPanel );
466 data->SetConstraintCode( code );
467
468 // Find the first matching constraint from the claimed set
469 for( DRC_CONSTRAINT_T type : aClaimedConstraints )
470 {
471 const DRC_CONSTRAINT* constraint = findConstraint( aRule, type );
472
473 if( constraint )
474 {
475 if( type == VIA_COUNT_CONSTRAINT )
476 data->SetNumericInputValue( constraint->GetValue().Max() );
477 else if( type == MIN_RESOLVED_SPOKES_CONSTRAINT )
478 data->SetNumericInputValue( constraint->GetValue().Min() );
479 else
480 data->SetNumericInputValue( toMM( constraint->GetValue().Min() ) );
481
482 break;
483 }
484 }
485
486 return data;
487 }
488
489 // Fallback to custom rule
490 auto data = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
491 data->SetRuleName( aRule.m_Name );
492 return data;
493 }
494 }
495}
496
497
498std::vector<DRC_RE_LOADED_PANEL_ENTRY> DRC_RULE_LOADER::LoadRule( const DRC_RULE& aRule,
499 const wxString& aOriginalText )
500{
501 std::vector<DRC_RE_LOADED_PANEL_ENTRY> entries;
502
503 // Get condition expression if present
504 wxString condition;
505
506 if( aRule.m_Condition )
507 condition = aRule.m_Condition->GetExpression();
508
509 // Match the rule to panels
510 std::vector<DRC_PANEL_MATCH> matches = m_matcher.MatchRule( aRule );
511
512 for( DRC_PANEL_MATCH& match : matches )
513 {
514 if( match.panelType == PERMITTED_LAYERS && match.claimedConstraints.count( ASSERTION_CONSTRAINT ) )
515 {
516 const DRC_CONSTRAINT* assertion = findConstraint( aRule, ASSERTION_CONSTRAINT );
517
518 if( assertion && assertion->m_Test )
519 {
520 wxString expr = assertion->m_Test->GetExpression();
521
522 if( expr.Contains( wxS( "Orientation" ) ) && !expr.Contains( wxS( "Layer" ) ) )
523 match.panelType = ALLOWED_ORIENTATION;
524 }
525 }
526
527 if( match.panelType == SILK_TO_SILK_CLEARANCE && match.claimedConstraints.count( SILK_CLEARANCE_CONSTRAINT ) )
528 {
529 if( !condition.IsEmpty()
530 && ( condition.Contains( wxS( "L == 'F.Mask'" ) ) || condition.Contains( wxS( "L == 'B.Mask'" ) ) ) )
531 {
532 match.panelType = SILK_TO_SOLDERMASK_CLEARANCE;
533 }
534 else if( !condition.IsEmpty() && !condition.Contains( wxS( "L == 'F.SilkS'" ) )
535 && !condition.Contains( wxS( "L == 'B.SilkS'" ) ) )
536 {
537 match.panelType = CUSTOM_RULE;
538 }
539 }
540
541 auto constraintData = createConstraintData( match.panelType, aRule, match.claimedConstraints );
542
543 if( !constraintData )
544 continue;
545
546 // If createConstraintData returned a custom rule fallback (e.g. non-standard
547 // orientation angles), update the panel type to match the actual data type
548 auto customFallback = std::dynamic_pointer_cast<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>( constraintData );
549
550 if( customFallback && match.panelType != CUSTOM_RULE )
551 {
552 match.panelType = CUSTOM_RULE;
553 }
554
555 if( match.panelType == SILK_TO_SOLDERMASK_CLEARANCE )
556 {
557 wxString cleanedCondition = condition;
558
559 // New format: L == 'F.Mask' || L == 'B.Mask'
560 bool hasBothSides = condition.Contains( wxS( "L == 'F.Mask' || L == 'B.Mask'" ) );
561
562 if( hasBothSides )
563 {
564 constraintData->SetLayers( { F_SilkS, B_SilkS } );
565 constraintData->SetLayerSource( wxS( "" ) );
566
567 cleanedCondition.Replace( wxS( "L == 'F.Mask' || L == 'B.Mask'" ), wxS( "" ) );
568 }
569 else
570 {
571 bool isFront = condition.Contains( wxS( "F.Mask" ) );
572 PCB_LAYER_ID layer = isFront ? F_SilkS : B_SilkS;
573 constraintData->SetLayers( { layer } );
574 constraintData->SetLayerSource( isFront ? wxS( "F.SilkS" ) : wxS( "B.SilkS" ) );
575
576 cleanedCondition.Replace( wxS( "L == 'F.Mask'" ), wxS( "" ) );
577 cleanedCondition.Replace( wxS( "L == 'B.Mask'" ), wxS( "" ) );
578 }
579
580 constraintData->SetRuleCondition( cleanStrippedCondition( cleanedCondition ) );
581 }
582
583 if( match.panelType == SILK_TO_SILK_CLEARANCE )
584 {
585 wxString cleanedCondition = condition;
586
587 bool hasBothSides = condition.Contains( wxS( "L == 'F.SilkS' || L == 'B.SilkS'" ) );
588
589 if( hasBothSides )
590 {
591 constraintData->SetLayers( { F_SilkS, B_SilkS } );
592 constraintData->SetLayerSource( wxS( "" ) );
593
594 cleanedCondition.Replace( wxS( "L == 'F.SilkS' || L == 'B.SilkS'" ), wxS( "" ) );
595 }
596 else if( condition.Contains( wxS( "L == 'F.SilkS'" ) ) || condition.Contains( wxS( "L == 'B.SilkS'" ) ) )
597 {
598 bool isFront = condition.Contains( wxS( "F.SilkS" ) );
599 PCB_LAYER_ID layer = isFront ? F_SilkS : B_SilkS;
600 constraintData->SetLayers( { layer } );
601 constraintData->SetLayerSource( isFront ? wxS( "F.SilkS" ) : wxS( "B.SilkS" ) );
602
603 cleanedCondition.Replace( wxS( "L == 'F.SilkS'" ), wxS( "" ) );
604 cleanedCondition.Replace( wxS( "L == 'B.SilkS'" ), wxS( "" ) );
605 }
606
607 constraintData->SetRuleCondition( cleanStrippedCondition( cleanedCondition ) );
608 }
609
610 if( match.panelType == VIAS_UNDER_SMD )
611 {
612 wxString cleanedCondition = condition;
613
614 cleanedCondition.Replace( wxS( "A.Pad_Type == 'SMD'" ), wxS( "" ) );
615 cleanedCondition.Replace( wxS( "B.Pad_Type == 'SMD'" ), wxS( "" ) );
616
617 constraintData->SetRuleCondition( cleanStrippedCondition( cleanedCondition ) );
618 }
619
620 if( match.panelType == CUSTOM_RULE && customFallback )
621 {
622 customFallback->SetRuleText( ExtractRuleBody( aOriginalText ) );
623 }
624
625 if( match.panelType != VIA_STYLE && match.panelType != SILK_TO_SOLDERMASK_CLEARANCE
626 && match.panelType != SILK_TO_SILK_CLEARANCE && match.panelType != VIAS_UNDER_SMD )
627 constraintData->SetRuleCondition( condition );
628
629 DRC_RE_LOADED_PANEL_ENTRY entry( match.panelType, constraintData, aRule.m_Name, condition, aRule.m_Severity,
630 aRule.m_LayerCondition );
631
632 // Preserve original layer source text for round-trip fidelity
633 wxString source = aRule.m_LayerSource;
634 if( source.StartsWith( wxS( "'" ) ) && source.EndsWith( wxS( "'" ) ) )
635 source = source.Mid( 1, source.Length() - 2 );
636 entry.layerSource = source;
637
638 wxString comment = ExtractRuleComment( aOriginalText );
639 if( !comment.IsEmpty() )
640 constraintData->SetComment( comment );
641
642 // Store original text only for the first entry to avoid duplication issues
643 if( entries.empty() )
644 entry.originalRuleText = aOriginalText;
645
646 entries.push_back( std::move( entry ) );
647 }
648
649 // If no matches, create a custom rule entry
650 if( entries.empty() )
651 {
652 auto customData = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
653 customData->SetRuleName( aRule.m_Name );
654 customData->SetRuleCondition( condition );
655
656 wxString comment = ExtractRuleComment( aOriginalText );
657 if( !comment.IsEmpty() )
658 customData->SetComment( comment );
659
660 customData->SetRuleText( ExtractRuleBody( aOriginalText ) );
661
662 DRC_RE_LOADED_PANEL_ENTRY entry( CUSTOM_RULE, customData, aRule.m_Name, condition,
663 aRule.m_Severity, aRule.m_LayerCondition );
664 wxString source = aRule.m_LayerSource;
665 if( source.StartsWith( wxS( "'" ) ) && source.EndsWith( wxS( "'" ) ) )
666 source = source.Mid( 1, source.Length() - 2 );
667 entry.layerSource = source;
668 entry.originalRuleText = aOriginalText;
669 entries.push_back( std::move( entry ) );
670 }
671
672 return entries;
673}
674
675
676std::vector<DRC_RE_LOADED_PANEL_ENTRY> DRC_RULE_LOADER::LoadFromString( const wxString& aRulesText )
677{
678 std::vector<DRC_RE_LOADED_PANEL_ENTRY> allEntries;
679 std::vector<std::shared_ptr<DRC_RULE>> parsedRules;
680
681 wxString rulesText = aRulesText;
682
683 if( !rulesText.Contains( "(version" ) )
684 rulesText.Prepend( "(version 2)\n" );
685
686 try
687 {
688 DRC_RULES_PARSER parser( rulesText, "Rule Loader" );
689 parser.Parse( parsedRules, nullptr );
690 }
691 catch( const IO_ERROR& )
692 {
693 return allEntries;
694 }
695
696 for( const auto& rule : parsedRules )
697 {
698 // Extract the actual original text from the file content
699 wxString originalText = ExtractRuleText( aRulesText, rule->m_Name );
700
701 std::vector<DRC_RE_LOADED_PANEL_ENTRY> ruleEntries = LoadRule( *rule, originalText );
702
703 for( auto& entry : ruleEntries )
704 allEntries.push_back( std::move( entry ) );
705 }
706
707 return allEntries;
708}
709
710
711wxString DRC_RULE_LOADER::ExtractRuleText( const wxString& aContent, const wxString& aRuleName )
712{
713 // Search for the rule by name, handling both quoted and unquoted names.
714 // The quoted form includes the closing quote as a boundary so partial
715 // matches like "Clearance" vs "Clearance for BGA" are not possible.
716 // The unquoted form needs an explicit boundary check.
717 wxString quotedSearch = wxString::Format( wxS( "(rule \"%s\"" ), aRuleName );
718 wxString unquotedSearch = wxString::Format( wxS( "(rule %s" ), aRuleName );
719
720 size_t startPos = aContent.find( quotedSearch );
721
722 if( startPos == wxString::npos )
723 {
724 size_t pos = 0;
725
726 while( ( pos = aContent.find( unquotedSearch, pos ) ) != wxString::npos )
727 {
728 size_t afterMatch = pos + unquotedSearch.length();
729
730 if( afterMatch >= aContent.length()
731 || aContent[afterMatch] == ')'
732 || aContent[afterMatch] == ' '
733 || aContent[afterMatch] == '\n'
734 || aContent[afterMatch] == '\r'
735 || aContent[afterMatch] == '\t' )
736 {
737 startPos = pos;
738 break;
739 }
740
741 pos = afterMatch;
742 }
743 }
744
745 if( startPos == wxString::npos )
746 return wxEmptyString;
747
748 // Find the matching closing parenthesis by counting balanced parens
749 int parenCount = 0;
750 size_t endPos = startPos;
751 bool inString = false;
752 bool escaped = false;
753
754 for( size_t i = startPos; i < aContent.length(); ++i )
755 {
756 wxUniChar c = aContent[i];
757
758 if( escaped )
759 {
760 escaped = false;
761 continue;
762 }
763
764 if( c == '\\' )
765 {
766 escaped = true;
767 continue;
768 }
769
770 if( c == '"' )
771 {
772 inString = !inString;
773 continue;
774 }
775
776 if( inString )
777 continue;
778
779 if( c == '(' )
780 {
781 parenCount++;
782 }
783 else if( c == ')' )
784 {
785 parenCount--;
786
787 if( parenCount == 0 )
788 {
789 endPos = i;
790 break;
791 }
792 }
793 }
794
795 if( parenCount != 0 )
796 return wxEmptyString;
797
798 return aContent.Mid( startPos, endPos - startPos + 1 );
799}
800
801
802std::vector<DRC_RE_LOADED_PANEL_ENTRY> DRC_RULE_LOADER::LoadFile( const wxString& aPath )
803{
804 std::vector<DRC_RE_LOADED_PANEL_ENTRY> allEntries;
805
806 wxFFile file( aPath, "r" );
807
808 if( !file.IsOpened() )
809 return allEntries;
810
811 wxString content;
812 file.ReadAll( &content );
813 file.Close();
814
815 return LoadFromString( content );
816}
DRC_RULE_CONDITION * m_Test
Definition drc_rule.h:243
int m_DisallowFlags
Definition drc_rule.h:241
const MINOPTMAX< int > & GetValue() const
Definition drc_rule.h:196
DRC_CONSTRAINT_T m_Type
Definition drc_rule.h:239
bool GetOption(OPTIONS option) const
Definition drc_rule.h:229
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
wxString GetExpression() const
static bool IsNumericInputType(const DRC_RULE_EDITOR_CONSTRAINT_NAME &aConstraintType)
static wxString GetConstraintCode(DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType)
Translate a rule tree node type into the keyword used by the rules file for that constraint.
static std::shared_ptr< DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA > CreateNumericConstraintData(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
double toMM(int aValue)
Convert internal units (nanometers) to millimeters.
DRC_PANEL_MATCHER m_matcher
std::vector< DRC_RE_LOADED_PANEL_ENTRY > LoadRule(const DRC_RULE &aRule, const wxString &aOriginalText)
Load a single DRC_RULE and convert it to panel entries.
std::vector< DRC_RE_LOADED_PANEL_ENTRY > LoadFromString(const wxString &aRulesText)
Load rules from a text string.
const DRC_CONSTRAINT * findConstraint(const DRC_RULE &aRule, DRC_CONSTRAINT_T aType)
Find a constraint of a specific type in a rule.
static wxString ExtractRuleComment(const wxString &aOriginalText)
Extract comment lines from a rule.
static wxString ExtractRuleBody(const wxString &aOriginalText)
Extract the body of a rule from its original text, stripping the (rule "name" ...) wrapper.
static wxString ExtractRuleText(const wxString &aContent, const wxString &aRuleName)
Extract the complete original text of a rule from file content.
std::vector< DRC_RE_LOADED_PANEL_ENTRY > LoadFile(const wxString &aPath)
Load all rules from a .kicad_dru file.
wxString cleanStrippedCondition(const wxString &aCondition)
Clean up a condition string after auto-generated tokens have been removed.
std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > createConstraintData(DRC_RULE_EDITOR_CONSTRAINT_NAME aPanel, const DRC_RULE &aRule, const std::set< DRC_CONSTRAINT_T > &aClaimedConstraints)
Create the appropriate constraint data object for a panel type.
SEVERITY m_Severity
Definition drc_rule.h:158
DRC_RULE_CONDITION * m_Condition
Definition drc_rule.h:156
LSET m_LayerCondition
Definition drc_rule.h:155
std::vector< DRC_CONSTRAINT > m_Constraints
Definition drc_rule.h:157
wxString m_Name
Definition drc_rule.h:153
wxString m_LayerSource
Definition drc_rule.h:154
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
T Min() const
Definition minoptmax.h:29
T PinnedOpt() const
Definition minoptmax.h:32
T Max() const
Definition minoptmax.h:30
static std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > makeCustomRuleData(const DRC_RULE &aRule)
static bool isSymmetricMinOptMax(const DRC_CONSTRAINT *aConstraint)
@ DRC_DISALLOW_BURIED_VIAS
Definition drc_rule.h:98
@ DRC_DISALLOW_BLIND_VIAS
Definition drc_rule.h:97
@ DRC_DISALLOW_THROUGH_VIAS
Definition drc_rule.h:95
@ DRC_DISALLOW_MICRO_VIAS
Definition drc_rule.h:96
DRC_CONSTRAINT_T
Definition drc_rule.h:49
@ VIA_DIAMETER_CONSTRAINT
Definition drc_rule.h:72
@ DIFF_PAIR_GAP_CONSTRAINT
Definition drc_rule.h:78
@ DISALLOW_CONSTRAINT
Definition drc_rule.h:71
@ TRACK_WIDTH_CONSTRAINT
Definition drc_rule.h:61
@ SILK_CLEARANCE_CONSTRAINT
Definition drc_rule.h:58
@ MIN_RESOLVED_SPOKES_CONSTRAINT
Definition drc_rule.h:67
@ TEXT_THICKNESS_CONSTRAINT
Definition drc_rule.h:60
@ LENGTH_CONSTRAINT
Definition drc_rule.h:73
@ VIA_COUNT_CONSTRAINT
Definition drc_rule.h:81
@ MAX_UNCOUPLED_CONSTRAINT
Definition drc_rule.h:79
@ ASSERTION_CONSTRAINT
Definition drc_rule.h:84
@ SKEW_CONSTRAINT
Definition drc_rule.h:77
@ HOLE_SIZE_CONSTRAINT
Definition drc_rule.h:56
@ TEXT_HEIGHT_CONSTRAINT
Definition drc_rule.h:59
DRC_RULE_EDITOR_CONSTRAINT_NAME
@ ALLOWED_ORIENTATION
@ SILK_TO_SILK_CLEARANCE
@ ROUTING_DIFF_PAIR
@ SILK_TO_SOLDERMASK_CLEARANCE
@ ABSOLUTE_LENGTH
@ PERMITTED_LAYERS
@ MINIMUM_TEXT_HEIGHT_AND_THICKNESS
@ ROUTING_WIDTH
@ MATCHED_LENGTH_DIFF_PAIR
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_SilkS
Definition layer_ids.h:96
@ B_SilkS
Definition layer_ids.h:97
Result of matching a panel to constraints.
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
wxString originalRuleText
wxString layerSource
Original layer text: "inner", "outer", or layer name.