KiCad PCB EDA Suite
Loading...
Searching...
No Matches
microwave_footprint.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-2022 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
26#include <footprint.h>
27#include <pad.h>
28#include <confirm.h>
31#include <trigo.h>
32
33
35{
36 int offsetX;
37 int offsetY;
38 PAD* pad;
40 wxString msg;
41 wxString cmp_name;
42 int pad_count = 2;
43 EDA_ANGLE angle = ANGLE_0;
44
45 PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
46
47 // Ref and value text size (O = use board default value.
48 // will be set to a value depending on the footprint size, if possible
49 int text_size = 0;
50
51 // Enter the size of the gap or stub
52 int gap_size = editFrame->GetDesignSettings().GetCurrentTrackWidth();
53
54 switch( aFootprintShape )
55 {
56 case MICROWAVE_FOOTPRINT_SHAPE::GAP:
57 msg = _( "Gap Size:" );
58 cmp_name = wxT( "muwave_gap" );
59 text_size = gap_size;
60 break;
61
62 case MICROWAVE_FOOTPRINT_SHAPE::STUB:
63 msg = _( "Stub Size:" );
64 cmp_name = wxT( "muwave_stub" );
65 text_size = gap_size;
66 pad_count = 2;
67 break;
68
69 case MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC:
70 msg = _( "Arc Stub Radius Value:" );
71 cmp_name = wxT( "muwave_arcstub" );
72 pad_count = 1;
73 break;
74
75 default:
76 msg = wxT( "???" );
77 break;
78 }
79
80 wxString value = editFrame->StringFromValue( gap_size );
81 WX_TEXT_ENTRY_DIALOG dlg( editFrame, msg, _( "Create Microwave Footprint" ), value );
82
83 // TODO: why is this QuasiModal?
84 if( dlg.ShowQuasiModal() != wxID_OK )
85 return nullptr; // cancelled by user
86
87 value = dlg.GetValue();
88 gap_size = editFrame->ValueFromString( value );
89
90 bool abort = false;
91
92 if( aFootprintShape == MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC )
93 {
94 msg = wxT( "0.0" );
95 WX_TEXT_ENTRY_DIALOG angledlg( editFrame, _( "Angle in degrees:" ),
96 _( "Create Microwave Footprint" ), msg );
97
98 // TODO: why is this QuasiModal?
99 if( angledlg.ShowQuasiModal() != wxID_OK )
100 return nullptr; // cancelled by user
101
102 msg = angledlg.GetValue();
103
104 double fval;
105
106 if( !msg.ToDouble( &fval ) )
107 {
108 DisplayError( editFrame, _( "Incorrect number, abort" ) );
109 abort = true;
110 }
111
112 angle = EDA_ANGLE( fval, DEGREES_T );
113
114 if( angle < ANGLE_0 )
115 angle = -angle;
116
117 if( angle > ANGLE_180 )
118 angle = ANGLE_180;
119 }
120
121 if( abort )
122 return nullptr;
123
124 footprint = createBaseFootprint( cmp_name, text_size, pad_count );
125 auto it = footprint->Pads().begin();
126 pad = *it;
127
128 switch( aFootprintShape )
129 {
130 case MICROWAVE_FOOTPRINT_SHAPE::GAP: //Gap :
131 offsetX = -( gap_size + pad->GetSize().x ) / 2;
132
133 pad->SetX( pad->GetPosition().x + offsetX );
134
135 pad = *( it + 1 );
136
137 pad->SetX( pad->GetPosition().x + offsetX + gap_size + pad->GetSize().x );
138 break;
139
140 case MICROWAVE_FOOTPRINT_SHAPE::STUB: //Stub :
141 pad->SetNumber( wxT( "1" ) );
142 offsetY = -( gap_size + pad->GetSize().y ) / 2;
143
144 pad = *( it + 1 );
145 pad->SetSize( VECTOR2I( pad->GetSize().x, gap_size ) );
146 pad->SetY( pad->GetPosition().y + offsetY );
147 break;
148
149 case MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC: // Arc Stub created by a polygonal approach:
150 {
151 pad->SetShape( PAD_SHAPE::CUSTOM );
152 pad->SetAnchorPadShape( PAD_SHAPE::RECTANGLE );
153
154 int numPoints = ( angle.AsDegrees() / 5.0 ) + 3;
155 std::vector<VECTOR2I> polyPoints;
156 polyPoints.reserve( numPoints );
157
158 polyPoints.emplace_back( VECTOR2I( 0, 0 ) );
159
160 EDA_ANGLE theta = -angle / 2;
161
162 for( int ii = 1; ii < numPoints - 1; ii++ )
163 {
164 VECTOR2I pt( 0, -gap_size );
165 RotatePoint( &pt.x, &pt.y, theta );
166 polyPoints.push_back( pt );
167
168 theta += EDA_ANGLE( 5.0, DEGREES_T );
169
170 if( theta > angle / 2 )
171 theta = angle / 2;
172 }
173
174 // Close the polygon:
175 polyPoints.push_back( polyPoints[0] );
176
177 pad->AddPrimitivePoly( polyPoints, 0, true ); // add a polygonal basic shape
178 break;
179 }
180
181 default:
182 break;
183 }
184
185 // Update the footprint and board
186 editFrame->OnModify();
187
188 return footprint;
189}
190
191
193 int aTextSize, int aPadCount )
194{
195 PCB_EDIT_FRAME& editFrame = *getEditFrame<PCB_EDIT_FRAME>();
196
197 FOOTPRINT* footprint = editFrame.CreateNewFootprint( aValue, wxEmptyString, true );
198
200
201 if( aTextSize > 0 )
202 {
203 footprint->Reference().SetTextSize( VECTOR2I( aTextSize, aTextSize ) );
204 footprint->Reference().SetTextThickness( aTextSize / 5 );
205 footprint->Value().SetTextSize( VECTOR2I( aTextSize, aTextSize ) );
206 footprint->Value().SetTextThickness( aTextSize / 5 );
207 }
208
209 // Create 2 pads used in gaps and stubs. The gap is between these 2 pads
210 // the stub is the pad 2
211 int pad_num = 1;
212
213 while( aPadCount-- )
214 {
215 PAD* pad = new PAD( footprint );
216
217 footprint->Add( pad, ADD_MODE::INSERT );
218
219 int tw = editFrame.GetDesignSettings().GetCurrentTrackWidth();
220 pad->SetSize( VECTOR2I( tw, tw ) );
221
222 pad->SetPosition( footprint->GetPosition() );
223 pad->SetShape( PAD_SHAPE::RECTANGLE );
224 pad->SetAttribute( PAD_ATTRIB::SMD );
225 pad->SetLayerSet( F_Cu );
226
227 pad->SetNumber( wxString::Format( wxT( "%d" ), pad_num ) );
228 pad_num++;
229 }
230
231 return footprint;
232}
int ShowQuasiModal()
double AsDegrees() const
Definition: eda_angle.h:155
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:374
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.cpp:197
void SetAttributes(int aAttributes)
Definition: footprint.h:270
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:617
PADS & Pads()
Definition: footprint.h:188
PCB_FIELD & Reference()
Definition: footprint.h:618
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: footprint.cpp:720
VECTOR2I GetPosition() const override
Definition: footprint.h:206
FOOTPRINT * createFootprint(MICROWAVE_FOOTPRINT_SHAPE aFootprintShape)
Create a footprint "GAP" or "STUB" used in micro wave designs.
FOOTPRINT * createBaseFootprint(const wxString &aValue, int aTextSize, int aPadCount)
Create a basic footprint for micro wave applications.
Definition: pad.h:59
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Returns the BOARD_DESIGN_SETTINGS for the open project.
FOOTPRINT * CreateNewFootprint(const wxString &aFootprintName, const wxString &aLibName, bool aQuiet)
Creates a new footprint, at position 0,0.
The main frame for Pcbnew.
void OnModify() override
Must be called after a board change to set the modified flag.
FOOTPRINT * footprint() const
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.
A KICAD version of wxTextEntryDialog which supports the various improvements/work-arounds from DIALOG...
wxString GetValue() const
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
This file is part of the common library.
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:435
@ DEGREES_T
Definition: eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:439
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:74
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:75
@ F_Cu
Definition: layer_ids.h:65
MICROWAVE_FOOTPRINT_SHAPE
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:228
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588