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
29#include <string_utils.h>
30#include <wx/arrstr.h>
31
32
34{
35public:
37
38 explicit DRC_RE_BASE_CONSTRAINT_DATA( int aId, int aParentId, wxString aRuleName ) :
39 RULE_EDITOR_DATA_BASE( aId, aParentId, aRuleName )
40 {
41 }
42
43 virtual ~DRC_RE_BASE_CONSTRAINT_DATA() = default;
44
51 virtual VALIDATION_RESULT Validate() const { return VALIDATION_RESULT(); }
52
53 virtual wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) { return wxEmptyString; }
54
55 std::vector<PCB_LAYER_ID> GetLayers() { return m_layers; }
56
57 void SetLayers( std::vector<PCB_LAYER_ID> aLayers ) { m_layers = aLayers; }
58
59 wxString GetRuleCondition() { return m_ruleCondition; }
60
61 void SetRuleCondition( wxString aRuleCondition ) { m_ruleCondition = aRuleCondition; }
62
63 wxString GetConstraintCode() { return m_constraintCode; }
64
65 void SetConstraintCode( wxString aCode ) { m_constraintCode = aCode; }
66
67 wxString GetGeneratedRule() const { return m_generatedRule; }
68
69 void SetGeneratedRule( const wxString& aRule ) { m_generatedRule = aRule; }
70
71 wxString GetOriginalRuleText() const { return m_originalRuleText; }
72
73 void SetOriginalRuleText( const wxString& aText ) { m_originalRuleText = aText; }
74
75 bool WasEdited() const { return m_wasEdited; }
76
77 void SetWasEdited( bool aEdited ) { m_wasEdited = aEdited; }
78
79 void CopyFrom( const ICopyable& aSource ) override
80 {
81 const auto& source = dynamic_cast<const DRC_RE_BASE_CONSTRAINT_DATA&>( aSource );
82
84
85 m_layers = source.m_layers;
86 m_constraintCode = source.m_constraintCode;
87 m_generatedRule = source.m_generatedRule;
88 m_originalRuleText = source.m_originalRuleText;
89 m_wasEdited = source.m_wasEdited;
90 }
91
92protected:
93 static wxString sanitizeRuleName( const wxString& aRuleName )
94 {
95 if( aRuleName.IsEmpty() )
96 return wxString( wxS( "rule" ) );
97
98 wxString result;
99 result.reserve( aRuleName.length() );
100
101 for( wxUniChar c : aRuleName )
102 {
103 if( wxIsspace( c ) )
104 {
105 result.append( '_' );
106 }
107 else if( wxIsalnum( c ) || c == '_' || c == '-' || c == '.' )
108 {
109 result.append( c );
110 }
111 else
112 {
113 result.append( '_' );
114 }
115 }
116
117 // Avoid names starting with a digit which S-expression parsers treat specially.
118 if( !result.empty() && wxIsdigit( *result.begin() ) )
119 result.insert( 0, "_" );
120
121 return result;
122 }
123
124 static wxString quoteString( const wxString& aCondition )
125 {
126 return EscapeString( aCondition, CTX_QUOTED_STR );
127 }
128
129 static wxString trimTrailingZeros( const wxString& aValue )
130 {
131 wxString result( aValue );
132
133 if( !result.Contains( '.' ) )
134 return result;
135
136 while( result.Length() > 1 && result.Last() == '0' )
137 result.Truncate( result.Length() - 1 );
138
139 if( result.Last() == '.' )
140 result.Truncate( result.Length() - 1 );
141
142 return result;
143 }
144
145 static wxString formatDouble( double aValue, int aPrecision = 6 )
146 {
147 wxString formatted = wxString::FromCDouble( aValue, aPrecision );
148 return trimTrailingZeros( formatted );
149 }
150
151 wxString buildRule( const RULE_GENERATION_CONTEXT& aContext,
152 const std::vector<wxString>& aConstraintClauses ) const
153 {
154 wxString rule;
155 rule << wxS( "(rule " ) << sanitizeRuleName( aContext.ruleName ) << wxS( "\n" );
156
157 if( !aContext.comment.IsEmpty() )
158 {
159 wxArrayString lines = wxSplit( aContext.comment, '\n', '\0' );
160
161 for( const wxString& line : lines )
162 {
163 if( line.IsEmpty() )
164 continue;
165
166 rule << wxS( "\t# " ) << line << wxS( "\n" );
167 }
168 }
169
170 if( !aContext.layerClause.IsEmpty() )
171 rule << wxS( "\t" ) << aContext.layerClause << wxS( "\n" );
172
173 for( const wxString& clause : aConstraintClauses )
174 {
175 if( clause.IsEmpty() )
176 continue;
177
178 rule << wxS( "\t" ) << clause << wxS( "\n" );
179 }
180
181 if( !aContext.conditionExpression.IsEmpty() )
182 rule << wxS( "\t(condition \"" ) << quoteString( aContext.conditionExpression ) << wxS( "\")\n" );
183
184 rule << wxS( ")" );
185
186 return rule;
187 }
188
189private:
190 std::vector<PCB_LAYER_ID> m_layers;
195 bool m_wasEdited = false;
196};
197
198#endif // DRC_RE_BASE_CONSTRAINT_DATA_H_
virtual VALIDATION_RESULT Validate() const
Validates the constraint data.
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
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)
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.
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.