46#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(
"hole_clearance" )
485 || token == wxT(
"hole_size" )
486 || token == wxT(
"hole_to_hole" )
487 || token == wxT(
"min_resolved_spokes" )
488 || token == wxT(
"physical_clearance" )
489 || token == wxT(
"physical_hole_clearance" )
490 || token == wxT(
"silk_clearance" )
491 || token == wxT(
"skew" )
492 || token == wxT(
"solder_mask_expansion" )
493 || token == wxT(
"solder_paste_abs_margin" )
494 || token == wxT(
"solder_paste_rel_margin" )
495 || token == wxT(
"text_height" )
496 || token == wxT(
"text_thickness" )
497 || token == wxT(
"thermal_relief_gap" )
498 || token == wxT(
"thermal_spoke_width" )
499 || token == wxT(
"track_width" )
500 || token == wxT(
"track_angle" )
501 || token == wxT(
"track_segment_length" )
502 || token == wxT(
"via_count" )
503 || token == wxT(
"via_diameter" )
504 || token == wxT(
"zone_connection" );
507 std::stack<wxString> sexprs;
510 wxString constraintType;
511 int context = NO_CONTEXT;
512 int expr_context = NO_CONTEXT;
514 for(
int i = startPos; i < currentPos; ++i )
522 else if( context ==
STRING )
526 context = NO_CONTEXT;
530 if( expr_context ==
STRING )
533 expr_context = NO_CONTEXT;
540 partial = wxEmptyString;
545 partial = wxEmptyString;
546 expr_context = STRUCT_REF;
557 partial = wxEmptyString;
562 if( context == SEXPR_OPEN && !partial.IsEmpty() )
565 sexprs.push( partial );
568 partial = wxEmptyString;
569 context = SEXPR_OPEN;
573 while( !sexprs.empty() && ( sexprs.top() == wxT(
"assertion" )
574 || sexprs.top() == wxT(
"disallow" )
575 || isDisallowToken( sexprs.top() )
576 || sexprs.top() == wxT(
"min_resolved_spokes" )
577 || sexprs.top() == wxT(
"zone_connection" ) ) )
582 if( !sexprs.empty() )
585 if( partial == wxT(
"within_diff_pairs" ) )
587 partial = wxEmptyString;
591 if( sexprs.top() == wxT(
"constraint" ) )
593 constraintType = wxEmptyString;
600 context = NO_CONTEXT;
604 if( context == SEXPR_OPEN && !partial.IsEmpty() )
607 sexprs.push( partial );
609 if( partial == wxT(
"constraint" )
610 || partial == wxT(
"layer" )
611 || partial == wxT(
"severity" ) )
613 context = SEXPR_TOKEN;
615 else if( partial == wxT(
"rule" )
616 || partial == wxT(
"condition" ) )
618 context = SEXPR_STRING;
622 context = NO_CONTEXT;
625 partial = wxEmptyString;
628 else if( partial == wxT(
"disallow" )
629 || isDisallowToken( partial )
630 || partial == wxT(
"min_resolved_spokes" )
631 || partial == wxT(
"zone_connection" ) )
634 sexprs.push( partial );
636 partial = wxEmptyString;
637 context = SEXPR_TOKEN;
640 else if( partial == wxT(
"assertion" ) )
643 sexprs.push( partial );
645 partial = wxEmptyString;
646 context = SEXPR_STRING;
649 else if( isConstraintTypeToken( partial ) )
651 constraintType = partial;
654 context = NO_CONTEXT;
664 if( context == SEXPR_OPEN )
668 tokens = wxT(
"rule|"
671 else if( sexprs.top() == wxT(
"rule" ) )
673 tokens = wxT(
"condition|"
678 else if( sexprs.top() == wxT(
"constraint" ) )
680 if( constraintType == wxT(
"skew" ) )
681 tokens = wxT(
"max|min|opt|within_diff_pairs" );
683 tokens = wxT(
"max|min|opt" );
686 else if( context == SEXPR_TOKEN )
692 else if( sexprs.top() == wxT(
"constraint" ) )
694 tokens = wxT(
"annular_width|"
699 "courtyard_clearance|"
702 "diff_pair_uncoupled|"
709 "min_resolved_spokes|"
710 "physical_clearance|"
711 "physical_hole_clearance|"
714 "solder_mask_expansion|"
715 "solder_paste_abs_margin|"
716 "solder_paste_rel_margin|"
719 "thermal_relief_gap|"
720 "thermal_spoke_width|"
723 "track_segment_length|"
728 else if( sexprs.top() == wxT(
"disallow" ) || isDisallowToken( sexprs.top() ) )
730 tokens = wxT(
"blind_via|"
743 else if( sexprs.top() == wxT(
"zone_connection" ) )
745 tokens = wxT(
"none|solid|thermal_reliefs" );
747 else if( sexprs.top() == wxT(
"min_resolved_spokes" ) )
749 tokens = wxT(
"0|1|2|3|4" );
751 else if( sexprs.top() == wxT(
"layer" ) )
753 tokens = wxT(
"inner|outer|\"x\"" );
755 else if( sexprs.top() == wxT(
"severity" ) )
757 tokens = wxT(
"warning|error|ignore|exclusion" );
760 else if( context == SEXPR_STRING && !sexprs.empty()
761 && ( sexprs.top() == wxT(
"condition" ) || sexprs.top() == wxT(
"assertion" ) ) )
765 else if( context ==
STRING && !sexprs.empty()
766 && ( sexprs.top() == wxT(
"condition" ) || sexprs.top() == wxT(
"assertion" ) ) )
768 if( expr_context == STRUCT_REF )
771 std::set<wxString> propNames;
775 const std::vector<PROPERTY_BASE*>& props = propMgr.
GetProperties( cls.type );
782 if( prop->IsHiddenFromRulesEditor() )
785 wxString ref( prop->Name() );
786 ref.Replace( wxT(
" " ), wxT(
"_" ) );
787 propNames.insert( ref );
791 for(
const wxString& propName : propNames )
792 tokens += wxT(
"|" ) + propName;
798 if( !funcSig.Contains(
"DEPRECATED" ) )
799 tokens += wxT(
"|" ) + funcSig;
802 else if( expr_context ==
STRING )
806 std::shared_ptr<NET_SETTINGS>& netSettings =
m_frame->GetBoard()->GetDesignSettings().m_NetSettings;
808 for(
const auto& [
name, netclass] : netSettings->GetNetclasses() )
809 tokens += wxT(
"|" ) +
name;
816 tokens += wxT(
"|" ) + netnameCandidate;
820 tokens = wxT(
"Bitmap|"
836 tokens = wxT(
"Through|"
843 tokens = wxT(
"Through-hole|"
846 "NPTH, mechanical" );
850 tokens = wxT(
"Input|"
865 tokens = wxT(
"None|"
867 "Fiducial, global to board|"
868 "Fiducial, local to footprint|"
875 tokens = wxT(
"Segment|"
884 tokens = wxT(
"Circle|"
889 "Chamfered rectangle|"
894 tokens = wxT(
"Inherited|"
898 "Thermal reliefs for PTH" );
902 tokens = wxT(
"Inherited|"
909 tokens = wxT(
"Default|"
918 tokens = wxT(
"Left|"
931 if( !tokens.IsEmpty() )
942 std::vector<std::shared_ptr<DRC_RULE>> dummyRules;
944 std::function<bool( wxString* )> resolver =
945 [&]( wxString* token ) ->
bool
950 return m_frame->GetBoard()->ResolveTextVar( token, 0 );
954 rulesText =
m_frame->GetBoard()->ConvertCrossReferencesToKIIDs( rulesText );
964 m_errorsReport->Report( wxString::Format( wxT(
"%s <a href='%d:%d'>%s</a>%s" ),
983 std::map<std::pair<wxString, wxString>, wxString> seenConditions;
984 std::regex netclassPattern(
"NetClass\\s*[!=]=\\s*'\"?([^\"\\s]+)'\"?" );
986 for(
const auto& rule : aRules )
990 if( rule->m_Condition )
991 condition = rule->m_Condition->GetExpression();
993 condition.Trim(
true ).Trim(
false );
995 auto key = std::make_pair( condition, rule->m_LayerSource );
997 if( seenConditions.count( key ) )
999 m_errorsReport->Report( wxString::Format(
_(
"Rules '%s' and '%s' share the same condition." ),
1001 seenConditions[key] ),
1006 seenConditions[key] = rule->m_Name;
1009 std::string condUtf8 = condition.ToStdString();
1010 std::sregex_iterator it( condUtf8.begin(), condUtf8.end(), netclassPattern );
1011 std::sregex_iterator
end;
1013 for( ; it !=
end; ++it )
1015 wxString ncName = wxString::FromUTF8( ( *it )[1].str() );
1019 m_errorsReport->Report( wxString::Format(
_(
"Rule '%s' references undefined netclass '%s'." ),
1026 const bool isInner = rule->m_LayerSource.IsSameAs( wxT(
"'inner'" ),
false );
1027 const bool isOuter = rule->m_LayerSource.IsSameAs( wxT(
"'outer'" ),
false );
1029 if( !rule->m_LayerSource.IsEmpty() && !isInner && !isOuter )
1031 LSET invalid = rule->m_LayerCondition & ~enabledLayers;
1039 if( !badLayers.IsEmpty() )
1045 m_errorsReport->Report( wxString::Format(
_(
"Rule '%s' references undefined layer(s): %s." ),
1057 wxString
link =
event.GetLinkInfo().GetHref();
1058 wxArrayString parts;
1059 long line = 0, offset = 0;
1063 if( parts.size() > 1 )
1065 parts[0].ToLong( &line );
1066 parts[1].ToLong( &offset );
1069 int pos =
m_textEditor->PositionFromLine( line - 1 ) + ( offset - 1 );
1082 wxFileName rulesFile(
m_frame->GetBoard()->GetDesignRulesPath() );
1084 if( rulesFile.FileExists() )
1086 wxTextFile file( rulesFile.GetFullPath() );
1090 for ( wxString str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
1098 wxCommandEvent
dummy;
1109 if(
m_frame->Prj().IsNullProject() )
1112 m_textEditor->AddText(
_(
"Design rules cannot be added without a project" ) );
1125 wxString rulesFilepath =
m_frame->GetBoard()->GetDesignRulesPath();
1128 std::string utf8 = std::string( content.mb_str( wxConvUTF8 ) );
1129 wxString writeError;
1133 wxLogError(
_(
"Cannot save design rules to '%s': %s" ), rulesFilepath, writeError );
1139 m_frame->GetBoard()->GetDesignSettings().m_DRCEngine->InitEngine( rulesFilepath );
1158 std::vector<wxString> msg;
1164 msg.emplace_back( t );
1168 msg.emplace_back( t );
1172 msg.emplace_back( t );
1176 msg.emplace_back( t );
1180 msg.emplace_back( t );
1184 msg.emplace_back( t );
1188 msg.emplace_back( t );
1192 msg.emplace_back( t );
1196 msg.emplace_back( t );
1200 msg.emplace_back( t );
1202 wxString msg_txt = wxEmptyString;
1204 for( wxString i : msg )
1205 msg_txt << wxGetTranslation( i );
1208 msg_txt.Replace( wxT(
"Ctrl+" ), wxT(
"Cmd+" ) );
1210 const wxString& msGg_txt = msg_txt;
1215 wxString html_txt = wxEmptyString;
1218 html_txt.Replace( wxS(
"<td" ), wxS(
"<td valign=top" ) );
1227 if( !
m_frame->Prj().IsNullProject() )
1234 if( absFile.FileExists() )
1236 wxTextFile file( absFile.GetFullPath() );
1242 for ( wxString str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
1250 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 & GetFileName() const
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
PROJECT * GetProject() const
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)
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)
void ImportSettingsFrom(BOARD *aBoard)
void OnContextMenu(wxMouseEvent &event) override
~PANEL_SETUP_RULES() override
HTML_MESSAGE_BOX * m_helpWindow
void OnCompile(wxCommandEvent &event) override
wxRegEx m_padConnectionsRegex
wxRegEx m_zoneConnStyleRegex
bool TransferDataFromWindow() override
void OnSyntaxHelp(wxHyperlinkEvent &aEvent) override
PANEL_SETUP_RULES(wxWindow *aParentWindow, PCB_EDIT_FRAME *aFrame)
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
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
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)
bool IsComponentClassSelector(const wxString &aToken)
${Class:X} inside a DRC rule is a component-class selector consumed by testFootprintSelector(),...
static const std::string DesignRulesFileExtension
PCB_LAYER_ID
A quick note on layer IDs:
PGM_BASE & Pgm()
The global program "get" accessor.
std::vector< FAB_LAYER_COLOR > dummy
bool ConvertSmartQuotesAndDashes(wxString *aString)
Convert curly quotes and em/en dashes to straight quotes and dashes.
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
Definition of file extensions used in Kicad.