KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_abs_length_two_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_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA_H_
21#define DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA_H_
22
24
25
27{
28public:
30
36
37 explicit DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA( int aId, int aParentId, double aOptLength, double aTolerance,
38 const wxString& aRuleName ) :
39 DRC_RE_BASE_CONSTRAINT_DATA( aId, aParentId, aRuleName ),
40 m_optLength( aOptLength ),
41 m_tolerance( aTolerance )
42 {
43 }
44
46
48
49 std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const override
50 {
51 // Format: { xStart, xEnd, yCenter }
52 // Two fields side-by-side, opt_length and tolerance
53 return {
54 { 80, 135, 127, wxS( "mm" ), LABEL_POSITION::RIGHT }, // opt_length
55 { 185, 240, 127, wxS( "mm" ), LABEL_POSITION::RIGHT, wxS( "\u00B1" ) }, // tolerance (±)
56 };
57 }
58
59 double GetOptimumLength() const { return m_optLength; }
60
61 void SetOptimumLength( double aLength ) { m_optLength = aLength; }
62
63 double GetTolerance() const { return m_tolerance; }
64
65 void SetTolerance( double aTolerance ) { m_tolerance = aTolerance; }
66
67 // Computed from opt +/- tolerance
68 double GetMinimumLength() const { return m_optLength - m_tolerance; }
69
70 double GetMaximumLength() const { return m_optLength + m_tolerance; }
71
72 VALIDATION_RESULT Validate() const override
73 {
75
76 if( m_optLength <= 0 )
77 result.AddError( _( "Optimum Length must be greater than 0" ) );
78
79 if( m_tolerance < 0 )
80 result.AddError( _( "Tolerance must be greater than or equal to 0" ) );
81
82 if( result.isValid )
83 {
84 if( GetMinimumLength() <= 0 )
85 result.AddError( _( "Tolerance is too large: resulting minimum length is not positive" ) );
86 }
87
88 return result;
89 }
90
91 std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const override
92 {
93 auto formatDistance = []( double aValue )
94 {
95 return formatDouble( aValue ) + wxS( "mm" );
96 };
97
98 wxString code = GetConstraintCode();
99
100 if( code.IsEmpty() )
101 code = wxS( "length" );
102
103 wxString clause = wxString::Format( wxS( "(constraint %s (min %s) (opt %s) (max %s))" ), code,
104 formatDistance( GetMinimumLength() ), formatDistance( m_optLength ),
105 formatDistance( GetMaximumLength() ) );
106
107 return { clause };
108 }
109
110 wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) override
111 {
112 return buildRule( aContext, GetConstraintClauses( aContext ) );
113 }
114
115 void CopyFrom( const ICopyable& aSource ) override
116 {
117 const auto& source =
118 dynamic_cast<const DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA&>( aSource );
119
121
122 m_optLength = source.m_optLength;
123 m_tolerance = source.m_tolerance;
124 }
125
126private:
127 double m_optLength{ 0 };
128 double m_tolerance{ 0 };
129};
130
132{
133public:
135
137
138 std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const override
139 {
140 return {
141 { 80, 135, 142, wxS( "mm" ), LABEL_POSITION::RIGHT }, // opt_length
142 { 185, 240, 142, wxS( "mm" ), LABEL_POSITION::RIGHT, wxS( "\u00B1" ) }, // tolerance (±)
143 { 17, 72, 15, wxS( "mm" ), LABEL_POSITION::RIGHT }, // max_skew
144 { 113, 313, 15, _( "Within diff pairs" ), LABEL_POSITION::RIGHT }, // checkbox (within_diff_pairs)
145 };
146 }
147
148 double GetMaxSkew() const { return m_maxSkew; }
149
150 void SetMaxSkew( double aSkew ) { m_maxSkew = aSkew; }
151
152 bool GetWithinDiffPairs() const { return m_withinDiffPairs; }
153
154 void SetWithinDiffPairs( bool aWithinDiffPairs ) { m_withinDiffPairs = aWithinDiffPairs; }
155
156 std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const override
157 {
158 std::vector<wxString> clauses;
159
160 if( GetOptimumLength() > 0 )
162
163 if( m_maxSkew > 0 )
164 {
165 wxString skewClause;
166
168 {
169 skewClause = wxString::Format( wxS( "(constraint skew (max %smm) (within_diff_pairs))" ),
171 }
172 else
173 {
174 skewClause = wxString::Format( wxS( "(constraint skew (max %smm))" ), formatDouble( m_maxSkew ) );
175 }
176
177 clauses.push_back( skewClause );
178 }
179
180 return clauses;
181 }
182
183 void CopyFrom( const ICopyable& aSource ) override
184 {
185 const auto& source =
186 dynamic_cast<const DRC_RE_MATCHED_LENGTH_DIFF_PAIR_CONSTRAINT_DATA&>( aSource );
187
189
190 m_maxSkew = source.m_maxSkew;
191 m_withinDiffPairs = source.m_withinDiffPairs;
192 }
193
195 {
197
198 bool hasLength = ( GetOptimumLength() > 0 || GetTolerance() > 0 );
199 bool hasSkew = ( m_maxSkew != 0 );
200
201 if( !hasLength && !hasSkew )
202 result.AddError( _( "At least one constraint must be defined" ) );
203
204 if( hasLength )
206
207 if( m_maxSkew < 0 )
208 result.AddError( _( "Maximum Skew must be greater than or equal to 0" ) );
209
210 return result;
211 }
212
213private:
214 double m_maxSkew{ 0 };
215 bool m_withinDiffPairs{ false };
216};
217
218#endif // DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA_H_
BITMAPS
A list of all bitmap identifiers.
@ constraint_matched_length_diff_pair_v2
@ constraint_absolute_length_2
VALIDATION_RESULT Validate() const override
Validates the constraint data.
BITMAPS GetOverlayBitmap() const override
Returns the bitmap to use for the overlay panel background.
DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA(int aId, int aParentId, double aOptLength, double aTolerance, const wxString &aRuleName)
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
std::vector< wxString > GetConstraintClauses(const RULE_GENERATION_CONTEXT &aContext) const override
Returns just the constraint clauses without the rule wrapper.
std::vector< DRC_RE_FIELD_POSITION > GetFieldPositions() const override
Returns the field positions for controls overlaid on the constraint bitmap.
DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA(const DRC_RE_BASE_CONSTRAINT_DATA &aBaseData)
virtual ~DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA()=default
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
std::vector< DRC_RE_FIELD_POSITION > GetFieldPositions() const override
Returns the field positions for controls overlaid on the constraint bitmap.
BITMAPS GetOverlayBitmap() const override
Returns the bitmap to use for the overlay panel background.
VALIDATION_RESULT Validate() const override
Validates the constraint data.
std::vector< wxString > GetConstraintClauses(const RULE_GENERATION_CONTEXT &aContext) const override
Returns just the constraint clauses without the rule wrapper.
Abstract interface class to enable polymorphic copying between objects.
@ RIGHT
Label to the right of the field.
#define _(s)
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.