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>
27 #include <dialog_text_entry.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.ShowModal() != 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.ShowModal() != 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
179  editFrame.OnModify();
180 
181  return footprint;
182 }
183 
184 
186  int aTextSize, int aPadCount )
187 {
188  PCB_EDIT_FRAME& editFrame = *getEditFrame<PCB_EDIT_FRAME>();
189 
190  FOOTPRINT* footprint = editFrame.CreateNewFootprint( aValue );
191 
192  if( aTextSize > 0 )
193  {
194  footprint->Reference().SetTextSize( wxSize( aTextSize, aTextSize ) );
195  footprint->Reference().SetTextThickness( aTextSize / 5 );
196  footprint->Value().SetTextSize( wxSize( aTextSize, aTextSize ) );
197  footprint->Value().SetTextThickness( aTextSize / 5 );
198  }
199 
200  // Create 2 pads used in gaps and stubs. The gap is between these 2 pads
201  // the stub is the pad 2
202  wxString Line;
203  int pad_num = 1;
204 
205  while( aPadCount-- )
206  {
207  PAD* pad = new PAD( footprint );
208 
209  footprint->Add( pad, ADD_MODE::INSERT );
210 
211  int tw = editFrame.GetDesignSettings().GetCurrentTrackWidth();
212  pad->SetSize( wxSize( tw, tw ) );
213 
214  pad->SetPosition( footprint->GetPosition() );
215  pad->SetShape( PAD_SHAPE_RECT );
217  pad->SetLayerSet( F_Cu );
218 
219  Line.Printf( wxT( "%d" ), pad_num );
220  pad->SetName( Line );
221  pad_num++;
222  }
223 
224  return footprint;
225 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:240
wxString GetValue() const
void OnModify() override
Function OnModify 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 Overloaded in FOOTPRINT_EDIT_FRAME.
int GetCurrentTrackWidth() const
Function GetCurrentTrackWidth.
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)
Function SetAnchorPadShape Set the shape of the anchor pad for custm shped pads.
Definition: pad.h:200
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:126
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:244
void SetSize(const wxSize &aSize)
Definition: pad.h:225
FOOTPRINT * createFootprint(MICROWAVE_FOOTPRINT_SHAPE aFootprintShape)
Creates a footprint "GAP" or "STUB" used in micro wave designs.
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
PADS & Pads()
Definition: footprint.h:182
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:473
FP_TEXT & Reference()
Definition: footprint.h:474
#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:444
const wxSize & GetSize() const
Definition: pad.h:226
FOOTPRINT * footprint() const
void SetX0(int x)
Definition: pad.h:223
void SetShape(PAD_SHAPE_T aShape)
Set the new shape of this pad.
Definition: pad.h:150
FOOTPRINT * CreateNewFootprint(const wxString &aFootprintName)
Function CreateNewFootprint Creates a new footprint, at position 0,0 The new footprint contains only ...
const wxPoint & GetPos0() const
Definition: pad.h:220
void SetLayerSet(LSET aLayers) override
Definition: pad.h:344
wxPoint GetPosition() const override
Definition: pad.h:167
#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:161
void SetY(int y)
Definition: pad.h:216
PCB_EDIT_FRAME is 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:200
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, EDA_DATA_TYPE aType)
Function StringFromValue returns the string from aValue according to units (inch, mm ....
Definition: base_units.cpp:220
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:164
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Definition: footprint.cpp:442
Definition: pad.h:59
MICROWAVE_FOOTPRINT_SHAPE
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetY0(int y)
Definition: pad.h:222
void CalculateBoundingBox()
Function CalculateBoundingBox calculates the bounding box in board coordinates.
Definition: footprint.cpp:565
void SetX(int x)
Definition: pad.h:217
void SetAttribute(PAD_ATTR_T aAttribute)
Definition: pcbnew/pad.cpp:512