KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gerbview_control.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 <[email protected]>
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#include <confirm.h>
22#include <common.h>
24#include <export_to_pcbnew.h>
25#include <gerber_file_image.h>
27#include <gerbview_painter.h>
28#include <gerbview_frame.h>
29#include <gerbview_settings.h>
30#include <string_utils.h>
31#include <excellon_image.h>
32#include <tool/tool_manager.h>
33#include <project.h>
34#include <view/view.h>
36#include <wx/filedlg.h>
37
38#include "gerbview_actions.h"
39#include "gerbview_control.h"
41
42
43GERBVIEW_CONTROL::GERBVIEW_CONTROL() : TOOL_INTERACTIVE( "gerbview.Control" ), m_frame( nullptr )
44{
45}
46
47
52
53
55{
56 m_frame->LoadAutodetectedFiles( wxEmptyString );
57
58 return 0;
59}
60
61
63{
64 m_frame->LoadGerberFiles( wxEmptyString );
65
66 return 0;
67}
68
69
71{
72 m_frame->LoadExcellonFiles( wxEmptyString );
73
74 return 0;
75}
76
77
79{
80 m_frame->LoadGerberJobFile( wxEmptyString );
81 canvas()->Refresh();
82
83 return 0;
84}
85
86
88{
89 m_frame->LoadZipArchiveFile( wxEmptyString );
90 canvas()->Refresh();
91
92 return 0;
93}
94
95
97{
98 m_frame->ToggleLayerManager();
99
100 return 0;
101}
102
103
105{
106 int layercount = 0;
107
108 GERBER_FILE_IMAGE_LIST* images = m_frame->GetGerberLayout()->GetImagesList();
109
110 // Count the Gerber layers which are actually currently used
111 for( int ii = 0; ii < (int) images->ImagesMaxCount(); ++ii )
112 {
113 if( images->GetGbrImage( ii ) )
114 layercount++;
115 }
116
117 if( layercount == 0 )
118 {
119 DisplayInfoMessage( m_frame, _( "None of the Gerber layers contain any data" ) );
120 return 0;
121 }
122
123 wxString fileDialogName( NAMELESS_PROJECT + wxT( "." ) + FILEEXT::KiCadPcbFileExtension );
124 wxString path = m_frame->GetMruPath();
125
126 wxFileDialog filedlg( m_frame, _( "Export as KiCad Board File" ), path, fileDialogName,
127 FILEEXT::PcbFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
128
129 if( filedlg.ShowModal() == wxID_CANCEL )
130 return 0;
131
132 wxFileName fileName = EnsureFileExtension( filedlg.GetPath(), FILEEXT::KiCadPcbFileExtension );
133
135
136 if( layerdlg.ShowModal() != wxID_OK )
137 return 0;
138
139 m_frame->SetMruPath( fileName.GetPath() );
140
141 GBR_TO_PCB_EXPORTER gbr_exporter( m_frame, fileName.GetFullPath() );
142
143 gbr_exporter.ExportPcb( layerdlg.GetLayersLookUpTable(), layerdlg.GetCopperLayersCount() );
144
145 return 0;
146}
147
148
150{
151 auto settings = static_cast<KIGFX::GERBVIEW_PAINTER*>( getView()->GetPainter() )->GetSettings();
152 const auto& selection = m_toolMgr->GetTool<GERBVIEW_SELECTION_TOOL>()->GetSelection();
153 GERBER_DRAW_ITEM* item = nullptr;
154
155 if( selection.Size() == 1 )
156 {
157 item = static_cast<GERBER_DRAW_ITEM*>( selection[0] );
158 }
159
161 {
162 m_frame->m_SelComponentBox->SetSelection( 0 );
163 m_frame->m_SelNetnameBox->SetSelection( 0 );
164 m_frame->m_SelAperAttributesBox->SetSelection( 0 );
165
166 settings->ClearHighlightSelections();
167
168 GERBER_FILE_IMAGE* gerber = m_frame->GetGbrImage( m_frame->GetActiveLayer() );
169
170 if( gerber )
171 gerber->m_Selected_Tool = settings->m_dcodeHighlightValue;
172 }
173 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightNet ) )
174 {
175 wxString net_name = item->GetNetAttributes().m_Netname;
176 settings->m_netHighlightString = net_name;
177 m_frame->m_SelNetnameBox->SetStringSelection( UnescapeString( net_name ) );
178 }
179 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightComponent ) )
180 {
181 wxString net_attr = item->GetNetAttributes().m_Cmpref;
182 settings->m_componentHighlightString = net_attr;
183 m_frame->m_SelComponentBox->SetStringSelection( net_attr );
184 }
185 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightAttribute ) )
186 {
187 D_CODE* apertDescr = item->GetDcodeDescr();
188
189 if( apertDescr )
190 {
191 wxString ap_name = apertDescr->m_AperFunction;
192 settings->m_attributeHighlightString = ap_name;
193 m_frame->m_SelAperAttributesBox->SetStringSelection( ap_name );
194 }
195 }
196 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightDCode ) )
197 {
198 D_CODE* apertDescr = item->GetDcodeDescr();
199
200 if( apertDescr )
201 {
202 int dcodeSelected = -1;
203 GERBER_FILE_IMAGE* gerber = m_frame->GetGbrImage( m_frame->GetActiveLayer() );
204
205 if( gerber )
206 dcodeSelected = apertDescr->m_Num_Dcode;
207
208 if( dcodeSelected > 0 )
209 {
210 settings->m_dcodeHighlightValue = dcodeSelected;
211 gerber->m_Selected_Tool = dcodeSelected;
212 m_frame->syncLayerBox( false );
213 }
214 }
215 }
216
218 canvas()->Refresh();
219
220 return 0;
221}
222
223
225{
226 GERBVIEW_SETTINGS* cfg = m_frame->gvconfig();
227 KIGFX::VIEW* view = m_frame->GetCanvas()->GetView();
228
230 {
232
234 []( KIGFX::VIEW_ITEM* aItem )
235 {
236 GERBER_DRAW_ITEM* item = static_cast<GERBER_DRAW_ITEM*>( aItem );
237
238 switch( item->m_ShapeType )
239 {
240 case GBR_CIRCLE:
241 case GBR_ARC:
242 case GBR_SEGMENT:
243 return true;
244
245 default:
246 return false;
247 }
248 } );
249 }
251 {
253
255 []( KIGFX::VIEW_ITEM* aItem )
256 {
257 GERBER_DRAW_ITEM* item = static_cast<GERBER_DRAW_ITEM*>( aItem );
258
259 switch( item->m_ShapeType )
260 {
261 case GBR_SPOT_CIRCLE:
262 case GBR_SPOT_RECT:
263 case GBR_SPOT_OVAL:
264 case GBR_SPOT_POLY:
265 case GBR_SPOT_MACRO:
266 return true;
267
268 default:
269 return false;
270 }
271 } );
272 }
274 {
276
278 []( KIGFX::VIEW_ITEM* aItem )
279 {
280 GERBER_DRAW_ITEM* item = static_cast<GERBER_DRAW_ITEM*>( aItem );
281
282 return ( item->m_ShapeType == GBR_POLYGON );
283 } );
284 }
286 {
288 }
289 else if( aEvent.IsAction( &GERBVIEW_ACTIONS::dcodeDisplay ) )
290 {
291 m_frame->SetElementVisibility( LAYER_DCODES, !cfg->m_Appearance.show_dcodes );
292 }
293 else if( aEvent.IsAction( &ACTIONS::highContrastMode )
295 {
297 }
299 {
301
303 cfg->m_Display.m_XORMode = false;
304
305 m_frame->UpdateXORLayers();
306 }
307 else if( aEvent.IsAction( &GERBVIEW_ACTIONS::toggleXORMode ) )
308 {
310
312 cfg->m_Display.m_ForceOpacityMode = false;
313
314 m_frame->UpdateXORLayers();
315 }
316 else if( aEvent.IsAction( &GERBVIEW_ACTIONS::flipGerberView ) )
317 {
319 view->SetMirror( cfg->m_Display.m_FlipGerberView, false );
320 }
321
322 m_frame->ApplyDisplaySettingsToGAL();
323
325 m_frame->GetCanvas()->Refresh();
326
327 return 0;
328}
329
330
332{
333 int layer = m_frame->GetActiveLayer();
334
335 if( layer < ( (int) GERBER_FILE_IMAGE_LIST::GetImagesList().GetLoadedImageCount() - 1 ) )
336 m_frame->SetActiveLayer( layer + 1, true );
337
338 return 0;
339}
340
341
343{
344 int layer = m_frame->GetActiveLayer();
345
346 if( layer > 0 )
347 m_frame->SetActiveLayer( layer - 1, true );
348
349 return 0;
350}
351
352
354{
355 int layer = m_frame->GetActiveLayer();
356
357 if( layer > 0 )
358 {
359 m_frame->RemapLayers(
360 GERBER_FILE_IMAGE_LIST::GetImagesList().SwapImages( layer, layer - 1 ) );
361 m_frame->SetActiveLayer( layer - 1 );
362 }
363
364 return 0;
365}
366
367
369{
370 int layer = m_frame->GetActiveLayer();
372
373 if( layer < ( (int) list.GetLoadedImageCount() - 1 ) )
374 {
375 m_frame->RemapLayers( list.SwapImages( layer, layer + 1 ) );
376 m_frame->SetActiveLayer( layer + 1 );
377 }
378
379 return 0;
380}
381
382
384{
385 m_frame->Erase_Current_DrawLayer( true );
386 m_frame->ClearMsgPanel();
387
388 return 0;
389}
390
391
393{
394 m_frame->Clear_DrawLayers( false );
395 m_toolMgr->RunAction( ACTIONS::zoomFitScreen );
396 canvas()->Refresh();
397 m_frame->ClearMsgPanel();
398
399 // Clear pending highlight selections, now outdated
401 static_cast<KIGFX::GERBVIEW_PAINTER*>( getView()->GetPainter() )->GetSettings();
402 settings->ClearHighlightSelections();
403
404 return 0;
405}
406
407
409{
410 // Store filenames
411 wxArrayString listOfGerberFiles;
412 std::vector<int> fileType;
413 GERBER_FILE_IMAGE_LIST* list = m_frame->GetImagesList();
414
415 for( unsigned i = 0; i < list->ImagesMaxCount(); i++ )
416 {
417 if( list->GetGbrImage( i ) == nullptr )
418 continue;
419
420 if( !list->GetGbrImage( i )->m_InUse )
421 continue;
422
423 EXCELLON_IMAGE* drill_file = dynamic_cast<EXCELLON_IMAGE*>( list->GetGbrImage( i ) );
424
425 if( drill_file )
426 fileType.push_back( 1 );
427 else
428 fileType.push_back( 0 );
429
430 listOfGerberFiles.Add( list->GetGbrImage( i )->m_FileName );
431 }
432
433 // Clear all layers
434 m_frame->Clear_DrawLayers( false );
435 m_frame->ClearMsgPanel();
436
437 // Load the layers from stored paths
438 wxBusyCursor wait;
439 m_frame->LoadListOfGerberAndDrillFiles( wxEmptyString, listOfGerberFiles, &fileType );
440
441 return 0;
442}
443
444
446{
448 GERBVIEW_SELECTION& selection = selTool->GetSelection();
449
450 if( selection.GetSize() == 1 )
451 {
452 EDA_ITEM* item = (EDA_ITEM*) selection.Front();
453
454 std::vector<MSG_PANEL_ITEM> msgItems;
455 item->GetMsgPanelInfo( m_frame, msgItems );
456 m_frame->SetMsgPanel( msgItems );
457 }
458 else
459 {
460 m_frame->EraseMsgBox();
461 }
462
463 return 0;
464}
465
466
468{
469 m_frame->LoadZipArchiveFile( *aEvent.Parameter<wxString*>() );
470 canvas()->Refresh();
471
472 return 0;
473}
474
475
477{
478 // The event parameter is a string containing names of dropped files.
479 // Each file name has been enclosed with "", so file names with space character are allowed.
480 wxString files = *aEvent.Parameter<wxString*>();
481 // ie : files = "file1""another file""file3"...
482
483 std::vector<wxString> aFileNameList;
484
485 // Isolate each file name, deletting ""
486 files = files.AfterFirst( '"' );
487
488 // Gerber files are enclosed with "".
489 // Load files names in array.
490 while( !files.empty() )
491 {
492 wxString fileName = files.BeforeFirst( '"' );
493 // Check if file exists. If not, keep on and ignore fileName
494 if( wxFileName( fileName ).Exists() )
495 aFileNameList.push_back( fileName );
496 files = files.AfterFirst( '"' );
497 files = files.AfterFirst( '"' );
498 }
499
500 if( !aFileNameList.empty() )
501 m_frame->OpenProjectFiles( aFileNameList, KICTL_CREATE );
502
503 return 0;
504}
505
506
508{
517
523
531
542
547
550}
static TOOL_ACTION updateUnits
Definition actions.h:206
static TOOL_ACTION highContrastModeCycle
Definition actions.h:155
static TOOL_ACTION highContrastMode
Definition actions.h:154
static TOOL_ACTION zoomFitScreen
Definition actions.h:141
static TOOL_ACTION print
Definition actions.h:64
Show the Gerber files loaded and allow the user to choose between Gerber layers and pcb layers.
int ShowModal() override
A gerber DCODE (also called Aperture) definition.
Definition dcode.h:80
wxString m_AperFunction
the aperture attribute (created by a TA.AperFunction command).
Definition dcode.h:214
int m_Num_Dcode
D code value ( >= 10 )
Definition dcode.h:204
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual void GetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
Populate aList of MSG_PANEL_ITEM objects with it's internal state for display purposes.
Definition eda_item.h:220
static const TOOL_EVENT ClearedEvent
Definition actions.h:348
static const TOOL_EVENT SelectedEvent
Definition actions.h:346
static const TOOL_EVENT UnselectedEvent
Definition actions.h:347
Handle a drill image.
bool m_DisplayFlashedItemsFill
Option to draw flashed items (filled/sketch)
bool m_HighContrastMode
High contrast mode (dim un-highlighted objects)
bool m_XORMode
Display layers in exclusive-or mode.
bool m_FlipGerberView
Display as a mirror image.
bool m_ForceOpacityMode
Display layers in transparency (alpha channel) forced mode.
bool m_DisplayPolygonsFill
Option to draw polygons (filled/sketch)
bool m_DisplayLinesFill
Option to draw line items (filled/sketch)
wxString m_Cmpref
the component reference parent of the data
wxString m_Netname
for items associated to a net: the netname
A helper class to export a Gerber set of files to Pcbnew.
bool ExportPcb(const int *aLayerLookUpTable, int aCopperLayers)
Save a board from a set of Gerber images.
D_CODE * GetDcodeDescr() const
Return the GetDcodeDescr of this object, or NULL.
GBR_BASIC_SHAPE_TYPE m_ShapeType
const GBR_NETLIST_METADATA & GetNetAttributes() const
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
static GERBER_FILE_IMAGE_LIST & GetImagesList()
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
Hold the image data and parameters for one gerber file and layer parameters.
static TOOL_ACTION dcodeDisplay
static TOOL_ACTION negativeObjectDisplay
static TOOL_ACTION flashedDisplayOutlines
static TOOL_ACTION highlightAttribute
static TOOL_ACTION exportToPcbnew
static TOOL_ACTION highlightNet
static TOOL_ACTION toggleXORMode
static TOOL_ACTION loadZipFile
static TOOL_ACTION toggleLayerManager
static TOOL_ACTION openGerber
static TOOL_ACTION clearAllLayers
static TOOL_ACTION layerNext
static TOOL_ACTION flipGerberView
static TOOL_ACTION openAutodetected
static TOOL_ACTION reloadAllLayers
static TOOL_ACTION toggleForceOpacityMode
static TOOL_ACTION linesDisplayOutlines
static TOOL_ACTION layerPrev
static TOOL_ACTION openDrillFile
static TOOL_ACTION polygonsDisplayOutlines
static TOOL_ACTION highlightComponent
static TOOL_ACTION openZipFile
static TOOL_ACTION highlightDCode
static TOOL_ACTION moveLayerUp
static TOOL_ACTION moveLayerDown
static TOOL_ACTION loadGerbFiles
static TOOL_ACTION openJobFile
static TOOL_ACTION clearLayer
static TOOL_ACTION highlightClear
EDA_DRAW_PANEL_GAL * canvas()
int OpenDrillFile(const TOOL_EVENT &aEvent)
int OpenAutodetected(const TOOL_EVENT &aEvent)
int LayerNext(const TOOL_EVENT &aEvent)
int HighlightControl(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int MoveLayerDown(const TOOL_EVENT &aEvent)
int ClearLayer(const TOOL_EVENT &aEvent)
int LayerPrev(const TOOL_EVENT &aEvent)
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
int ReloadAllLayers(const TOOL_EVENT &aEvent)
GERBVIEW_FRAME * m_frame
int ToggleLayerManager(const TOOL_EVENT &aEvent)
int OpenJobFile(const TOOL_EVENT &aEvent)
int MoveLayerUp(const TOOL_EVENT &aEvent)
int ExportToPcbnew(const TOOL_EVENT &aEvent)
int OpenGerber(const TOOL_EVENT &aEvent)
int LoadGerbFiles(const TOOL_EVENT &aEvent)
Set up handlers for various events.
int ClearAllLayers(const TOOL_EVENT &aEvent)
int DisplayControl(const TOOL_EVENT &aEvent)
int LoadZipfile(const TOOL_EVENT &aEvent)
int OpenZipFile(const TOOL_EVENT &aEvent)
int UpdateMessagePanel(const TOOL_EVENT &aEvent)
int Print(const TOOL_EVENT &aEvent)
Selection tool for GerbView, based on the one in Pcbnew.
GERBVIEW_SELECTION & GetSelection()
Return the set of currently selected items.
GBR_DISPLAY_OPTIONS m_Display
Methods for drawing GerbView specific items.
Store GerbView specific render settings.
void ClearHighlightSelections()
Clear all highlight selections (dcode, net, component, attribute selection)
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:86
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:66
void SetMirror(bool aMirrorX, bool aMirrorY)
Control the mirroring of the VIEW.
Definition view.cpp:557
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition view.cpp:1561
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:220
void UpdateAllItemsConditionally(int aUpdateFlags, std::function< bool(VIEW_ITEM *)> aCondition)
Update items in the view according to the given flags and condition.
Definition view.cpp:1571
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:186
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
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:171
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:473
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).
TOOL_INTERACTIVE(TOOL_ID aId, const std::string &aName)
Create a tool with given id & name.
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:429
The common library.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:222
This file is part of the common library.
#define _(s)
@ GBR_SPOT_OVAL
@ GBR_SEGMENT
@ GBR_SPOT_POLY
@ GBR_SPOT_RECT
@ GBR_CIRCLE
@ GBR_POLYGON
@ GBR_SPOT_CIRCLE
@ GBR_ARC
@ GBR_SPOT_MACRO
static const std::string KiCadPcbFileExtension
static wxString PcbFileWildcard()
#define KICTL_CREATE
caller thinks requested project files may not exist.
@ LAYER_DCODES
Definition layer_ids.h:526
@ LAYER_NEGATIVE_OBJECTS
Definition layer_ids.h:527
@ COLOR
Color has changed.
Definition view_item.h:54
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:58
#define NAMELESS_PROJECT
default name for nameless projects
Definition project.h:43
MODEL3D_FORMAT_TYPE fileType(const char *aFileName)
wxString UnescapeString(const wxString &aSource)
Definition of file extensions used in Kicad.