KiCad PCB EDA Suite
ee_inspection_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) 2019 CERN
5  * Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <sch_component.h>
26 #include <id.h>
27 #include <kiway.h>
28 #include <confirm.h>
29 #include <tool/conditional_menu.h>
31 #include <tools/ee_actions.h>
34 #include <tools/ee_selection.h>
35 #include <search_stack.h>
36 #include <sim/sim_plot_frame.h>
37 #include <sch_edit_frame.h>
38 #include <symbol_edit_frame.h>
39 #include <lib_view_frame.h>
40 #include <eda_doc.h>
41 #include <invoke_sch_dialog.h>
42 #include <project.h>
44 #include <dialogs/dialog_erc.h>
45 #include <math/util.h> // for KiROUND
46 
47 
49  EE_TOOL_BASE<SCH_BASE_FRAME>( "eeschema.InspectionTool" ),
50  m_ercDialog( nullptr )
51 {
52 }
53 
54 
56 {
58 
59  auto singleMarkerCondition = SELECTION_CONDITIONS::OnlyType( SCH_MARKER_T )
61 
62  // Add inspection actions to the selection tool menu
63  //
65 
67 
68  return true;
69 }
70 
71 
73 {
74  EE_TOOL_BASE::Reset( aReason );
75 
76  if( aReason == MODEL_RELOAD )
77  {
79  }
80 }
81 
82 
84 {
85  if( m_frame->IsType( FRAME_SCH ) )
86  {
87  if( m_ercDialog )
88  {
89  // Needed at least on Windows. Raise() is not enough
90  m_ercDialog->Show( true );
91  // Bring it to the top if already open. Dual monitor users need this.
92  m_ercDialog->Raise();
93  }
94  else
95  {
96  // This is a modeless dialog, so new it rather than instantiating on stack.
97  m_ercDialog = new DIALOG_ERC( static_cast<SCH_EDIT_FRAME*>( m_frame ) );
98 
99  m_ercDialog->Show( true );
100  }
101  }
102 
103  return 0;
104 }
105 
106 
108 {
109  if( m_ercDialog )
110  m_ercDialog->Destroy();
111 
112  m_ercDialog = nullptr;
113 }
114 
115 
116 // helper function to sort pins by pin num
117 bool sort_by_pin_number( const LIB_PIN* ref, const LIB_PIN* tst )
118 {
119  // Use number as primary key
120  int test = ref->GetNumber().Cmp( tst->GetNumber() );
121 
122  // Use DeMorgan variant as secondary key
123  if( test == 0 )
124  test = ref->GetConvert() - tst->GetConvert();
125 
126  // Use unit as tertiary key
127  if( test == 0 )
128  test = ref->GetUnit() - tst->GetUnit();
129 
130  return test < 0;
131 }
132 
133 
135 {
136  LIB_PART* part = static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurPart();
137 
138  if( !part )
139  return 0;
140 
141  wxString msg;
142  const int min_grid_size = 25;
143  const int grid_size = KiROUND( getView()->GetGAL()->GetGridSize().x );
144  const int clamped_grid_size = ( grid_size < min_grid_size ) ? min_grid_size : grid_size;
145  LIB_PINS pinList;
146 
147  part->GetPins( pinList );
148 
149  if( pinList.empty() )
150  {
151  DisplayInfoMessage( m_frame, _( "No pins!" ) );
152  return 0;
153  }
154 
155  // Sort pins by pin num, so 2 duplicate pins
156  // (pins with the same number) will be consecutive in list
157  sort( pinList.begin(), pinList.end(), sort_by_pin_number );
158 
159  // Test for duplicates:
160  DIALOG_DISPLAY_HTML_TEXT_BASE error_display( m_frame, wxID_ANY, _( "Marker Information" ),
161  wxDefaultPosition, wxSize( 750, 600 ) );
162 
163  std::vector<wxString> messages;
164 
165  int dup_error = 0;
166 
167  for( unsigned ii = 1; ii < pinList.size(); ii++ )
168  {
169  LIB_PIN* pin = pinList[ii - 1];
170  LIB_PIN* next = pinList[ii];
171 
172  if( pin->GetNumber() != next->GetNumber() || pin->GetConvert() != next->GetConvert() )
173  continue;
174 
175  dup_error++;
176 
177  if( part->HasConversion() && next->GetConvert() )
178  {
179  if( part->GetUnitCount() <= 1 )
180  {
181  msg = wxString::Format( _( "<b>Duplicate pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>"
182  " conflicts with pin %s \"%s\" at location <b>(%.3f, %.3f)</b> of converted" ),
183  next->GetNumber(),
184  next->GetName(),
185  next->GetPosition().x / 1000.0, -next->GetPosition().y / 1000.0,
186  pin->GetNumber(),
187  pin->GetName(),
188  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
189  }
190  else
191  {
192  msg = wxString::Format( _( "<b>Duplicate pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>"
193  " conflicts with pin %s \"%s\" at location <b>(%.3f, %.3f)</b>"
194  " in units %c and %c of converted" ),
195  next->GetNumber(),
196  next->GetName(),
197  next->GetPosition().x / 1000.0, -next->GetPosition().y / 1000.0,
198  pin->GetNumber(),
199  pin->GetName(),
200  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0,
201  'A' + next->GetUnit() - 1,
202  'A' + pin->GetUnit() - 1 );
203  }
204  }
205  else
206  {
207  if( part->GetUnitCount() <= 1 )
208  {
209  msg = wxString::Format( _( "<b>Duplicate pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>"
210  " conflicts with pin %s \"%s\" at location <b>(%.3f, %.3f)</b>" ),
211  next->GetNumber(),
212  next->GetName(),
213  next->GetPosition().x / 1000.0, -next->GetPosition().y / 1000.0,
214  pin->GetNumber(),
215  pin->GetName(),
216  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
217  }
218  else
219  {
220  msg = wxString::Format( _( "<b>Duplicate pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>"
221  " conflicts with pin %s \"%s\" at location <b>(%.3f, %.3f)</b>"
222  " in units %c and %c" ),
223  next->GetNumber(),
224  next->GetName(),
225  next->GetPosition().x / 1000.0, -next->GetPosition().y / 1000.0,
226  pin->GetNumber(),
227  pin->GetName(),
228  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0,
229  'A' + next->GetUnit() - 1,
230  'A' + pin->GetUnit() - 1 );
231  }
232  }
233 
234  msg += wxT( ".<br>" );
235 
236  messages.push_back( msg );
237  }
238 
239  // Test for off grid pins:
240  int offgrid_error = 0;
241 
242  for( LIB_PIN* pin : pinList )
243  {
244  if( ( (pin->GetPosition().x % clamped_grid_size) == 0 ) &&
245  ( (pin->GetPosition().y % clamped_grid_size) == 0 ) )
246  continue;
247 
248  // "pin" is off grid here.
249  offgrid_error++;
250 
251  if( part->HasConversion() && pin->GetConvert() )
252  {
253  if( part->GetUnitCount() <= 1 )
254  {
255  msg = wxString::Format( _( "<b>Off grid pin %s</b> \"%s\" at location "
256  "<b>(%.3f, %.3f)</b> of converted.<br>" ),
257  pin->GetNumber(),
258  pin->GetName(),
259  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
260  }
261  else
262  {
263  msg = wxString::Format( _( "<b>Off grid pin %s</b> \"%s\" at location "
264  "<b>(%.3f, %.3f)</b> in symbol %c of converted.<br>" ),
265  pin->GetNumber(),
266  pin->GetName(),
267  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0,
268  'A' + pin->GetUnit() - 1 );
269  }
270  }
271  else
272  {
273  if( part->GetUnitCount() <= 1 )
274  {
275  msg = wxString::Format( _( "<b>Off grid pin %s</b> \"%s\" at location "
276  "<b>(%.3f, %.3f)</b>.<br>" ),
277  pin->GetNumber(),
278  pin->GetName(),
279  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
280  }
281  else
282  {
283  msg = wxString::Format( _( "<b>Off grid pin %s</b> \"%s\" at location "
284  "<b>(%.3f, %.3f)</b> in symbol %c.<br>" ),
285  pin->GetNumber(),
286  pin->GetName(),
287  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0,
288  'A' + pin->GetUnit() - 1 );
289  }
290  }
291 
292  messages.push_back( msg );
293  }
294 
295  if( !dup_error && !offgrid_error )
296  DisplayInfoMessage( m_frame, _( "No off grid or duplicate pins were found." ) );
297  else
298  {
299  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
300  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
301  wxString outmsg = wxString::Format( "<html><body bgcolor='%s' text='%s'>",
302  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
303  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ) );
304 
305  for( auto& msgPart : messages )
306  outmsg += msgPart;
307 
308  outmsg += "</body></html>";
309 
310  error_display.m_htmlWindow->SetPage( outmsg );
311  error_display.ShowModal();
312  }
313 
314  return 0;
315 }
316 
317 
319 {
320 #ifdef KICAD_SPICE
322  simFrame->Show( true );
323 
324  // On Windows, Raise() does not bring the window on screen, when iconized
325  if( simFrame->IsIconized() )
326  simFrame->Iconize( false );
327 
328  simFrame->Raise();
329 #endif /* KICAD_SPICE */
330  return 0;
331 }
332 
333 
335 {
336  wxString datasheet;
337 
339  {
340  LIB_PART* part = static_cast<SYMBOL_EDIT_FRAME*>( m_frame )->GetCurPart();
341 
342  if( !part )
343  return 0;
344 
345  datasheet = part->GetDatasheetField().GetText();
346  }
348  {
349  LIB_PART* entry = static_cast<LIB_VIEW_FRAME*>( m_frame )->GetSelectedSymbol();
350 
351  if( !entry )
352  return 0;
353 
354  datasheet = entry->GetDatasheetField().GetText();
355  }
356  else if( m_frame->IsType( FRAME_SCH ) )
357  {
359 
360  if( selection.Empty() )
361  return 0;
362 
363  SCH_COMPONENT* component = (SCH_COMPONENT*) selection.Front();
364 
365  datasheet = component->GetField( DATASHEET_FIELD )->GetText();
366  }
367 
368  if( !datasheet.IsEmpty() && datasheet != wxT( "~" ) )
369  GetAssociatedDocument( m_frame, datasheet, &m_frame->Prj() );
370 
371  return 0;
372 }
373 
374 
376 {
378  EE_SELECTION& selection = selTool->GetSelection();
379 
380  if( selection.GetSize() == 1 )
381  {
382  EDA_ITEM* item = (EDA_ITEM*) selection.Front();
383 
384  MSG_PANEL_ITEMS msgItems;
385  item->GetMsgPanelInfo( m_frame, msgItems );
386  m_frame->SetMsgPanel( msgItems );
387  }
388  else
389  {
391  }
392 
393  if( SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame ) )
394  editFrame->UpdateNetHighlightStatus();
395 
396  return 0;
397 }
398 
399 
401 {
405 
407 
412 }
413 
414 
CITER next(CITER it)
Definition: ptree.cpp:126
static TOOL_ACTION showDatasheet
Inspection and Editing.
Definition: ee_actions.h:140
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: ee_tool_base.h:90
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject)
Function GetAssociatedDocument open a document (file) with the suitable browser.
Definition: eda_doc.cpp:79
static const TOOL_EVENT SelectedEvent
Definition: actions.h:208
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
DIALOG_ERC * m_ercDialog
int RunSimulation(const TOOL_EVENT &aEvent)
LIB_FIELD & GetDatasheetField()
Return reference to the datasheet field.
static SELECTION_CONDITION SingleSymbol
Model changes (required full reload)
Definition: tool_base.h:82
static TOOL_ACTION runERC
Definition: ee_actions.h:141
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:209
This file is part of the common library.
bool HasConversion() const
Test if part has more than one body conversion type (DeMorgan).
void setTransitions() override
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:56
This file is part of the common library.
static bool Idle(const SELECTION &aSelection)
Tests if there no items selected or being edited.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
TOOL_MENU & GetToolMenu()
Schematic editor (Eeschema) main window.
bool sort_by_pin_number(const LIB_PIN *ref, const LIB_PIN *tst)
static const KICAD_T ComponentsOnly[]
Definition: ee_collectors.h:47
static SELECTION_CONDITION Count(int aNumber)
Creates a functor that tests if the number of selected items is equal to the value given as parameter...
static TOOL_ACTION checkSymbol
Definition: ee_actions.h:156
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
int UpdateMessagePanel(const TOOL_EVENT &aEvent)
static const TOOL_EVENT SelectedItemsModified
Definition: actions.h:213
name of datasheet
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:69
static TOOL_ACTION runSimulation
Definition: ee_actions.h:210
EE_SELECTION & GetSelection()
Function GetSelection()
EE_SELECTION & RequestSelection(const KICAD_T *aFilterList=EE_COLLECTOR::AllItems)
Function RequestSelection()
void GetPins(LIB_PINS &aList, int aUnit=0, int aConvert=0)
Return a list of pin object pointers from the draw item list.
int GetUnit() const
Definition: lib_item.h:296
Class DIALOG_DISPLAY_HTML_TEXT_BASE.
int GetUnitCount() const override
For items with units, return the number of units.
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:345
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
const wxString & GetName() const
Definition: lib_pin.h:156
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
virtual void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
Definition: eda_item.h:283
TOOL_EVENT.
Definition: tool_event.h:171
Define a library symbol object.
Subclass of SIM_PLOT_FRAME_BASE, which is generated by wxFormBuilder.
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:181
static const TOOL_EVENT ClearedEvent
Definition: actions.h:210
virtual void ClearMsgPanel()
Clear all messages from the message panel.
int GetConvert() const
Definition: lib_item.h:299
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
bool Show(bool show) override
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:120
Implementing SIM_PLOT_FRAME_BASE.
wxPoint GetPosition() const override
Definition: lib_pin.h:258
int RunERC(const TOOL_EVENT &aEvent)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
SCH_FIELD * GetField(int aFieldNdx)
Returns a field in this symbol.
const wxString & GetNumber() const
Definition: lib_pin.h:165
bool IsType(FRAME_T aType) const
#define _(s)
Definition: 3d_actions.cpp:33
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Creates a functor that tests if the selected items are only of given type.
Schematic symbol object.
Definition: sch_component.h:79
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
EDA_ITEM is a base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:148
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:97
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
EE_TOOL_BASE.
Definition: ee_tool_base.h:50
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
int ShowDatasheet(const TOOL_EVENT &aEvent)
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:268
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Adds a menu entry to run a TOOL_ACTION on selected items.
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
EDA_ITEM * Front() const
Definition: selection.h:201
int CheckSymbol(const TOOL_EVENT &aEvent)