KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_rule_editor_utils.h
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#ifndef DRC_RULE_EDITOR_UTILS_H_
21#define DRC_RULE_EDITOR_UTILS_H_
22
23#include <wx/wx.h>
24#include <wx/object.h>
25
26#include <optional>
27#include <memory>
28#include <vector>
29#include <bitmaps.h>
30
33
34class BOARD;
35class DRC_RULE;
37
39{
40public:
41 using RuleConverter = std::function<std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>( const std::shared_ptr<DRC_RULE>& )>;
42
43 static void RegisterRuleConverter( RuleConverter aConverter );
44
45 static bool IsBoolInputType( const DRC_RULE_EDITOR_CONSTRAINT_NAME& aConstraintType );
46
47 static bool IsNumericInputType( const DRC_RULE_EDITOR_CONSTRAINT_NAME& aConstraintType );
48
49 static std::shared_ptr<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA> CreateNumericConstraintData( DRC_RULE_EDITOR_CONSTRAINT_NAME aType );
50
55 static std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>
57
58 static std::shared_ptr<DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA>
60 const DRC_RE_BASE_CONSTRAINT_DATA& aBase );
61
62 static wxString FormatErrorMessage( int aErrorCount, const wxString& aErrorMessage );
63
64 static std::vector<std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>> ParseRules( const wxString& aRules );
65
66 static bool SaveRules( const wxString& aFilename,
67 const std::vector<std::shared_ptr<DRC_RE_BASE_CONSTRAINT_DATA>>& aRules,
68 const BOARD* aBoard );
69
78 static wxString GetConstraintCode( DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType );
79
88 static std::optional<DRC_RULE_EDITOR_CONSTRAINT_NAME> GetConstraintTypeFromCode( const wxString& aCode );
89
97
109 static bool ValidateNumericCtrl( wxTextCtrl* aTextCtrl, const wxString& aLabel, bool aCanBeZero, int* aErrorCount,
110 wxString* aValidationMessage );
111
123 static bool ValidateIntegerCtrl( wxTextCtrl* aTextCtrl, const wxString& aLabel, bool aCanBeZero, int* aErrorCount,
124 wxString* aValidationMessage );
125
136 static bool ValidateComboCtrl( wxComboBox* aComboBox, const wxString& aLabel, int* aErrorCount,
137 wxString* aValidationMessage );
138
151 static bool ValidateMinMaxCtrl( wxTextCtrl* aMinTextCtrl, wxTextCtrl* aMaxTextCtrl, const wxString& aMinLabel,
152 const wxString& aMaxLabel, int* aErrorCount, wxString* aValidationMessage );
153
171 static bool ValidateMinPreferredMaxCtrl( wxTextCtrl* aMinTextCtrl, wxTextCtrl* aPreferredTextCtrl,
172 wxTextCtrl* aMaxTextCtrl, const wxString& aMinLabel,
173 const wxString& aPreferredLabel, const wxString& aMaxLabel,
174 int* aErrorCount, wxString* aValidationMessage );
175
186 static bool ValidateCheckBoxCtrls( const std::vector<wxCheckBox*>& aCheckboxes, const wxString& aLabel,
187 int* aErrorCount, wxString* aValidationMessage );
188
189 // ==================== Pure Validators (No GUI Dependencies) ====================
190 // These can be used in headless tests and by constraint data classes.
191
201 static bool ValidateNumericValue( double aValue, bool aCanBeZero, const wxString& aLabel,
202 VALIDATION_RESULT* aResult );
203
214 static bool ValidateNumericString( const wxString& aValueStr, bool aCanBeZero, bool aIntegerOnly,
215 const wxString& aLabel, VALIDATION_RESULT* aResult );
216
227 static bool ValidateMinMax( double aMin, double aMax, const wxString& aMinLabel,
228 const wxString& aMaxLabel, VALIDATION_RESULT* aResult );
229
242 static bool ValidateMinPreferredMax( double aMin, double aPreferred, double aMax,
243 const wxString& aMinLabel, const wxString& aPrefLabel,
244 const wxString& aMaxLabel, VALIDATION_RESULT* aResult );
245
254 static bool ValidateAtLeastOneSelected( const std::vector<bool>& aSelected, const wxString& aLabel,
255 VALIDATION_RESULT* aResult );
256
265 static bool ValidateSelection( int aSelectionIndex, const wxString& aLabel,
266 VALIDATION_RESULT* aResult );
267
268 // ==================== Layer Filtering ====================
269
278
288 static wxString TranslateTopBottomLayer( DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType, bool aIsTop );
289
297 static PCB_LAYER_ID TopBottomLayerId( DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType, bool aIsTop );
298
306
308 {
310 wxString operand;
311 };
312
319 static std::optional<SELECTOR_CONDITION> ParseSelectorCondition( const wxString& aExpr );
320};
321
322#endif // DRC_RULE_EDITOR_UTILS_H_
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
static bool IsNumericInputType(const DRC_RULE_EDITOR_CONSTRAINT_NAME &aConstraintType)
static wxString ConstraintToKicadDrc(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
Convert a constraint type into the keyword used in a .kicad_drc file.
static bool ValidateNumericValue(double aValue, bool aCanBeZero, const wxString &aLabel, VALIDATION_RESULT *aResult)
Validates a numeric value.
static bool ValidateAtLeastOneSelected(const std::vector< bool > &aSelected, const wxString &aLabel, VALIDATION_RESULT *aResult)
Validates that at least one option is selected.
static bool ValidateSelection(int aSelectionIndex, const wxString &aLabel, VALIDATION_RESULT *aResult)
Validates that a selection has been made (index >= 0).
static std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > CreateConstraintData(DRC_RULE_EDITOR_CONSTRAINT_NAME aType, const DRC_RE_BASE_CONSTRAINT_DATA &aBase)
Create the data object that holds a rule of the given constraint type, taking the identity fields fro...
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 bool ValidateNumericCtrl(wxTextCtrl *aTextCtrl, const wxString &aLabel, bool aCanBeZero, int *aErrorCount, wxString *aValidationMessage)
Validates a numeric input control, checking if the value is valid, non-empty, and greater than zero.
static bool ValidateMinMax(double aMin, double aMax, const wxString &aMinLabel, const wxString &aMaxLabel, VALIDATION_RESULT *aResult)
Validates that min <= max.
static bool ValidateMinPreferredMaxCtrl(wxTextCtrl *aMinTextCtrl, wxTextCtrl *aPreferredTextCtrl, wxTextCtrl *aMaxTextCtrl, const wxString &aMinLabel, const wxString &aPreferredLabel, const wxString &aMaxLabel, int *aErrorCount, wxString *aValidationMessage)
Validates the minimum, preferred, and maximum value controls, ensuring that:
static wxString FormatErrorMessage(int aErrorCount, const wxString &aErrorMessage)
static std::optional< DRC_RULE_EDITOR_CONSTRAINT_NAME > GetConstraintTypeFromCode(const wxString &aCode)
Resolve a constraint keyword from a rules file into the corresponding rule tree enumeration value.
static bool ValidateComboCtrl(wxComboBox *aComboBox, const wxString &aLabel, int *aErrorCount, wxString *aValidationMessage)
Validates a combo box control, ensuring that a selection has been made.
static bool SaveRules(const wxString &aFilename, const std::vector< std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > > &aRules, const BOARD *aBoard)
static void RegisterRuleConverter(RuleConverter aConverter)
static wxString TranslateTopBottomLayer(DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType, bool aIsTop)
Translate a top/bottom selection to the appropriate layer clause or condition.
static bool ValidateCheckBoxCtrls(const std::vector< wxCheckBox * > &aCheckboxes, const wxString &aLabel, int *aErrorCount, wxString *aValidationMessage)
Validates a list of checkboxes, ensuring that at least one option is selected.
static bool ValidateNumericString(const wxString &aValueStr, bool aCanBeZero, bool aIntegerOnly, const wxString &aLabel, VALIDATION_RESULT *aResult)
Validates that a string represents a valid numeric value.
static bool ValidateMinMaxCtrl(wxTextCtrl *aMinTextCtrl, wxTextCtrl *aMaxTextCtrl, const wxString &aMinLabel, const wxString &aMaxLabel, int *aErrorCount, wxString *aValidationMessage)
Validates the minimum and maximum value controls, ensuring that the minimum value is not greater than...
std::function< std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA >(const std::shared_ptr< DRC_RULE > &)> RuleConverter
static std::optional< SELECTOR_CONDITION > ParseSelectorCondition(const wxString &aExpr)
Match a condition that a net, netclass or area selector can show.
static std::shared_ptr< DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA > CreateNumericConstraintData(DRC_RULE_EDITOR_CONSTRAINT_NAME aType)
static DRC_LAYER_CATEGORY GetLayerCategoryForConstraint(DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType)
Get the layer category for a constraint type.
static PCB_LAYER_ID TopBottomLayerId(DRC_RULE_EDITOR_CONSTRAINT_NAME aConstraintType, bool aIsTop)
The layer a side of the board means for a constraint, which is not copper for every one.
static bool ValidateMinPreferredMax(double aMin, double aPreferred, double aMax, const wxString &aMinLabel, const wxString &aPrefLabel, const wxString &aMaxLabel, VALIDATION_RESULT *aResult)
Validates that min <= preferred <= max.
static bool ValidateIntegerCtrl(wxTextCtrl *aTextCtrl, const wxString &aLabel, bool aCanBeZero, int *aErrorCount, wxString *aValidationMessage)
Validates an integer input control, ensuring the value is a valid integer, non-empty,...
static bool IsBoolInputType(const DRC_RULE_EDITOR_CONSTRAINT_NAME &aConstraintType)
static std::vector< std::shared_ptr< DRC_RE_BASE_CONSTRAINT_DATA > > ParseRules(const wxString &aRules)
DRC_RULE_EDITOR_CONSTRAINT_NAME
DRC_LAYER_CATEGORY
Layer categories for filtering the layer selector dropdown.
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
Result of a validation operation.