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#include "dsnlexer.h"
34
35
37{
38public:
40
41 explicit DRC_RE_BASE_CONSTRAINT_DATA( int aId, int aParentId, const wxString& aRuleName ) :
42 RULE_EDITOR_DATA_BASE( aId, aParentId, aRuleName )
43 {
44 }
45
46 virtual ~DRC_RE_BASE_CONSTRAINT_DATA() = default;
47
54 virtual VALIDATION_RESULT Validate() const { return VALIDATION_RESULT(); }
55
56 virtual wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) { return wxEmptyString; }
57
65
73 virtual std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const { return {}; }
74
79 virtual std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const
80 {
81 return {};
82 }
83
84 std::vector<PCB_LAYER_ID> GetLayers() { return m_layers; }
85
86 void SetLayers( std::vector<PCB_LAYER_ID> aLayers ) { m_layers = aLayers; }
87
88 wxString GetLayerSource() { return m_layerSource; }
89
90 void SetLayerSource( const wxString& aSource ) { m_layerSource = aSource; }
91
92 wxString GetRuleCondition() { return m_ruleCondition; }
93
94 void SetRuleCondition( const wxString &aRuleCondition ) { m_ruleCondition = aRuleCondition; }
95
96 wxString GetConstraintCode() const { return m_constraintCode; }
97
98 void SetConstraintCode( const wxString& aCode ) { m_constraintCode = aCode; }
99
100 wxString GetGeneratedRule() const { return m_generatedRule; }
101
102 void SetGeneratedRule( const wxString& aRule ) { m_generatedRule = aRule; }
103
104 wxString GetOriginalRuleText() const { return m_originalRuleText; }
105
106 void SetOriginalRuleText( const wxString& aText ) { m_originalRuleText = aText; }
107
108 bool WasEdited() const { return m_wasEdited; }
109
110 void SetWasEdited( bool aEdited ) { m_wasEdited = aEdited; }
111
113
114 void SetOriginalEntryCount( int aCount ) { m_originalEntryCount = aCount; }
115
116 int GetSourceRule() const { return m_sourceRule; }
117
118 void SetSourceRule( int aIndex ) { m_sourceRule = aIndex; }
119
120 SEVERITY GetSeverity() const { return m_severity; }
121
122 void SetSeverity( SEVERITY aSeverity ) { m_severity = aSeverity; }
123
124 void CopyFrom( const ICopyable& aSource ) override
125 {
126 const auto& source = dynamic_cast<const DRC_RE_BASE_CONSTRAINT_DATA&>( aSource );
127
129
130 m_layers = source.m_layers;
131 m_layerSource = source.m_layerSource;
132 m_constraintCode = source.m_constraintCode;
133 m_generatedRule = source.m_generatedRule;
134 m_originalRuleText = source.m_originalRuleText;
135 m_wasEdited = source.m_wasEdited;
136 m_originalEntryCount = source.m_originalEntryCount;
137 m_severity = source.m_severity;
138 m_ruleCondition = source.m_ruleCondition;
139 }
140
148 static wxString formatRuleName( const wxString& aRuleName )
149 {
150 if( aRuleName.IsEmpty() )
151 return wxString( wxS( "\"\"" ) );
152
153 bool needsQuoting = false;
154
155 char ruleName[1024];
156
157 for( size_t ii = 0; ii < sizeof( ruleName ) && ii < aRuleName.size(); ++ii )
158 ruleName[ii] = aRuleName[ii];
159
160 if( DSNLEXER::isNumber( ruleName, ruleName + std::min( aRuleName.size(), sizeof( ruleName ) ) ) )
161 {
162 needsQuoting = true;
163 }
164 else
165 {
166 for( wxUniChar c : aRuleName )
167 {
168 if( !( wxIsalnum( c ) || c == '_' || c == '-' || c == '.' ) )
169 {
170 needsQuoting = true;
171 break;
172 }
173 }
174 }
175
176 if( !needsQuoting )
177 return aRuleName;
178
179 wxString escaped = EscapeString( aRuleName, CTX_QUOTED_STR );
180 return wxString( wxS( "\"" ) ) + escaped + wxS( "\"" );
181 }
182
183protected:
184 static wxString quoteString( const wxString& aCondition )
185 {
186 return EscapeString( aCondition, CTX_QUOTED_STR );
187 }
188
189 static wxString trimTrailingZeros( const wxString& aValue )
190 {
191 wxString result( aValue );
192
193 if( !result.Contains( '.' ) )
194 return result;
195
196 while( result.Length() > 1 && result.Last() == '0' )
197 result.Truncate( result.Length() - 1 );
198
199 if( result.Last() == '.' )
200 result.Truncate( result.Length() - 1 );
201
202 return result;
203 }
204
205 static wxString formatDouble( double aValue, int aPrecision = 6 )
206 {
207 wxString formatted = wxString::FromCDouble( aValue, aPrecision );
208 return trimTrailingZeros( formatted );
209 }
210
211 wxString buildRule( const RULE_GENERATION_CONTEXT& aContext,
212 const std::vector<wxString>& aConstraintClauses ) const
213 {
214 wxString rule;
215 rule << wxS( "(rule " ) << formatRuleName( aContext.ruleName ) << wxS( "\n" );
216
217 if( !aContext.comment.IsEmpty() )
218 {
219 wxArrayString lines = wxSplit( aContext.comment, '\n', '\0' );
220
221 for( const wxString& line : lines )
222 {
223 if( line.IsEmpty() )
224 continue;
225
226 rule << wxS( "\t# " ) << line << wxS( "\n" );
227 }
228 }
229
230 wxString condExpr = aContext.conditionExpression;
231
232 if( !aContext.layerClause.IsEmpty() )
233 {
234 if( aContext.layerClause.StartsWith( wxS( "(condition " ) ) )
235 {
236 wxString expr = aContext.layerClause.AfterFirst( '"' ).BeforeLast( '"' );
237
238 if( !condExpr.IsEmpty() )
239 condExpr = expr + wxS( " && " ) + condExpr;
240 else
241 condExpr = expr;
242 }
243 else
244 {
245 rule << wxS( "\t" ) << aContext.layerClause << wxS( "\n" );
246 }
247 }
248
249 for( const wxString& clause : aConstraintClauses )
250 {
251 if( clause.IsEmpty() )
252 continue;
253
254 rule << wxS( "\t" ) << clause << wxS( "\n" );
255 }
256
257 if( !condExpr.IsEmpty() )
258 rule << wxS( "\t(condition \"" ) << quoteString( condExpr ) << wxS( "\")\n" );
259
260 rule << wxS( ")" );
261
262 return rule;
263 }
264
265private:
266 std::vector<PCB_LAYER_ID> m_layers;
267 wxString m_layerSource;
272 bool m_wasEdited = false;
274 int m_sourceRule = -1;
276};
277
278#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.
static bool isNumber(const char *cp, const char *limit)
Return true if the next sequence of text is a number: either an integer, fixed point,...
Definition dsnlexer.cpp:494
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.