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, yTop, tabOrder }
52 // Two fields side-by-side, opt_length and tolerance
53 return {
54 { 80 + DRC_RE_OVERLAY_XO, 120 + DRC_RE_OVERLAY_XO, 115 + DRC_RE_OVERLAY_YO, 1, wxS( "mm" ),
55 LABEL_POSITION::RIGHT }, // opt_length
56 { 160 + DRC_RE_OVERLAY_XO, 200 + DRC_RE_OVERLAY_XO, 115 + DRC_RE_OVERLAY_YO, 2, wxS( "mm" ),
57 LABEL_POSITION::RIGHT }, // tolerance
58 };
59 }
60
61 double GetOptimumLength() const { return m_optLength; }
62
63 void SetOptimumLength( double aLength ) { m_optLength = aLength; }
64
65 double GetTolerance() const { return m_tolerance; }
66
67 void SetTolerance( double aTolerance ) { m_tolerance = aTolerance; }
68
69 // Computed from opt +/- tolerance
70 double GetMinimumLength() const { return m_optLength - m_tolerance; }
71
72 double GetMaximumLength() const { return m_optLength + m_tolerance; }
73
74 VALIDATION_RESULT Validate() const override
75 {
77
78 if( m_optLength <= 0 )
79 result.AddError( _( "Optimum Length must be greater than 0" ) );
80
81 if( m_tolerance < 0 )
82 result.AddError( _( "Tolerance must be greater than or equal to 0" ) );
83
84 if( result.isValid )
85 {
86 if( GetMinimumLength() <= 0 )
87 result.AddError( _( "Tolerance is too large: resulting minimum length is not positive" ) );
88 }
89
90 return result;
91 }
92
93 std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const override
94 {
95 auto formatDistance = []( double aValue )
96 {
97 return formatDouble( aValue ) + wxS( "mm" );
98 };
99
100 wxString code = GetConstraintCode();
101
102 if( code.IsEmpty() )
103 code = wxS( "length" );
104
105 wxString clause = wxString::Format( wxS( "(constraint %s (min %s) (opt %s) (max %s))" ), code,
106 formatDistance( GetMinimumLength() ), formatDistance( m_optLength ),
107 formatDistance( GetMaximumLength() ) );
108
109 return { clause };
110 }
111
112 wxString GenerateRule( const RULE_GENERATION_CONTEXT& aContext ) override
113 {
114 return buildRule( aContext, GetConstraintClauses( aContext ) );
115 }
116
117 void CopyFrom( const ICopyable& aSource ) override
118 {
119 const auto& source =
120 dynamic_cast<const DRC_RE_ABSOLUTE_LENGTH_TWO_CONSTRAINT_DATA&>( aSource );
121
123
124 m_optLength = source.m_optLength;
125 m_tolerance = source.m_tolerance;
126 }
127
128private:
129 double m_optLength{ 0 };
130 double m_tolerance{ 0 };
131};
132
134{
135public:
137
139
140 std::vector<DRC_RE_FIELD_POSITION> GetFieldPositions() const override
141 {
142 return {
143 { 80 + DRC_RE_OVERLAY_XO, 120 + DRC_RE_OVERLAY_XO, 130 + DRC_RE_OVERLAY_YO, 1, wxS( "mm" ),
144 LABEL_POSITION::RIGHT }, // opt_length
145 { 160 + DRC_RE_OVERLAY_XO, 200 + DRC_RE_OVERLAY_XO, 130 + DRC_RE_OVERLAY_YO, 2, wxS( "mm" ),
146 LABEL_POSITION::RIGHT }, // tolerance
147 { 25 + DRC_RE_OVERLAY_XO, 65 + DRC_RE_OVERLAY_XO, 0 + DRC_RE_OVERLAY_YO, 3, wxS( "mm" ),
148 LABEL_POSITION::RIGHT }, // max_skew
149 };
150 }
151
152 double GetMaxSkew() const { return m_maxSkew; }
153
154 void SetMaxSkew( double aSkew ) { m_maxSkew = aSkew; }
155
156 bool GetWithinDiffPairs() const { return m_withinDiffPairs; }
157
158 void SetWithinDiffPairs( bool aWithinDiffPairs ) { m_withinDiffPairs = aWithinDiffPairs; }
159
160 std::vector<wxString> GetConstraintClauses( const RULE_GENERATION_CONTEXT& aContext ) const override
161 {
162 std::vector<wxString> clauses;
163
164 if( GetOptimumLength() > 0 )
166
167 if( m_maxSkew > 0 )
168 {
169 wxString skewClause;
170
172 {
173 skewClause = wxString::Format( wxS( "(constraint skew (max %smm) (within_diff_pairs))" ),
175 }
176 else
177 {
178 skewClause = wxString::Format( wxS( "(constraint skew (max %smm))" ), formatDouble( m_maxSkew ) );
179 }
180
181 clauses.push_back( skewClause );
182 }
183
184 return clauses;
185 }
186
187 void CopyFrom( const ICopyable& aSource ) override
188 {
189 const auto& source =
190 dynamic_cast<const DRC_RE_MATCHED_LENGTH_DIFF_PAIR_CONSTRAINT_DATA&>( aSource );
191
193
194 m_maxSkew = source.m_maxSkew;
195 m_withinDiffPairs = source.m_withinDiffPairs;
196 }
197
199 {
201
202 bool hasLength = ( GetOptimumLength() > 0 || GetTolerance() > 0 );
203 bool hasSkew = ( m_maxSkew != 0 );
204
205 if( !hasLength && !hasSkew )
206 result.AddError( _( "At least one constraint must be defined" ) );
207
208 if( hasLength )
210
211 if( m_maxSkew < 0 )
212 result.AddError( _( "Maximum Skew must be greater than or equal to 0" ) );
213
214 return result;
215 }
216
217private:
218 double m_maxSkew{ 0 };
219 bool m_withinDiffPairs{ false };
220};
221
222#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.
constexpr int DRC_RE_OVERLAY_XO
constexpr int DRC_RE_OVERLAY_YO
@ 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.