KiCad PCB EDA Suite
microwave_tool.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-2019 Kicad Developers, see change_log.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 #include "microwave_tool.h"
26 #include <class_draw_panel_gal.h>
27 #include <view/view_controls.h>
28 #include <view/view.h>
29 #include <tool/tool_manager.h>
30 #include <board_commit.h>
31 #include <confirm.h>
34 #include <pcbnew_id.h>
35 #include <bitmaps.h>
36 #include <class_board_item.h>
37 #include <class_module.h>
39 #include "pcb_actions.h"
40 #include "selection_tool.h"
41 
42 
44  PCB_TOOL_BASE( "pcbnew.MicrowaveTool" )
45 {
46 }
47 
48 
50 {}
51 
52 
54 {
55 }
56 
57 
59 {
60  PCB_EDIT_FRAME* frame = getEditFrame<PCB_EDIT_FRAME>();
61 
62  struct MICROWAVE_PLACER : public INTERACTIVE_PLACER_BASE
63  {
64  MICROWAVE_PLACER( PCB_EDIT_FRAME* aFrame, int aType ) :
65  m_frame( aFrame ),
66  m_itemType( aType )
67  { };
68 
69  std::unique_ptr<BOARD_ITEM> CreateItem() override
70  {
71  switch( m_itemType )
72  {
74  return std::unique_ptr<MODULE>( m_frame->Create_MuWaveComponent( 0 ) );
76  return std::unique_ptr<MODULE>( m_frame->Create_MuWaveComponent( 1 ) );
78  return std::unique_ptr<MODULE>( m_frame->Create_MuWaveComponent( 2 ) );
80  return std::unique_ptr<MODULE>( m_frame->Create_MuWavePolygonShape() );
81  default:
82  return std::unique_ptr<MODULE>();
83  };
84  }
85 
86  private:
87  PCB_EDIT_FRAME* m_frame;
88  int m_itemType;
89  };
90 
91  MICROWAVE_PLACER placer( frame, aEvent.Parameter<intptr_t>() );
92 
93  doInteractiveItemPlacement( aEvent.GetCommandStr().get(), &placer,
94  _( "Place microwave feature" ),
96 
97  return 0;
98 }
99 
100 
101 void MICROWAVE_TOOL::createInductorBetween( const VECTOR2I& aStart, const VECTOR2I& aEnd )
102 {
103  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
104 
105  MWAVE::INDUCTOR_PATTERN pattern;
106 
108 
109  pattern.m_Start = { aStart.x, aStart.y };
110  pattern.m_End = { aEnd.x, aEnd.y };
111 
112  wxString errorMessage;
113 
114  auto inductorModule = std::unique_ptr<MODULE>( CreateMicrowaveInductor( pattern, &frame,
115  errorMessage ) );
116 
117  // on any error, report if we can
118  if ( !inductorModule || !errorMessage.IsEmpty() )
119  {
120  if ( !errorMessage.IsEmpty() )
121  DisplayError( &frame, errorMessage );
122  }
123  else
124  {
125  // at this point, we can save the module
126  m_toolMgr->RunAction( PCB_ACTIONS::selectItem, true, inductorModule.get() );
127 
128  BOARD_COMMIT commit( this );
129  commit.Add( inductorModule.release() );
130  commit.Push( _("Add microwave inductor" ) );
131  }
132 }
133 
134 
135 static const COLOR4D inductorAreaFill( 0.3, 0.3, 0.5, 0.3 );
136 static const COLOR4D inductorAreaStroke( 0.4, 1.0, 1.0, 1.0 );
137 static const double inductorAreaStrokeWidth = 1.0;
138 
141 static const double inductorAreaAspect = 0.5;
142 
143 
145 {
146  using namespace KIGFX::PREVIEW;
147 
148  KIGFX::VIEW& view = *getView();
150  auto& frame = *getEditFrame<PCB_EDIT_FRAME>();
151 
152  std::string tool = aEvent.GetCommandStr().get();
153  frame.PushTool( tool );
154  Activate();
155 
156  TWO_POINT_GEOMETRY_MANAGER tpGeomMgr;
157 
158  CENTRELINE_RECT_ITEM previewRect( tpGeomMgr, inductorAreaAspect );
159 
160  previewRect.SetFillColor( inductorAreaFill );
161  previewRect.SetStrokeColor( inductorAreaStroke );
162  previewRect.SetLineWidth( inductorAreaStrokeWidth );
163 
164  bool originSet = false;
165 
166  controls.ShowCursor( true );
167  controls.SetSnapping( true );
168  controls.CaptureCursor( false );
169  controls.SetAutoPan( false );
170 
171  view.Add( &previewRect );
172 
173  while( auto evt = Wait() )
174  {
175  frame.GetCanvas()->SetCurrentCursor( wxCURSOR_PENCIL );
176  VECTOR2I cursorPos = controls.GetCursorPosition();
177 
178  auto cleanup = [&] () {
179  originSet = false;
180  controls.CaptureCursor( false );
181  controls.SetAutoPan( false );
182  view.SetVisible( &previewRect, false );
183  view.Update( &previewRect, KIGFX::GEOMETRY );
184  };
185 
186  if( evt->IsCancelInteractive() )
187  {
188  if( originSet )
189  cleanup();
190  else
191  {
192  frame.PopTool( tool );
193  break;
194  }
195  }
196 
197  else if( evt->IsActivate() )
198  {
199  if( originSet )
200  cleanup();
201 
202  if( evt->IsMoveTool() )
203  {
204  // leave ourselves on the stack so we come back after the move
205  break;
206  }
207  else
208  {
209  frame.PopTool( tool );
210  break;
211  }
212  }
213 
214  // A click or drag starts
215  else if( !originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsDrag( BUT_LEFT ) ) )
216  {
217  tpGeomMgr.SetOrigin( cursorPos );
218  tpGeomMgr.SetEnd( cursorPos );
219 
220  originSet = true;
221  controls.CaptureCursor( true );
222  controls.SetAutoPan( true );
223  }
224 
225  // another click after origin set is the end
226  // left up is also the end, as you'll only get that after a drag
227  else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
228  {
229  // second click, we're done:
230  // delegate to the point-to-point inductor creator function
231  createInductorBetween( tpGeomMgr.GetOrigin(), tpGeomMgr.GetEnd() );
232 
233  // start again if needed
234  originSet = false;
235  controls.CaptureCursor( false );
236  controls.SetAutoPan( false );
237 
238  view.SetVisible( &previewRect, false );
239  view.Update( &previewRect, KIGFX::GEOMETRY );
240  }
241 
242  // any move or drag once the origin was set updates
243  // the end point
244  else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
245  {
246  tpGeomMgr.SetAngleSnap( evt->Modifier( MD_CTRL ) );
247  tpGeomMgr.SetEnd( cursorPos );
248 
249  view.SetVisible( &previewRect, true );
250  view.Update( &previewRect, KIGFX::GEOMETRY );
251  }
252 
253  else if( evt->IsClick( BUT_RIGHT ) )
254  {
256  }
257 
258  else
259  evt->SetPassEvent();
260  }
261 
262  controls.CaptureCursor( false );
263  controls.SetAutoPan( false );
264  view.Remove( &previewRect );
265  return 0;
266 }
267 
268 
270 {
275 
277 }
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
static const double inductorAreaStrokeWidth
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
void SetLineWidth(double aNewWidth)
Set the line width to set before drawing preview
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
BOARD * board() const
void SetStrokeColor(const COLOR4D &aNewColor)
Set the stroke colour to set before drawing preview
int GetCurrentTrackWidth() const
Function GetCurrentTrackWidth.
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags) override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:91
This file is part of the common library.
static const double inductorAreaAspect
Aspect of the preview rectangle - this is hardcoded in the microwave backend for now
static TOOL_ACTION microwaveCreateStubArc
Definition: pcb_actions.h:387
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Parameters for construction of a microwave inductor.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
void Reset(RESET_REASON aReason) override
React to model/view changes
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
static TOOL_ACTION microwaveCreateLine
Definition: pcb_actions.h:391
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:531
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
static const COLOR4D inductorAreaFill(0.3, 0.3, 0.5, 0.3)
virtual void Remove(VIEW_ITEM *aItem) override
Function Remove() Removes a VIEW_ITEM from the view.
Definition: pcb_view.cpp:74
PCB_BASE_EDIT_FRAME * frame() const
void createInductorBetween(const VECTOR2I &aStart, const VECTOR2I &aEnd)
Create an inductor between the two points
const PCBNEW_SELECTION & selection() const
static const COLOR4D inductorAreaStroke(0.4, 1.0, 1.0, 1.0)
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:435
Class TOOL_EVENT.
Definition: tool_event.h:171
void SetOrigin(const VECTOR2I &aOrigin)
Set the origin of the ruler (the fixed end)
KIGFX::PCB_VIEW * view() const
Class VIEW_CONTROLS is an interface for classes handling user events controlling the view behaviour (...
static TOOL_ACTION microwaveCreateGap
Definition: pcb_actions.h:383
void SetFillColor(const COLOR4D &aNewColor)
Set the fill colour to set before drawing preview
#define _(s)
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
static TOOL_ACTION microwaveCreateStub
Definition: pcb_actions.h:385
int drawMicrowaveInductor(const TOOL_EVENT &aEvent)
Draw a microwave inductor interactively
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:463
static TOOL_ACTION microwaveCreateFunctionShape
Definition: pcb_actions.h:389
MODULE * CreateMicrowaveInductor(INDUCTOR_PATTERN &aPattern, PCB_EDIT_FRAME *aPcbFrame, wxString &aErrorMessage)
Creates an S-shaped coil footprint for microwave applications.
int addMicrowaveFootprint(const TOOL_EVENT &aEvent)
Main interactive tool
KIGFX::VIEW_CONTROLS * controls() const
static TOOL_ACTION selectItem
Selects an item (specified as the event parameter).
Definition: pcb_actions.h:76
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
virtual void PopTool(const std::string &actionName)
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1486
void Activate()
Function Activate() Runs the tool.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Function Add() Adds a VIEW_ITEM to the view.
Definition: pcb_view.cpp:58
Module description (excepted pads)
void doInteractiveItemPlacement(const std::string &aTool, INTERACTIVE_PLACER_BASE *aPlacer, const wxString &aCommitMessage, int aOptions=IPO_ROTATE|IPO_FLIP|IPO_REPEAT)
Helper function for performing a common interactive idiom: wait for a left click, place an item there...
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
Class VIEW.
Definition: view.h:61
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the end that moves with the cursor.
Color has changed.
Definition: view_item.h:57
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39