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 <boost/optional.hpp>
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 using boost::optional;
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 
57 static TOOL_ACTION ACT_SpacingIncrease( "pcbnew.LengthTuner.SpacingIncrease", AS_CONTEXT, '1',
58  _( "Increase spacing" ), _( "Increase meander spacing by one step." ) );
59 
60 static TOOL_ACTION ACT_SpacingDecrease( "pcbnew.LengthTuner.SpacingDecrease", AS_CONTEXT, '2',
61  _( "Decrease spacing" ), _( "Decrease meander spacing by one step." ) );
62 
63 static TOOL_ACTION ACT_AmplIncrease( "pcbnew.LengthTuner.AmplIncrease", AS_CONTEXT, '3',
64  _( "Increase amplitude" ), _( "Increase meander amplitude by one step." ) );
65 
66 static TOOL_ACTION ACT_AmplDecrease( "pcbnew.LengthTuner.AmplDecrease", AS_CONTEXT, '4',
67  _( "Decrease amplitude" ), _( "Decrease meander amplitude by one step." ) );
68 
69 
71  TOOL_BASE( "pcbnew.LengthTuner" )
72 {
73 }
74 
75 
77 {
78 public:
80  {
81  SetTitle( _( "Length Tuner" ) );
82  DisplayTitle( true );
83 
84  //Add( ACT_StartTuning );
85  //Add( ACT_EndTuning );
86 
87  //AppendSeparator();
88 
93  Add( ACT_Settings );
94  }
95 
96 private:
97  CONTEXT_MENU* create() const override
98  {
99  return new TUNER_TOOL_MENU();
100  }
101 };
102 
103 
105 {
106 }
107 
108 
110 {
111  TOOL_BASE::Reset( aReason );
112 }
113 
114 
116 {
117  wxPoint p = wxGetMousePosition();
118 
119  p.x += 20;
120  p.y += 20;
121 
122  aPopup.UpdateStatus( m_router );
123  aPopup.Move( p );
124 }
125 
126 
128 {
129  if( m_startItem )
130  {
132 
133  if( m_startItem->Net() >= 0 )
134  highlightNet( true, m_startItem->Net() );
135  }
136 
137  controls()->ForceCursorPosition( false );
138  controls()->SetAutoPan( true );
139 
141  {
142  wxMessageBox( m_router->FailureReason(), _( "Error" ) );
143  highlightNet( false );
144  return;
145  }
146 
147  PNS::MEANDER_PLACER_BASE* placer = static_cast<PNS::MEANDER_PLACER_BASE*>(
148  m_router->Placer() );
149 
151 
152  VECTOR2I end( m_startSnapPoint );
153 
154  PNS_TUNE_STATUS_POPUP statusPopup( frame() );
155  statusPopup.Popup();
156 
157  m_router->Move( end, NULL );
158  updateStatusPopup( statusPopup );
159 
160  while( OPT_TOOL_EVENT evt = Wait() )
161  {
162  if( evt->IsCancel() || evt->IsActivate() )
163  break;
164  else if( evt->IsMotion() )
165  {
166  end = evt->Position();
167  m_router->Move( end, NULL );
168  updateStatusPopup( statusPopup );
169  }
170  else if( evt->IsClick( BUT_LEFT ) )
171  {
172  if( m_router->FixRoute( evt->Position(), NULL ) )
173  break;
174  }
175  else if( evt->IsAction( &ACT_EndTuning ) )
176  {
177  if( m_router->FixRoute( end, NULL ) )
178  break;
179  }
180  else if( evt->IsAction( &ACT_AmplDecrease ) )
181  {
182  placer->AmplitudeStep( -1 );
183  m_router->Move( end, NULL );
184  updateStatusPopup( statusPopup );
185  }
186  else if( evt->IsAction( &ACT_AmplIncrease ) )
187  {
188  placer->AmplitudeStep( 1 );
189  m_router->Move( end, NULL );
190  updateStatusPopup( statusPopup );
191  }
192  else if(evt->IsAction( &ACT_SpacingDecrease ) )
193  {
194  placer->SpacingStep( -1 );
195  m_router->Move( end, NULL );
196  updateStatusPopup( statusPopup );
197  }
198  else if( evt->IsAction( &ACT_SpacingIncrease ) )
199  {
200  placer->SpacingStep( 1 );
201  m_router->Move( end, NULL );
202  updateStatusPopup( statusPopup );
203  }
204  }
205 
207  highlightNet( false );
208 }
209 
210 
212 {
213  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Trace Length" ) );
215 }
216 
217 
219 {
220  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Length" ) );
222 }
223 
224 
226 {
227  frame()->SetToolID( ID_TRACK_BUTT, wxCURSOR_PENCIL, _( "Tune Diff Pair Skew" ) );
229 }
230 
231 
233 {
237 
239 }
240 
241 
243 {
244  // Deselect all items
246 
247  Activate();
248 
249  m_router->SetMode( aMode );
250 
251  controls()->SetSnapping( true );
252  controls()->ShowCursor( true );
253  frame()->UndoRedoBlock( true );
254 
255  std::unique_ptr<TUNER_TOOL_MENU> ctxMenu( new TUNER_TOOL_MENU );
256  SetContextMenu( ctxMenu.get() );
257 
258  // Main loop: keep receiving events
259  while( OPT_TOOL_EVENT evt = Wait() )
260  {
261  if( evt->IsCancel() || evt->IsActivate() )
262  {
263  break; // Finish
264  }
265  else if( evt->IsMotion() )
266  {
267  updateStartItem( *evt );
268  }
269  else if( evt->IsClick( BUT_LEFT ) || evt->IsAction( &ACT_StartTuning ) )
270  {
271  updateStartItem( *evt );
272  performTuning();
273  }
274  }
275 
277  frame()->UndoRedoBlock( false );
278 
279  // Store routing settings till the next invocation
282 
283  return 0;
284 }
285 
287 {
289 
290  if( !placer )
291  return 0;
292 
293  PNS::MEANDER_SETTINGS settings = placer->MeanderSettings();
294  DIALOG_PNS_LENGTH_TUNING_SETTINGS settingsDlg( frame(), settings, m_router->Mode() );
295 
296  if( settingsDlg.ShowModal() )
297  placer->UpdateSettings( settings );
298 
300 
301  return 0;
302 }
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:58
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:192
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:124
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:198
PNS::MEANDER_SETTINGS m_savedMeanderSettings
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:125
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:195
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:560
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:888
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:187
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:767
virtual void updateStartItem(TOOL_EVENT &aEvent)