KiCad PCB EDA Suite
Loading...
Searching...
No Matches
microwave_polygon.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 2015-2016 Wayne Stambaugh <[email protected]>
7 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <confirm.h>
24#include <widgets/unit_binder.h>
25#include <pcb_edit_frame.h>
26#include <dialog_shim.h>
27#include <locale_io.h>
28#include <filter_reader.h>
30#include <board.h>
31#include <footprint.h>
32#include <pcb_shape.h>
34#include <pad.h>
35#include <math/util.h> // for KiROUND
36
37#include <wx/button.h>
38#include <wx/dialog.h>
39#include <wx/filedlg.h>
40#include <wx/radiobox.h>
41#include <wx/sizer.h>
42#include <wx/statbox.h>
43
44static std::vector< wxRealPoint > g_PolyEdges;
46static wxSize g_ShapeSize;
47static int g_PolyShapeType;
48
49
53
54
56{
57public:
58 MWAVE_POLYGONAL_SHAPE_DLG( PCB_EDIT_FRAME* parent, const wxPoint& pos );
59
61 {
62 delete m_sizeX;
63 delete m_sizeY;
64 };
65
66 bool TransferDataFromWindow() override;
67
68private:
69 void OnCancelClick( wxCommandEvent& event );
70
87 void ReadDataShapeDescr( wxCommandEvent& event );
88
90
92 wxRadioBox* m_shapeOptionCtrl;
95};
96
97
98BEGIN_EVENT_TABLE( MWAVE_POLYGONAL_SHAPE_DLG, DIALOG_SHIM )
99 EVT_BUTTON( wxID_CANCEL, MWAVE_POLYGONAL_SHAPE_DLG::OnCancelClick )
101END_EVENT_TABLE()
102
103
105 const wxPoint& framepos ) :
106 DIALOG_SHIM( parent, -1, _( "Complex Shape" ), framepos, wxDefaultSize,
107 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
108 m_sizeX(),
109 m_sizeY()
110{
111 m_frame = parent;
112
113 g_PolyEdges.clear();
114
115 wxBoxSizer* mainBoxSizer = new wxBoxSizer( wxVERTICAL );
116 SetSizer( mainBoxSizer );
117
118 // Controls
119
120 wxBoxSizer* topBoxSizer = new wxBoxSizer( wxHORIZONTAL );
121 mainBoxSizer->Add( topBoxSizer, 1, wxGROW | wxALL, 5 );
122
123 wxString shapelist[] = { _( "Normal" ), _( "Symmetrical" ), _( "Mirrored" ) };
124
125 m_shapeOptionCtrl = new wxRadioBox( this, -1, _( "Shape" ),
126 wxDefaultPosition, wxDefaultSize, 3,
127 shapelist, 1,
128 wxRA_SPECIFY_COLS );
129 topBoxSizer->Add( m_shapeOptionCtrl, 1, wxGROW | wxALL, 5 );
130
131 auto sizeSizer = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _( "Size" ) ),
132 wxVERTICAL );
133 wxBoxSizer* xSizer = new wxBoxSizer( wxHORIZONTAL );
134 wxBoxSizer* ySizer = new wxBoxSizer( wxHORIZONTAL );
135
136 topBoxSizer->Add( sizeSizer, 1, wxGROW | wxALL, 5 );
137 sizeSizer->Add( xSizer, 0, 0, 5 );
138 sizeSizer->Add( ySizer, 0, 0, 5 );
139
140 wxStaticText* xLabel = new wxStaticText( this, -1, _( "X:" ) );
141 wxTextCtrl* xCtrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString );
142 wxStaticText* xUnits = new wxStaticText( this, -1, _( "units" ) );
143
144 xSizer->Add( xLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
145 xSizer->Add( xCtrl, 1, wxALIGN_CENTER_VERTICAL, 5 );
146 xSizer->Add( xUnits, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
147 m_sizeX = new UNIT_BINDER( m_frame, xLabel, xCtrl, xUnits );
148
149 wxStaticText* yLabel = new wxStaticText( this, -1, _( "Y:" ) );
150 wxTextCtrl* yCtrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString );
151 wxStaticText* yUnits = new wxStaticText( this, -1, _( "units" ) );
152
153 ySizer->Add( yLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
154 ySizer->Add( yCtrl, 1, wxALIGN_CENTER_VERTICAL, 5 );
155 ySizer->Add( yUnits, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
156 m_sizeY = new UNIT_BINDER( m_frame, yLabel, yCtrl, yUnits );
157
158 // Buttons
159
160 wxBoxSizer* buttonsBoxSizer = new wxBoxSizer( wxHORIZONTAL );
161 mainBoxSizer->Add( buttonsBoxSizer, 0, wxALL, 5 );
162
163 wxButton* btn = new wxButton( this, ID_READ_SHAPE_FILE, _( "Read Shape Description File..." ) );
164 buttonsBoxSizer->Add( btn, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 10 );
165 buttonsBoxSizer->AddStretchSpacer();
166
167 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
168 buttonsBoxSizer->Add( sdbSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
169 wxButton* sdbSizerOK = new wxButton( this, wxID_OK );
170 sdbSizer->AddButton( sdbSizerOK );
171 wxButton* sdbSizerCancel = new wxButton( this, wxID_CANCEL );
172 sdbSizer->AddButton( sdbSizerCancel );
173 sdbSizer->Realize();
174
175 GetSizer()->SetSizeHints( this );
176}
177
178
179void MWAVE_POLYGONAL_SHAPE_DLG::OnCancelClick( wxCommandEvent& event )
180{
181 g_PolyEdges.clear();
182 event.Skip();
183}
184
185
187{
188 if( !wxDialog::TransferDataFromWindow() )
189 return false;
190
191 g_ShapeSize.x = m_sizeX->GetValue();
192 g_ShapeSize.y = m_sizeY->GetValue();
193 g_PolyShapeType = m_shapeOptionCtrl->GetSelection();
194
195 return true;
196}
197
198
200{
201 static wxString s_lastpath; // To remember the last open path during a session
202 wxString fullFileName;
203 wxString mask = wxFileSelectorDefaultWildcardStr;
204
205 fullFileName = wxFileSelector( _( "Shape Description File" ), s_lastpath,
206 fullFileName, wxEmptyString, mask, wxFD_OPEN, this );
207
208 if( fullFileName.IsEmpty() )
209 return;
210
211 wxFileName fn( fullFileName );
212 s_lastpath = fn.GetPath();
213 g_PolyEdges.clear();
214
215 FILE* File = wxFopen( fullFileName, wxT( "rt" ) );
216
217 if( File == nullptr )
218 {
219 DisplayError( this, _( "File not found" ) );
220 return;
221 }
222
223 double unitconv = pcbIUScale.IU_PER_MM;
225
226 FILE_LINE_READER fileReader( File, fullFileName );
227 FILTER_READER reader( fileReader );
228
229 LOCALE_IO toggle;
230
231 while( reader.ReadLine() )
232 {
233 char* Line = reader.Line();
234 char* param1 = strtok( Line, " =\n\r" );
235 char* param2 = strtok( nullptr, " \t\n\r" );
236
237 if( strncasecmp( param1, "Unit", 4 ) == 0 )
238 {
239 if( strncasecmp( param2, "inch", 4 ) == 0 )
240 unitconv = pcbIUScale.IU_PER_MILS*1000;
241
242 if( strncasecmp( param2, "mm", 2 ) == 0 )
243 unitconv = pcbIUScale.IU_PER_MM;
244 }
245
246 if( strncasecmp( param1, "$ENDCOORD", 8 ) == 0 )
247 break;
248
249 if( strncasecmp( param1, "$COORD", 6 ) == 0 )
250 {
251 while( reader.ReadLine() )
252 {
253 Line = reader.Line();
254 param1 = strtok( Line, " \t\n\r" );
255 param2 = strtok( nullptr, " \t\n\r" );
256
257 if( strncasecmp( param1, "$ENDCOORD", 8 ) == 0 )
258 break;
259
260 wxRealPoint coord( atof( param1 ), atof( param2 ) );
261 g_PolyEdges.push_back( coord );
262 }
263 }
264
265 if( strncasecmp( Line, "XScale", 6 ) == 0 )
266 g_ShapeScaleX = atof( param2 );
267
268 if( strncasecmp( Line, "YScale", 6 ) == 0 )
269 g_ShapeScaleY = atof( param2 );
270 }
271
272 g_ShapeScaleX *= unitconv;
273 g_ShapeScaleY *= unitconv;
274
275 m_sizeX->SetValue( (int) g_ShapeScaleX );
276 m_sizeY->SetValue( (int) g_ShapeScaleY );
277}
278
279
281{
282 PAD* pad1;
283 PAD* pad2;
285 wxString cmp_name;
286 int pad_count = 2;
287 PCB_SHAPE* shape;
288
290
291 MWAVE_POLYGONAL_SHAPE_DLG dlg( &editFrame, wxDefaultPosition );
292
293 int ret = dlg.ShowModal();
294
295 if( ret != wxID_OK )
296 {
297 g_PolyEdges.clear();
298 return nullptr;
299 }
300
301 if( g_PolyShapeType == 2 ) // mirrored
303
306
307 if(( g_ShapeSize.x ) == 0 || ( g_ShapeSize.y == 0 ) )
308 {
309 editFrame.ShowInfoBarError( _( "Shape has a null size." ) );
310 return nullptr;
311 }
312
313 if( g_PolyEdges.size() == 0 )
314 {
315 editFrame.ShowInfoBarError( _( "Shape has no points." ) );
316 return nullptr;
317 }
318
319 cmp_name = wxT( "muwave_polygon" );
320
321 // Create a footprint with 2 pads, orientation = 0, pos 0
322 footprint = createBaseFootprint( cmp_name, 0, pad_count );
323
324 // We try to place the footprint anchor to the middle of the shape len
325 VECTOR2I offset;
326 offset.x = -g_ShapeSize.x / 2;
327
328 auto it = footprint->Pads().begin();
329
330 pad1 = *it;
331 pad1->SetX( offset.x );
332
333 pad2 = *( ++it );
334 pad2->SetX( offset.x + g_ShapeSize.x );
335
336 // Add a polygonal edge (corners will be added later) on copper layer
337 shape = new PCB_SHAPE( footprint, SHAPE_T::POLY );
338 shape->SetFilled( true );
339 shape->SetLayer( F_Cu );
340
341 footprint->Add( shape, ADD_MODE::INSERT );
342
343 // Get the corner buffer of the polygonal edge
344 std::vector<VECTOR2I> polyPoints;
345 polyPoints.reserve( g_PolyEdges.size() + 2 );
346
347 // Init start point coord:
348 polyPoints.emplace_back( VECTOR2I( offset.x, 0 ) );
349
350 VECTOR2I last_coordinate;
351
352 for( wxRealPoint& pt: g_PolyEdges ) // Copy points
353 {
354 last_coordinate.x = KiROUND( pt.x * g_ShapeScaleX );
355 last_coordinate.y = -KiROUND( pt.y * g_ShapeScaleY );
356 last_coordinate += offset;
357 polyPoints.push_back( last_coordinate );
358 }
359
360 // finish the polygonal shape
361 if( last_coordinate.y != 0 )
362 polyPoints.emplace_back( VECTOR2I( last_coordinate.x, 0 ) );
363
364 switch( g_PolyShapeType )
365 {
366 case 0: // shape from file
367 case 2: // shape from file, mirrored (the mirror is already done)
368 break;
369
370 case 1: // Symmetric shape: add the symmetric (mirrored) shape
371 for( int ndx = (int) polyPoints.size() - 1; ndx >= 0; --ndx )
372 {
373 VECTOR2I pt = polyPoints[ndx];
374 pt.y = -pt.y; // mirror about X axis
375 polyPoints.push_back( pt );
376 }
377
378 break;
379 }
380
381 shape->SetPolyPoints( polyPoints );
382
383 // Set the polygon outline thickness to 0, only the polygonal shape is filled
384 // without extra thickness.
386 g_PolyEdges.clear();
387
388 editFrame.OnModify();
389 return footprint;
390}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
int ShowModal() override
void ShowInfoBarError(const wxString &aErrorMsg, bool aShowCloseButton=false, INFOBAR_MESSAGE_TYPE aType=INFOBAR_MESSAGE_TYPE::GENERIC)
Show the WX_INFOBAR displayed on the top of the canvas with a message and an error icon on the left o...
virtual void SetFilled(bool aFlag)
Definition eda_shape.h:152
void SetPolyPoints(const std::vector< VECTOR2I > &aPoints)
A LINE_READER that reads from an open file.
Definition richio.h:154
Read lines of text from another LINE_READER but only returns non-comment lines and non-blank lines fr...
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
char * Line() const
Return a pointer to the last line that was read in.
Definition richio.h:98
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
FOOTPRINT * createBaseFootprint(const wxString &aValue, int aTextSize, int aPadCount)
Create a basic footprint for micro wave applications.
FOOTPRINT * createPolygonShape()
void OnCancelClick(wxCommandEvent &event)
MWAVE_POLYGONAL_SHAPE_DLG(PCB_EDIT_FRAME *parent, const wxPoint &pos)
void ReadDataShapeDescr(wxCommandEvent &event)
Read a description shape file.
Definition pad.h:61
void SetX(int x)
Definition pad.h:264
The main frame for Pcbnew.
void OnModify() override
Must be called after a board change to set the modified flag.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStroke(const STROKE_PARAMS &aStroke) override
FOOTPRINT * footprint() const
Simple container to manage line stroke parameters.
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:182
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
#define _(s)
@ F_Cu
Definition layer_ids.h:60
static double g_ShapeScaleX
static double g_ShapeScaleY
static std::vector< wxRealPoint > g_PolyEdges
static int g_PolyShapeType
static wxSize g_ShapeSize
@ ID_READ_SHAPE_FILE
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683