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#include <kiplatform/ui.h>
38
39#include "gerbview_actions.h"
40#include "gerbview_control.h"
42
43
44GERBVIEW_CONTROL::GERBVIEW_CONTROL() : TOOL_INTERACTIVE( "gerbview.Control" ), m_frame( nullptr )
45{
46}
47
48
53
54
56{
57 m_frame->LoadAutodetectedFiles( wxEmptyString );
58
59 return 0;
60}
61
62
64{
65 m_frame->LoadGerberFiles( wxEmptyString );
66
67 return 0;
68}
69
70
72{
73 m_frame->LoadExcellonFiles( wxEmptyString );
74
75 return 0;
76}
77
78
80{
81 m_frame->LoadGerberJobFile( wxEmptyString );
82 canvas()->Refresh();
83
84 return 0;
85}
86
87
89{
90 m_frame->LoadZipArchiveFile( wxEmptyString );
91 canvas()->Refresh();
92
93 return 0;
94}
95
96
98{
99 m_frame->ToggleLayerManager();
100
101 return 0;
102}
103
104
106{
107 int layercount = 0;
108
109 GERBER_FILE_IMAGE_LIST* images = m_frame->GetGerberLayout()->GetImagesList();
110
111 // Count the Gerber layers which are actually currently used
112 for( int ii = 0; ii < (int) images->ImagesMaxCount(); ++ii )
113 {
114 if( images->GetGbrImage( ii ) )
115 layercount++;
116 }
117
118 if( layercount == 0 )
119 {
120 DisplayInfoMessage( m_frame, _( "None of the Gerber layers contain any data" ) );
121 return 0;
122 }
123
124 wxString fileDialogName( NAMELESS_PROJECT + wxT( "." ) + FILEEXT::KiCadPcbFileExtension );
125 wxString path = m_frame->GetMruPath();
126
127 wxFileDialog filedlg( m_frame, _( "Export as KiCad Board File" ), path, fileDialogName,
128 FILEEXT::PcbFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
129
131
132 if( filedlg.ShowModal() == wxID_CANCEL )
133 return 0;
134
135 wxFileName fileName = EnsureFileExtension( filedlg.GetPath(), FILEEXT::KiCadPcbFileExtension );
136
138
139 if( layerdlg.ShowModal() != wxID_OK )
140 return 0;
141
142 m_frame->SetMruPath( fileName.GetPath() );
143
144 GBR_TO_PCB_EXPORTER gbr_exporter( m_frame, fileName.GetFullPath() );
145
146 gbr_exporter.ExportPcb( layerdlg.GetLayersLookUpTable(), layerdlg.GetCopperLayersCount() );
147
148 return 0;
149}
150
151
153{
154 auto settings = static_cast<KIGFX::GERBVIEW_PAINTER*>( getView()->GetPainter() )->GetSettings();
155 const auto& selection = m_toolMgr->GetTool<GERBVIEW_SELECTION_TOOL>()->GetSelection();
156 GERBER_DRAW_ITEM* item = nullptr;
157
158 if( selection.Size() == 1 )
159 {
160 item = static_cast<GERBER_DRAW_ITEM*>( selection[0] );
161 }
162
164 {
165 m_frame->m_SelComponentBox->SetSelection( 0 );
166 m_frame->m_SelNetnameBox->SetSelection( 0 );
167 m_frame->m_SelAperAttributesBox->SetSelection( 0 );
168
169 settings->ClearHighlightSelections();
170
171 GERBER_FILE_IMAGE* gerber = m_frame->GetGbrImage( m_frame->GetActiveLayer() );
172
173 if( gerber )
174 gerber->m_Selected_Tool = settings->m_dcodeHighlightValue;
175 }
176 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightNet ) )
177 {
178 wxString net_name = item->GetNetAttributes().m_Netname;
179 settings->m_netHighlightString = net_name;
180 m_frame->m_SelNetnameBox->SetStringSelection( UnescapeString( net_name ) );
181 }
182 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightComponent ) )
183 {
184 wxString net_attr = item->GetNetAttributes().m_Cmpref;
185 settings->m_componentHighlightString = net_attr;
186 m_frame->m_SelComponentBox->SetStringSelection( net_attr );
187 }
188 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightAttribute ) )
189 {
190 D_CODE* apertDescr = item->GetDcodeDescr();
191
192 if( apertDescr )
193 {
194 wxString ap_name = apertDescr->m_AperFunction;
195 settings->m_attributeHighlightString = ap_name;
196 m_frame->m_SelAperAttributesBox->SetStringSelection( ap_name );
197 }
198 }
199 else if( item && aEvent.IsAction( &GERBVIEW_ACTIONS::highlightDCode ) )
200 {
201 D_CODE* apertDescr = item->GetDcodeDescr();
202
203 if( apertDescr )
204 {
205 int dcodeSelected = -1;
206 GERBER_FILE_IMAGE* gerber = m_frame->GetGbrImage( m_frame->GetActiveLayer() );
207
208 if( gerber )
209 dcodeSelected = apertDescr->m_Num_Dcode;
210
211 if( dcodeSelected > 0 )
212 {
213 settings->m_dcodeHighlightValue = dcodeSelected;
214 gerber->m_Selected_Tool = dcodeSelected;
215 m_frame->syncLayerBox( false );
216 }
217 }
218 }
219
221 canvas()->Refresh();
222
223 return 0;
224}
225
226
228{
229 GERBVIEW_SETTINGS* cfg = m_frame->gvconfig();
230 KIGFX::VIEW* view = m_frame->GetCanvas()->GetView();
231
233 {
235
237 []( KIGFX::VIEW_ITEM* aItem )
238 {
239 GERBER_DRAW_ITEM* item = static_cast<GERBER_DRAW_ITEM*>( aItem );
240
241 switch( item->m_ShapeType )
242 {
243 case GBR_CIRCLE:
244 case GBR_ARC:
245 case GBR_SEGMENT:
246 return true;
247
248 default:
249 return false;
250 }
251 } );
252 }
254 {
256
258 []( KIGFX::VIEW_ITEM* aItem )
259 {
260 GERBER_DRAW_ITEM* item = static_cast<GERBER_DRAW_ITEM*>( aItem );
261
262 switch( item->m_ShapeType )
263 {
264 case GBR_SPOT_CIRCLE:
265 case GBR_SPOT_RECT:
266 case GBR_SPOT_OVAL:
267 case GBR_SPOT_POLY:
268 case GBR_SPOT_MACRO:
269 return true;
270
271 default:
272 return false;
273 }
274 } );
275 }
277 {
279
281 []( KIGFX::VIEW_ITEM* aItem )
282 {
283 GERBER_DRAW_ITEM* item = static_cast<GERBER_DRAW_ITEM*>( aItem );
284
285 return ( item->m_ShapeType == GBR_POLYGON );
286 } );
287 }
289 {
291 }
292 else if( aEvent.IsAction( &GERBVIEW_ACTIONS::dcodeDisplay ) )
293 {
294 m_frame->SetElementVisibility( LAYER_DCODES, !cfg->m_Appearance.show_dcodes );
295 }
296 else if( aEvent.IsAction( &ACTIONS::highContrastMode )
298 {
300 }
302 {
304
306 cfg->m_Display.m_XORMode = false;
307
308 m_frame->UpdateXORLayers();
309 }
310 else if( aEvent.IsAction( &GERBVIEW_ACTIONS::toggleXORMode ) )
311 {
313
315 cfg->m_Display.m_ForceOpacityMode = false;
316
317 m_frame->UpdateXORLayers();
318 }
319 else if( aEvent.IsAction( &GERBVIEW_ACTIONS::flipGerberView ) )
320 {
322 view->SetMirror( cfg->m_Display.m_FlipGerberView, false );
323 }
324
325 m_frame->ApplyDisplaySettingsToGAL();
326
328 m_frame->GetCanvas()->Refresh();
329
330 return 0;
331}
332
333
335{
336 int layer = m_frame->GetActiveLayer();
337
338 if( layer < ( (int) GERBER_FILE_IMAGE_LIST::GetImagesList().GetLoadedImageCount() - 1 ) )
339 m_frame->SetActiveLayer( layer + 1, true );
340
341 return 0;
342}
343
344
346{
347 int layer = m_frame->GetActiveLayer();
348
349 if( layer > 0 )
350 m_frame->SetActiveLayer( layer - 1, true );
351
352 return 0;
353}
354
355
357{
358 int layer = m_frame->GetActiveLayer();
359
360 if( layer > 0 )
361 {
362 m_frame->RemapLayers(
363 GERBER_FILE_IMAGE_LIST::GetImagesList().SwapImages( layer, layer - 1 ) );
364 m_frame->SetActiveLayer( layer - 1 );
365 }
366
367 return 0;
368}
369
370
372{
373 int layer = m_frame->GetActiveLayer();
375
376 if( layer < ( (int) list.GetLoadedImageCount() - 1 ) )
377 {
378 m_frame->RemapLayers( list.SwapImages( layer, layer + 1 ) );
379 m_frame->SetActiveLayer( layer + 1 );
380 }
381
382 return 0;
383}
384
385
387{
388 m_frame->Erase_Current_DrawLayer( true );
389 m_frame->ClearMsgPanel();
390
391 return 0;
392}
393
394
396{
397 m_frame->Clear_DrawLayers( false );
398 m_toolMgr->RunAction( ACTIONS::zoomFitScreen );
399 canvas()->Refresh();
400 m_frame->ClearMsgPanel();
401
402 // Clear pending highlight selections, now outdated
404 static_cast<KIGFX::GERBVIEW_PAINTER*>( getView()->GetPainter() )->GetSettings();
405 settings->ClearHighlightSelections();
406
407 return 0;
408}
409
410
412{
413 // Store filenames
414 wxArrayString listOfGerberFiles;
415 std::vector<int> fileType;
416 GERBER_FILE_IMAGE_LIST* list = m_frame->GetImagesList();
417
418 for( unsigned i = 0; i < list->ImagesMaxCount(); i++ )
419 {
420 if( list->GetGbrImage( i ) == nullptr )
421 continue;
422
423 if( !list->GetGbrImage( i )->m_InUse )
424 continue;
425
426 EXCELLON_IMAGE* drill_file = dynamic_cast<EXCELLON_IMAGE*>( list->GetGbrImage( i ) );
427
428 if( drill_file )
429 fileType.push_back( 1 );
430 else
431 fileType.push_back( 0 );
432
433 listOfGerberFiles.Add( list->GetGbrImage( i )->m_FileName );
434 }
435
436 // Clear all layers
437 m_frame->Clear_DrawLayers( false );
438 m_frame->ClearMsgPanel();
439
440 // Load the layers from stored paths
441 wxBusyCursor wait;
442 m_frame->LoadListOfGerberAndDrillFiles( wxEmptyString, listOfGerberFiles, &fileType );
443
444 return 0;
445}
446
447
449{
451 GERBVIEW_SELECTION& selection = selTool->GetSelection();
452
453 if( selection.GetSize() == 1 )
454 {
455 EDA_ITEM* item = (EDA_ITEM*) selection.Front();
456
457 std::vector<MSG_PANEL_ITEM> msgItems;
458 item->GetMsgPanelInfo( m_frame, msgItems );
459 m_frame->SetMsgPanel( msgItems );
460 }
461 else
462 {
463 m_frame->EraseMsgBox();
464 }
465
466 return 0;
467}
468
469
471{
472 m_frame->LoadZipArchiveFile( *aEvent.Parameter<wxString*>() );
473 canvas()->Refresh();
474
475 return 0;
476}
477
478
480{
481 // The event parameter is a string containing names of dropped files.
482 // Each file name has been enclosed with "", so file names with space character are allowed.
483 wxString files = *aEvent.Parameter<wxString*>();
484 // ie : files = "file1""another file""file3"...
485
486 std::vector<wxString> aFileNameList;
487
488 // Isolate each file name, deletting ""
489 files = files.AfterFirst( '"' );
490
491 // Gerber files are enclosed with "".
492 // Load files names in array.
493 while( !files.empty() )
494 {
495 wxString fileName = files.BeforeFirst( '"' );
496 // Check if file exists. If not, keep on and ignore fileName
497 if( wxFileName( fileName ).Exists() )
498 aFileNameList.push_back( fileName );
499 files = files.AfterFirst( '"' );
500 files = files.AfterFirst( '"' );
501 }
502
503 if( !aFileNameList.empty() )
504 m_frame->OpenProjectFiles( aFileNameList, KICTL_CREATE );
505
506 return 0;
507}
508
509
511{
520
526
534
545
550
553}
static TOOL_ACTION updateUnits
Definition actions.h:207
static TOOL_ACTION highContrastModeCycle
Definition actions.h:156
static TOOL_ACTION highContrastMode
Definition actions.h:155
static TOOL_ACTION zoomFitScreen
Definition actions.h:142
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:225
static const TOOL_EVENT ClearedEvent
Definition actions.h:347
static const TOOL_EVENT SelectedEvent
Definition actions.h:345
static const TOOL_EVENT UnselectedEvent
Definition actions.h:346
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:558
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition view.cpp:1572
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:1582
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:629
The common library.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:230
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:529
@ LAYER_NEGATIVE_OBJECTS
Definition layer_ids.h:530
@ COLOR
Color has changed.
Definition view_item.h:54
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:58
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
#define NAMELESS_PROJECT
default name for nameless projects
Definition project.h:44
MODEL3D_FORMAT_TYPE fileType(const char *aFileName)
wxString UnescapeString(const wxString &aSource)
std::string path
Definition of file extensions used in Kicad.