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 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 <view/view_controls.h>
26 #include <sch_component.h>
27 #include <sch_marker.h>
28 #include <id.h>
29 #include <kiway.h>
30 #include <confirm.h>
31 #include <tool/conditional_menu.h>
33 #include <tool/tool_manager.h>
34 #include <tools/ee_actions.h>
37 #include <tools/ee_selection.h>
38 #include <search_stack.h>
39 #include <sim/sim_plot_frame.h>
40 #include <sch_view.h>
41 #include <sch_edit_frame.h>
42 #include <lib_edit_frame.h>
43 #include <viewlib_frame.h>
44 #include <eda_doc.h>
45 #include <invoke_sch_dialog.h>
46 #include <project.h>
48 
49 
51  : EE_TOOL_BASE<SCH_BASE_FRAME>( "eeschema.InspectionTool" )
52 {
53 }
54 
55 
57 {
59 
60  auto singleMarkerCondition = SELECTION_CONDITIONS::OnlyType( SCH_MARKER_T )
62 
63  // Add inspection actions to the selection tool menu
64  //
66 
68  selToolMenu.AddItem( EE_ACTIONS::showMarkerInfo, singleMarkerCondition && EE_CONDITIONS::Idle, 220 );
69 
70  return true;
71 }
72 
73 
75 {
77  {
78  checkPart( static_cast<LIB_EDIT_FRAME*>( m_frame )->GetCurPart() );
79  }
80  else if( m_frame->IsType( FRAME_SCH ) )
81  {
82  wxWindow* erc = wxWindow::FindWindowById( ID_DIALOG_ERC, m_frame );
83 
84  if( erc )
85  // Bring it to the top if already open. Dual monitor users need this.
86  erc->Raise();
87  else
88  InvokeDialogERC( static_cast<SCH_EDIT_FRAME*>( m_frame ) );
89  }
90 
91  return 0;
92 }
93 
94 
95 // helper function to sort pins by pin num
96 bool sort_by_pin_number( const LIB_PIN* ref, const LIB_PIN* tst )
97 {
98  // Use number as primary key
99  int test = ref->GetNumber().Cmp( tst->GetNumber() );
100 
101  // Use DeMorgan variant as secondary key
102  if( test == 0 )
103  test = ref->GetConvert() - tst->GetConvert();
104 
105  // Use unit as tertiary key
106  if( test == 0 )
107  test = ref->GetUnit() - tst->GetUnit();
108 
109  return test < 0;
110 }
111 
112 
114 {
115  if( !aPart )
116  return;
117 
118  wxString msg;
119  wxRealPoint curr_grid_size = m_frame->GetScreen()->GetGridSize();
120  const int min_grid_size = 25;
121  const int grid_size = KiROUND( curr_grid_size.x );
122  const int clamped_grid_size = ( grid_size < min_grid_size ) ? min_grid_size : grid_size;
123  LIB_PINS pinList;
124 
125  aPart->GetPins( pinList );
126 
127  if( pinList.empty() )
128  {
129  DisplayInfoMessage( m_frame, _( "No pins!" ) );
130  return;
131  }
132 
133  // Sort pins by pin num, so 2 duplicate pins
134  // (pins with the same number) will be consecutive in list
135  sort( pinList.begin(), pinList.end(), sort_by_pin_number );
136 
137  // Test for duplicates:
138  DIALOG_DISPLAY_HTML_TEXT_BASE error_display( m_frame, wxID_ANY, _( "Marker Information" ),
139  wxDefaultPosition, wxSize( 750, 600 ) );
140 
141  int dup_error = 0;
142 
143  for( unsigned ii = 1; ii < pinList.size(); ii++ )
144  {
145  LIB_PIN* pin = pinList[ii - 1];
146  LIB_PIN* next = pinList[ii];
147 
148  if( pin->GetNumber() != next->GetNumber() || pin->GetConvert() != next->GetConvert() )
149  continue;
150 
151  dup_error++;
152 
153  /* TODO I dare someone to find a way to make happy translators on this thing! Lorenzo */
154 
155  msg = wxString::Format( _( "<b>Duplicate pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>"
156  " conflicts with pin %s \"%s\" at location <b>(%.3f, %.3f)</b>" ),
157  next->GetNumber(),
158  next->GetName(),
159  next->GetPosition().x / 1000.0, -next->GetPosition().y / 1000.0,
160  pin->GetNumber(),
161  pin->GetName(),
162  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
163 
164  if( aPart->GetUnitCount() > 1 )
165  {
166  msg += wxString::Format( _( " in units %c and %c" ),
167  'A' + next->GetUnit() - 1,
168  'A' + pin->GetUnit() - 1 );
169  }
170 
171  if( aPart->HasConversion() )
172  {
173  if( next->GetConvert() )
174  msg += _( " of converted" );
175  else
176  msg += _( " of normal" );
177  }
178 
179  msg += wxT( ".<br>" );
180 
181  error_display.m_htmlWindow->AppendToPage( msg );
182  }
183 
184  // Test for off grid pins:
185  int offgrid_error = 0;
186 
187  for( LIB_PIN* pin : pinList )
188  {
189  if( ( (pin->GetPosition().x % clamped_grid_size) == 0 ) &&
190  ( (pin->GetPosition().y % clamped_grid_size) == 0 ) )
191  continue;
192 
193  // "pin" is off grid here.
194  offgrid_error++;
195 
196  msg = wxString::Format( _( "<b>Off grid pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>" ),
197  pin->GetNumber(),
198  pin->GetName(),
199  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
200 
201  if( aPart->GetUnitCount() > 1 )
202  msg += wxString::Format( _( " in symbol %c" ), 'A' + pin->GetUnit() - 1 );
203 
204  if( aPart->HasConversion() )
205  {
206  if( pin->GetConvert() )
207  msg += _( " of converted" );
208  else
209  msg += _( " of normal" );
210  }
211 
212  msg += wxT( ".<br>" );
213 
214  error_display.m_htmlWindow->AppendToPage( msg );
215  }
216 
217  if( !dup_error && !offgrid_error )
218  DisplayInfoMessage( m_frame, _( "No off grid or duplicate pins were found." ) );
219  else
220  error_display.ShowModal();
221 }
222 
223 
225 {
226 #ifdef KICAD_SPICE
228  simFrame->Show( true );
229 
230  // On Windows, Raise() does not bring the window on screen, when iconized
231  if( simFrame->IsIconized() )
232  simFrame->Iconize( false );
233 
234  simFrame->Raise();
235 #endif /* KICAD_SPICE */
236  return 0;
237 }
238 
239 
241 {
242  wxString datasheet;
243 
245  {
246  LIB_PART* part = static_cast<LIB_EDIT_FRAME*>( m_frame )->GetCurPart();
247 
248  if( !part )
249  return 0;
250 
251  datasheet = part->GetDocFileName();
252  }
254  {
255  LIB_PART* entry = static_cast<LIB_VIEW_FRAME*>( m_frame )->GetSelectedSymbol();
256 
257  if( !entry )
258  return 0;
259 
260  datasheet = entry->GetDocFileName();
261  }
262  else if( m_frame->IsType( FRAME_SCH ) )
263  {
265 
266  if( selection.Empty() )
267  return 0;
268 
269  SCH_COMPONENT* component = (SCH_COMPONENT*) selection.Front();
270 
271  datasheet = component->GetField( DATASHEET )->GetText();
272  }
273 
274  if( !datasheet.IsEmpty() && datasheet != wxT( "~" ) )
275  {
276  SEARCH_STACK* lib_search = m_frame->Prj().SchSearchS();
277 
278  GetAssociatedDocument( m_frame, datasheet, lib_search );
279  }
280 
281  return 0;
282 }
283 
284 
286 {
287  EE_SELECTION& selection = m_selectionTool->GetSelection();
288 
289  if( selection.Empty() )
290  return 0;
291 
292  SCH_MARKER* marker = dynamic_cast<SCH_MARKER*>( selection.Front() );
293 
294  if( marker )
295  marker->DisplayMarkerInfo( m_frame );
296 
297  return 0;
298 }
299 
300 
302 {
304  EE_SELECTION& selection = selTool->GetSelection();
305 
306  if( selection.GetSize() == 1 )
307  {
308  EDA_ITEM* item = (EDA_ITEM*) selection.Front();
309 
310  MSG_PANEL_ITEMS msgItems;
311  item->GetMsgPanelInfo( m_frame->GetUserUnits(), msgItems );
312  m_frame->SetMsgPanel( msgItems );
313  }
314  else
315  {
317  }
318 
319  return 0;
320 }
321 
322 
324 {
327 
330 
335 }
336 
337 
CITER next(CITER it)
Definition: ptree.cpp:130
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
Definition: base_screen.h:279
static TOOL_ACTION showDatasheet
Inspection and Editing.
Definition: ee_actions.h:143
static const TOOL_EVENT SelectedEvent
Definition: actions.h:197
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
int RunSimulation(const TOOL_EVENT &aEvent)
name of datasheet
static SELECTION_CONDITION SingleSymbol
static TOOL_ACTION runERC
Definition: ee_actions.h:144
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:198
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:55
VIEW_CONTROLS class definition.
This file is part of the common library.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
TOOL_MENU & GetToolMenu()
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, const wxPathList *aPaths)
Function GetAssociatedDocument open a document (file) with the suitable browser.
Definition: eda_doc.cpp:87
bool sort_by_pin_number(const LIB_PIN *ref, const LIB_PIN *tst)
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
static const KICAD_T ComponentsOnly[]
Definition: ee_collectors.h:44
static SELECTION_CONDITION Idle
static SELECTION_CONDITION Count(int aNumber)
Function Count Creates a functor that tests if the number of selected items is equal to the value giv...
int ShowMarkerInfo(const TOOL_EVENT &aEvent)
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:201
SCH_FIELD * GetField(int aFieldNdx) const
Returns a field in this symbol.
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:161
static TOOL_ACTION showMarkerInfo
Definition: ee_actions.h:145
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.
void DisplayMarkerInfo(EDA_DRAW_FRAME *aFrame)
Function DisplayMarkerInfo displays the full info of this marker, in a HTML window.
int GetUnit() const
Definition: lib_item.h:301
Class DIALOG_DISPLAY_HTML_TEXT_BASE.
void checkPart(LIB_PART *aPart)
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:341
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:149
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Class 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:141
static const TOOL_EVENT ClearedEvent
Definition: actions.h:199
wxString GetDocFileName() const
virtual void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList)
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
Definition: base_struct.h:328
virtual void ClearMsgPanel()
Clear all messages from the message panel.
int GetConvert() const
Definition: lib_item.h:304
#define _(s)
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:119
Implementing SIM_PLOT_FRAME_BASE.
wxPoint GetPosition() const override
Definition: lib_pin.h:427
int RunERC(const TOOL_EVENT &aEvent)
const wxString & GetNumber() const
Definition: lib_pin.h:177
eeschema ERC modeless dialog ID
Definition: id.h:252
bool IsType(FRAME_T aType) const
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
EDA_UNITS_T GetUserUnits() const
Return the user units currently in use.
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Function OnlyType Creates a functor that tests if the selected items are only of given type.
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:73
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
Class 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:264
A shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRAME, LIB_VIEW_FRAME,...
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
wxDialog * InvokeDialogERC(SCH_EDIT_FRAME *aCaller)
Create the modeless DIALOG_ERC and show it, return something to destroy or close it.
Definition: dialog_erc.cpp:654
virtual const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:124
EDA_ITEM * Front() const
Definition: selection.h:182
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:114