KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_numeric_input_panel.cpp
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
25
26#include <cmath>
27#include <map>
28
29#include <wx/log.h>
30
31
32const std::map<DRC_RULE_EDITOR_CONSTRAINT_NAME, BITMAPS> NumericConstraintBitMapPairs = {
61 };
62
63
65 const DRC_RE_NUMERIC_INPUT_CONSTRAINT_PANEL_PARAMS& aConstraintPanelParams ) :
67 m_isCountInput( aConstraintPanelParams.m_isCountInput ),
68 m_constraintData( aConstraintPanelParams.m_constraintData )
69{
70 auto it = NumericConstraintBitMapPairs.find( aConstraintPanelParams.m_constraintType );
71
72 bConstraintImageSizer->Add( GetConstraintImage( this, it->second ), 0,
73 wxALL | wxEXPAND, 10 );
74
75 if( !aConstraintPanelParams.m_customLabelText.IsEmpty() )
76 m_numericConstraintLabel->SetLabelText( aConstraintPanelParams.m_customLabelText );
77 else
78 m_numericConstraintLabel->SetLabelText( aConstraintPanelParams.m_constraintTitle );
79
80 if( aConstraintPanelParams.m_isCountInput )
82}
83
84
88
89
91{
93 {
94 if( m_isCountInput )
95 {
97 wxString::Format( wxS( "%d" ),
98 static_cast<int>( m_constraintData->GetNumericInputValue() ) ) );
99 }
100 else
101 {
102 m_numericConstraintCtrl->SetValue(
103 wxString::Format( _( "%.2f" ), m_constraintData->GetNumericInputValue() ) );
104 }
105 }
106
107 return true;
108}
109
110
112{
113 m_constraintData->SetNumericInputValue(
114 std::stod( m_numericConstraintCtrl->GetValue().ToStdString() ) );
115 return true;
116}
117
118
119bool DRC_RE_NUMERIC_INPUT_PANEL::ValidateInputs( int* aErrorCount, std::string* aValidationMessage )
120{
123
124 if( !result.isValid )
125 {
126 *aErrorCount = result.errors.size();
127
128 for( size_t i = 0; i < result.errors.size(); i++ )
129 *aValidationMessage += DRC_RULE_EDITOR_UTILS::FormatErrorMessage( i + 1, result.errors[i] );
130
131 return false;
132 }
133
134 return true;
135}
136
137
139{
140 if( !m_constraintData )
141 return wxEmptyString;
142
143 enum class VALUE_KIND
144 {
145 MIN,
146 MAX,
147 OPT
148 };
149
150 enum class VALUE_UNIT
151 {
152 DISTANCE,
153 ANGLE,
154 COUNT,
156 };
157
158 static const std::map<wxString, std::pair<VALUE_KIND, VALUE_UNIT>> sDescriptor = {
159 { wxS( "annular_width" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
160 { wxS( "clearance" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
161 { wxS( "connection_width" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
162 { wxS( "courtyard_clearance" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
163 { wxS( "creepage" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
164 { wxS( "daisy_chain_stub" ), { VALUE_KIND::MAX, VALUE_UNIT::DISTANCE } },
165 { wxS( "daisy_chain_stub_2" ), { VALUE_KIND::MAX, VALUE_UNIT::DISTANCE } },
166 { wxS( "edge_clearance" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
167 { wxS( "hole" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
168 { wxS( "hole_clearance" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
169 { wxS( "hole_to_hole" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
170 { wxS( "length" ), { VALUE_KIND::MAX, VALUE_UNIT::DISTANCE } },
171 { wxS( "maximum_allowed_deviation" ), { VALUE_KIND::MAX, VALUE_UNIT::DISTANCE } },
172 { wxS( "min_resolved_spokes" ), { VALUE_KIND::MIN, VALUE_UNIT::COUNT } },
173 { wxS( "net_antenna" ), { VALUE_KIND::MAX, VALUE_UNIT::DISTANCE } },
174 { wxS( "physical_clearance" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
175 { wxS( "smd_corner" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
176 { wxS( "smd_to_plane_plus" ), { VALUE_KIND::MAX, VALUE_UNIT::DISTANCE } },
177 { wxS( "silk_clearance" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
178 { wxS( "solder_mask_expansion" ), { VALUE_KIND::OPT, VALUE_UNIT::DISTANCE } },
179 { wxS( "solder_mask_sliver" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
180 { wxS( "solder_paste_abs_margin" ), { VALUE_KIND::OPT, VALUE_UNIT::DISTANCE } },
181 { wxS( "thermal_spoke_width" ), { VALUE_KIND::OPT, VALUE_UNIT::DISTANCE } },
182 { wxS( "track_angle" ), { VALUE_KIND::MIN, VALUE_UNIT::ANGLE } },
183 { wxS( "track_width" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } },
184 { wxS( "via_count" ), { VALUE_KIND::MAX, VALUE_UNIT::COUNT } },
185 { wxS( "via_diameter" ), { VALUE_KIND::MIN, VALUE_UNIT::DISTANCE } }
186 };
187
188 wxString code = m_constraintData->GetConstraintCode();
189
190 if( code.IsEmpty() )
191 code = wxS( "numeric_value" );
192
193 // Adjust mismatched mappings for count-based inputs.
194 if( code == wxS( "thermal_spoke_width" ) && m_isCountInput )
195 code = wxS( "min_resolved_spokes" );
196
197 auto descriptorIt = sDescriptor.find( code );
198
199 VALUE_KIND valueKind = VALUE_KIND::MIN;
200 VALUE_UNIT valueUnit = VALUE_UNIT::DISTANCE;
201
202 if( descriptorIt != sDescriptor.end() )
203 {
204 valueKind = descriptorIt->second.first;
205 valueUnit = descriptorIt->second.second;
206 }
207 else
208 {
209 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ),
210 wxS( "No numeric descriptor for constraint '%s', defaulting to minimum distance." ),
211 code );
212 }
213
214 auto formatValue = [&]( double aValue )
215 {
216 switch( valueUnit )
217 {
218 case VALUE_UNIT::ANGLE: return formatDouble( aValue ) + wxS( "deg" );
219 case VALUE_UNIT::COUNT:
220 {
221 long long count = static_cast<long long>( std::llround( aValue ) );
222 return wxString::Format( wxS( "%lld" ), count );
223 }
224 case VALUE_UNIT::UNITLESS: return formatDouble( aValue );
225 case VALUE_UNIT::DISTANCE:
226 default: return formatDouble( aValue ) + wxS( "mm" );
227 }
228 };
229
230 double rawValue = m_constraintData->GetNumericInputValue();
231 wxString formattedValue = formatValue( rawValue );
232
233 wxString clause;
234
235 switch( valueKind )
236 {
237 case VALUE_KIND::MAX:
238 clause = wxString::Format( wxS( "(constraint %s (max %s))" ), code, formattedValue );
239 break;
240 case VALUE_KIND::OPT:
241 clause = wxString::Format( wxS( "(constraint %s (opt %s))" ), code, formattedValue );
242 break;
243 case VALUE_KIND::MIN:
244 default:
245 clause = wxString::Format( wxS( "(constraint %s (min %s))" ), code, formattedValue );
246 break;
247 }
248
249 wxLogTrace( wxS( "KI_TRACE_DRC_RULE_EDITOR" ), wxS( "Numeric constraint clause: %s" ), clause );
250
251 return buildRule( aContext, { clause } );
252}
@ constraint_minimum_annular_width
@ constraint_minimum_via_diameter
@ constraint_hole_size
@ constraint_soldermask_expansion
@ constraint_minimum_uvia_hole
@ constraint_hole_to_hole_distance
@ constraint_minimum_track_width
@ constraint_hole_to_hole_clearance
@ constraint_absolute_length
@ constraint_maximum_allowed_deviation
@ constraint_creepage_distance
@ constraint_minimum_angular_ring
@ constraint_silk_to_soldermask_clearance
@ constraint_minimum_item_clearance
@ constraint_minimum_connection_width
@ constraint_copper_to_edge_clearance
@ constraint_matched_length_diff_pair
@ constraint_basic_clearance
@ constraint_minimum_uvia_diameter
@ constraint_outline_clearance
@ constraint_solderpaste_expansion
@ constraint_maximum_via_count
@ constraint_minimum_soldermask_silver
@ constraint_silk_to_silk_clearance
@ constraint_minimum_thermal_relief_spoke_count
@ constraint_copper_to_hole_clearance
@ constraint_minimum_through_hole
@ constraint_minimum_clearance
DRC_RULE_EDITOR_CONSTRAINT_NAME m_constraintType
DRC_RE_NUMERIC_INPUT_PANEL_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
DRC_RE_NUMERIC_INPUT_PANEL(wxWindow *aParent, const DRC_RE_NUMERIC_INPUT_CONSTRAINT_PANEL_PARAMS &aConstraintPanelParams)
wxString GenerateRule(const RULE_GENERATION_CONTEXT &aContext) override
bool ValidateInputs(int *aErrorCount, std::string *aValidationMessage) override
std::shared_ptr< DRC_RE_NUMERIC_INPUT_CONSTRAINT_DATA > m_constraintData
wxString buildRule(const RULE_GENERATION_CONTEXT &aContext, const std::vector< wxString > &aConstraintClauses) const
static wxString formatDouble(double aValue, int aPrecision=6)
wxStaticBitmap * GetConstraintImage(wxPanel *aParent, BITMAPS aBitMap)
static std::string FormatErrorMessage(const int &aErrorCount, const std::string aErrorMessage)
const std::map< DRC_RULE_EDITOR_CONSTRAINT_NAME, BITMAPS > NumericConstraintBitMapPairs
@ MINIMUM_SOLDERMASK_SILVER
@ MINIMUM_ITEM_CLEARANCE
@ SILK_TO_SILK_CLEARANCE
@ MINIMUM_TRACK_WIDTH
@ HOLE_TO_HOLE_CLEARANCE
@ SOLDERPASTE_EXPANSION
@ SILK_TO_SOLDERMASK_CLEARANCE
@ MINIMUM_ANGULAR_RING
@ MINIMUM_UVIA_HOLE
@ BOARD_OUTLINE_CLEARANCE
@ MINIMUM_CONNECTION_WIDTH
@ SOLDERMASK_EXPANSION
@ MAXIMUM_VIA_COUNT
@ MINIMUM_ANNULAR_WIDTH
@ MINIMUM_THROUGH_HOLE
@ CREEPAGE_DISTANCE
@ ABSOLUTE_LENGTH
@ MINIMUM_CLEARANCE
@ MINIMUM_THERMAL_RELIEF_SPOKE_COUNT
@ BASIC_CLEARANCE
@ COPPER_TO_HOLE_CLEARANCE
@ MAXIMUM_ALLOWED_DEVIATION
@ MINIMUM_UVIA_DIAMETER
@ HOLE_TO_HOLE_DISTANCE
@ MATCHED_LENGTH_DIFF_PAIR
@ COPPER_TO_EDGE_CLEARANCE
@ MINIMUM_VIA_DIAMETER
#define _(s)
@ MIN
@ MAX
Result of a validation operation.
wxString result
Test unit parsing edge cases and error handling.