KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_base_constraint_data.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_RE_BASE_CONSTRAINT_DATA_H_
21#define DRC_RE_BASE_CONSTRAINT_DATA_H_
22
23#include <bitmaps.h>
28#include <string_utils.h>
29#include <wx/arrstr.h>
30
31#include <vector>
32
33
35{
36public:
38
39 explicit DRC_RE_BASE_CONSTRAINT_DATA( int aId, int aParentId, const wxString& aRuleName ) :
40 RULE_EDITOR_DATA_BASE( aId, aParentId, aRuleName )
41 {
42 }
43
44 virtual ~DRC_RE_BASE_CONSTRAINT_DATA() = default;
45
52 virtual VALIDATION_RESULT Validate() const { return VALIDATION_RESULT(); }
53
54 virtual wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) { return wxEmptyString; }
55
63
71 virtual std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const { return {}; }
72
77 virtual std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const
78 {
79 return {};
80 }
81
82 std::vector<PCB_LAYER_ID> GetLayers() { return m_layers; }
83
84 void SetLayers( std::vector<PCB_LAYER_ID> aLayers ) { m_layers = aLayers; }
85
86 wxString GetLayerSource() { return m_layerSource; }
87
88 void SetLayerSource( const wxString& aSource ) { m_layerSource = aSource; }
89
90 wxString GetRuleCondition() { return m_ruleCondition; }
91
92 void SetRuleCondition( const wxString &aRuleCondition ) { m_ruleCondition = aRuleCondition; }
93
94 wxString GetConstraintCode() const { return m_constraintCode; }
95
96 void SetConstraintCode( const wxString& aCode ) { m_constraintCode = aCode; }
97
98 wxString GetGeneratedRule() const { return m_generatedRule; }
99
100 void SetGeneratedRule( const wxString& aRule ) { m_generatedRule = aRule; }
101
102 wxString GetOriginalRuleText() const { return m_originalRuleText; }
103
104 void SetOriginalRuleText( const wxString& aText ) { m_originalRuleText = aText; }
105
106 bool WasEdited() const { return m_wasEdited; }
107
108 void SetWasEdited( bool aEdited ) { m_wasEdited = aEdited; }
109
111
112 void SetOriginalEntryCount( int aCount ) { m_originalEntryCount = aCount; }
113
114 int GetSourceRule() const { return m_sourceRule; }
115
116 void SetSourceRule( int aIndex ) { m_sourceRule = aIndex; }
117
118 SEVERITY GetSeverity() const { return m_severity; }
119
120 void SetSeverity( SEVERITY aSeverity ) { m_severity = aSeverity; }
121
122 void CopyFrom( const ICopyable& aSource ) override
123 {
124 const auto& source = dynamic_cast<const DRC_RE_BASE_CONSTRAINT_DATA&>( aSource );
125
127
128 m_layers = source.m_layers;
129 m_layerSource = source.m_layerSource;
130 m_constraintCode = source.m_constraintCode;
131 m_generatedRule = source.m_generatedRule;
132 m_originalRuleText = source.m_originalRuleText;
133 m_wasEdited = source.m_wasEdited;
134 m_originalEntryCount = source.m_originalEntryCount;
135 m_severity = source.m_severity;
136 m_ruleCondition = source.m_ruleCondition;
137 }
138
146 static wxString formatRuleName( const wxString& aRuleName )
147 {
148 if( aRuleName.IsEmpty() )
149 return wxString( wxS( "\"\"" ) );
150
151 bool needsQuoting = false;
152
153 if( wxIsdigit( *aRuleName.begin() ) )
154 {
155 needsQuoting = true;
156 }
157 else
158 {
159 for( wxUniChar c : aRuleName )
160 {
161 if( !( wxIsalnum( c ) || c == '_' || c == '-' || c == '.' ) )
162 {
163 needsQuoting = true;
164 break;
165 }
166 }
167 }
168
169 if( !needsQuoting )
170 return aRuleName;
171
172 wxString escaped = EscapeString( aRuleName, CTX_QUOTED_STR );
173 return wxString( wxS( "\"" ) ) + escaped + wxS( "\"" );
174 }
175
176protected:
177 static wxString quoteString( const wxString& aCondition )
178 {
179 return EscapeString( aCondition, CTX_QUOTED_STR );
180 }
181
182 static wxString trimTrailingZeros( const wxString& aValue )
183 {
184 wxString result( aValue );
185
186 if( !result.Contains( '.' ) )
187 return result;
188
189 while( result.Length() > 1 && result.Last() == '0' )
190 result.Truncate( result.Length() - 1 );
191
192 if( result.Last() == '.' )
193 result.Truncate( result.Length() - 1 );
194
195 return result;
196 }
197
198 static wxString formatDouble( double aValue, int aPrecision = 6 )
199 {
200 wxString formatted = wxString::FromCDouble( aValue, aPrecision );
201 return trimTrailingZeros( formatted );
202 }
203
204 wxString buildRule( const RULE_GENERATION_CONTEXT& aContext,
205 const std::vector<wxString>& aConstraintClauses ) const
206 {
207 wxString rule;
208 rule << wxS( "(rule " ) << formatRuleName( aContext.ruleName ) << wxS( "\n" );
209
210 if( !aContext.comment.IsEmpty() )
211 {
212 wxArrayString lines = wxSplit( aContext.comment, '\n', '\0' );
213
214 for( const wxString& line : lines )
215 {
216 if( line.IsEmpty() )
217 continue;
218
219 rule << wxS( "\t# " ) << line << wxS( "\n" );
220 }
221 }
222
223 wxString condExpr = aContext.conditionExpression;
224
225 if( !aContext.layerClause.IsEmpty() )
226 {
227 if( aContext.layerClause.StartsWith( wxS( "(condition " ) ) )
228 {
229 wxString expr = aContext.layerClause.AfterFirst( '"' ).BeforeLast( '"' );
230
231 if( !condExpr.IsEmpty() )
232 condExpr = expr + wxS( " && " ) + condExpr;
233 else
234 condExpr = expr;
235 }
236 else
237 {
238 rule << wxS( "\t" ) << aContext.layerClause << wxS( "\n" );
239 }
240 }
241
242 for( const wxString& clause : aConstraintClauses )
243 {
244 if( clause.IsEmpty() )
245 continue;
246
247 rule << wxS( "\t" ) << clause << wxS( "\n" );
248 }
249
250 if( !condExpr.IsEmpty() )
251 rule << wxS( "\t(condition \"" ) << quoteString( condExpr ) << wxS( "\")\n" );
252
253 rule << wxS( ")" );
254
255 return rule;
256 }
257
258private:
259 std::vector<PCB_LAYER_ID> m_layers;
260 wxString m_layerSource;
265 bool m_wasEdited = false;
267 int m_sourceRule = -1;
269};
270
271#endif // DRC_RE_BASE_CONSTRAINT_DATA_H_
BITMAPS
A list of all bitmap identifiers.
@ INVALID_BITMAP
virtual VALIDATION_RESULT Validate() const
Validates the constraint data.
wxString m_layerSource
Original layer text: "inner", "outer", or layer name.
static wxString quoteString(const wxString &aCondition)
static wxString formatRuleName(const wxString &aRuleName)
Format a rule name for use in S-expression output while preserving the exact name the user chose.
void SetConstraintCode(const wxString &aCode)
static wxString formatDouble(double aValue, int aPrecision=6)
void SetLayers(std::vector< PCB_LAYER_ID > aLayers)
void SetGeneratedRule(const wxString &aRule)
virtual ~DRC_RE_BASE_CONSTRAINT_DATA()=default
void SetLayerSource(const wxString &aSource)
virtual std::vector< wxString > GetConstraintClauses(const RULE_GENERATION_CONTEXT &aContext) const
Returns just the constraint clauses without the rule wrapper.
virtual std::vector< DRC_RE_FIELD_POSITION > GetFieldPositions() const
Returns the field positions for controls overlaid on the constraint bitmap.
wxString buildRule(const RULE_GENERATION_CONTEXT &aContext, const std::vector< wxString > &aConstraintClauses) const
static wxString trimTrailingZeros(const wxString &aValue)
DRC_RE_BASE_CONSTRAINT_DATA(int aId, int aParentId, const wxString &aRuleName)
std::vector< PCB_LAYER_ID > m_layers
void SetRuleCondition(const wxString &aRuleCondition)
virtual BITMAPS GetOverlayBitmap() const
Returns the bitmap to use for the overlay panel background.
void CopyFrom(const ICopyable &aSource) override
virtual wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext)
void SetOriginalRuleText(const wxString &aText)
std::vector< PCB_LAYER_ID > GetLayers()
int m_sourceRule
Not copied, since a duplicate is a new rule.
Abstract interface class to enable polymorphic copying between objects.
RULE_EDITOR_DATA_BASE()=default
void CopyFrom(const ICopyable &aSource) override
Implementation of the polymorphic CopyFrom method.
std::string source
SEVERITY
@ RPT_SEVERITY_UNDEFINED
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_QUOTED_STR
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.