KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_via_style_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_VIA_STYLE_CONSTRAINT_DATA_H_
21#define DRC_RE_VIA_STYLE_CONSTRAINT_DATA_H_
22
24
25
34
35
37{
38public:
40
43 {
44 }
45
46 explicit DRC_RE_VIA_STYLE_CONSTRAINT_DATA( int aId, const wxString& aRuleName, double aMinViaDiameter,
47 double aMaxViaDiameter, double aMinViaHoleSize, double aMaxViaHoleSize,
49
50 DRC_RE_BASE_CONSTRAINT_DATA( aId, aRuleName ),
51 m_minViaDiameter( aMinViaDiameter ),
52 m_maxViaDiameter( aMaxViaDiameter ),
53 m_minViaHoleSize( aMinViaHoleSize ),
54 m_maxViaHoleSize( aMaxViaHoleSize ),
55 m_viaType( aViaType )
56 {
57 }
58
60
62
63 std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const override
64 {
65 // Positions measured from constraint_via_style.png bitmap
66 // Format: { xStart, xEnd, yCenter }
67 return {
68 { 155, 210, 22, wxS( "mm" ), LABEL_POSITION::RIGHT,
69 _( "Smallest allowed via diameter" ) }, // [0] min_via_diameter
70 { 265, 320, 22, wxS( "mm" ), LABEL_POSITION::RIGHT, _( "Largest allowed via diameter" ),
71 wxS( "– " ) }, // [1] max_via_diameter
72 { 195, 250, 202, wxS( "mm" ), LABEL_POSITION::RIGHT,
73 _( "Smallest allowed via hole diameter" ) }, // [2] min_via_hole
74 { 305, 360, 202, wxS( "mm" ), LABEL_POSITION::RIGHT, _( "Largest allowed via hole diameter" ),
75 wxS( "– " ) }, // [3] max_via_hole
76 { 420, 525, 122, _( "Via type:" ), LABEL_POSITION::LEFT,
77 _( "Restrict this rule to a single via type" ) }, // [4] via_type dropdown
78 };
79 }
80
81 VALIDATION_RESULT Validate() const override
82 {
84
85 bool hasDiameter = m_minViaDiameter != 0 || m_maxViaDiameter != 0;
86 bool hasHoleSize = m_minViaHoleSize != 0 || m_maxViaHoleSize != 0;
87
88 if( hasDiameter )
89 {
90 if( m_minViaDiameter < 0 )
91 result.AddError( _( "Minimum Via Diameter cannot be negative" ) );
92
93 if( m_maxViaDiameter < 0 )
94 result.AddError( _( "Maximum Via Diameter cannot be negative" ) );
95
97 result.AddError( _( "Minimum Via Diameter cannot be greater than Maximum Via Diameter" ) );
98 }
99
100 if( hasHoleSize )
101 {
102 if( m_minViaHoleSize < 0 )
103 result.AddError( _( "Minimum Via Hole Size cannot be negative" ) );
104
105 if( m_maxViaHoleSize < 0 )
106 result.AddError( _( "Maximum Via Hole Size cannot be negative" ) );
107
109 result.AddError( _( "Minimum Via Hole Size cannot be greater than Maximum Via Hole Size" ) );
110 }
111
112 if( !hasDiameter && !hasHoleSize )
113 result.AddError( _( "At least one constraint must be specified" ) );
114
115 return result;
116 }
117
118 std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const override
119 {
120 auto formatDimension = []( double aValue )
121 {
122 return formatDouble( aValue ) + wxS( "mm" );
123 };
124
125 auto formatRange = [&]( const wxString& aCode, double aMin, double aOpt, double aMax )
126 {
127 wxString clause = wxS( "(constraint " ) + aCode;
128
129 if( aMin > 0 )
130 clause << wxS( " (min " ) << formatDimension( aMin ) << wxS( ")" );
131
132 if( aOpt > 0 && aOpt >= aMin && ( aMax <= 0 || aOpt <= aMax ) )
133 clause << wxS( " (opt " ) << formatDimension( aOpt ) << wxS( ")" );
134
135 if( aMax > 0 )
136 clause << wxS( " (max " ) << formatDimension( aMax ) << wxS( ")" );
137
138 return clause + wxS( ")" );
139 };
140
141 std::vector<wxString> clauses;
142
143 if( m_minViaDiameter > 0 || m_maxViaDiameter > 0 )
144 clauses.push_back(
145 formatRange( wxS( "via_diameter" ), m_minViaDiameter, m_optViaDiameter, m_maxViaDiameter ) );
146
147 if( m_minViaHoleSize > 0 || m_maxViaHoleSize > 0 )
148 clauses.push_back(
149 formatRange( wxS( "hole_size" ), m_minViaHoleSize, m_optViaHoleSize, m_maxViaHoleSize ) );
150
151 return clauses;
152 }
153
154 wxString GetConditionPredicate() const override { return GetViaTypeCondition(); }
155
156 wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) override
157 {
158 RULE_GENERATION_CONTEXT ctx = aContext;
159
160 wxString viaTypeCondition = GetConditionPredicate();
161
162 if( !viaTypeCondition.IsEmpty() )
163 {
164 if( ctx.conditionExpression.IsEmpty() )
165 ctx.conditionExpression = viaTypeCondition;
166 else
167 ctx.conditionExpression = viaTypeCondition + wxS( " && " ) + ctx.conditionExpression;
168 }
169
170 return buildRule( ctx, GetConstraintClauses( ctx ) );
171 }
172
174
175 void SetMinViaDiameter( double aMinViaDiameter ) { m_minViaDiameter = aMinViaDiameter; }
176
178
179 void SetMaxViaDiameter( double aMaxViaDiameter ) { m_maxViaDiameter = aMaxViaDiameter; }
180
182
183 void SetMinViaHoleSize( double aMinViaHoleSize ) { m_minViaHoleSize = aMinViaHoleSize; }
184
186
187 void SetMaxViaHoleSize( double aMaxViaHoleSize ) { m_maxViaHoleSize = aMaxViaHoleSize; }
188
189 // The panel has no fields for the optimums, so a loaded rule's values are carried
190 // through an edit unchanged.
191 double GetOptViaDiameter() const { return m_optViaDiameter; }
192
193 void SetOptViaDiameter( double aOptViaDiameter ) { m_optViaDiameter = aOptViaDiameter; }
194
195 double GetOptViaHoleSize() const { return m_optViaHoleSize; }
196
197 void SetOptViaHoleSize( double aOptViaHoleSize ) { m_optViaHoleSize = aOptViaHoleSize; }
198
200
201 void SetViaType( VIA_STYLE_TYPE aType ) { m_viaType = aType; }
202
203 wxString GetViaTypeCondition() const
204 {
205 switch( m_viaType )
206 {
207 case VIA_STYLE_TYPE::THROUGH: return wxS( "A.Via_Type == 'Through'" );
208 case VIA_STYLE_TYPE::MICRO: return wxS( "A.Via_Type == 'Micro'" );
209 case VIA_STYLE_TYPE::BLIND: return wxS( "A.Via_Type == 'Blind'" );
210 case VIA_STYLE_TYPE::BURIED: return wxS( "A.Via_Type == 'Buried'" );
212 default: return wxEmptyString;
213 }
214 }
215
216 void CopyFrom( const ICopyable& aSource ) override
217 {
218 const auto& source = dynamic_cast<const DRC_RE_VIA_STYLE_CONSTRAINT_DATA&>( aSource );
219
221
222 m_minViaDiameter = source.m_minViaDiameter;
223 m_maxViaDiameter = source.m_maxViaDiameter;
224 m_minViaHoleSize = source.m_minViaHoleSize;
225 m_maxViaHoleSize = source.m_maxViaHoleSize;
226 m_optViaDiameter = source.m_optViaDiameter;
227 m_optViaHoleSize = source.m_optViaHoleSize;
228
229 m_viaType = source.m_viaType;
230 }
231
232private:
233 double m_minViaDiameter{ 0 };
234 double m_maxViaDiameter{ 0 };
235 double m_minViaHoleSize{ 0 };
236 double m_maxViaHoleSize{ 0 };
237 double m_optViaDiameter{ 0 };
238 double m_optViaHoleSize{ 0 };
240};
241
242#endif // DRC_RE_VIA_STYLE_CONSTRAINT_DATA_H_
BITMAPS
A list of all bitmap identifiers.
@ constraint_via_style
static wxString formatDouble(double aValue, int aPrecision=6)
wxString buildRule(const RULE_GENERATION_CONTEXT &aContext, const std::vector< wxString > &aConstraintClauses) const
void CopyFrom(const ICopyable &aSource) override
wxString GetConditionPredicate() const override
Returns a predicate this panel holds in its own fields rather than in the rule condition,...
BITMAPS GetOverlayBitmap() const override
Returns the bitmap to use for the overlay panel background.
DRC_RE_VIA_STYLE_CONSTRAINT_DATA(int aId, const wxString &aRuleName, double aMinViaDiameter, double aMaxViaDiameter, double aMinViaHoleSize, double aMaxViaHoleSize, VIA_STYLE_TYPE aViaType=VIA_STYLE_TYPE::ANY)
void CopyFrom(const ICopyable &aSource) override
std::vector< wxString > GetConstraintClauses(const RULE_GENERATION_CONTEXT &aContext) const override
Returns just the constraint clauses without the rule wrapper.
virtual ~DRC_RE_VIA_STYLE_CONSTRAINT_DATA()=default
DRC_RE_VIA_STYLE_CONSTRAINT_DATA(const DRC_RE_BASE_CONSTRAINT_DATA &aBaseData)
std::vector< DRC_RE_FIELD_POSITION > GetFieldPositions() const override
Returns the field positions for controls overlaid on the constraint bitmap.
VALIDATION_RESULT Validate() const override
Validates the constraint data.
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
Abstract interface class to enable polymorphic copying between objects.
@ RIGHT
Label to the right of the field.
@ LEFT
Label to the left of the field.
#define _(s)
std::string source
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.