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 <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  {
82  // Needed at least on Windows. Raise() is not enough
83  erc->Show( true );
84  // Bring it to the top if already open. Dual monitor users need this.
85  erc->Raise();
86  }
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  const int min_grid_size = 25;
120  const int grid_size = KiROUND( getView()->GetGAL()->GetGridSize().x );
121  const int clamped_grid_size = ( grid_size < min_grid_size ) ? min_grid_size : grid_size;
122  LIB_PINS pinList;
123 
124  aPart->GetPins( pinList );
125 
126  if( pinList.empty() )
127  {
128  DisplayInfoMessage( m_frame, _( "No pins!" ) );
129  return;
130  }
131 
132  // Sort pins by pin num, so 2 duplicate pins
133  // (pins with the same number) will be consecutive in list
134  sort( pinList.begin(), pinList.end(), sort_by_pin_number );
135 
136  // Test for duplicates:
137  DIALOG_DISPLAY_HTML_TEXT_BASE error_display( m_frame, wxID_ANY, _( "Marker Information" ),
138  wxDefaultPosition, wxSize( 750, 600 ) );
139 
140  std::vector<wxString> messages;
141 
142  int dup_error = 0;
143 
144  for( unsigned ii = 1; ii < pinList.size(); ii++ )
145  {
146  LIB_PIN* pin = pinList[ii - 1];
147  LIB_PIN* next = pinList[ii];
148 
149  if( pin->GetNumber() != next->GetNumber() || pin->GetConvert() != next->GetConvert() )
150  continue;
151 
152  dup_error++;
153 
154  /* TODO I dare someone to find a way to make happy translators on this thing! Lorenzo */
155 
156  msg = wxString::Format( _( "<b>Duplicate pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>"
157  " conflicts with pin %s \"%s\" at location <b>(%.3f, %.3f)</b>" ),
158  next->GetNumber(),
159  next->GetName(),
160  next->GetPosition().x / 1000.0, -next->GetPosition().y / 1000.0,
161  pin->GetNumber(),
162  pin->GetName(),
163  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
164 
165  if( aPart->GetUnitCount() > 1 )
166  {
167  msg += wxString::Format( _( " in units %c and %c" ),
168  'A' + next->GetUnit() - 1,
169  'A' + pin->GetUnit() - 1 );
170  }
171 
172  if( aPart->HasConversion() )
173  {
174  if( next->GetConvert() )
175  msg += _( " of converted" );
176  else
177  msg += _( " of normal" );
178  }
179 
180  msg += wxT( ".<br>" );
181 
182  messages.push_back( msg );
183  }
184 
185  // Test for off grid pins:
186  int offgrid_error = 0;
187 
188  for( LIB_PIN* pin : pinList )
189  {
190  if( ( (pin->GetPosition().x % clamped_grid_size) == 0 ) &&
191  ( (pin->GetPosition().y % clamped_grid_size) == 0 ) )
192  continue;
193 
194  // "pin" is off grid here.
195  offgrid_error++;
196 
197  msg = wxString::Format( _( "<b>Off grid pin %s</b> \"%s\" at location <b>(%.3f, %.3f)</b>" ),
198  pin->GetNumber(),
199  pin->GetName(),
200  pin->GetPosition().x / 1000.0, -pin->GetPosition().y / 1000.0 );
201 
202  if( aPart->GetUnitCount() > 1 )
203  msg += wxString::Format( _( " in symbol %c" ), 'A' + pin->GetUnit() - 1 );
204 
205  if( aPart->HasConversion() )
206  {
207  if( pin->GetConvert() )
208  msg += _( " of converted" );
209  else
210  msg += _( " of normal" );
211  }
212 
213  msg += wxT( ".<br>" );
214  messages.push_back( msg );
215  }
216 
217  if( !dup_error && !offgrid_error )
218  DisplayInfoMessage( m_frame, _( "No off grid or duplicate pins were found." ) );
219  else
220  {
221  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
222  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
223  wxString outmsg = wxString::Format( "<html><body bgcolor='%s' text='%s'>",
224  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
225  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ) );
226 
227  for( auto& msgPart : messages )
228  outmsg += msgPart;
229 
230  outmsg += "</body></html>";
231 
232  error_display.m_htmlWindow->SetPage( outmsg );
233  error_display.ShowModal();
234  }
235 }
236 
237 
239 {
240 #ifdef KICAD_SPICE
242  simFrame->Show( true );
243 
244  // On Windows, Raise() does not bring the window on screen, when iconized
245  if( simFrame->IsIconized() )
246  simFrame->Iconize( false );
247 
248  simFrame->Raise();
249 #endif /* KICAD_SPICE */
250  return 0;
251 }
252 
253 
255 {
256  wxString datasheet;
257 
259  {
260  LIB_PART* part = static_cast<LIB_EDIT_FRAME*>( m_frame )->GetCurPart();
261 
262  if( !part )
263  return 0;
264 
265  datasheet = part->GetDatasheetField().GetText();
266  }
268  {
269  LIB_PART* entry = static_cast<LIB_VIEW_FRAME*>( m_frame )->GetSelectedSymbol();
270 
271  if( !entry )
272  return 0;
273 
274  datasheet = entry->GetDatasheetField().GetText();
275  }
276  else if( m_frame->IsType( FRAME_SCH ) )
277  {
279 
280  if( selection.Empty() )
281  return 0;
282 
283  SCH_COMPONENT* component = (SCH_COMPONENT*) selection.Front();
284 
285  datasheet = component->GetField( DATASHEET )->GetText();
286  }
287 
288  if( !datasheet.IsEmpty() && datasheet != wxT( "~" ) )
289  GetAssociatedDocument( m_frame, datasheet, &m_frame->Prj() );
290 
291  return 0;
292 }
293 
294 
296 {
298  EE_SELECTION& selection = selTool->GetSelection();
299 
300  if( selection.GetSize() == 1 )
301  {
302  EDA_ITEM* item = (EDA_ITEM*) selection.Front();
303 
304  MSG_PANEL_ITEMS msgItems;
305  item->GetMsgPanelInfo( m_frame, msgItems );
306  m_frame->SetMsgPanel( msgItems );
307  }
308  else
309  {
311  }
312 
313  return 0;
314 }
315 
316 
318 {
321 
323 
328 }
329 
330 
CITER next(CITER it)
Definition: ptree.cpp:130
static TOOL_ACTION showDatasheet
Inspection and Editing.
Definition: ee_actions.h:139
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject)
Function GetAssociatedDocument open a document (file) with the suitable browser.
Definition: eda_doc.cpp:80
static const TOOL_EVENT SelectedEvent
Definition: actions.h:202
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
LIB_FIELD & GetDatasheetField()
Return reference to the datasheet field.
static SELECTION_CONDITION SingleSymbol
static TOOL_ACTION runERC
Definition: ee_actions.h:140
static const TOOL_EVENT UnselectedEvent
Definition: actions.h:203
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
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
TOOL_MENU & GetToolMenu()
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 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:206
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:160
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:295
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:342
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:151
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: base_struct.h:295
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:204
const wxPoint GetPosition() const override
Definition: lib_pin.h:430
virtual void ClearMsgPanel()
Clear all messages from the message panel.
int GetConvert() const
Definition: lib_item.h:298
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:121
Implementing SIM_PLOT_FRAME_BASE.
int RunERC(const TOOL_EVENT &aEvent)
SCH_FIELD * GetField(int aFieldNdx)
Returns a field in this symbol.
const wxString & GetNumber() const
Definition: lib_pin.h:179
eeschema ERC modeless dialog ID
Definition: id.h:184
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.
Schematic symbol object.
Definition: sch_component.h:88
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: base_struct.h:159
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.
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:658
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:126
EDA_ITEM * Front() const
Definition: selection.h:184