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-2021 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 <board_design_settings.h>
26 #include <footprint.h>
27 #include <pad.h>
28 #include <confirm.h>
31 #include <trigo.h>
32 
33 
35 {
36  int oX;
37  PAD* pad;
39  wxString msg;
40  wxString cmp_name;
41  int pad_count = 2;
42  int angle = 0;
43 
44  PCB_EDIT_FRAME& editFrame = *getEditFrame<PCB_EDIT_FRAME>();
45 
46  // Ref and value text size (O = use board default value.
47  // will be set to a value depending on the footprint size, if possible
48  int text_size = 0;
49 
50  // Enter the size of the gap or stub
51  int gap_size = editFrame.GetDesignSettings().GetCurrentTrackWidth();
52 
53  switch( aFootprintShape )
54  {
56  msg = _( "Gap Size:" );
57  cmp_name = "muwave_gap";
58  text_size = gap_size;
59  break;
60 
62  msg = _( "Stub Size:" );
63  cmp_name = "muwave_stub";
64  text_size = gap_size;
65  pad_count = 2;
66  break;
67 
69  msg = _( "Arc Stub Radius Value:" );
70  cmp_name = "muwave_arcstub";
71  pad_count = 1;
72  break;
73 
74  default:
75  msg = wxT( "???" );
76  break;
77  }
78 
79  wxString value = StringFromValue( editFrame.GetUserUnits(), gap_size );
80  WX_TEXT_ENTRY_DIALOG dlg( &editFrame, msg, _( "Create microwave footprint" ), value );
81 
82  if( dlg.ShowQuasiModal() != wxID_OK )
83  return nullptr; // cancelled by user
84 
85  value = dlg.GetValue();
86  gap_size = ValueFromString( editFrame.GetUserUnits(), value );
87 
88  bool abort = false;
89 
90  if( aFootprintShape == MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC )
91  {
92  double fcoeff = 10.0, fval;
93  msg.Printf( wxT( "%3.1f" ), angle / fcoeff );
94  WX_TEXT_ENTRY_DIALOG angledlg( &editFrame, _( "Angle in degrees:" ),
95  _( "Create microwave footprint" ), msg );
96 
97  if( angledlg.ShowQuasiModal() != wxID_OK )
98  return nullptr; // cancelled by user
99 
100  msg = angledlg.GetValue();
101 
102  if( !msg.ToDouble( &fval ) )
103  {
104  DisplayError( &editFrame, _( "Incorrect number, abort" ) );
105  abort = true;
106  }
107 
108  angle = std::abs( KiROUND( fval * fcoeff ) );
109 
110  if( angle > 1800 )
111  angle = 1800;
112  }
113 
114  if( abort )
115  return nullptr;
116 
117  footprint = createBaseFootprint( cmp_name, text_size, pad_count );
118  auto it = footprint->Pads().begin();
119  pad = *it;
120 
121  switch( aFootprintShape )
122  {
123  case MICROWAVE_FOOTPRINT_SHAPE::GAP: //Gap :
124  oX = -( gap_size + pad->GetSize().x ) / 2;
125  pad->SetX0( oX );
126 
127  pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
128 
129  pad = *( it + 1 );
130 
131  pad->SetX0( oX + gap_size + pad->GetSize().x );
132  pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
133  break;
134 
135  case MICROWAVE_FOOTPRINT_SHAPE::STUB: //Stub :
136  pad->SetName( wxT( "1" ) );
137  pad = *( it + 1 );
138  pad->SetY0( -( gap_size + pad->GetSize().y ) / 2 );
139  pad->SetSize( wxSize( pad->GetSize().x, gap_size ) );
140  pad->SetY( pad->GetPos0().y + pad->GetPosition().y );
141  break;
142 
143  case MICROWAVE_FOOTPRINT_SHAPE::STUB_ARC: // Arc Stub created by a polygonal approach:
144  {
145  pad->SetShape( PAD_SHAPE::CUSTOM );
146  pad->SetAnchorPadShape( PAD_SHAPE::RECT );
147 
148  int numPoints = ( angle / 50 ) + 3; // Note: angles are in 0.1 degrees
149  std::vector<wxPoint> polyPoints;
150  polyPoints.reserve( numPoints );
151 
152  polyPoints.emplace_back( wxPoint( 0, 0 ) );
153 
154  int theta = -angle / 2;
155 
156  for( int ii = 1; ii < numPoints - 1; ii++ )
157  {
158  wxPoint pt( 0, -gap_size );
159  RotatePoint( &pt.x, &pt.y, theta );
160  polyPoints.push_back( pt );
161 
162  theta += 50;
163 
164  if( theta > angle / 2 )
165  theta = angle / 2;
166  }
167 
168  // Close the polygon:
169  polyPoints.push_back( polyPoints[0] );
170 
171  pad->AddPrimitivePoly( polyPoints, 0, true ); // add a polygonal basic shape
172  break;
173  }
174 
175  default:
176  break;
177  }
178 
179  // Update the footprint and board
180  editFrame.OnModify();
181 
182  return footprint;
183 }
184 
185 
187  int aTextSize, int aPadCount )
188 {
189  PCB_EDIT_FRAME& editFrame = *getEditFrame<PCB_EDIT_FRAME>();
190 
191  FOOTPRINT* footprint = editFrame.CreateNewFootprint( aValue );
192 
193  if( aTextSize > 0 )
194  {
195  footprint->Reference().SetTextSize( wxSize( aTextSize, aTextSize ) );
196  footprint->Reference().SetTextThickness( aTextSize / 5 );
197  footprint->Value().SetTextSize( wxSize( aTextSize, aTextSize ) );
198  footprint->Value().SetTextThickness( aTextSize / 5 );
199  }
200 
201  // Create 2 pads used in gaps and stubs. The gap is between these 2 pads
202  // the stub is the pad 2
203  wxString Line;
204  int pad_num = 1;
205 
206  while( aPadCount-- )
207  {
208  PAD* pad = new PAD( footprint );
209 
211 
212  int tw = editFrame.GetDesignSettings().GetCurrentTrackWidth();
213  pad->SetSize( wxSize( tw, tw ) );
214 
215  pad->SetPosition( footprint->GetPosition() );
216  pad->SetShape( PAD_SHAPE::RECT );
217  pad->SetAttribute( PAD_ATTRIB::SMD );
218  pad->SetLayerSet( F_Cu );
219 
220  Line.Printf( wxT( "%d" ), pad_num );
221  pad->SetName( Line );
222  pad_num++;
223  }
224 
225  return footprint;
226 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:252
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 improvements/work-arounds from DIALOG...
Smd pad, appears on the solder paste layer (default)
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:237
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:159
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:457
FP_TEXT & Reference()
Definition: footprint.h:458
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:416
int ShowQuasiModal()
FOOTPRINT * footprint() const
#define _(s)
FOOTPRINT * CreateNewFootprint(const wxString &aFootprintName)
Creates a new footprint, at position 0,0.
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)
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:73
wxPoint GetPosition() const override
Definition: footprint.h:177
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:204
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:159
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Removes an item from the container.
Definition: footprint.cpp:472
Definition: pad.h:57
MICROWAVE_FOOTPRINT_SHAPE
EDA_UNITS GetUserUnits() const
Return the user units currently in use.