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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef DRC_RE_BASE_CONSTRAINT_DATA_H_
25#define DRC_RE_BASE_CONSTRAINT_DATA_H_
26
27#include <bitmaps.h>
32#include <string_utils.h>
33#include <wx/arrstr.h>
34
35#include <vector>
36
37
39{
40public:
42
43 explicit DRC_RE_BASE_CONSTRAINT_DATA( int aId, int aParentId, wxString aRuleName ) :
44 RULE_EDITOR_DATA_BASE( aId, aParentId, aRuleName )
45 {
46 }
47
48 virtual ~DRC_RE_BASE_CONSTRAINT_DATA() = default;
49
56 virtual VALIDATION_RESULT Validate() const { return VALIDATION_RESULT(); }
57
58 virtual wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) { return wxEmptyString; }
59
67
75 virtual std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const { return {}; }
76
81 virtual std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const
82 {
83 return {};
84 }
85
86 std::vector<PCB_LAYER_ID> GetLayers() { return m_layers; }
87
88 void SetLayers( std::vector<PCB_LAYER_ID> aLayers ) { m_layers = aLayers; }
89
90 wxString GetLayerSource() { return m_layerSource; }
91
92 void SetLayerSource( wxString aSource ) { m_layerSource = aSource; }
93
94 wxString GetRuleCondition() { return m_ruleCondition; }
95
96 void SetRuleCondition( wxString aRuleCondition ) { m_ruleCondition = aRuleCondition; }
97
98 wxString GetConstraintCode() const { return m_constraintCode; }
99
100 void SetConstraintCode( wxString aCode ) { m_constraintCode = aCode; }
101
102 wxString GetGeneratedRule() const { return m_generatedRule; }
103
104 void SetGeneratedRule( const wxString& aRule ) { m_generatedRule = aRule; }
105
106 wxString GetOriginalRuleText() const { return m_originalRuleText; }
107
108 void SetOriginalRuleText( const wxString& aText ) { m_originalRuleText = aText; }
109
110 bool WasEdited() const { return m_wasEdited; }
111
112 void SetWasEdited( bool aEdited ) { m_wasEdited = aEdited; }
113
114 SEVERITY GetSeverity() const { return m_severity; }
115
116 void SetSeverity( SEVERITY aSeverity ) { m_severity = aSeverity; }
117
118 void CopyFrom( const ICopyable& aSource ) override
119 {
120 const auto& source = dynamic_cast<const DRC_RE_BASE_CONSTRAINT_DATA&>( aSource );
121
123
124 m_layers = source.m_layers;
125 m_layerSource = source.m_layerSource;
126 m_constraintCode = source.m_constraintCode;
127 m_generatedRule = source.m_generatedRule;
128 m_originalRuleText = source.m_originalRuleText;
129 m_wasEdited = source.m_wasEdited;
130 m_severity = source.m_severity;
131 m_ruleCondition = source.m_ruleCondition;
132 }
133
138 static wxString sanitizeRuleName( const wxString& aRuleName )
139 {
140 if( aRuleName.IsEmpty() )
141 return wxString( wxS( "rule" ) );
142
143 wxString result;
144 result.reserve( aRuleName.length() );
145
146 for( wxUniChar c : aRuleName )
147 {
148 if( wxIsspace( c ) )
149 {
150 result.append( '_' );
151 }
152 else if( wxIsalnum( c ) || c == '_' || c == '-' || c == '.' )
153 {
154 result.append( c );
155 }
156 else
157 {
158 result.append( '_' );
159 }
160 }
161
162 // Avoid names starting with a digit which S-expression parsers treat specially.
163 if( !result.empty() && wxIsdigit( *result.begin() ) )
164 result.insert( 0, "_" );
165
166 return result;
167 }
168
169protected:
170 static wxString quoteString( const wxString& aCondition )
171 {
172 return EscapeString( aCondition, CTX_QUOTED_STR );
173 }
174
175 static wxString trimTrailingZeros( const wxString& aValue )
176 {
177 wxString result( aValue );
178
179 if( !result.Contains( '.' ) )
180 return result;
181
182 while( result.Length() > 1 && result.Last() == '0' )
183 result.Truncate( result.Length() - 1 );
184
185 if( result.Last() == '.' )
186 result.Truncate( result.Length() - 1 );
187
188 return result;
189 }
190
191 static wxString formatDouble( double aValue, int aPrecision = 6 )
192 {
193 wxString formatted = wxString::FromCDouble( aValue, aPrecision );
194 return trimTrailingZeros( formatted );
195 }
196
197 wxString buildRule( const RULE_GENERATION_CONTEXT& aContext,
198 const std::vector<wxString>& aConstraintClauses ) const
199 {
200 wxString rule;
201 rule << wxS( "(rule " ) << sanitizeRuleName( aContext.ruleName ) << wxS( "\n" );
202
203 if( !aContext.comment.IsEmpty() )
204 {
205 wxArrayString lines = wxSplit( aContext.comment, '\n', '\0' );
206
207 for( const wxString& line : lines )
208 {
209 if( line.IsEmpty() )
210 continue;
211
212 rule << wxS( "\t# " ) << line << wxS( "\n" );
213 }
214 }
215
216 if( !aContext.layerClause.IsEmpty() )
217 rule << wxS( "\t" ) << aContext.layerClause << wxS( "\n" );
218
219 for( const wxString& clause : aConstraintClauses )
220 {
221 if( clause.IsEmpty() )
222 continue;
223
224 rule << wxS( "\t" ) << clause << wxS( "\n" );
225 }
226
227 if( !aContext.conditionExpression.IsEmpty() )
228 rule << wxS( "\t(condition \"" ) << quoteString( aContext.conditionExpression ) << wxS( "\")\n" );
229
230 rule << wxS( ")" );
231
232 return rule;
233 }
234
235private:
236 std::vector<PCB_LAYER_ID> m_layers;
237 wxString m_layerSource;
242 bool m_wasEdited = false;
244};
245
246#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)
DRC_RE_BASE_CONSTRAINT_DATA(int aId, int aParentId, wxString aRuleName)
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
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)
std::vector< PCB_LAYER_ID > m_layers
static wxString sanitizeRuleName(const wxString &aRuleName)
Sanitize a rule name for use in S-expression output.
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)
void SetRuleCondition(wxString aRuleCondition)
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.