KiCad PCB EDA Suite
length_tuner_tool.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2017 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <core/optional.h>
23 
24 #include "class_draw_panel_gal.h"
25 #include "class_board.h"
26 
27 #include <pcb_edit_frame.h>
28 #include <pcbnew_id.h>
29 #include <view/view_controls.h>
30 #include <pcb_painter.h>
33 
34 #include <tool/context_menu.h>
35 #include <tool/tool_manager.h>
36 #include <tools/pcb_actions.h>
37 
38 #include "pns_segment.h"
39 #include "pns_router.h"
40 #include "pns_meander_placer.h" // fixme: move settings to separate header
41 #include "pns_tune_status_popup.h"
42 
43 #include "length_tuner_tool.h"
44 #include <bitmaps.h>
45 
46 using namespace KIGFX;
47 
48 static TOOL_ACTION ACT_StartTuning( "pcbnew.LengthTuner.StartTuning", AS_CONTEXT, 'X',
49  _( "New Track" ), _( "Starts laying a new track." ) );
50 
51 static TOOL_ACTION ACT_EndTuning( "pcbnew.LengthTuner.EndTuning", AS_CONTEXT, WXK_END,
52  _( "End Track" ), _( "Stops laying the current meander." ) );
53 
54 static TOOL_ACTION ACT_Settings( "pcbnew.LengthTuner.Settings", AS_CONTEXT, 'L',
55  _( "Length Tuning Settings..." ), _( "Sets the length tuning parameters for currently routed item." ),
56  router_len_tuner_setup_xpm );
57 
58 static TOOL_ACTION ACT_SpacingIncrease( "pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, '1',
59  _( "Increase Spacing" ), _( "Increase meander spacing by one step." ),
60  router_len_tuner_dist_incr_xpm );
61 
62 static TOOL_ACTION ACT_SpacingDecrease( "pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, '2',
63  _( "Decrease Spacing" ), _( "Decrease meander spacing by one step." ),
64  router_len_tuner_dist_decr_xpm );
65 
66 static TOOL_ACTION ACT_AmplIncrease( "pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, '3',
67  _( "Increase Amplitude" ), _( "Increase meander amplitude by one step." ),
68  router_len_tuner_amplitude_incr_xpm );
69 
70 static TOOL_ACTION ACT_AmplDecrease( "pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, '4',
71  _( "Decrease Amplitude" ), _( "Decrease meander amplitude by one step." ),
72  router_len_tuner_amplitude_decr_xpm );
73 
74 
76  TOOL_BASE( "pcbnew.LengthTuner" )
77 {
78 }
79 
80 
82 {
83 public:
85  {
86  SetTitle( _( "Length Tuner" ) );
87  SetIcon( router_len_tuner_xpm );
88  DisplayTitle( true );
89 
90  //Add( ACT_StartTuning );
91  //Add( ACT_EndTuning );
92 
93  //AppendSeparator();
94 
95  Add( ACT_SpacingIncrease );
96  Add( ACT_SpacingDecrease );
97  Add( ACT_AmplIncrease );
98  Add( ACT_AmplDecrease );
99  Add( ACT_Settings );
100  }
101 
102 private:
103  CONTEXT_MENU* create() const override
104  {
105  return new TUNER_TOOL_MENU();
106  }
107 };
108 
109 
111 {
112 }
113 
114 
116 {
117  TOOL_BASE::Reset( aReason );
118 }
119 
120 
122 {
123  wxPoint p = wxGetMousePosition();
124 
125  p.x += 20;
126  p.y += 20;
127 
128  aPopup.UpdateStatus( m_router );
129  aPopup.Move( p );
130 }
131 
132 
134 {
135  if( m_startItem )
136  {
138 
139  if( m_startItem->Net() >= 0 )
140  highlightNet( true, m_startItem->Net() );
141  }
142 
143  controls()->ForceCursorPosition( false );
144  controls()->SetAutoPan( true );
145 
147  {
148  wxMessageBox( m_router->FailureReason(), _( "Error" ) );
149  highlightNet( false );
150  return;
151  }
152 
153  PNS::MEANDER_PLACER_BASE* placer = static_cast<PNS::MEANDER_PLACER_BASE*>(
154  m_router->Placer() );
155 
157 
158  VECTOR2I end( m_startSnapPoint );
159 
160  PNS_TUNE_STATUS_POPUP statusPopup( frame() );
161  statusPopup.Popup();
162 
163  m_router->Move( end, NULL );
164  updateStatusPopup( statusPopup );
165 
166  while( OPT_TOOL_EVENT evt = Wait() )
167  {
168  if( evt->IsCancel() || evt->IsActivate() )
169  break;
170  else if( evt->IsMotion() )
171  {
172  end = evt->Position();
173  m_router->Move( end, NULL );
174  updateStatusPopup( statusPopup );
175  }
176  else if( evt->IsClick( BUT_LEFT ) )
177  {
178  if( m_router->FixRoute( evt->Position(), NULL ) )
179  break;
180  }
181  else if( evt->IsAction( &ACT_EndTuning ) )
182  {
183  if( m_router->FixRoute( end, NULL ) )
184  break;
185  }
186  else if( evt->IsAction( &ACT_AmplDecrease ) )
187  {
188  placer->AmplitudeStep( -1 );
189  m_router->Move( end, NULL );
190  updateStatusPopup( statusPopup );
191  }
192  else if( evt->IsAction( &ACT_AmplIncrease ) )
193  {
194  placer->AmplitudeStep( 1 );
195  m_router->Move( end, NULL );
196  updateStatusPopup( statusPopup );
197  }
198  else if(evt->IsAction( &ACT_SpacingDecrease ) )
199  {
200  placer->SpacingStep( -1 );
201  m_router->Move( end, NULL );
202  updateStatusPopup( statusPopup );
203  }
204  else if( evt->IsAction( &ACT_SpacingIncrease ) )
205  {
206  placer->SpacingStep( 1 );
207  m_router->Move( end, NULL );
208  updateStatusPopup( statusPopup );
209  }
210  }
211 
213  highlightNet( false );
214 }
215 
216 
218 {
219  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Trace Length" ) );
221 }
222 
223 
225 {
226  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Length" ) );
228 }
229 
230 
232 {
233  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Skew" ) );
235 }
236 
237 
239 {
243 
245 }
246 
247 
249 {
250  // Deselect all items
252 
253  Activate();
254 
255  m_router->SetMode( aMode );
256 
257  controls()->SetSnapping( true );
258  controls()->ShowCursor( true );
259  frame()->UndoRedoBlock( true );
260 
261  std::unique_ptr<TUNER_TOOL_MENU> ctxMenu( new TUNER_TOOL_MENU );
262  SetContextMenu( ctxMenu.get() );
263 
264  // Main loop: keep receiving events
265  while( OPT_TOOL_EVENT evt = Wait() )
266  {
267  if( evt->IsCancel() || evt->IsActivate() )
268  {
269  break; // Finish
270  }
271  else if( evt->IsMotion() )
272  {
273  updateStartItem( *evt );
274  }
275  else if( evt->IsClick( BUT_LEFT ) || evt->IsAction( &ACT_StartTuning ) )
276  {
277  updateStartItem( *evt );
278  performTuning();
279  }
280  }
281 
283  frame()->UndoRedoBlock( false );
284 
285  // Store routing settings till the next invocation
288 
289  return 0;
290 }
291 
293 {
295 
296  if( !placer )
297  return 0;
298 
299  PNS::MEANDER_SETTINGS settings = placer->MeanderSettings();
300  DIALOG_PNS_LENGTH_TUNING_SETTINGS settingsDlg( frame(), settings, m_router->Mode() );
301 
302  if( settingsDlg.ShowModal() )
303  placer->UpdateSettings( settings );
304 
306 
307  return 0;
308 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
Class MEANDER_PLACER_BASE.
static TOOL_ACTION ACT_AmplIncrease("pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, '3', _("Increase Amplitude"), _("Increase meander amplitude by one step."), router_len_tuner_amplitude_incr_xpm)
virtual void SetToolID(int aId, int aCursor, const wxString &aToolMsg) override
Function SetToolID sets the tool command ID to aId and sets the cursor to aCursor.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:209
const wxString & FailureReason() const
Definition: pns_router.h:214
Class CONTEXT_MENU.
Definition: context_menu.h:44
static TOOL_ACTION ACT_StartTuning("pcbnew.LengthTuner.StartTuning", AS_CONTEXT, 'X', _("New Track"), _("Starts laying a new track."))
VIEW_CONTROLS class definition.
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:71
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Class BOARD to handle a board.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
PLACEMENT_ALGO * Placer()
Definition: pns_router.h:216
int TuneSingleTrace(const TOOL_EVENT &aEvent)
SIZES_SETTINGS m_savedSizes
Stores sizes settings between router invocations.
Definition: pns_tool_base.h:68
OPT_TOOL_EVENT Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
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:125
void updateStatusPopup(PNS_TUNE_STATUS_POPUP &aPopup)
void UndoRedoBlock(bool aBlock=true)
Function UndoRedoBlock Enables/disable undo and redo operations.
static TOOL_ACTION ACT_SpacingIncrease("pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, '1', _("Increase Spacing"), _("Increase meander spacing by one step."), router_len_tuner_dist_incr_xpm)
Class MEANDER_SETTINGS.
Definition: pns_meander.h:57
SIZES_SETTINGS & Sizes()
Definition: pns_router.h:206
CONTEXT_MENU * create() const override
Returns an instance of this class. It has to be overridden in inheriting classes. ...
virtual void UpdateSettings(const MEANDER_SETTINGS &aSettings)
static TOOL_ACTION routerActivateTuneSingleTrace
Activation of the Push and Shove router (tune single line mode)
Definition: pcb_actions.h:201
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
int meanderSettingsDialog(const TOOL_EVENT &aEvent)
virtual void Move(const wxPoint &aWhere)
KIGFX::VIEW_CONTROLS * controls() const
Definition: pcb_tool.h:133
virtual void Reset(RESET_REASON aReason)=0
Function Reset() Brings the tool to a known, initial state.
static TOOL_ACTION routerActivateTuneDiffPairSkew
Activation of the Push and Shove router (skew tuning mode)
Definition: pcb_actions.h:207
static TOOL_ACTION ACT_Settings("pcbnew.LengthTuner.Settings", AS_CONTEXT, 'L', _("Length Tuning Settings..."), _("Sets the length tuning parameters for currently routed item."), router_len_tuner_setup_xpm)
PNS::MEANDER_SETTINGS m_savedMeanderSettings
static TOOL_ACTION ACT_SpacingDecrease("pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, '2', _("Decrease Spacing"), _("Decrease meander spacing by one step."), router_len_tuner_dist_decr_xpm)
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:134
Class TOOL_EVENT.
Definition: tool_event.h:162
ITEM * m_startItem
Definition: pns_tool_base.h:69
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
bool FixRoute(const VECTOR2I &aP, ITEM *aItem)
Definition: pns_router.cpp:364
ROUTER * m_router
Definition: pns_tool_base.h:78
void SetContextMenu(CONTEXT_MENU *aMenu, CONTEXT_MENU_TRIGGER aTrigger=CMENU_BUTTON)
Function SetContextMenu()
void UpdateStatus(PNS::ROUTER *aRouter)
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Function ForceCursorPosition() Places the cursor immediately at a given point.
static TOOL_ACTION routerActivateTuneDiffPair
Activation of the Push and Shove router (diff pair tuning mode)
Definition: pcb_actions.h:204
ROUTING_SETTINGS m_savedSettings
Stores routing settings between router invocations.
Definition: pns_tool_base.h:67
int Start() const
Definition: pns_layerset.h:83
int TuneDiffPairSkew(const TOOL_EVENT &aEvent)
void Move(const VECTOR2I &aP, ITEM *aItem)
Definition: pns_router.cpp:225
virtual void SpacingStep(int aSign)
Function SpacingStep()
bool StartRouting(const VECTOR2I &aP, ITEM *aItem, int aLayer)
Definition: pns_router.cpp:170
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg...
void StopRouting()
Definition: pns_router.cpp:389
virtual void highlightNet(bool aEnabled, int aNetcode=-1)
int TuneDiffPair(const TOOL_EVENT &aEvent)
virtual void SetNoToolSelected()
Select the ID_NO_TOOL_SELECTED id tool (Idle tool)
Definition: draw_frame.cpp:574
ROUTER_MODE
Definition: pns_router.h:64
virtual void Popup(wxWindow *aFocus=NULL)
TOOL_EVENT MakeEvent() const
Function HasHotKey() Checks if the action has a hot key assigned.
Definition: tool_action.h:104
void SetMode(ROUTER_MODE aMode)
Definition: pns_router.cpp:507
ROUTER_MODE Mode() const
Definition: pns_router.h:124
Class TOOL_BASE.
Definition: tool_base.h:68
int mainLoop(PNS::ROUTER_MODE aMode)
Class TOOL_ACTION.
Definition: tool_action.h:46
static TOOL_ACTION ACT_EndTuning("pcbnew.LengthTuner.EndTuning", AS_CONTEXT, WXK_END, _("End Track"), _("Stops laying the current meander."))
int Net() const
Function Net()
Definition: pns_item.h:179
static TOOL_ACTION ACT_AmplDecrease("pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, '4', _("Decrease Amplitude"), _("Decrease meander amplitude by one step."), router_len_tuner_amplitude_decr_xpm)
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
virtual const MEANDER_SETTINGS & MeanderSettings() const
Function MeanderSettings()
void Activate()
Function Activate() Runs the tool.
virtual void AmplitudeStep(int aSign)
Function AmplitudeStep()
virtual void SetActiveLayer(PCB_LAYER_ID aLayer) override
Function SetActiveLayer will change the currently active layer to aLayer and also update the PCB_LAYE...
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:187
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:796
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:465
virtual void updateStartItem(TOOL_EVENT &aEvent)