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 <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 <lib_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 <math/util.h> // for KiROUND
45 
46 
48  : EE_TOOL_BASE<SCH_BASE_FRAME>( "eeschema.InspectionTool" )
49 {
50 }
51 
52 
54 {
56 
57  auto singleMarkerCondition = SELECTION_CONDITIONS::OnlyType( SCH_MARKER_T )
59 
60  // Add inspection actions to the selection tool menu
61  //
63 
65 
66  return true;
67 }
68 
69 
71 {
73  {
74  checkPart( static_cast<LIB_EDIT_FRAME*>( m_frame )->GetCurPart() );
75  }
76  else if( m_frame->IsType( FRAME_SCH ) )
77  {
78  wxWindow* erc = wxWindow::FindWindowById( ID_DIALOG_ERC, m_frame );
79 
80  if( erc )
81  // Bring it to the top if already open. Dual monitor users need this.
82  erc->Raise();
83  else
84  InvokeDialogERC( static_cast<SCH_EDIT_FRAME*>( m_frame ) );
85  }
86 
87  return 0;
88 }
89 
90 
91 // helper function to sort pins by pin num
92 bool sort_by_pin_number( const LIB_PIN* ref, const LIB_PIN* tst )
93 {
94  // Use number as primary key
95  int test = ref->GetNumber().Cmp( tst->GetNumber() );
96 
97  // Use DeMorgan variant as secondary key
98  if( test == 0 )
99  test = ref->GetConvert() - tst->GetConvert();
100 
101  // Use unit as tertiary key
102  if( test == 0 )
103  test = ref->GetUnit() - tst->GetUnit();
104 
105  return test < 0;
106 }
107 
108 
110 {
111  if( !aPart )
112  return;
113 
114  wxString msg;
115  wxRealPoint curr_grid_size = m_frame->GetScreen()->GetGridSize();
116  const int min_grid_size = 25;
117  const int grid_size = KiROUND( curr_grid_size.x );
118  const int clamped_grid_size = ( grid_size < min_grid_size ) ? min_grid_size : grid_size;
119  LIB_PINS pinList;
120 
121  aPart->GetPins( pinList );
122 
123  if( pinList.empty() )
124  {
125  DisplayInfoMessage( m_frame, _( "No pins!" ) );
126  return;
127  }
128 
129  // Sort pins by pin num, so 2 duplicate pins
130  // (pins with the same number) will be consecutive in list
131  sort( pinList.begin(), pinList.end(), sort_by_pin_number );
132 
133  // Test for duplicates:
134  DIALOG_DISPLAY_HTML_TEXT_BASE error_display( m_frame, wxID_ANY, _( "Marker Information" ),
135  wxDefaultPosition, wxSize( 750, 600 ) );
136 
137  std::vector<wxString> messages;
138 
139  int dup_error = 0;
140 
141  for( unsigned ii = 1; ii < pinList.size(); ii++ )
142  {
143  LIB_PIN* pin = pinList[ii - 1];
144  LIB_PIN* next = pinList[ii];
145 
146  if( pin->GetNumber() != next->GetNumber() || pin->GetConvert() != next->GetConvert() )
147  continue;
148 
149  dup_error++;
150 
151  /* TODO I dare someone to find a way to make happy translators on this thing! Lorenzo */
152 
153  msg = wxString::Format( _( "<b>Duplicate pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>"
154  " conflicts with pin %s \"%s\" at location <b>(%.3f, %.3f)</b>" ),
155  next->GetNumber(),
156  next->GetName(),
157  next->GetPosition().x / 1000.0, -next->GetPosition().y / 1000.0,
158  pin->GetNumber(),
159  pin->GetName(),
160  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
161 
162  if( aPart->GetUnitCount() > 1 )
163  {
164  msg += wxString::Format( _( " in units %c and %c" ),
165  'A' + next->GetUnit() - 1,
166  'A' + pin->GetUnit() - 1 );
167  }
168 
169  if( aPart->HasConversion() )
170  {
171  if( next->GetConvert() )
172  msg += _( " of converted" );
173  else
174  msg += _( " of normal" );
175  }
176 
177  msg += wxT( ".<br>" );
178 
179  messages.push_back( msg );
180  }
181 
182  // Test for off grid pins:
183  int offgrid_error = 0;
184 
185  for( LIB_PIN* pin : pinList )
186  {
187  if( ( (pin->GetPosition().x % clamped_grid_size) == 0 ) &&
188  ( (pin->GetPosition().y % clamped_grid_size) == 0 ) )
189  continue;
190 
191  // "pin" is off grid here.
192  offgrid_error++;
193 
194  msg = wxString::Format( _( "<b>Off grid pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>" ),
195  pin->GetNumber(),
196  pin->GetName(),
197  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
198 
199  if( aPart->GetUnitCount() > 1 )
200  msg += wxString::Format( _( " in symbol %c" ), 'A' + pin->GetUnit() - 1 );
201 
202  if( aPart->HasConversion() )
203  {
204  if( pin->GetConvert() )
205  msg += _( " of converted" );
206  else
207  msg += _( " of normal" );
208  }
209 
210  msg += wxT( ".<br>" );
211  messages.push_back( msg );
212  }
213 
214  if( !dup_error && !offgrid_error )
215  DisplayInfoMessage( m_frame, _( "No off grid or duplicate pins were found." ) );
216  else
217  {
218  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
219  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
220  wxString outmsg = wxString::Format( "<html><body bgcolor='%s' text='%s'>",
221  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
222  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ) );
223 
224  for( auto& msgPart : messages )
225  outmsg += msgPart;
226 
227  outmsg += "</body></html>";
228 
229  error_display.m_htmlWindow->SetPage( outmsg );
230  error_display.ShowModal();
231  }
232 }
233 
234 
236 {
237 #ifdef KICAD_SPICE
239  simFrame->Show( true );
240 
241  // On Windows, Raise() does not bring the window on screen, when iconized
242  if( simFrame->IsIconized() )
243  simFrame->Iconize( false );
244 
245  simFrame->Raise();
246 #endif /* KICAD_SPICE */
247  return 0;
248 }
249 
250 
252 {
253  wxString datasheet;
254 
256  {
257  LIB_PART* part = static_cast<LIB_EDIT_FRAME*>( m_frame )->GetCurPart();
258 
259  if( !part )
260  return 0;
261 
262  datasheet = part->GetDocFileName();
263  }
265  {
266  LIB_PART* entry = static_cast<LIB_VIEW_FRAME*>( m_frame )->GetSelectedSymbol();
267 
268  if( !entry )
269  return 0;
270 
271  datasheet = entry->GetDocFileName();
272  }
273  else if( m_frame->IsType( FRAME_SCH ) )
274  {
276 
277  if( selection.Empty() )
278  return 0;
279 
280  SCH_COMPONENT* component = (SCH_COMPONENT*) selection.Front();
281 
282  datasheet = component->GetField( DATASHEET )->GetText();
283  }
284 
285  if( !datasheet.IsEmpty() && datasheet != wxT( "~" ) )
286  {
287  SEARCH_STACK* lib_search = m_frame->Prj().SchSearchS();
288 
289  GetAssociatedDocument( m_frame, datasheet, lib_search );
290  }
291 
292  return 0;
293 }
294 
295 
297 {
299  EE_SELECTION& selection = selTool->GetSelection();
300 
301  if( selection.GetSize() == 1 )
302  {
303  EDA_ITEM* item = (EDA_ITEM*) selection.Front();
304 
305  MSG_PANEL_ITEMS msgItems;
306  item->GetMsgPanelInfo( m_frame->GetUserUnits(), msgItems );
307  m_frame->SetMsgPanel( msgItems );
308  }
309  else
310  {
312  }
313 
314  return 0;
315 }
316 
317 
319 {
322 
324 
329 }
330 
331 
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:201
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:202
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:54
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:218
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:80
bool sort_by_pin_number(const LIB_PIN *ref, const LIB_PIN *tst)
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...
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:205
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
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:309
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:152
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
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:153
static const TOOL_EVENT ClearedEvent
Definition: actions.h:203
wxString GetDocFileName() const
virtual void ClearMsgPanel()
Clear all messages from the message panel.
int GetConvert() const
Definition: lib_item.h:312
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:432
int RunERC(const TOOL_EVENT &aEvent)
const wxString & GetNumber() const
Definition: lib_pin.h:180
eeschema ERC modeless dialog ID
Definition: id.h:220
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
#define _(s)
Definition: 3d_actions.cpp:33
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Function OnlyType Creates a functor that tests if the selected items are only of given type.
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:99
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:61
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
EE_TOOL_BASE.
Definition: ee_tool_base.h:50
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
virtual void GetMsgPanelInfo(EDA_UNITS 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:320
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:267
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:618
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
EDA_ITEM * Front() const
Definition: selection.h:183