KiCad PCB EDA Suite
Loading...
Searching...
No Matches
microwave_inductor.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) 2017-2023 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#include <base_units.h>
25#include <board_commit.h>
27#include <pad.h>
28#include <pcb_shape.h>
29#include <footprint.h>
30#include <confirm.h>
33#include <math/util.h> // for KiROUND
35#include <tool/tool_manager.h>
36#include <tools/pcb_actions.h>
37#include <pcb_edit_frame.h>
38#include <validators.h>
39
50static void gen_arc( std::vector<VECTOR2I>& aBuffer, const VECTOR2I& aStartPoint,
51 const VECTOR2I& aCenter, const EDA_ANGLE& a_ArcAngle )
52{
53 VECTOR2D first_point = VECTOR2D( aStartPoint ) - aCenter;
54 double radius = first_point.EuclideanNorm();
55 int seg_count = GetArcToSegmentCount( radius, ARC_HIGH_DEF, a_ArcAngle );
56
57 double increment_angle = a_ArcAngle.AsRadians() / seg_count;
58
59 // Creates nb_seg point to approximate arc by segments:
60 for( int ii = 1; ii <= seg_count; ii++ )
61 {
62 double rot_angle = increment_angle * ii;
63 double fcos = cos( rot_angle );
64 double fsin = sin( rot_angle );
65 VECTOR2I currpt;
66
67 // Rotate current point:
68 currpt.x = KiROUND( ( first_point.x * fcos + first_point.y * fsin ) );
69 currpt.y = KiROUND( ( first_point.y * fcos - first_point.x * fsin ) );
70
71 auto corner = aCenter + currpt;
72 aBuffer.push_back( corner );
73 }
74}
75
76
78{
79 OK,
80 TOO_LONG,
81 TOO_SHORT,
82 NO_REPR,
83};
84
85
95static INDUCTOR_S_SHAPE_RESULT BuildCornersList_S_Shape( std::vector<VECTOR2I>& aBuffer,
96 const VECTOR2I& aStartPoint,
97 const VECTOR2I& aEndPoint, int aLength,
98 int aWidth )
99{
100/* We must determine:
101 * segm_count = number of segments perpendicular to the direction
102 * segm_len = length of a strand
103 * radius = radius of rounded parts of the coil
104 * stubs_len = length of the 2 stubs( segments parallel to the direction)
105 * connecting the start point to the start point of the S shape
106 * and the ending point to the end point of the S shape
107 * The equations are (assuming the area size of the entire shape is Size:
108 * Size.x = 2 * radius + segm_len
109 * Size.y = (segm_count + 2 ) * 2 * radius + 2 * stubs_len
110 * aInductorPattern.m_length = 2 * delta // connections to the coil
111 * + (segm_count-2) * segm_len // length of the strands except 1st and last
112 * + (segm_count) * (PI * radius) // length of rounded
113 * segm_len + / 2 - radius * 2) // length of 1st and last bit
114 *
115 * The constraints are:
116 * segm_count >= 2
117 * radius < m_Size.x
118 * Size.y = (radius * 4) + (2 * stubs_len)
119 * segm_len > radius * 2
120 *
121 * The calculation is conducted in the following way:
122 * first:
123 * segm_count = 2
124 * radius = 4 * Size.x (arbitrarily fixed value)
125 * Then:
126 * Increasing the number of segments to the desired length
127 * (radius decreases if necessary)
128 */
129 wxPoint size;
130
131 // This scale factor adjusts the arc length to handle
132 // the arc to segment approximation.
133 // because we use SEGM_COUNT_PER_360DEG segment to approximate a circle,
134 // the trace len must be corrected when calculated using arcs
135 // this factor adjust calculations and must be changed if SEGM_COUNT_PER_360DEG is modified
136 // because trace using segment is shorter the corresponding arc
137 // ADJUST_SIZE is the ratio between tline len and the arc len for an arc
138 // of 360/ADJUST_SIZE angle
139 #define ADJUST_SIZE 0.988
140
141 auto pt = aEndPoint - aStartPoint;
142 EDA_ANGLE angle( pt );
143 int min_len = pt.EuclideanNorm();
144 int segm_len = 0; // length of segments
145 int full_len; // full len of shape (sum of length of all segments + arcs)
146
147 angle = -angle;
148
149 /* Note: calculations are made for a vertical coil (more easy calculations)
150 * and after points are rotated to their actual position
151 * So the main direction is the Y axis.
152 * the 2 stubs are on the Y axis
153 * the others segments are parallel to the X axis.
154 */
155
156 // Calculate the size of area (for a vertical shape)
157 size.x = min_len / 2;
158 size.y = min_len;
159
160 // Choose a reasonable starting value for the radius of the arcs.
161 int radius = std::min( aWidth * 5, size.x / 4 );
162
163 int segm_count; // number of full len segments
164 // the half size segments (first and last segment) are not counted here
165 int stubs_len = 0; // length of first or last segment (half size of others segments)
166
167 for( segm_count = 0; ; segm_count++ )
168 {
169 stubs_len = ( size.y - ( radius * 2 * (segm_count + 2 ) ) ) / 2;
170
171 if( stubs_len < size.y / 10 ) // Reduce radius.
172 {
173 stubs_len = size.y / 10;
174 radius = ( size.y - (2 * stubs_len) ) / ( 2 * (segm_count + 2) );
175
176 if( radius < aWidth ) // Radius too small.
177 {
178 // Unable to create line: Requested length value is too large for room
180 }
181 }
182
183 segm_len = size.x - ( radius * 2 );
184 full_len = 2 * stubs_len; // Length of coil connections.
185 full_len += segm_len * segm_count; // Length of full length segments.
186 full_len += KiROUND( ( segm_count + 2 ) * M_PI * ADJUST_SIZE * radius ); // Ard arcs len
187 full_len += segm_len - (2 * radius); // Length of first and last segments
188 // (half size segments len = segm_len/2 - radius).
189
190 if( full_len >= aLength )
191 break;
192 }
193
194 // Adjust len by adjusting segm_len:
195 int delta_size = full_len - aLength;
196
197 // reduce len of the segm_count segments + 2 half size segments (= 1 full size segment)
198 segm_len -= delta_size / (segm_count + 1);
199
200 // at this point, it could still be that the requested length is too
201 // short (because 4 quarter-circles are too long)
202 // to fix this is a relatively complex numerical problem which probably
203 // needs a refactor in this area. For now, just reject these cases:
204 {
205 const int min_total_length = 2 * stubs_len + 2 * M_PI * ADJUST_SIZE * radius;
206 if( min_total_length > aLength )
207 {
208 // we can't express this inductor with 90-deg arcs of this radius
210 }
211 }
212
213 if( segm_len - 2 * radius < 0 )
214 {
215 // we can't represent this exact requested length with this number
216 // of segments (using the current algorithm). This stems from when
217 // you add a segment, you also add another half-circle, so there's a
218 // little bit of "dead" space.
219 // It's a bit ugly to just reject the input, as it might be possible
220 // to tweak the radius, but, again, that probably needs a refactor.
222 }
223
224 // Generate first line (the first stub) and first arc (90 deg arc)
225 pt = aStartPoint;
226 aBuffer.push_back( pt );
227 pt.y += stubs_len;
228 aBuffer.push_back( pt );
229
230 auto centre = pt;
231 centre.x -= radius;
232 gen_arc( aBuffer, pt, centre, -ANGLE_90 );
233 pt = aBuffer.back();
234
235 int half_size_seg_len = segm_len / 2 - radius;
236
237 if( half_size_seg_len )
238 {
239 pt.x -= half_size_seg_len;
240 aBuffer.push_back( pt );
241 }
242
243 // Create shape.
244 int ii;
245 int sign = 1;
246 segm_count += 1; // increase segm_count to create the last half_size segment
247
248 for( ii = 0; ii < segm_count; ii++ )
249 {
250 if( ii & 1 ) // odd order arcs are greater than 0
251 sign = -1;
252 else
253 sign = 1;
254
255 centre = pt;
256 centre.y += radius;
257 gen_arc( aBuffer, pt, centre, ANGLE_180 * sign );
258 pt = aBuffer.back();
259 pt.x += segm_len * sign;
260 aBuffer.push_back( pt );
261 }
262
263 // The last point is false:
264 // it is the end of a full size segment, but must be
265 // the end of the second half_size segment. Change it.
266 sign *= -1;
267 aBuffer.back().x = aStartPoint.x + radius * sign;
268
269 // create last arc
270 pt = aBuffer.back();
271 centre = pt;
272 centre.y += radius;
273 gen_arc( aBuffer, pt, centre, ANGLE_90 * sign );
274
275 // Rotate point
276 angle += ANGLE_90;
277
278 for( unsigned jj = 0; jj < aBuffer.size(); jj++ )
279 RotatePoint( aBuffer[jj], aStartPoint, angle );
280
281 // push last point (end point)
282 aBuffer.push_back( aEndPoint );
283
285}
286
287
289{
290 PCB_EDIT_FRAME& editFrame = *getEditFrame<PCB_EDIT_FRAME>();
291
293
295
296 pattern.m_Start = { aStart.x, aStart.y };
297 pattern.m_End = { aEnd.x, aEnd.y };
298
299 wxString errorMessage;
300
301 auto inductorFP = std::unique_ptr<FOOTPRINT>( createMicrowaveInductor( pattern, errorMessage ) );
302
303 // on any error, report if we can
304 if ( !inductorFP || !errorMessage.IsEmpty() )
305 {
306 if ( !errorMessage.IsEmpty() )
307 editFrame.ShowInfoBarError( errorMessage );
308 }
309 else
310 {
311 // at this point, we can save the footprint
312 m_toolMgr->RunAction<EDA_ITEM*>( PCB_ACTIONS::selectItem, inductorFP.get() );
313
314 BOARD_COMMIT commit( this );
315 commit.Add( inductorFP.release() );
316 commit.Push( _("Add Microwave Inductor" ) );
317 }
318}
319
320
322 wxString& aErrorMessage )
323{
324 /* Build a microwave inductor footprint.
325 * - Length Mself.lng
326 * - Extremities Mself.m_Start and Mself.m_End
327 * We must determine:
328 * Mself.nbrin = number of segments perpendicular to the direction
329 * (The coil nbrin will demicercles + 1 + 2 1 / 4 circle)
330 * Mself.lbrin = length of a strand
331 * Mself.radius = radius of rounded parts of the coil
332 * Mself.delta = segments extremities connection between him and the coil even
333 *
334 * The equations are
335 * Mself.m_Size.x = 2 * Mself.radius + Mself.lbrin
336 * Mself.m_Size.y * Mself.delta = 2 + 2 * Mself.nbrin * Mself.radius
337 * Mself.lng = 2 * Mself.delta / / connections to the coil
338 + (Mself.nbrin-2) * Mself.lbrin / / length of the strands except 1st and last
339 + (Mself.nbrin 1) * (PI * Mself.radius) / / length of rounded
340 * Mself.lbrin + / 2 - Melf.radius * 2) / / length of 1st and last bit
341 *
342 * The constraints are:
343 * Nbrin >= 2
344 * Mself.radius < Mself.m_Size.x
345 * Mself.m_Size.y = Mself.radius * 4 + 2 * Mself.raccord
346 * Mself.lbrin> Mself.radius * 2
347 *
348 * The calculation is conducted in the following way:
349 * Initially:
350 * Nbrin = 2
351 * Radius = 4 * m_Size.x (arbitrarily fixed value)
352 * Then:
353 * Increasing the number of segments to the desired length
354 * (Radius decreases if necessary)
355 */
356
357 PAD* pad;
358 PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
359
360 VECTOR2I pt = aInductorPattern.m_End - aInductorPattern.m_Start;
361 int min_len = pt.EuclideanNorm();
362 aInductorPattern.m_Length = min_len;
363
364 // Enter the desired length.
365 wxString msg = editFrame->StringFromValue( aInductorPattern.m_Length );
366 WX_TEXT_ENTRY_DIALOG dlg( editFrame, _( "Length of Trace:" ), wxEmptyString, msg );
367
368 // TODO: why is this QuasiModal?
369 if( dlg.ShowQuasiModal() != wxID_OK )
370 return nullptr; // canceled by user
371
372 aInductorPattern.m_Length = editFrame->ValueFromString( dlg.GetValue() );
373
374 // Control values (ii = minimum length)
375 if( aInductorPattern.m_Length < min_len )
376 {
377 aErrorMessage = _( "Requested length < minimum length" );
378 return nullptr;
379 }
380
381 // Calculate the elements.
382 std::vector<VECTOR2I> buffer;
383 const INDUCTOR_S_SHAPE_RESULT res = BuildCornersList_S_Shape( buffer, aInductorPattern.m_Start,
384 aInductorPattern.m_End,
385 aInductorPattern.m_Length,
386 aInductorPattern.m_Width );
387
388 switch( res )
389 {
390 case INDUCTOR_S_SHAPE_RESULT::TOO_LONG:
391 aErrorMessage = _( "Requested length too large" );
392 return nullptr;
393 case INDUCTOR_S_SHAPE_RESULT::TOO_SHORT:
394 aErrorMessage = _( "Requested length too small" );
395 return nullptr;
396 case INDUCTOR_S_SHAPE_RESULT::NO_REPR:
397 aErrorMessage = _( "Requested length can't be represented" );
398 return nullptr;
399 case INDUCTOR_S_SHAPE_RESULT::OK:
400 break;
401 }
402
403 // Generate footprint. the value is also used as footprint name.
404 msg = wxT( "L" );
405 WX_TEXT_ENTRY_DIALOG cmpdlg( editFrame, _( "Component Value:" ), wxEmptyString, msg );
407
408 // TODO: why is this QuasiModal?
409 if( ( cmpdlg.ShowQuasiModal() != wxID_OK ) || msg.IsEmpty() )
410 return nullptr; // Aborted by user
411
412 FOOTPRINT* footprint = editFrame->CreateNewFootprint( msg, wxEmptyString );
413
414 footprint->SetFPID( LIB_ID( wxEmptyString, wxT( "mw_inductor" ) ) );
417 footprint->SetPosition( aInductorPattern.m_End );
418
419 // Generate segments
420 for( unsigned jj = 1; jj < buffer.size(); jj++ )
421 {
422 PCB_SHAPE* seg = new PCB_SHAPE( footprint, SHAPE_T::SEGMENT );
423 seg->SetStart( buffer[jj - 1] );
424 seg->SetEnd( buffer[jj] );
425 seg->SetStroke( STROKE_PARAMS( aInductorPattern.m_Width, LINE_STYLE::SOLID ) );
426 seg->SetLayer( footprint->GetLayer() );
427 footprint->Add( seg );
428 }
429
430 // Place a pad on each end of coil.
431 pad = new PAD( footprint );
432
433 footprint->Add( pad );
434
435 pad->SetNumber( wxT( "1" ) );
436 pad->SetPosition( aInductorPattern.m_End );
437
438 pad->SetSize( PADSTACK::ALL_LAYERS,
439 VECTOR2I( aInductorPattern.m_Width, aInductorPattern.m_Width ) );
440
441 pad->SetLayerSet( LSET( { footprint->GetLayer() } ) );
442 pad->SetAttribute( PAD_ATTRIB::SMD );
443 pad->SetShape( PADSTACK::ALL_LAYERS, PAD_SHAPE::CIRCLE );
444
445 PAD* newpad = new PAD( *pad );
446 const_cast<KIID&>( newpad->m_Uuid ) = KIID();
447
448 footprint->Add( newpad );
449
450 pad = newpad;
451 pad->SetNumber( wxT( "2" ) );
452 pad->SetPosition( aInductorPattern.m_Start );
453
454 // Modify text positions.
455 VECTOR2I refPos( ( aInductorPattern.m_Start.x + aInductorPattern.m_End.x ) / 2,
456 ( aInductorPattern.m_Start.y + aInductorPattern.m_End.y ) / 2 );
457
458 VECTOR2I valPos = refPos;
459
460 refPos.y -= footprint->Reference().GetTextSize().y;
461 footprint->Reference().SetPosition( refPos );
462 valPos.y += footprint->Value().GetTextSize().y;
463 footprint->Value().SetPosition( valPos );
464
465 return footprint;
466}
constexpr int ARC_HIGH_DEF
Definition: base_units.h:120
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:892
int ShowQuasiModal()
double AsRadians() const
Definition: eda_angle.h:117
void ShowInfoBarError(const wxString &aErrorMsg, bool aShowCloseButton=false, WX_INFOBAR::MESSAGE_TYPE aType=WX_INFOBAR::MESSAGE_TYPE::GENERIC)
Show the WX_INFOBAR displayed on the top of the canvas with a message and an error icon on the left o...
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
const KIID m_Uuid
Definition: eda_item.h:489
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:129
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:134
void SetEnd(const VECTOR2I &aEnd)
Definition: eda_shape.h:171
VECTOR2I GetTextSize() const
Definition: eda_text.h:241
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:60
void SetPosition(const VECTOR2I &aPos) override
Definition: footprint.cpp:2341
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:249
void SetAttributes(int aAttributes)
Definition: footprint.h:291
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:638
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: footprint.h:236
PCB_FIELD & Reference()
Definition: footprint.h:639
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: footprint.cpp:982
Definition: kiid.h:49
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:36
FOOTPRINT * createMicrowaveInductor(MICROWAVE_INDUCTOR_PATTERN &aPattern, wxString &aErrorMessage)
Create an S-shaped coil footprint for microwave applications.
void createInductorBetween(const VECTOR2I &aStart, const VECTOR2I &aEnd)
Draw a microwave inductor interactively.
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:144
Definition: pad.h:54
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: pcb_actions.h:71
FOOTPRINT * CreateNewFootprint(wxString aFootprintName, const wxString &aLibName)
Creates a new footprint, at position 0,0.
The main frame for Pcbnew.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition: pcb_shape.cpp:326
void SetStroke(const STROKE_PARAMS &aStroke) override
Definition: pcb_shape.h:90
virtual void SetPosition(const VECTOR2I &aPos) override
Definition: pcb_text.h:87
BOARD * board() const
FOOTPRINT * footprint() const
Simple container to manage line stroke parameters.
Definition: stroke_params.h:79
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:218
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:150
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
int ValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
A KICAD version of wxTextEntryDialog which supports the various improvements/work-arounds from DIALOG...
wxString GetValue() const
void SetTextValidator(wxTextValidatorStyle style)
This file is part of the common library.
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:405
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:77
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:78
a few functions useful in geometry calculations.
int GetArcToSegmentCount(int aRadius, int aErrorMax, const EDA_ANGLE &aArcAngle)
static INDUCTOR_S_SHAPE_RESULT BuildCornersList_S_Shape(std::vector< VECTOR2I > &aBuffer, const VECTOR2I &aStartPoint, const VECTOR2I &aEndPoint, int aLength, int aWidth)
Function BuildCornersList_S_Shape Create a path like a S-shaped coil.
static void gen_arc(std::vector< VECTOR2I > &aBuffer, const VECTOR2I &aStartPoint, const VECTOR2I &aCenter, const EDA_ANGLE &a_ArcAngle)
Function gen_arc generates an arc using arc approximation by lines: Center aCenter Angle "angle" (in ...
INDUCTOR_S_SHAPE_RESULT
@ TOO_SHORT
Requested length too long.
@ TOO_LONG
S-shape constructed.
@ NO_REPR
Requested length too short.
#define ADJUST_SIZE
Parameters for construction of a microwave inductor.
VECTOR3I res
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:229
constexpr int sign(T val)
Definition: util.h:159
Custom text control validator definitions.
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690