KiCad PCB EDA Suite
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-2020 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 
25 #include <footprint.h>
26 #include <confirm.h>
29 #include <trigo.h>
30 
31 
33 {
34  int oX;
35  PAD* pad;
37  wxString msg;
38  wxString cmp_name;
39  int pad_count = 2;
40  int angle = 0;
41 
42  PCB_EDIT_FRAME& editFrame = *getEditFrame<PCB_EDIT_FRAME>();
43 
44  // Ref and value text size (O = use board default value.
45  // will be set to a value depending on the footprint size, if possible
46  int text_size = 0;
47 
48  // Enter the size of the gap or stub
49  int gap_size = editFrame.GetDesignSettings().GetCurrentTrackWidth();
50 
51  switch( aFootprintShape )
52  {
54  msg = _( "Gap Size:" );
55  cmp_name = "muwave_gap";
56  text_size = gap_size;
57  break;
58 
60  msg = _( "Stub Size:" );
61  cmp_name = "muwave_stub";
62  text_size = gap_size;
63  pad_count = 2;
64  break;
65 
67  msg = _( "Arc Stub Radius Value:" );
68  cmp_name = "muwave_arcstub";
69  pad_count = 1;
70  break;
71 
72  default:
73  msg = wxT( "???" );
74  break;
75  }
76 
77  wxString value = StringFromValue( editFrame.GetUserUnits(), gap_size );
78  WX_TEXT_ENTRY_DIALOG dlg( &editFrame, msg, _( "Create microwave footprint" ), value );
79 
80  if( dlg.ShowQuasiModal() != wxID_OK )
81  return NULL; // cancelled by user
82 
83  value = dlg.GetValue();
84  gap_size = ValueFromString( editFrame.GetUserUnits(), value );
85 
86  bool abort = false;
87 
88  if( aFootprintShape == MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC )
89  {
90  double fcoeff = 10.0, fval;
91  msg.Printf( wxT( "%3.1f" ), angle / fcoeff );
92  WX_TEXT_ENTRY_DIALOG angledlg( &editFrame, _( "Angle in degrees:" ),
93  _( "Create microwave footprint" ), msg );
94 
95  if( angledlg.ShowQuasiModal() != wxID_OK )
96  return NULL; // cancelled by user
97 
98  msg = angledlg.GetValue();
99 
100  if( !msg.ToDouble( &fval ) )
101  {
102  DisplayError( &editFrame, _( "Incorrect number, abort" ) );
103  abort = true;
104  }
105 
106  angle = std::abs( KiROUND( fval * fcoeff ) );
107 
108  if( angle > 1800 )
109  angle = 1800;
110  }
111 
112  if( abort )
113  return NULL;
114 
115  footprint = createBaseFootprint( cmp_name, text_size, pad_count );
116  auto it = footprint->Pads().begin();
117  pad = *it;
118 
119  switch( aFootprintShape )
120  {
121  case MICROWAVE_FOOTPRINT_SHAPE::GAP: //Gap :
122  oX = -( gap_size + pad->GetSize().x ) / 2;
123  pad->SetX0( oX );
124 
125  pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
126 
127  pad = *( it + 1 );
128 
129  pad->SetX0( oX + gap_size + pad->GetSize().x );
130  pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
131  break;
132 
133  case MICROWAVE_FOOTPRINT_SHAPE::STUB: //Stub :
134  pad->SetName( wxT( "1" ) );
135  pad = *( it + 1 );
136  pad->SetY0( -( gap_size + pad->GetSize().y ) / 2 );
137  pad->SetSize( wxSize( pad->GetSize().x, gap_size ) );
138  pad->SetY( pad->GetPos0().y + pad->GetPosition().y );
139  break;
140 
141  case MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC: // Arc Stub created by a polygonal approach:
142  {
143  pad->SetShape( PAD_SHAPE_CUSTOM );
145 
146  int numPoints = (angle / 50) + 3; // Note: angles are in 0.1 degrees
147  std::vector<wxPoint> polyPoints;
148  polyPoints.reserve( numPoints );
149 
150  polyPoints.emplace_back( wxPoint( 0, 0 ) );
151 
152  int theta = -angle / 2;
153 
154  for( int ii = 1; ii < numPoints - 1; ii++ )
155  {
156  wxPoint pt( 0, -gap_size );
157  RotatePoint( &pt.x, &pt.y, theta );
158  polyPoints.push_back( pt );
159 
160  theta += 50;
161 
162  if( theta > angle / 2 )
163  theta = angle / 2;
164  }
165 
166  // Close the polygon:
167  polyPoints.push_back( polyPoints[0] );
168 
169  pad->AddPrimitivePoly( polyPoints, 0, true ); // add a polygonal basic shape
170  }
171  break;
172 
173  default:
174  break;
175  }
176 
177  // Update the footprint and board
178  editFrame.OnModify();
179 
180  return footprint;
181 }
182 
183 
185  int aTextSize, int aPadCount )
186 {
187  PCB_EDIT_FRAME& editFrame = *getEditFrame<PCB_EDIT_FRAME>();
188 
189  FOOTPRINT* footprint = editFrame.CreateNewFootprint( aValue );
190 
191  if( aTextSize > 0 )
192  {
193  footprint->Reference().SetTextSize( wxSize( aTextSize, aTextSize ) );
194  footprint->Reference().SetTextThickness( aTextSize / 5 );
195  footprint->Value().SetTextSize( wxSize( aTextSize, aTextSize ) );
196  footprint->Value().SetTextThickness( aTextSize / 5 );
197  }
198 
199  // Create 2 pads used in gaps and stubs. The gap is between these 2 pads
200  // the stub is the pad 2
201  wxString Line;
202  int pad_num = 1;
203 
204  while( aPadCount-- )
205  {
206  PAD* pad = new PAD( footprint );
207 
208  footprint->Add( pad, ADD_MODE::INSERT );
209 
210  int tw = editFrame.GetDesignSettings().GetCurrentTrackWidth();
211  pad->SetSize( wxSize( tw, tw ) );
212 
213  pad->SetPosition( footprint->GetPosition() );
214  pad->SetShape( PAD_SHAPE_RECT );
216  pad->SetLayerSet( F_Cu );
217 
218  Line.Printf( wxT( "%d" ), pad_num );
219  pad->SetName( Line );
220  pad_num++;
221  }
222 
223  return footprint;
224 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:253
wxString GetValue() const
void OnModify() override
Must be called after a board change to set the modified flag.
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Returns the BOARD_DESIGN_SETTINGS for the open project.
This file is part of the common library.
A KICAD version of wxTextEntryDialog which supports the various improvments/work-arounds from DIALOG_...
void SetAnchorPadShape(PAD_SHAPE_T aShape)
Set the shape of the anchor pad for custom shaped pads.
Definition: pad.h:208
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:81
void SetName(const wxString &aName)
Set the pad name (sometimes called pad number, although it can be an array reference like AA12).
Definition: pad.h:132
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:244
void SetSize(const wxSize &aSize)
Definition: pad.h:231
FOOTPRINT * createFootprint(MICROWAVE_FOOTPRINT_SHAPE aFootprintShape)
Create a footprint "GAP" or "STUB" used in micro wave designs.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:228
PADS & Pads()
Definition: footprint.h:164
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:459
FP_TEXT & Reference()
Definition: footprint.h:460
#define NULL
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:451
int ShowQuasiModal()
const wxSize & GetSize() const
Definition: pad.h:232
FOOTPRINT * footprint() const
void SetX0(int x)
Definition: pad.h:229
void SetShape(PAD_SHAPE_T aShape)
Set the new shape of this pad.
Definition: pad.h:160
FOOTPRINT * CreateNewFootprint(const wxString &aFootprintName)
Creates a new footprint, at position 0,0.
const wxPoint & GetPos0() const
Definition: pad.h:226
void SetLayerSet(LSET aLayers) override
Definition: pad.h:359
wxPoint GetPosition() const override
Definition: pad.h:177
#define _(s)
Definition: 3d_actions.cpp:33
FOOTPRINT * createBaseFootprint(const wxString &aValue, int aTextSize, int aPadCount)
Create a basic footprint for micro wave applications.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void AddPrimitivePoly(const SHAPE_POLY_SET &aPoly, int aThickness, bool aFilled)
Has meaning only for custom shape pads.
void SetPosition(const wxPoint &aPos) override
Definition: pad.h:171
void SetY(int y)
Definition: pad.h:222
The main frame for Pcbnew.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
wxPoint GetPosition() const override
Definition: footprint.h:182
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:225
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:166
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Removes an item from the container.
Definition: footprint.cpp:467
Definition: pad.h:60
MICROWAVE_FOOTPRINT_SHAPE
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetY0(int y)
Definition: pad.h:228
void SetX(int x)
Definition: pad.h:223
void SetAttribute(PAD_ATTR_T aAttribute)
Definition: pcbnew/pad.cpp:560