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 <wxPcbStruct.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 
45 using namespace KIGFX;
46 
47 static TOOL_ACTION ACT_StartTuning( "pcbnew.LengthTuner.StartTuning", AS_CONTEXT, 'X',
48  _( "New Track" ), _( "Starts laying a new track." ) );
49 
50 static TOOL_ACTION ACT_EndTuning( "pcbnew.LengthTuner.EndTuning", AS_CONTEXT, WXK_END,
51  _( "End Track" ), _( "Stops laying the current meander." ) );
52 
53 static TOOL_ACTION ACT_Settings( "pcbnew.LengthTuner.Settings", AS_CONTEXT, 'L',
54  _( "Length Tuning Settings" ), _( "Sets the length tuning parameters for currently routed item." ) );
55 
56 static TOOL_ACTION ACT_SpacingIncrease( "pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, '1',
57  _( "Increase spacing" ), _( "Increase meander spacing by one step." ) );
58 
59 static TOOL_ACTION ACT_SpacingDecrease( "pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, '2',
60  _( "Decrease spacing" ), _( "Decrease meander spacing by one step." ) );
61 
62 static TOOL_ACTION ACT_AmplIncrease( "pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, '3',
63  _( "Increase amplitude" ), _( "Increase meander amplitude by one step." ) );
64 
65 static TOOL_ACTION ACT_AmplDecrease( "pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, '4',
66  _( "Decrease amplitude" ), _( "Decrease meander amplitude by one step." ) );
67 
68 
70  TOOL_BASE( "pcbnew.LengthTuner" )
71 {
72 }
73 
74 
76 {
77 public:
79  {
80  SetTitle( _( "Length Tuner" ) );
81  DisplayTitle( true );
82 
83  //Add( ACT_StartTuning );
84  //Add( ACT_EndTuning );
85 
86  //AppendSeparator();
87 
92  Add( ACT_Settings );
93  }
94 
95 private:
96  CONTEXT_MENU* create() const override
97  {
98  return new TUNER_TOOL_MENU();
99  }
100 };
101 
102 
104 {
105 }
106 
107 
109 {
110  TOOL_BASE::Reset( aReason );
111 }
112 
113 
115 {
116  wxPoint p = wxGetMousePosition();
117 
118  p.x += 20;
119  p.y += 20;
120 
121  aPopup.UpdateStatus( m_router );
122  aPopup.Move( p );
123 }
124 
125 
127 {
128  if( m_startItem )
129  {
131 
132  if( m_startItem->Net() >= 0 )
133  highlightNet( true, m_startItem->Net() );
134  }
135 
136  controls()->ForceCursorPosition( false );
137  controls()->SetAutoPan( true );
138 
140  {
141  wxMessageBox( m_router->FailureReason(), _( "Error" ) );
142  highlightNet( false );
143  return;
144  }
145 
146  PNS::MEANDER_PLACER_BASE* placer = static_cast<PNS::MEANDER_PLACER_BASE*>(
147  m_router->Placer() );
148 
150 
151  VECTOR2I end( m_startSnapPoint );
152 
153  PNS_TUNE_STATUS_POPUP statusPopup( frame() );
154  statusPopup.Popup();
155 
156  m_router->Move( end, NULL );
157  updateStatusPopup( statusPopup );
158 
159  while( OPT_TOOL_EVENT evt = Wait() )
160  {
161  if( evt->IsCancel() || evt->IsActivate() )
162  break;
163  else if( evt->IsMotion() )
164  {
165  end = evt->Position();
166  m_router->Move( end, NULL );
167  updateStatusPopup( statusPopup );
168  }
169  else if( evt->IsClick( BUT_LEFT ) )
170  {
171  if( m_router->FixRoute( evt->Position(), NULL ) )
172  break;
173  }
174  else if( evt->IsAction( &ACT_EndTuning ) )
175  {
176  if( m_router->FixRoute( end, NULL ) )
177  break;
178  }
179  else if( evt->IsAction( &ACT_AmplDecrease ) )
180  {
181  placer->AmplitudeStep( -1 );
182  m_router->Move( end, NULL );
183  updateStatusPopup( statusPopup );
184  }
185  else if( evt->IsAction( &ACT_AmplIncrease ) )
186  {
187  placer->AmplitudeStep( 1 );
188  m_router->Move( end, NULL );
189  updateStatusPopup( statusPopup );
190  }
191  else if(evt->IsAction( &ACT_SpacingDecrease ) )
192  {
193  placer->SpacingStep( -1 );
194  m_router->Move( end, NULL );
195  updateStatusPopup( statusPopup );
196  }
197  else if( evt->IsAction( &ACT_SpacingIncrease ) )
198  {
199  placer->SpacingStep( 1 );
200  m_router->Move( end, NULL );
201  updateStatusPopup( statusPopup );
202  }
203  }
204 
206  highlightNet( false );
207 }
208 
209 
211 {
212  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Trace Length" ) );
214 }
215 
216 
218 {
219  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Length" ) );
221 }
222 
223 
225 {
226  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Skew" ) );
228 }
229 
230 
232 {
236 
238 }
239 
240 
242 {
243  // Deselect all items
245 
246  Activate();
247 
248  m_router->SetMode( aMode );
249 
250  controls()->SetSnapping( true );
251  controls()->ShowCursor( true );
252  frame()->UndoRedoBlock( true );
253 
254  std::unique_ptr<TUNER_TOOL_MENU> ctxMenu( new TUNER_TOOL_MENU );
255  SetContextMenu( ctxMenu.get() );
256 
257  // Main loop: keep receiving events
258  while( OPT_TOOL_EVENT evt = Wait() )
259  {
260  if( evt->IsCancel() || evt->IsActivate() )
261  {
262  break; // Finish
263  }
264  else if( evt->IsMotion() )
265  {
266  updateStartItem( *evt );
267  }
268  else if( evt->IsClick( BUT_LEFT ) || evt->IsAction( &ACT_StartTuning ) )
269  {
270  updateStartItem( *evt );
271  performTuning();
272  }
273  }
274 
276  frame()->UndoRedoBlock( false );
277 
278  // Store routing settings till the next invocation
281 
282  return 0;
283 }
284 
286 {
288 
289  if( !placer )
290  return 0;
291 
292  PNS::MEANDER_SETTINGS settings = placer->MeanderSettings();
293  DIALOG_PNS_LENGTH_TUNING_SETTINGS settingsDlg( frame(), settings, m_router->Mode() );
294 
295  if( settingsDlg.ShowModal() )
296  placer->UpdateSettings( settings );
297 
299 
300  return 0;
301 }
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.
static TOOL_ACTION ACT_SpacingDecrease("pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, '2', _("Decrease spacing"), _("Decrease meander spacing by one step."))
Class MEANDER_PLACER_BASE.
static TOOL_ACTION ACT_AmplDecrease("pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, '4', _("Decrease amplitude"), _("Decrease meander amplitude by one step."))
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:207
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
static TOOL_ACTION ACT_SpacingIncrease("pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, '1', _("Increase spacing"), _("Increase meander spacing by one step."))
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.
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:195
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
int meanderSettingsDialog(const TOOL_EVENT &aEvent)
virtual void Move(const wxPoint &aWhere)
KIGFX::VIEW_CONTROLS * controls() const
Definition: pcb_tool.h:132
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:201
PNS::MEANDER_SETTINGS m_savedMeanderSettings
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:133
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:343
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:198
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)
static TOOL_ACTION ACT_Settings("pcbnew.LengthTuner.Settings", AS_CONTEXT, 'L', _("Length Tuning Settings"), _("Sets the length tuning parameters for currently routed item."))
void Move(const VECTOR2I &aP, ITEM *aItem)
Definition: pns_router.cpp:204
virtual void SpacingStep(int aSign)
Function SpacingStep()
bool StartRouting(const VECTOR2I &aP, ITEM *aItem, int aLayer)
Definition: pns_router.cpp:156
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 ACT_AmplIncrease("pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, '3', _("Increase amplitude"), _("Increase meander amplitude by one step."))
void StopRouting()
Definition: pns_router.cpp:368
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:571
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:486
ROUTER_MODE Mode() const
Definition: pns_router.h:124
Class TOOL_BASE.
Definition: tool_base.h:68
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the context menu.
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:177
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()
void DisplayTitle(bool aDisplay=true)
Function DisplayTitle() Decides whether a title for a pop up menu should be displayed.
virtual void SetActiveLayer(PCB_LAYER_ID aLayer) override
Function SetActiveLayer will change the currently active layer to aLayer and also update the PCB_LAYE...
Definition: pcbframe.cpp:890
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:187
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:774
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:460
virtual void updateStartItem(TOOL_EVENT &aEvent)