KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
gerbview_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) 2017 Jon Evans <jon@craftyjon.com>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22#include <eda_item.h>
23#include <bitmaps.h>
26#include <gestfich.h>
27#include <gerber_file_image.h>
28#include <gerbview_id.h>
30#include "gerbview_actions.h"
31#include <gal/painter.h>
32#include <pgm_base.h>
35#include <tool/tool_event.h>
36#include <tool/tool_manager.h>
37#include <view/view.h>
38#include <view/view_controls.h>
39#include <view/view_group.h>
40#include <wx/msgdlg.h>
41#include <wx/textdlg.h>
42#include <wx/choicdlg.h>
43
44
46 TOOL_INTERACTIVE( "gerbview.Inspection" ),
47 m_frame( nullptr )
48{
49}
50
51
53{
54}
55
56
58{
59 return true;
60}
61
62
64{
65 m_frame = getEditFrame<GERBVIEW_FRAME>();
66}
67
68
70{
71 wxString Line;
72 wxArrayString list;
73 int curr_layer = m_frame->GetActiveLayer();
74
75 double scale = 1.0;
76 wxString units;
77
78 switch( m_frame->GetUserUnits() )
79 {
80 case EDA_UNITS::MM:
82 units = wxT( "mm" );
83 break;
84
85 case EDA_UNITS::INCH:
87 units = wxT( "in" );
88 break;
89
90 case EDA_UNITS::MILS:
92 units = wxT( "mil" );
93 break;
94
95 default:
96 wxASSERT_MSG( false, wxT( "Invalid units" ) );
97 }
98
99 for( unsigned int layer = 0; layer < m_frame->ImagesMaxCount(); ++layer )
100 {
101 GERBER_FILE_IMAGE* gerber = m_frame->GetGbrImage( layer );
102
103 if( !gerber )
104 continue;
105
106 if( gerber->GetDcodesCount() == 0 )
107 continue;
108
109 if( curr_layer == static_cast<int>( layer ) )
110 Line.Printf( wxT( "*** Active layer (%2.2d) ***" ), layer + 1 );
111 else
112 Line.Printf( wxT( "*** layer %2.2d ***" ), layer + 1 );
113
114 list.Add( Line );
115
116 int ii = 1;
117 for( const auto &[_, pt_D_code] : gerber->m_ApertureList )
118 {
119 if( pt_D_code == nullptr )
120 continue;
121
122 if( !pt_D_code->m_InUse && !pt_D_code->m_Defined )
123 continue;
124
125 Line.Printf( wxT( "tool %d: Dcode D%d V %.4f %s H %.4f %s %s attribute '%s'" ),
126 ii,
127 pt_D_code->m_Num_Dcode,
128 pt_D_code->m_Size.y / scale, units,
129 pt_D_code->m_Size.x / scale, units,
130 D_CODE::ShowApertureType( pt_D_code->m_ApertType ),
131 pt_D_code->m_AperFunction.IsEmpty()? wxString( wxT( "none" ) ) : pt_D_code->m_AperFunction
132 );
133
134 if( !pt_D_code->m_Defined )
135 Line += wxT( " (not defined)" );
136
137 if( pt_D_code->m_InUse )
138 Line += wxT( " (in use)" );
139
140 list.Add( Line );
141 ii++;
142 }
143 }
144
145 wxSingleChoiceDialog dlg( m_frame, wxEmptyString, _( "D Codes" ), list, (void**) nullptr,
146 wxCHOICEDLG_STYLE & ~wxCANCEL );
147
148 dlg.ShowModal();
149
150 return 0;
151}
152
153
155{
156 int layer = m_frame->GetActiveLayer();
157 GERBER_FILE_IMAGE* gerber_layer = m_frame->GetGbrImage( layer );
158
159 if( gerber_layer )
160 {
161 wxString editorname = Pgm().GetTextEditor();
162
163 if( !editorname.IsEmpty() )
164 {
165 wxFileName fn( gerber_layer->m_FileName );
166
167 // Call the editor only if the Gerber/drill source file is available.
168 // This is not always the case, because it can be a temporary file
169 // if it comes from a zip archive.
170 if( !fn.FileExists() )
171 {
172 wxString msg;
173 msg.Printf( _( "Source file '%s' not found." ), fn.GetFullPath() );
174 wxMessageBox( msg );
175 }
176 else
177 {
178 ExecuteFile( editorname, fn.GetFullPath() );
179 }
180 }
181 else
182 {
183 wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
184 }
185 }
186 else
187 {
188 wxString msg;
189 msg.Printf( _( "No file loaded on the active layer %d." ), layer + 1 );
190 wxMessageBox( msg );
191 }
192
193 return 0;
194}
195
196
198
199
201{
203 bool originSet = false;
205 EDA_UNITS units = m_frame->GetUserUnits();
206 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, gerbIUScale, units, false, false );
207
208 m_frame->PushTool( aEvent );
209
210 auto setCursor =
211 [&]()
212 {
213 m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::MEASURE );
214 };
215
216 auto cleanup =
217 [&] ()
218 {
219 getView()->SetVisible( &ruler, false );
220 controls.SetAutoPan( false );
221 controls.CaptureCursor( false );
222 originSet = false;
223 };
224
225 Activate();
226 // Must be done after Activate() so that it gets set into the correct context
227 controls.ShowCursor( true );
228 // Set initial cursor
229 setCursor();
230
231 getView()->Add( &ruler );
232 getView()->SetVisible( &ruler, false );
233
234 while( TOOL_EVENT* evt = Wait() )
235 {
236 setCursor();
237 const VECTOR2I cursorPos = controls.GetCursorPosition();
238
239 if( evt->IsCancelInteractive() )
240 {
241 if( originSet )
242 {
243 cleanup();
244 }
245 else
246 {
247 m_frame->PopTool( aEvent );
248 break;
249 }
250 }
251 else if( evt->IsActivate() )
252 {
253 if( originSet )
254 cleanup();
255
256 if( evt->IsMoveTool() )
257 {
258 // leave ourselves on the stack so we come back after the move
259 break;
260 }
261 else
262 {
263 m_frame->PopTool( aEvent );
264 break;
265 }
266 }
267 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
268 {
269 // click or drag starts
270 twoPtMgr.SetOrigin( cursorPos );
271 twoPtMgr.SetEnd( cursorPos );
272
273 controls.CaptureCursor( true );
274 controls.SetAutoPan( true );
275
276 originSet = true;
277 }
278 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
279 {
280 // second click or mouse up after drag ends
281 originSet = false;
282
283 controls.SetAutoPan( false );
284 controls.CaptureCursor( false );
285 }
286 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
287 {
288 // move or drag when origin set updates rules
289 twoPtMgr.SetAngleSnap( evt->Modifier( MD_SHIFT ) );
290 twoPtMgr.SetEnd( cursorPos );
291
292 getView()->SetVisible( &ruler, true );
293 getView()->Update( &ruler, KIGFX::GEOMETRY );
294 }
295 else if( evt->IsAction( &ACTIONS::updateUnits ) )
296 {
297 if( m_frame->GetUserUnits() != units )
298 {
299 units = m_frame->GetUserUnits();
300 ruler.SwitchUnits( units );
301 getView()->Update( &ruler, KIGFX::GEOMETRY );
302 }
303 evt->SetPassEvent();
304 }
305 else if( evt->IsClick( BUT_RIGHT ) )
306 {
307 m_menu->ShowContextMenu( m_frame->GetCurrentSelection() );
308 }
309 else
310 {
311 evt->SetPassEvent();
312 }
313 }
314
315 getView()->SetVisible( &ruler, false );
316 getView()->Remove( &ruler );
317
318 m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
319 return 0;
320}
321
322
324{
328}
constexpr EDA_IU_SCALE gerbIUScale
Definition: base_units.h:107
static TOOL_ACTION updateUnits
Definition: actions.h:200
static TOOL_ACTION measureTool
Definition: actions.h:207
static const wxChar * ShowApertureType(APERTURE_T aType)
Return a character string telling what type of aperture type aType is.
Definition: dcode.cpp:86
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
Hold the image data and parameters for one gerber file and layer parameters.
wxString m_FileName
Full File Name for this layer.
std::map< int, D_CODE * > m_ApertureList
Dcode (Aperture) List for this layer (see dcode.h)
static TOOL_ACTION showDCodes
static TOOL_ACTION showSource
int GetActiveLayer() const
Return the active layer.
unsigned ImagesMaxCount() const
The max number of file images.
SELECTION & GetCurrentSelection() override
Get the current selection from the canvas area.
GERBER_FILE_IMAGE * GetGbrImage(int aIdx) const
bool Init() override
Init() is called once upon a registration of the tool.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int ShowSource(const TOOL_EVENT &aEvent)
Set up handlers for various events.
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
int ShowDCodes(const TOOL_EVENT &aEvent)
Show the source for the gerber file.
int MeasureTool(const TOOL_EVENT &aEvent)
Show a list of the DCodes.
A drawn ruler item for showing the distance between two points.
Definition: ruler_item.h:45
void SwitchUnits(EDA_UNITS aUnits)
Switch the ruler units.
Definition: ruler_item.h:91
Represent a very simple geometry manager for items that have a start and end point.
void SetOrigin(const VECTOR2I &aOrigin)
< Set the origin of the ruler (the fixed end)
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the end that moves with the cursor.
An interface for classes handling user events controlling the view behavior such as zooming,...
virtual void CaptureCursor(bool aEnabled)
Force the cursor to stay within the drawing panel area.
virtual void ShowCursor(bool aEnabled)
Enable or disables display of cursor.
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
virtual void SetAutoPan(bool aEnabled)
Turn on/off auto panning (this feature is used when there is a tool active (eg.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition: view.cpp:297
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition: view.cpp:332
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1673
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1600
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition: pgm_base.cpp:196
virtual void PopTool(const TOOL_EVENT &aEvent)
Pops a tool from the stack.
virtual void PushTool(const TOOL_EVENT &aEvent)
NB: the definition of "tool" is different at the user level.
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition: tool_base.cpp:44
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:38
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
std::unique_ptr< TOOL_MENU > m_menu
The functions below are not yet implemented - their interface may change.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
void Activate()
Run the tool.
EDA_UNITS GetUserUnits() const
#define _(s)
EDA_UNITS
Definition: eda_units.h:46
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
Definition: gestfich.cpp:143
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:55
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
see class PGM_BASE
const int scale
const double IU_PER_MM
Definition: base_units.h:76
const double IU_PER_MILS
Definition: base_units.h:77
@ MD_SHIFT
Definition: tool_event.h:143
@ BUT_LEFT
Definition: tool_event.h:132
@ BUT_RIGHT
Definition: tool_event.h:133