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
110 SEVERITY GetSeverity() const { return m_severity; }
111
112 void SetSeverity( SEVERITY aSeverity ) { m_severity = aSeverity; }
113
114 void CopyFrom( const ICopyable& aSource ) override
115 {
116 const auto& source = dynamic_cast<const DRC_RE_BASE_CONSTRAINT_DATA&>( aSource );
117
119
120 m_layers = source.m_layers;
121 m_layerSource = source.m_layerSource;
122 m_constraintCode = source.m_constraintCode;
123 m_generatedRule = source.m_generatedRule;
124 m_originalRuleText = source.m_originalRuleText;
125 m_wasEdited = source.m_wasEdited;
126 m_severity = source.m_severity;
127 m_ruleCondition = source.m_ruleCondition;
128 }
129
137 static wxString formatRuleName( const wxString& aRuleName )
138 {
139 if( aRuleName.IsEmpty() )
140 return wxString( wxS( "\"\"" ) );
141
142 bool needsQuoting = false;
143
144 if( wxIsdigit( *aRuleName.begin() ) )
145 {
146 needsQuoting = true;
147 }
148 else
149 {
150 for( wxUniChar c : aRuleName )
151 {
152 if( !( wxIsalnum( c ) || c == '_' || c == '-' || c == '.' ) )
153 {
154 needsQuoting = true;
155 break;
156 }
157 }
158 }
159
160 if( !needsQuoting )
161 return aRuleName;
162
163 wxString escaped = EscapeString( aRuleName, CTX_QUOTED_STR );
164 return wxString( wxS( "\"" ) ) + escaped + wxS( "\"" );
165 }
166
167protected:
168 static wxString quoteString( const wxString& aCondition )
169 {
170 return EscapeString( aCondition, CTX_QUOTED_STR );
171 }
172
173 static wxString trimTrailingZeros( const wxString& aValue )
174 {
175 wxString result( aValue );
176
177 if( !result.Contains( '.' ) )
178 return result;
179
180 while( result.Length() > 1 && result.Last() == '0' )
181 result.Truncate( result.Length() - 1 );
182
183 if( result.Last() == '.' )
184 result.Truncate( result.Length() - 1 );
185
186 return result;
187 }
188
189 static wxString formatDouble( double aValue, int aPrecision = 6 )
190 {
191 wxString formatted = wxString::FromCDouble( aValue, aPrecision );
192 return trimTrailingZeros( formatted );
193 }
194
195 wxString buildRule( const RULE_GENERATION_CONTEXT& aContext,
196 const std::vector<wxString>& aConstraintClauses ) const
197 {
198 wxString rule;
199 rule << wxS( "(rule " ) << formatRuleName( aContext.ruleName ) << wxS( "\n" );
200
201 if( !aContext.comment.IsEmpty() )
202 {
203 wxArrayString lines = wxSplit( aContext.comment, '\n', '\0' );
204
205 for( const wxString& line : lines )
206 {
207 if( line.IsEmpty() )
208 continue;
209
210 rule << wxS( "\t# " ) << line << wxS( "\n" );
211 }
212 }
213
214 wxString condExpr = aContext.conditionExpression;
215
216 if( !aContext.layerClause.IsEmpty() )
217 {
218 if( aContext.layerClause.StartsWith( wxS( "(condition " ) ) )
219 {
220 wxString expr = aContext.layerClause.AfterFirst( '"' ).BeforeLast( '"' );
221
222 if( !condExpr.IsEmpty() )
223 condExpr = expr + wxS( " && " ) + condExpr;
224 else
225 condExpr = expr;
226 }
227 else
228 {
229 rule << wxS( "\t" ) << aContext.layerClause << wxS( "\n" );
230 }
231 }
232
233 for( const wxString& clause : aConstraintClauses )
234 {
235 if( clause.IsEmpty() )
236 continue;
237
238 rule << wxS( "\t" ) << clause << wxS( "\n" );
239 }
240
241 if( !condExpr.IsEmpty() )
242 rule << wxS( "\t(condition \"" ) << quoteString( condExpr ) << wxS( "\")\n" );
243
244 rule << wxS( ")" );
245
246 return rule;
247 }
248
249private:
250 std::vector<PCB_LAYER_ID> m_layers;
251 wxString m_layerSource;
256 bool m_wasEdited = false;
258};
259
260#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()
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.
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.