304 for(
const wxString& line : wxSplit( aOriginalText,
'\n',
'\0' ) )
306 wxString trimmed = line;
307 trimmed.Trim(
false );
309 if( !trimmed.StartsWith( wxS(
"#" ) ) )
313 wxString
text = wxJoin( kept,
'\n',
'\0' );
315 int ruleKeyword =
text.Find( wxS(
"rule " ) );
316 if( ruleKeyword == wxNOT_FOUND )
317 return aOriginalText;
319 int bodyStart =
text.find(
'(', ruleKeyword + 5 );
320 if( bodyStart == (
int) wxString::npos )
321 return aOriginalText;
324 size_t lineStart =
text.rfind(
'\n', bodyStart );
325 lineStart = ( lineStart == wxString::npos ) ? 0 : lineStart + 1;
327 bool ownLine =
text.find_first_not_of( wxS(
" \t" ), lineStart ) == (size_t) bodyStart;
329 wxString body =
text.Mid( ownLine ? lineStart : bodyStart );
332 if( body.EndsWith( wxS(
")" ) ) )
333 body = body.Left( body.Length() - 1 );
337 wxArrayString lines = wxSplit( body,
'\n',
'\0' );
338 std::optional<wxString> indent;
340 for(
size_t i = ownLine ? 0 : 1; i < lines.size(); ++i )
342 size_t textStart = lines[i].find_first_not_of( wxS(
" \t" ) );
344 if( textStart == wxString::npos )
347 wxString lead = lines[i].Left( textStart );
354 while( common < indent->length() && common < lead.length() && ( *indent )[common] == lead[common] )
357 indent = indent->Left( common );
360 if( !indent || indent->IsEmpty() )
363 for(
size_t i = ownLine ? 0 : 1; i < lines.size(); ++i )
365 if( lines[i].StartsWith( *indent ) )
366 lines[i] = lines[i].Mid( indent->length() );
369 return wxJoin( lines,
'\n',
'\0' );
430 const std::set<DRC_CONSTRAINT_T>& aClaimedConstraints )
436 auto data = std::make_shared<DRC_RE_VIA_STYLE_CONSTRAINT_DATA>();
437 data->SetRuleName( aRule.
m_Name );
438 data->SetConstraintCode(
"via_style" );
467 if( condition.Contains( wxS(
"Via_Type" ) ) )
469 std::optional<wxString> rest;
474 data->SetViaType( type );
488 data->SetRuleCondition( condition );
502 auto data = std::make_shared<DRC_RE_ROUTING_DIFF_PAIR_CONSTRAINT_DATA>();
503 data->SetRuleName( aRule.
m_Name );
504 data->SetConstraintCode(
"diff_pair_gap" );
511 data->SetOptWidth( opt );
512 data->SetWidthTolerance( opt - min );
520 data->SetOptGap( opt );
521 data->SetGapTolerance( opt - min );
541 auto data = std::make_shared<DRC_RE_MINIMUM_TEXT_HEIGHT_THICKNESS_CONSTRAINT_DATA>();
542 data->SetRuleName( aRule.
m_Name );
543 data->SetConstraintCode(
"text_height" );
561 auto data = std::make_shared<DRC_RE_ROUTING_WIDTH_CONSTRAINT_DATA>();
562 data->SetRuleName( aRule.
m_Name );
563 data->SetConstraintCode(
"track_width" );
570 data->SetOptWidth( opt );
571 data->SetWidthTolerance( opt - min );
584 auto data = std::make_shared<DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA>();
585 data->SetRuleName( aRule.
m_Name );
586 data->SetConstraintCode(
"length" );
592 auto convert = [&](
int aValue )
594 return timeDomain ?
toPS( aValue ) :
toMM( aValue );
603 data->SetTimeDomain( timeDomain );
604 data->SetOptimumLength( opt );
605 data->SetTolerance( ( max - min ) / 2.0 );
619 auto data = std::make_shared<DRC_RE_MATCHED_LENGTH_DIFF_PAIR_CONSTRAINT_DATA>();
620 data->SetRuleName( aRule.
m_Name );
621 data->SetConstraintCode(
"length" );
630 : ( minMM + maxMM ) / 2.0;
632 data->SetOptimumLength( optMM );
633 data->SetTolerance( ( maxMM - minMM ) / 2.0 );
647 auto data = std::make_shared<DRC_RE_PERMITTED_LAYERS_CONSTRAINT_DATA>();
648 data->SetRuleName( aRule.
m_Name );
652 if( constraint && constraint->
m_Test )
656 data->SetTopLayerEnabled( expr.Contains( wxS(
"F.Cu" ) ) );
657 data->SetBottomLayerEnabled( expr.Contains( wxS(
"B.Cu" ) ) );
660 wxString remaining = expr;
661 remaining.Replace( wxS(
"A.Layer == 'F.Cu'" ), wxS(
"" ) );
662 remaining.Replace( wxS(
"A.Layer == 'B.Cu'" ), wxS(
"" ) );
664 if( remaining.Contains( wxS(
"A.Layer" ) ) )
667 auto customData = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
668 customData->SetRuleName( aRule.
m_Name );
678 auto data = std::make_shared<DRC_RE_ALLOWED_ORIENTATION_CONSTRAINT_DATA>();
679 data->SetRuleName( aRule.
m_Name );
680 data->SetConstraintCode( wxS(
"allowed_orientation" ) );
684 if( constraint && constraint->
m_Test )
688 data->SetIsZeroDegreesAllowed( expr.Contains( wxS(
"== 0 deg" ) ) );
689 data->SetIsNinetyDegreesAllowed( expr.Contains( wxS(
"== 90 deg" ) ) );
690 data->SetIsOneEightyDegreesAllowed( expr.Contains( wxS(
"== 180 deg" ) ) );
691 data->SetIsTwoSeventyDegreesAllowed( expr.Contains( wxS(
"== 270 deg" ) ) );
693 if( data->GetIsZeroDegreesAllowed() && data->GetIsNinetyDegreesAllowed()
694 && data->GetIsOneEightyDegreesAllowed() && data->GetIsTwoSeventyDegreesAllowed() )
696 data->SetIsAllDegreesAllowed(
true );
700 if( data->GetIsZeroDegreesAllowed() || data->GetIsNinetyDegreesAllowed()
701 || data->GetIsOneEightyDegreesAllowed() || data->GetIsTwoSeventyDegreesAllowed() )
708 auto customData = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
709 customData->SetRuleName( aRule.
m_Name );
714 data->SetIsAllDegreesAllowed(
true );
722 auto data = std::make_shared<DRC_RE_VIAS_UNDER_SMD_CONSTRAINT_DATA>();
723 data->SetRuleName( aRule.
m_Name );
724 data->SetConstraintCode( wxS(
"disallow_via" ) );
732 data->SetRuleCondition( *rest );
749 auto data = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
750 data->SetRuleName( aRule.
m_Name );
760 data->SetRuleName( aRule.
m_Name );
763 data->SetConstraintCode( code );
773 data->SetNumericInputValue( constraint->
GetValue().
Max() );
775 data->SetNumericInputValue( constraint->
GetValue().
Max() / 1000.0 );
777 data->SetNumericInputValue( constraint->
GetValue().
Min() );
789 auto data = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
790 data->SetRuleName( aRule.
m_Name );
798 const wxString& aOriginalText )
800 std::vector<DRC_RE_LOADED_PANEL_ENTRY> entries;
809 bool fitsStructuredPanels =
true;
816 fitsStructuredPanels =
false;
822 std::vector<DRC_PANEL_MATCH> matches;
824 if( fitsStructuredPanels )
833 if( assertion && assertion->
m_Test )
837 if( expr.Contains( wxS(
"Orientation" ) ) && !expr.Contains( wxS(
"Layer" ) ) )
844 if( !condition.IsEmpty()
845 && ( condition.Contains( wxS(
"L == 'F.Mask'" ) ) || condition.Contains( wxS(
"L == 'B.Mask'" ) ) ) )
849 else if( !condition.IsEmpty() && !condition.Contains( wxS(
"L == 'F.SilkS'" ) )
850 && !condition.Contains( wxS(
"L == 'B.SilkS'" ) ) )
858 if( !constraintData )
863 auto customFallback = std::dynamic_pointer_cast<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>( constraintData );
865 if( customFallback && match.panelType !=
CUSTOM_RULE )
872 wxString cleanedCondition = condition;
875 bool hasBothSides = condition.Contains( wxS(
"L == 'F.Mask' || L == 'B.Mask'" ) );
880 constraintData->SetLayerSource( wxS(
"" ) );
882 cleanedCondition.Replace( wxS(
"L == 'F.Mask' || L == 'B.Mask'" ), wxS(
"" ) );
886 bool isFront = condition.Contains( wxS(
"F.Mask" ) );
888 constraintData->SetLayers( { layer } );
889 constraintData->SetLayerSource( isFront ? wxS(
"F.SilkS" ) : wxS(
"B.SilkS" ) );
891 cleanedCondition.Replace( wxS(
"L == 'F.Mask'" ), wxS(
"" ) );
892 cleanedCondition.Replace( wxS(
"L == 'B.Mask'" ), wxS(
"" ) );
900 wxString cleanedCondition = condition;
902 bool hasBothSides = condition.Contains( wxS(
"L == 'F.SilkS' || L == 'B.SilkS'" ) );
907 constraintData->SetLayerSource( wxS(
"" ) );
909 cleanedCondition.Replace( wxS(
"L == 'F.SilkS' || L == 'B.SilkS'" ), wxS(
"" ) );
911 else if( condition.Contains( wxS(
"L == 'F.SilkS'" ) ) || condition.Contains( wxS(
"L == 'B.SilkS'" ) ) )
913 bool isFront = condition.Contains( wxS(
"F.SilkS" ) );
915 constraintData->SetLayers( { layer } );
916 constraintData->SetLayerSource( isFront ? wxS(
"F.SilkS" ) : wxS(
"B.SilkS" ) );
918 cleanedCondition.Replace( wxS(
"L == 'F.SilkS'" ), wxS(
"" ) );
919 cleanedCondition.Replace( wxS(
"L == 'B.SilkS'" ), wxS(
"" ) );
925 if( match.panelType ==
CUSTOM_RULE && customFallback )
932 constraintData->SetRuleCondition( condition );
938 wxString entryCondition = stripped ? constraintData->GetRuleCondition() : condition;
939 LSET entryLayers = stripped ?
LSET( constraintData->GetLayers() ) :
ruleLayers( aRule );
946 if(
source.StartsWith( wxS(
"'" ) ) &&
source.EndsWith( wxS(
"'" ) ) )
952 if( !comment.IsEmpty() )
953 constraintData->SetComment( comment );
956 if( entries.empty() )
959 entries.push_back( std::move( entry ) );
964 if( entries.size() > 1
965 && std::any_of( entries.begin(), entries.end(),
968 return aEntry.panelType == CUSTOM_RULE
969 || !aEntry.constraintData->GetConditionPredicate().IsEmpty();
976 if( entries.empty() )
978 auto customData = std::make_shared<DRC_RE_CUSTOM_RULE_CONSTRAINT_DATA>();
979 customData->SetRuleName( aRule.
m_Name );
980 customData->SetRuleCondition( condition );
983 if( !comment.IsEmpty() )
984 customData->SetComment( comment );
991 if(
source.StartsWith( wxS(
"'" ) ) &&
source.EndsWith( wxS(
"'" ) ) )
995 entries.push_back( std::move( entry ) );
999 entry.originalEntryCount =
static_cast<int>( entries.size() );
1007 std::vector<DRC_RE_LOADED_PANEL_ENTRY> allEntries;
1008 std::vector<std::shared_ptr<DRC_RULE>> parsedRules;
1012 wxString rulesText = aRulesText;
1017 rulesText.Prepend(
"(version 2)\n" );
1024 parser.
Parse( parsedRules,
nullptr );
1044 for(
const auto& rule : parsedRules )
1046 wxString originalText;
1047 wxString leadingTrivia;
1051 if( start != wxString::npos )
1053 originalText = rulesText.Mid( start,
end - start );
1054 leadingTrivia = rulesText.Mid( pos, start - pos );
1060 std::vector<DRC_RE_LOADED_PANEL_ENTRY> ruleEntries =
LoadRule( *rule, originalText );
1062 for(
auto& entry : ruleEntries )
1064 entry.sourceRule = ruleIndex;
1065 entry.ruleGroupId = ruleIndex + 1;
1066 allEntries.push_back( std::move( entry ) );
1084 wxString quotedSearch = wxString::Format( wxS(
"(rule \"%s\"" ), aRuleName );
1085 wxString unquotedSearch = wxString::Format( wxS(
"(rule %s" ), aRuleName );
1087 size_t startPos = aContent.find( quotedSearch );
1089 if( startPos == wxString::npos )
1093 while( ( pos = aContent.find( unquotedSearch, pos ) ) != wxString::npos )
1095 size_t afterMatch = pos + unquotedSearch.length();
1097 if( afterMatch >= aContent.length()
1098 || aContent[afterMatch] ==
')'
1099 || aContent[afterMatch] ==
' '
1100 || aContent[afterMatch] ==
'\n'
1101 || aContent[afterMatch] ==
'\r'
1102 || aContent[afterMatch] ==
'\t' )
1112 if( startPos == wxString::npos )
1113 return wxEmptyString;
1117 if( endPos == wxString::npos )
1118 return wxEmptyString;
1120 return aContent.Mid( startPos, endPos - startPos + 1 );
Represents a rule loaded from a .kicad_dru file and mapped to a panel.
wxString layerSource
Original layer text: "inner", "outer", or layer name.