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, int aParentId, const wxString& aRuleName,
47 double aMinViaDiameter, double aMaxViaDiameter,
48 double aMinViaHoleSize, double aMaxViaHoleSize,
50
51 DRC_RE_BASE_CONSTRAINT_DATA( aId, aParentId, aRuleName ),
52 m_minViaDiameter( aMinViaDiameter ),
53 m_maxViaDiameter( aMaxViaDiameter ),
54 m_minViaHoleSize( aMinViaHoleSize ),
55 m_maxViaHoleSize( aMaxViaHoleSize ),
56 m_viaType( aViaType )
57 {
58 }
59
61
63
64 std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const override
65 {
66 // Positions measured from constraint_via_style.png bitmap
67 // Format: { xStart, xEnd, yCenter }
68 return {
69 { 155, 210, 22, wxS( "mm" ), LABEL_POSITION::RIGHT,
70 _( "Smallest allowed via diameter" ) }, // [0] min_via_diameter
71 { 265, 320, 22, wxS( "mm" ), LABEL_POSITION::RIGHT, _( "Largest allowed via diameter" ),
72 wxS( "– " ) }, // [1] max_via_diameter
73 { 195, 250, 202, wxS( "mm" ), LABEL_POSITION::RIGHT,
74 _( "Smallest allowed via hole diameter" ) }, // [2] min_via_hole
75 { 305, 360, 202, wxS( "mm" ), LABEL_POSITION::RIGHT, _( "Largest allowed via hole diameter" ),
76 wxS( "– " ) }, // [3] max_via_hole
77 { 420, 525, 122, _( "Via type:" ), LABEL_POSITION::LEFT,
78 _( "Restrict this rule to a single via type" ) }, // [4] via_type dropdown
79 };
80 }
81
82 VALIDATION_RESULT Validate() const override
83 {
85
86 bool hasDiameter = m_minViaDiameter > 0 || m_maxViaDiameter > 0;
87 bool hasHoleSize = m_minViaHoleSize > 0 || m_maxViaHoleSize > 0;
88
89 if( hasDiameter )
90 {
91 if( m_minViaDiameter <= 0 )
92 result.AddError( _( "Minimum Via Diameter must be greater than 0" ) );
93
94 if( m_maxViaDiameter <= 0 )
95 result.AddError( _( "Maximum Via Diameter must be greater than 0" ) );
96
98 result.AddError( _( "Minimum Via Diameter cannot be greater than Maximum Via Diameter" ) );
99 }
100
101 if( hasHoleSize )
102 {
103 if( m_minViaHoleSize <= 0 )
104 result.AddError( _( "Minimum Via Hole Size must be greater than 0" ) );
105
106 if( m_maxViaHoleSize <= 0 )
107 result.AddError( _( "Maximum Via Hole Size must be greater than 0" ) );
108
110 result.AddError( _( "Minimum Via Hole Size cannot be greater than Maximum Via Hole Size" ) );
111 }
112
113 if( !hasDiameter && !hasHoleSize )
114 result.AddError( _( "At least one constraint must be specified" ) );
115
116 return result;
117 }
118
119 std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const override
120 {
121 auto formatDimension = []( double aValue )
122 {
123 return formatDouble( aValue ) + wxS( "mm" );
124 };
125
126 std::vector<wxString> clauses;
127
128 if( m_minViaDiameter > 0 && m_maxViaDiameter > 0 )
129 {
130 clauses.push_back( wxString::Format( wxS( "(constraint via_diameter (min %s) (max %s))" ),
131 formatDimension( m_minViaDiameter ),
132 formatDimension( m_maxViaDiameter ) ) );
133 }
134
135 if( m_minViaHoleSize > 0 && m_maxViaHoleSize > 0 )
136 {
137 clauses.push_back( wxString::Format( wxS( "(constraint hole_size (min %s) (max %s))" ),
138 formatDimension( m_minViaHoleSize ),
139 formatDimension( m_maxViaHoleSize ) ) );
140 }
141
142 return clauses;
143 }
144
145 wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) override
146 {
147 RULE_GENERATION_CONTEXT ctx = aContext;
148
149 wxString viaTypeCondition = GetViaTypeCondition();
150
151 if( !viaTypeCondition.IsEmpty() )
152 {
153 if( ctx.conditionExpression.IsEmpty() )
154 ctx.conditionExpression = viaTypeCondition;
155 else
156 ctx.conditionExpression = viaTypeCondition + wxS( " && " ) + ctx.conditionExpression;
157 }
158
159 return buildRule( ctx, GetConstraintClauses( ctx ) );
160 }
161
163
164 void SetMinViaDiameter( double aMinViaDiameter ) { m_minViaDiameter = aMinViaDiameter; }
165
167
168 void SetMaxViaDiameter( double aMaxViaDiameter ) { m_maxViaDiameter = aMaxViaDiameter; }
169
171
172 void SetMinViaHoleSize( double aMinViaHoleSize ) { m_minViaHoleSize = aMinViaHoleSize; }
173
175
176 void SetMaxViaHoleSize( double aMaxViaHoleSize ) { m_maxViaHoleSize = aMaxViaHoleSize; }
177
179
180 void SetViaType( VIA_STYLE_TYPE aType ) { m_viaType = aType; }
181
182 wxString GetViaTypeCondition() const
183 {
184 switch( m_viaType )
185 {
186 case VIA_STYLE_TYPE::THROUGH: return wxS( "A.Via_Type == 'Through'" );
187 case VIA_STYLE_TYPE::MICRO: return wxS( "A.Via_Type == 'Micro'" );
188 case VIA_STYLE_TYPE::BLIND: return wxS( "A.Via_Type == 'Blind'" );
189 case VIA_STYLE_TYPE::BURIED: return wxS( "A.Via_Type == 'Buried'" );
191 default: return wxEmptyString;
192 }
193 }
194
195 void CopyFrom( const ICopyable& aSource ) override
196 {
197 const auto& source = dynamic_cast<const DRC_RE_VIA_STYLE_CONSTRAINT_DATA&>( aSource );
198
200
201 m_minViaDiameter = source.m_minViaDiameter;
202 m_maxViaDiameter = source.m_maxViaDiameter;
203 m_minViaHoleSize = source.m_minViaHoleSize;
204 m_maxViaHoleSize = source.m_maxViaHoleSize;
205
206 m_viaType = source.m_viaType;
207 }
208
209private:
210 double m_minViaDiameter{ 0 };
211 double m_maxViaDiameter{ 0 };
212 double m_minViaHoleSize{ 0 };
213 double m_maxViaHoleSize{ 0 };
215};
216
217#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
DRC_RE_VIA_STYLE_CONSTRAINT_DATA(int aId, int aParentId, const wxString &aRuleName, double aMinViaDiameter, double aMaxViaDiameter, double aMinViaHoleSize, double aMaxViaHoleSize, VIA_STYLE_TYPE aViaType=VIA_STYLE_TYPE::ANY)
BITMAPS GetOverlayBitmap() const override
Returns the bitmap to use for the overlay panel background.
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)
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.