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