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