45#include <unordered_map>
58 [
this]( wxKeyEvent& aEvent )
61 wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
64 [
this]( wxStyledTextEvent& aEvent )
73 m_typeRegex.Compile(
"^Type\\s*[!=]=\\s*$", wxRE_ADVANCED );
74 m_viaTypeRegex.Compile(
"^Via_Type\\s*[!=]=\\s*$", wxRE_ADVANCED );
75 m_padTypeRegex.Compile(
"^Pad_Type\\s*[!=]=\\s*$", wxRE_ADVANCED );
76 m_pinTypeRegex.Compile(
"^Pin_Type\\s*[!=]=\\s*$", wxRE_ADVANCED );
77 m_fabPropRegex.Compile(
"^Fabrication_Property\\s*[!=]=\\s*$", wxRE_ADVANCED );
78 m_shapeRegex.Compile(
"^Shape\\s*[!=]=\\s*$", wxRE_ADVANCED );
83 m_hJustRegex.Compile(
"^Horizontal_Justification\\s*[!=]=\\s*$", wxRE_ADVANCED );
84 m_vJustRegex.Compile(
"^Vertical_Justification\\s*[!=]=\\s*$", wxRE_ADVANCED );
88 m_textEditor->SetZoom(
Pgm().GetCommonSettings()->m_Appearance.text_editor_zoom );
111 if( aEvent.GetKeyCode() == WXK_ESCAPE && !
m_textEditor->AutoCompActive() )
134 menu.Append( wxID_UNDO,
_(
"Undo" ) );
135 menu.Append( wxID_REDO,
_(
"Redo" ) );
137 menu.AppendSeparator();
139 menu.Append( 1,
_(
"Cut" ) );
140 menu.Append( 2,
_(
"Copy" ) );
141 menu.Append( 3,
_(
"Paste" ) );
142 menu.Append( 4,
_(
"Delete" ) );
144 menu.AppendSeparator();
146 menu.Append( 5,
_(
"Select All" ) );
148 menu.AppendSeparator();
150 menu.Append( wxID_ZOOM_IN,
_(
"Zoom In" ) );
151 menu.Append( wxID_ZOOM_OUT,
_(
"Zoom Out" ) );
153 menu.AppendSeparator();
155 menu.Append( 6,
_(
"Show Matching Items" ) );
157 switch( GetPopupMenuSelectionFromUser( menu ) )
206 if(
m_frame->Prj().IsNullProject() )
215 const int cursorLine =
m_textEditor->LineFromPosition( cursorPos ) + 1;
216 const int cursorCol = cursorPos -
m_textEditor->PositionFromLine( cursorLine - 1 ) + 1;
218 std::vector<std::shared_ptr<DRC_RULE>> rules;
222 std::function<bool( wxString* )> resolver =
223 [&]( wxString* token ) ->
bool
228 return m_frame->GetBoard()->ResolveTextVar( token, 0 );
231 wxString rulesText =
m_frame->GetBoard()->ConvertCrossReferencesToKIIDs(
text );
246 wxString targetRuleName;
247 bool foundRule =
false;
251 std::string
utf8(
text.mb_str( wxConvUTF8 ) );
252 DRC_RULES_LEXER lex(
utf8,
_(
"DRC rules" ) );
262 std::vector<RuleFrame> ruleStack;
267 int tok = lex.NextTok();
274 int leftLine = lex.CurLineNumber();
275 int leftCol = lex.CurOffset();
279 int sub = lex.NextTok();
281 if( sub == DRCRULE_T::T_rule )
284 int nameTok = lex.NextTok();
287 name = wxString::FromUTF8( lex.CurText() );
289 ruleStack.push_back( { depth, leftLine, leftCol,
name } );
306 if( !ruleStack.empty() && ruleStack.back().depth == depth )
308 RuleFrame opened = ruleStack.back();
309 ruleStack.pop_back();
311 int endLine = lex.CurLineNumber();
312 int endCol = lex.CurOffset();
314 bool afterStart = cursorLine > opened.startLine
315 || ( cursorLine == opened.startLine && cursorCol >= opened.startCol );
316 bool beforeEnd = cursorLine < endLine || ( cursorLine == endLine && cursorCol <= endCol );
318 if( afterStart && beforeEnd )
320 targetRuleName = opened.name;
332 m_errorsReport->Report( wxString::Format(
_(
"Could not scan rules text: %s" ), e.
What() ),
340 m_errorsReport->Report(
_(
"Place the cursor inside a (rule ...) block to show matching "
347 if( targetRuleName.IsEmpty() )
349 m_errorsReport->Report(
_(
"The rule under the cursor has no name; add a name to identify "
356 std::shared_ptr<DRC_RULE> targetRule;
358 for(
const auto& rule : rules )
360 if( rule->m_Name == targetRuleName )
369 m_errorsReport->Report( wxString::Format(
_(
"Rule '%s' could not be located after "
377 if( targetRule->m_Condition && !targetRule->m_Condition->GetExpression().IsEmpty()
384 std::shared_ptr<DRC_ENGINE> engine =
m_frame->GetBoard()->GetDesignSettings().m_DRCEngine;
393 std::vector<BOARD_ITEM*> allMatches = engine->GetItemsMatchingRule( targetRule,
m_errorsReport );
395 std::vector<BOARD_ITEM*> matches;
399 switch( item->Type() )
404 default: matches.push_back( item );
break;
408 m_frame->FocusOnItems( matches );
411 wxString::Format(
_(
"Rule '%s': %zu matching item(s)." ), targetRule->m_Name, matches.size() ),
419 if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKey() ==
' ' )
433 for(
int line =
m_textEditor->LineFromPosition( currentPos ); line > 0; line-- )
436 wxString beginning =
m_textEditor->GetTextRange( lineStart, lineStart + 10 );
438 if( beginning.StartsWith( wxT(
"(rule " ) ) )
440 startPos = lineStart;
455 auto isDisallowToken =
456 [](
const wxString& token ) ->
bool
458 return token == wxT(
"blind_via" )
459 || token == wxT(
"buried_via" )
460 || token == wxT(
"graphic" )
461 || token == wxT(
"hole" )
462 || token == wxT(
"micro_via" )
463 || token == wxT(
"pad" )
464 || token == wxT(
"text" )
465 || token == wxT(
"through_via" )
466 || token == wxT(
"track" )
467 || token == wxT(
"via" )
468 || token == wxT(
"zone" );
471 auto isConstraintTypeToken =
472 [](
const wxString& token ) ->
bool
474 return token == wxT(
"annular_width" )
475 || token == wxT(
"assertion" )
476 || token == wxT(
"clearance" )
477 || token == wxT(
"connection_width" )
478 || token == wxT(
"courtyard_clearance" )
479 || token == wxT(
"diff_pair_gap" )
480 || token == wxT(
"diff_pair_uncoupled" )
481 || token == wxT(
"disallow" )
482 || token == wxT(
"edge_clearance" )
483 || token == wxT(
"length" )
484 || token == wxT(
"line_width" )
485 || token == wxT(
"hole_clearance" )
486 || token == wxT(
"hole_size" )
487 || token == wxT(
"hole_to_hole" )
488 || token == wxT(
"min_resolved_spokes" )
489 || token == wxT(
"physical_clearance" )
490 || token == wxT(
"physical_hole_clearance" )
491 || token == wxT(
"silk_clearance" )
492 || token == wxT(
"skew" )
493 || token == wxT(
"solder_mask_expansion" )
494 || token == wxT(
"solder_paste_abs_margin" )
495 || token == wxT(
"solder_paste_rel_margin" )
496 || token == wxT(
"text_height" )
497 || token == wxT(
"text_thickness" )
498 || token == wxT(
"thermal_relief_gap" )
499 || token == wxT(
"thermal_spoke_width" )
500 || token == wxT(
"track_width" )
501 || token == wxT(
"track_angle" )
502 || token == wxT(
"track_segment_length" )
503 || token == wxT(
"via_count" )
504 || token == wxT(
"via_diameter" )
505 || token == wxT(
"zone_connection" );
508 std::stack<wxString> sexprs;
511 wxString constraintType;
512 int context = NO_CONTEXT;
513 int expr_context = NO_CONTEXT;
515 for(
int i = startPos; i < currentPos; ++i )
523 else if( context ==
STRING )
527 context = NO_CONTEXT;
531 if( expr_context ==
STRING )
534 expr_context = NO_CONTEXT;
541 partial = wxEmptyString;
546 partial = wxEmptyString;
547 expr_context = STRUCT_REF;
558 partial = wxEmptyString;
563 if( context == SEXPR_OPEN && !partial.IsEmpty() )
566 sexprs.push( partial );
569 partial = wxEmptyString;
570 context = SEXPR_OPEN;
574 while( !sexprs.empty() && ( sexprs.top() == wxT(
"assertion" )
575 || sexprs.top() == wxT(
"disallow" )
576 || isDisallowToken( sexprs.top() )
577 || sexprs.top() == wxT(
"min_resolved_spokes" )
578 || sexprs.top() == wxT(
"zone_connection" ) ) )
583 if( !sexprs.empty() )
586 if( partial == wxT(
"within_diff_pairs" ) )
588 partial = wxEmptyString;
592 if( sexprs.top() == wxT(
"constraint" ) )
594 constraintType = wxEmptyString;
601 context = NO_CONTEXT;
605 if( context == SEXPR_OPEN && !partial.IsEmpty() )
608 sexprs.push( partial );
610 if( partial == wxT(
"constraint" )
611 || partial == wxT(
"layer" )
612 || partial == wxT(
"severity" ) )
614 context = SEXPR_TOKEN;
616 else if( partial == wxT(
"rule" )
617 || partial == wxT(
"condition" ) )
619 context = SEXPR_STRING;
623 context = NO_CONTEXT;
626 partial = wxEmptyString;
629 else if( partial == wxT(
"disallow" )
630 || isDisallowToken( partial )
631 || partial == wxT(
"min_resolved_spokes" )
632 || partial == wxT(
"zone_connection" ) )
635 sexprs.push( partial );
637 partial = wxEmptyString;
638 context = SEXPR_TOKEN;
641 else if( partial == wxT(
"assertion" ) )
644 sexprs.push( partial );
646 partial = wxEmptyString;
647 context = SEXPR_STRING;
650 else if( isConstraintTypeToken( partial ) )
652 constraintType = partial;
655 context = NO_CONTEXT;
665 if( context == SEXPR_OPEN )
669 tokens = wxT(
"rule|"
672 else if( sexprs.top() == wxT(
"rule" ) )
674 tokens = wxT(
"condition|"
679 else if( sexprs.top() == wxT(
"constraint" ) )
681 if( constraintType == wxT(
"skew" ) )
682 tokens = wxT(
"max|min|opt|within_diff_pairs" );
684 tokens = wxT(
"max|min|opt" );
687 else if( context == SEXPR_TOKEN )
693 else if( sexprs.top() == wxT(
"constraint" ) )
695 tokens = wxT(
"annular_width|"
700 "courtyard_clearance|"
703 "diff_pair_uncoupled|"
711 "min_resolved_spokes|"
712 "physical_clearance|"
713 "physical_hole_clearance|"
716 "solder_mask_expansion|"
717 "solder_paste_abs_margin|"
718 "solder_paste_rel_margin|"
720 "thermal_relief_gap|"
721 "thermal_spoke_width|"
724 "track_segment_length|"
729 else if( sexprs.top() == wxT(
"disallow" ) || isDisallowToken( sexprs.top() ) )
731 tokens = wxT(
"blind_via|"
744 else if( sexprs.top() == wxT(
"zone_connection" ) )
746 tokens = wxT(
"none|solid|thermal_reliefs" );
748 else if( sexprs.top() == wxT(
"min_resolved_spokes" ) )
750 tokens = wxT(
"0|1|2|3|4" );
752 else if( sexprs.top() == wxT(
"layer" ) )
754 tokens = wxT(
"inner|outer|\"x\"" );
756 else if( sexprs.top() == wxT(
"severity" ) )
758 tokens = wxT(
"warning|error|ignore|exclusion" );
761 else if( context == SEXPR_STRING && !sexprs.empty()
762 && ( sexprs.top() == wxT(
"condition" ) || sexprs.top() == wxT(
"assertion" ) ) )
766 else if( context ==
STRING && !sexprs.empty()
767 && ( sexprs.top() == wxT(
"condition" ) || sexprs.top() == wxT(
"assertion" ) ) )
769 if( expr_context == STRUCT_REF )
772 std::set<wxString> propNames;
776 const std::vector<PROPERTY_BASE*>& props = propMgr.
GetProperties( cls.type );
783 if( prop->IsHiddenFromRulesEditor() )
786 wxString ref( prop->Name() );
787 ref.Replace( wxT(
" " ), wxT(
"_" ) );
788 propNames.insert( ref );
792 for(
const wxString& propName : propNames )
793 tokens += wxT(
"|" ) + propName;
799 if( !funcSig.Contains(
"DEPRECATED" ) )
800 tokens += wxT(
"|" ) + funcSig;
803 else if( expr_context ==
STRING )
807 std::shared_ptr<NET_SETTINGS>& netSettings =
m_frame->GetBoard()->GetDesignSettings().m_NetSettings;
809 for(
const auto& [
name, netclass] : netSettings->GetNetclasses() )
810 tokens += wxT(
"|" ) +
name;
817 tokens += wxT(
"|" ) + netnameCandidate;
821 tokens = wxT(
"Bitmap|"
837 tokens = wxT(
"Through|"
844 tokens = wxT(
"Through-hole|"
847 "NPTH, mechanical" );
851 tokens = wxT(
"Input|"
866 tokens = wxT(
"None|"
868 "Fiducial, global to board|"
869 "Fiducial, local to footprint|"
876 tokens = wxT(
"Segment|"
885 tokens = wxT(
"Circle|"
890 "Chamfered rectangle|"
895 tokens = wxT(
"Inherited|"
899 "Thermal reliefs for PTH" );
903 tokens = wxT(
"Inherited|"
910 tokens = wxT(
"Default|"
919 tokens = wxT(
"Left|"
932 if( !tokens.IsEmpty() )
943 std::vector<std::shared_ptr<DRC_RULE>> dummyRules;
945 std::function<bool( wxString* )> resolver =
946 [&]( wxString* token ) ->
bool
951 return m_frame->GetBoard()->ResolveTextVar( token, 0 );
955 rulesText =
m_frame->GetBoard()->ConvertCrossReferencesToKIIDs( rulesText );
965 m_errorsReport->Report( wxString::Format( wxT(
"%s <a href='%d:%d'>%s</a>%s" ),
984 std::map<std::pair<wxString, wxString>, wxString> seenConditions;
985 std::regex netclassPattern(
"NetClass\\s*[!=]=\\s*'\"?([^\"\\s]+)'\"?" );
987 for(
const auto& rule : aRules )
991 if( rule->m_Condition )
992 condition = rule->m_Condition->GetExpression();
994 condition.Trim(
true ).Trim(
false );
996 auto key = std::make_pair( condition, rule->m_LayerSource );
998 if( seenConditions.count( key ) )
1000 m_errorsReport->Report( wxString::Format(
_(
"Rules '%s' and '%s' share the same condition." ),
1002 seenConditions[key] ),
1007 seenConditions[key] = rule->m_Name;
1010 std::string condUtf8 = condition.ToStdString();
1011 std::sregex_iterator it( condUtf8.begin(), condUtf8.end(), netclassPattern );
1012 std::sregex_iterator
end;
1014 for( ; it !=
end; ++it )
1016 wxString ncName = wxString::FromUTF8( ( *it )[1].str() );
1020 m_errorsReport->Report( wxString::Format(
_(
"Rule '%s' references undefined netclass '%s'." ),
1027 const bool isInner = rule->m_LayerSource.IsSameAs( wxT(
"'inner'" ),
false );
1028 const bool isOuter = rule->m_LayerSource.IsSameAs( wxT(
"'outer'" ),
false );
1030 if( !rule->m_LayerSource.IsEmpty() && !isInner && !isOuter )
1032 LSET invalid = rule->m_LayerCondition & ~enabledLayers;
1040 if( !badLayers.IsEmpty() )
1046 m_errorsReport->Report( wxString::Format(
_(
"Rule '%s' references undefined layer(s): %s." ),
1058 wxString
link =
event.GetLinkInfo().GetHref();
1059 wxArrayString parts;
1060 long line = 0, offset = 0;
1064 if( parts.size() > 1 )
1066 parts[0].ToLong( &line );
1067 parts[1].ToLong( &offset );
1070 int pos =
m_textEditor->PositionFromLine( line - 1 ) + ( offset - 1 );
1085 if(
m_frame->Prj().IsNullProject() )
1088 m_textEditor->AddText(
_(
"Design rules cannot be added without a project" ) );
1115 std::vector<wxString> msg;
1121 msg.emplace_back( t );
1125 msg.emplace_back( t );
1129 msg.emplace_back( t );
1133 msg.emplace_back( t );
1137 msg.emplace_back( t );
1141 msg.emplace_back( t );
1145 msg.emplace_back( t );
1149 msg.emplace_back( t );
1153 msg.emplace_back( t );
1157 msg.emplace_back( t );
1159 wxString msg_txt = wxEmptyString;
1161 for( wxString i : msg )
1162 msg_txt << wxGetTranslation( i );
1165 msg_txt.Replace( wxT(
"Ctrl+" ), wxT(
"Cmd+" ) );
1167 const wxString& msGg_txt = msg_txt;
1172 wxString html_txt = wxEmptyString;
1175 html_txt.Replace( wxS(
"<td" ), wxS(
"<td valign=top" ) );
1191 wxCommandEvent
dummy;
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
std::shared_ptr< NET_SETTINGS > m_NetSettings
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Information pertinent to a Pcbnew printed circuit board.
std::set< wxString > GetNetClassAssignmentCandidates() const
Return the set of netname candidates for netclass assignment.
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
const LSET & GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
BOARD_DESIGN_SETTINGS & GetDesignSettings()
void Parse(std::vector< std::shared_ptr< DRC_RULE > > &aRules, REPORTER *aReporter)
The custom rules of one board, held in memory while Board Setup is open.
static bool IsSymbol(int aTok)
Test a token to see if it is a symbol.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
LSET is a set of PCB_LAYER_IDs.
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
bool HasNetclass(const wxString &netclassName) const
Determines if the given netclass exists.
static PAGED_DIALOG * GetDialog(wxWindow *aWindow)
STD_BITMAP_BUTTON * m_compileButton
wxStyledTextCtrl * m_textEditor
PANEL_SETUP_RULES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
WX_HTML_REPORT_BOX * m_errorsReport
bool TransferDataToWindow() override
void OnErrorLinkClicked(wxHtmlLinkEvent &event) override
void checkPlausibility(const std::vector< std::shared_ptr< DRC_RULE > > &aRules)
void onScintillaCharAdded(wxStyledTextEvent &aEvent)
DRC_RULE_DOCUMENT * m_document
void OnContextMenu(wxMouseEvent &event) override
~PANEL_SETUP_RULES() override
void RefreshFromDocument()
HTML_MESSAGE_BOX * m_helpWindow
void OnCompile(wxCommandEvent &event) override
wxRegEx m_padConnectionsRegex
wxRegEx m_zoneConnStyleRegex
PANEL_SETUP_RULES(wxWindow *aParentWindow, PCB_EDIT_FRAME *aFrame, DRC_RULE_DOCUMENT *aDocument)
bool TransferDataFromWindow() override
void OnSyntaxHelp(wxHyperlinkEvent &aEvent) override
void onCharHook(wxKeyEvent &aEvent)
SCINTILLA_TRICKS * m_scintillaTricks
const wxArrayString GetSignatures() const
static PCBEXPR_BUILTIN_FUNCTIONS & Instance()
The main frame for Pcbnew.
virtual COMMON_SETTINGS * GetCommonSettings() const
const std::vector< PROPERTY_BASE * > & GetProperties(TYPE_ID aType) const
Return all properties for a specific type.
static PROPERTY_MANAGER & Instance()
std::vector< CLASS_INFO > GetAllClasses()
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, RESOLUTION_CONTEXT aContext)
std::string utf8(const wxString &aText)
bool IsComponentClassSelector(const wxString &aToken)
${Class:X} inside a DRC rule is a component-class selector consumed by testFootprintSelector(),...
PCB_LAYER_ID
A quick note on layer IDs:
PGM_BASE & Pgm()
The global program "get" accessor.
std::vector< FAB_LAYER_COLOR > dummy
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)
A filename or source description, a problem input line, a line number, a byte offset,...
int lineNumber
at which line number, 1 based index.
const wxString ParseProblem()
int byteIndex
at which byte offset within the line, 1 based index
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net