KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_3d_viewer_frame.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) 2015-2016 Mario Luzeiro <[email protected]>
5 * Copyright (C) 2023 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <wx/filename.h>
23#include <wx/msgdlg.h>
24#include <wx/string.h>
25#include <wx/wupdlock.h>
26#include <wx/clipbrd.h>
27#include <wx/filedlg.h>
28#include <wx/dialog.h>
29#include "eda_3d_viewer_frame.h"
34#include <3d_viewer_id.h>
38#include <board.h>
39#include <advanced_config.h>
40#include <bitmaps.h>
42#include <core/arraydim.h>
43#include <dpi_scaling_common.h>
44#include <pgm_base.h>
45#include <project.h>
49#include <tool/action_manager.h>
50#include <tool/common_control.h>
51#include <tool/tool_manager.h>
53#include <tool/action_toolbar.h>
54#include <widgets/wx_infobar.h>
57#include <kiplatform/ui.h>
58#include <project_pcb.h>
59#include <toolbars_3d.h>
60
61#if defined(__linux__) || defined(__FreeBSD__)
64#else
66#endif
67
75const wxChar* EDA_3D_VIEWER_FRAME::m_logTrace = wxT( "KI_TRACE_EDA_3D_VIEWER" );
76
77
78BEGIN_EVENT_TABLE( EDA_3D_VIEWER_FRAME, KIWAY_PLAYER )
79
81 EVT_SET_FOCUS( EDA_3D_VIEWER_FRAME::OnSetFocus )
82
84 EDA_3D_VIEWER_FRAME::Process_Special_Functions )
85
86 EVT_MENU( wxID_CLOSE, EDA_3D_VIEWER_FRAME::Exit3DFrame )
88
89 EVT_CLOSE( EDA_3D_VIEWER_FRAME::OnCloseWindow )
90END_EVENT_TABLE()
91
92
94 const wxString& aTitle, long style ) :
95 KIWAY_PLAYER( aKiway, aParent, FRAME_PCB_DISPLAY3D, aTitle, wxDefaultPosition,
96 wxDefaultSize, style, QUALIFIED_VIEWER3D_FRAMENAME( aParent ), unityScale ),
97 m_canvasHolder( nullptr ),
98 m_canvas( nullptr ),
99 m_currentCamera( m_trackBallCamera ),
100 m_trackBallCamera( 2 * RANGE_SCALE_3D )
101{
102 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::EDA_3D_VIEWER_FRAME %s" ), aTitle );
103
104 m_disable_ray_tracing = false;
105 m_aboutTitle = _HKI( "KiCad 3D Viewer" );
106
107 // Give it an icon
108 wxIcon icon;
109 icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_3d ) );
110 SetIcon( icon );
111
112 // Create the status line
113 std::vector<int> status_dims{ -1,
114 -1,
115 -1,
116 GetTextExtent( wxS( "dx -88.88" ) ).x + 10,
117 GetTextExtent( wxS( "dy -88.88" ) ).x + 10,
118 GetTextExtent( wxS( "zoom -88.88" ) ).x + 10 };
119
120 wxStatusBar* status_bar = CreateStatusBar( status_dims.size() );
121 SetStatusWidths( status_dims.size(), status_dims.data() );
122
125
126 if( cfg )
127 aaMode = static_cast<ANTIALIASING_MODE>( cfg->m_Render.opengl_AA_mode );
128
129 m_canvasHolder = new wxPanel( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER );
130
131 m_canvas = new EDA_3D_CANVAS( m_canvasHolder, OGL_ATT_LIST::GetAttributesList( aaMode, true ),
132 m_boardAdapter, m_currentCamera,
134
135 wxBoxSizer* canvasSizer = new wxBoxSizer( wxVERTICAL );
136 canvasSizer->Add( m_canvas, 1, wxEXPAND, 0 );
137 m_canvasHolder->SetSizer( canvasSizer );
138
139 m_appearancePanel = new APPEARANCE_CONTROLS_3D( this, GetCanvas() );
140
141 LoadSettings( GetAppSettings<EDA_3D_VIEWER_SETTINGS>( "3d_viewer" ) );
142 loadCommonSettings();
143
144 ApplyProjectFileSettings();
145
146 // Create the manager
147 m_toolManager = new TOOL_MANAGER;
148 m_toolManager->SetEnvironment( GetBoard(), nullptr, nullptr,
149 GetAppSettings<EDA_3D_VIEWER_SETTINGS>( "3d_viewer" ), this );
150
151 m_actions = new EDA_3D_ACTIONS();
152 m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager );
153 m_canvas->SetEventDispatcher( m_toolDispatcher );
154
155 // Register tools
156 m_toolManager->RegisterTool( new COMMON_CONTROL );
157 m_toolManager->RegisterTool( new EDA_3D_CONTROLLER );
158 m_toolManager->InitTools();
159
160 setupUIConditions();
161
162 if( EDA_3D_CONTROLLER* ctrlTool = GetToolManager()->GetTool<EDA_3D_CONTROLLER>() )
163 ctrlTool->SetRotationIncrement( cfg ? cfg->m_Camera.rotation_increment : 10.0 );
164
165 // Run the viewer control tool, it is supposed to be always active
166 m_toolManager->InvokeTool( "3DViewer.Control" );
167
168 ReCreateMenuBar();
169
170 m_toolbarSettings = Pgm().GetSettingsManager().GetToolbarSettings<EDA_3D_VIEWER_TOOLBAR_SETTINGS>( "3d_viewer-toolbars" );
171 configureToolbars();
172 RecreateToolbars();
173
174 m_infoBar = new WX_INFOBAR( m_canvasHolder, wxID_ANY, true );
175
176 m_auimgr.SetManagedWindow( this );
177
178 m_auimgr.AddPane( m_tbTopMain, EDA_PANE().HToolbar().Name( wxS( "TopMainToolbar" ) )
179 .Top().Layer( 6 ) );
180 m_auimgr.AddPane( m_appearancePanel, EDA_PANE().Name( "LayersManager" )
181 .Right().Layer( 3 )
182 .Caption( _( "Appearance" ) ).PaneBorder( false )
183 .MinSize( FromDIP( 180 ), -1 ).BestSize( FromDIP( 190 ), -1 ) );
184 m_auimgr.AddPane( m_canvasHolder, EDA_PANE().Canvas().Name( wxS( "DrawFrame" ) )
185 .Center() );
186
187 wxAuiPaneInfo& layersManager = m_auimgr.GetPane( "LayersManager" );
188
189 if( cfg && cfg->m_AuiPanels.right_panel_width > 0 )
190 SetAuiPaneSize( m_auimgr, layersManager, cfg->m_AuiPanels.right_panel_width, -1 );
191
192 if( cfg )
193 layersManager.Show( cfg->m_AuiPanels.show_layer_manager );
194
195 m_auimgr.Update();
196
197 m_canvas->SetInfoBar( m_infoBar );
198 m_canvas->SetStatusBar( status_bar );
199
200 try
201 {
202#if defined(__linux__) || defined(__FreeBSD__)
203 m_spaceMouse = std::make_unique<SPNAV_VIEWER_PLUGIN>( m_canvas );
204#else
205 m_spaceMouse = std::make_unique<NL_3D_VIEWER_PLUGIN>( m_canvas );
206#endif
207 }
208 catch( const std::exception& e )
209 {
210 wxLogTrace( wxT( "KI_TRACE_NAVLIB" ), wxS( "%s" ), e.what() );
211 }
212 catch( ... )
213 {
214 wxLogTrace( wxT( "KI_TRACE_NAVLIB" ),
215 wxT( "Unknown exception during SpaceMouse initialization" ) );
216 }
217
218 // Fixes bug in Windows (XP and possibly others) where the canvas requires the focus
219 // in order to receive mouse events. Otherwise, the user has to click somewhere on
220 // the canvas before it will respond to mouse wheel events.
221 m_canvas->SetFocus();
222}
223
224
226{
227 // Shutdown all running tools
228 if( m_toolManager )
229 m_toolManager->ShutdownAllTools();
230
231 Prj().GetProjectFile().m_Viewports3D = m_appearancePanel->GetUserViewports();
232
233 m_canvas->SetEventDispatcher( nullptr );
234
235 m_auimgr.UnInit();
236
237 delete m_canvas;
238 m_canvas = nullptr;
239}
240
241
246
247
249{
251
252 ACTION_MANAGER* mgr = m_toolManager->GetActionManager();
254
255// Helper to define check conditions
256#define GridSizeCheck( x ) ACTION_CONDITIONS().Check( cond.GridSize( x ) )
257
258 auto raytracing =
259 [this]( const SELECTION& aSel )
260 {
261 return m_boardAdapter.m_Cfg->m_Render.engine != RENDER_ENGINE::OPENGL;
262 };
263 auto showMissingModels =
264 [this]( const SELECTION& aSel )
265 {
266 return m_boardAdapter.ShowMissingModels();
267 };
268 auto showTH =
269 [this]( const SELECTION& aSel )
270 {
271 return m_boardAdapter.m_Cfg->m_Render.show_footprints_normal;
272 };
273 auto showSMD =
274 [this]( const SELECTION& aSel )
275 {
276 return m_boardAdapter.m_Cfg->m_Render.show_footprints_insert;
277 };
278 auto showVirtual =
279 [this]( const SELECTION& aSel )
280 {
281 return m_boardAdapter.m_Cfg->m_Render.show_footprints_virtual;
282 };
283 auto show_NotInPosfile =
284 [this]( const SELECTION& aSel )
285 {
286 return m_boardAdapter.m_Cfg->m_Render.show_footprints_not_in_posfile;
287 };
288 auto show_DNP =
289 [this]( const SELECTION& aSel )
290 {
291 return m_boardAdapter.m_Cfg->m_Render.show_footprints_dnp;
292 };
293 auto showBBoxes =
294 [this]( const SELECTION& aSel )
295 {
296 return m_boardAdapter.m_Cfg->m_Render.show_model_bbox;
297 };
298 auto showNavig =
299 [this]( const SELECTION& aSel )
300 {
301 return m_boardAdapter.m_Cfg->m_Render.show_navigator;
302 };
303 auto ortho =
304 [this]( const SELECTION& )
305 {
306 return m_currentCamera.GetProjection() == PROJECTION_TYPE::ORTHO;
307 };
308
309 auto appearances =
310 [this]( const SELECTION& aSel )
311 {
312 return m_boardAdapter.m_Cfg->m_AuiPanels.show_layer_manager;
313 };
314
316 mgr->SetConditions( EDA_3D_ACTIONS::toggleShowMissingModels, ACTION_CONDITIONS().Check( showMissingModels ) );
317
319 mgr->SetConditions( EDA_3D_ACTIONS::showSMD, ACTION_CONDITIONS().Check( showSMD ) );
320 mgr->SetConditions( EDA_3D_ACTIONS::showVirtual, ACTION_CONDITIONS().Check( showVirtual ) );
322 ACTION_CONDITIONS().Check( show_NotInPosfile ) );
323 mgr->SetConditions( EDA_3D_ACTIONS::showDNP, ACTION_CONDITIONS().Check( show_DNP ) );
324
325 mgr->SetConditions( EDA_3D_ACTIONS::showBBoxes, ACTION_CONDITIONS().Check( showBBoxes ) );
327
334
336 ACTION_CONDITIONS().Check( appearances ) );
337
338#undef GridSizeCheck
339}
340
341
342bool EDA_3D_VIEWER_FRAME::TryBefore( wxEvent& aEvent )
343{
344 static bool s_presetSwitcherShown = false;
345 static bool s_viewportSwitcherShown = false;
346
347 // wxWidgets generates no key events for the tab key when the ctrl key is held down. One
348 // way around this is to look at all events and inspect the keyboard state of the tab key.
349 // However, this runs into issues on some linux VMs where querying the keyboard state is
350 // very slow. Fortunately we only use ctrl-tab on Mac, so we implement this lovely hack:
351#ifdef __WXMAC__
352 if( wxGetKeyState( WXK_TAB ) )
353#else
354 if( ( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
355 && static_cast<wxKeyEvent&>( aEvent ).GetKeyCode() == WXK_TAB )
356#endif
357 {
358 if( !s_presetSwitcherShown && wxGetKeyState( PRESET_SWITCH_KEY ) )
359 {
360 if( m_appearancePanel && this->IsActive() )
361 {
362 wxArrayString mru = m_appearancePanel->GetLayerPresetsMRU();
363
364 if( mru.size() > 0 )
365 {
366 for( wxString& str : mru )
367 {
368 if( str == FOLLOW_PCB )
369 str = _( "Follow PCB Editor" );
370 else if( str == FOLLOW_PLOT_SETTINGS )
371 str = _( "Follow PCB Plot Settings" );
372 }
373
374 EDA_VIEW_SWITCHER switcher( this, mru, PRESET_SWITCH_KEY );
375
376 s_presetSwitcherShown = true;
377 switcher.ShowModal();
378 s_presetSwitcherShown = false;
379
380 int idx = switcher.GetSelection();
381
382 if( idx >= 0 && idx < (int) mru.size() )
383 {
384 wxString internalName = m_appearancePanel->GetLayerPresetsMRU()[idx];
385 m_appearancePanel->ApplyLayerPreset( internalName );
386 }
387
388 return true;
389 }
390 }
391 }
392 else if( !s_viewportSwitcherShown && wxGetKeyState( VIEWPORT_SWITCH_KEY ) )
393 {
394 if( this->IsActive() )
395 {
396 const wxArrayString& viewportMRU = m_appearancePanel->GetViewportsMRU();
397
398 if( viewportMRU.size() > 0 )
399 {
400 EDA_VIEW_SWITCHER switcher( this, viewportMRU, VIEWPORT_SWITCH_KEY );
401
402 s_viewportSwitcherShown = true;
403 switcher.ShowModal();
404 s_viewportSwitcherShown = false;
405
406 int idx = switcher.GetSelection();
407
408 if( idx >= 0 && idx < (int) viewportMRU.size() )
409 m_appearancePanel->ApplyViewport( viewportMRU[idx] );
410
411 return true;
412 }
413 }
414 }
415 }
416
417 return wxFrame::TryBefore( aEvent );
418}
419
420
421void EDA_3D_VIEWER_FRAME::handleIconizeEvent( wxIconizeEvent& aEvent )
422{
424
425 if( m_spaceMouse && aEvent.IsIconized() )
426 m_spaceMouse->SetFocus( false );
427}
428
429
431{
432 m_appearancePanel->SetUserViewports( Prj().GetProjectFile().m_Viewports3D );
433}
434
435
437{
438 // This will schedule a request to load later
439 // ReloadRequest also updates the board pointer so always call it first
440 if( m_canvas )
441 m_canvas->ReloadRequest( GetBoard(), PROJECT_PCB::Get3DCacheManager( &Prj() ) );
442
444 m_appearancePanel->UpdateLayerCtls();
445}
446
447
448void EDA_3D_VIEWER_FRAME::NewDisplay( bool aForceImmediateRedraw )
449{
450 if( m_canvas )
451 m_canvas->ReloadRequest( GetBoard(), PROJECT_PCB::Get3DCacheManager( &Prj() ) );
452
453 // After the ReloadRequest call, the refresh often takes a bit of time,
454 // and it is made here only on request.
455 if( m_canvas && aForceImmediateRedraw )
456 m_canvas->Refresh();
457}
458
459
461{
462 // Only update in OpenGL for an interactive interaction
463 if( m_boardAdapter.m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
464 m_canvas->Request_refresh( true );
465}
466
467
469{
470 if( m_boardAdapter.m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
471 m_canvas->Request_refresh();
472 else
473 NewDisplay( true );
474}
475
476
477void EDA_3D_VIEWER_FRAME::Exit3DFrame( wxCommandEvent &event )
478{
479 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::Exit3DFrame" ) );
480
481 Close( true );
482}
483
484
485void EDA_3D_VIEWER_FRAME::OnCloseWindow( wxCloseEvent &event )
486{
487 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::OnCloseWindow" ) );
488
489 // Do not show the layer manager during closing to avoid flicker on some platforms (Windows)
490 // that generate useless redraw of items in the Layer Manager
491 if( m_auimgr.GetPane( wxS( "LayersManager" ) ).IsShown() )
492 m_auimgr.GetPane( wxS( "LayersManager" ) ).Show( false );
493
494 if( m_canvas )
495 m_canvas->Close();
496
497 Destroy();
498 event.Skip( true );
499}
500
501
503{
504 if( m_canvas == nullptr )
505 return;
506
507 switch( event.GetId() )
508 {
510 {
511 m_boardAdapter.SetLayerColors( m_boardAdapter.GetDefaultColors() );
512
514 cfg->ResetToDefaults();
515
517
518 // Tell canvas that we (may have) changed the render engine
520 NewDisplay( true );
521 return;
522 }
523
524 default:
525 wxFAIL_MSG( wxT( "Invalid event in EDA_3D_VIEWER_FRAME::Process_Special_Functions()" ) );
526 return;
527 }
528}
529
530
531void EDA_3D_VIEWER_FRAME::onDisableRayTracing( wxCommandEvent& aEvent )
532{
533 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::%s disabling ray tracing." ), __WXFUNCTION__ );
534
536 m_boardAdapter.m_Cfg->m_Render.engine = RENDER_ENGINE::OPENGL;
537}
538
539
540void EDA_3D_VIEWER_FRAME::OnActivate( wxActivateEvent &aEvent )
541{
542 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::OnActivate" ) );
543
544 if( aEvent.GetActive() && m_canvas )
545 {
546 // Reload data if 3D frame shows a board,
547 // because it can be changed since last frame activation
548 if( m_canvas->IsReloadRequestPending() )
549 m_canvas->Request_refresh();
550
551 // Activates again the focus of the canvas so it will catch mouse and key events
552 m_canvas->SetFocus();
553 }
554
555 if( m_spaceMouse )
556 m_spaceMouse->SetFocus( aEvent.GetActive() );
557
558 aEvent.Skip(); // required under wxMAC
559}
560
561
562void EDA_3D_VIEWER_FRAME::OnSetFocus( wxFocusEvent& aEvent )
563{
564 // Activates again the focus of the canvas so it will catch mouse and key events
565 if( m_canvas )
566 m_canvas->SetFocus();
567
568 aEvent.Skip();
569}
570
571
573{
575
576 // Dynamic_cast here will fail on Mac when called from CvPCB.
577 EDA_3D_VIEWER_SETTINGS* cfg = static_cast<EDA_3D_VIEWER_SETTINGS*>( aCfg );
578
579 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::LoadSettings" ) );
580
581 if( cfg )
582 {
583 applySettings( cfg );
584
585 m_boardAdapter.SetBoard( GetBoard() );
586
587 // When opening the 3D viewer, we use the OpenGL mode, never the ray tracing engine
588 // because the ray tracing is very time consuming, and can be seen as not working
589 // (freeze window) with large boards.
590 m_boardAdapter.m_Cfg->m_Render.engine = RENDER_ENGINE::OPENGL;
591
593 {
594 wxString legacyColorsPresetName = _( "legacy colors" );
595
596 cfg->m_UseStackupColors = false;
597
598 if( !cfg->FindPreset( legacyColorsPresetName ) )
599 {
600 cfg->m_LayerPresets.emplace_back( legacyColorsPresetName,
601 GetAdapter().GetDefaultVisibleLayers(),
602 GetAdapter().GetDefaultColors() );
603 }
604
606 }
607
608 m_boardAdapter.InitSettings( nullptr, nullptr );
609
611 m_appearancePanel->CommonSettingsChanged();
612 }
613}
614
615
617{
619
621 {
622 cfg->m_AuiPanels.right_panel_width = m_appearancePanel->GetSize().x;
623
624 cfg->m_Camera.animation_enabled = m_canvas->GetAnimationEnabled();
625 cfg->m_Camera.moving_speed_multiplier = m_canvas->GetMovingSpeedMultiplier();
626 cfg->m_Viewer.projection_mode = m_canvas->GetProjectionMode();
627 }
628}
629
630
632{
633 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::CommonSettingsChanged" ) );
634
635 // Regen menu bars, etc
637
639
641
642 if( cfg )
643 applySettings( cfg );
644
645 if( EDA_3D_CONTROLLER* ctrlTool = GetToolManager()->GetTool<EDA_3D_CONTROLLER>() )
646 ctrlTool->SetRotationIncrement( cfg ? cfg->m_Camera.rotation_increment : 10.0 );
647
648 m_appearancePanel->CommonSettingsChanged();
649
650 NewDisplay( true );
651}
652
653
655{
657
658 SetTitle( _( "3D Viewer" ) );
660
662 {
663 wxAuiPaneInfo& lm_pane_info = m_auimgr.GetPane( m_appearancePanel );
664 lm_pane_info.Caption( _( "Appearance" ) );
665 }
666
667 SetStatusText( wxEmptyString, ACTIVITY );
668 SetStatusText( wxEmptyString, RENDER_TIME );
669 SetStatusText( wxEmptyString, HOVERED_ITEM );
670}
671
672
674{
675 wxAuiPaneInfo& layersManager = m_auimgr.GetPane( "LayersManager" );
676
678 {
679 // show auxiliary Vertical layers and visibility manager toolbar
680 cfg->m_AuiPanels.show_layer_manager = !cfg->m_AuiPanels.show_layer_manager;
681
682 layersManager.Show( cfg->m_AuiPanels.show_layer_manager );
683
684 if( cfg->m_AuiPanels.show_layer_manager )
685 {
686 SetAuiPaneSize( m_auimgr, layersManager, cfg->m_AuiPanels.right_panel_width, -1 );
687 }
688 else
689 {
690 cfg->m_AuiPanels.right_panel_width = m_appearancePanel->GetSize().x;
691 m_auimgr.Update();
692 }
693 }
694}
695
696
698{
700 m_appearancePanel->OnDarkModeToggle();
701}
702
703
705{
706 wxString fullFileName;
707
709 {
710 if( !getExportFileName( aFormat, fullFileName ) )
711 return;
712 }
713
714 wxImage screenshotImage = captureCurrentViewScreenshot();
715
716 if( screenshotImage.IsOk() )
717 {
718 saveOrCopyImage( screenshotImage, aFormat, fullFileName );
719 }
720}
721
722
724{
725 // Ensure we have the latest 3D view (remember 3D view is buffered)
726 // Also ensure any highlighted item is not highlighted when creating screen shot
728 bool original_highlight = cfg.highlight_on_rollover;
729 cfg.highlight_on_rollover = false;
730
731 if( m_canvas )
732 {
733 // Ensure any in-progress background loading finishes before capture.
734 m_canvas->JoinBgWorker();
735 m_canvas->DoRePaint(); // init first buffer
736 m_canvas->JoinBgWorker();
737 m_canvas->DoRePaint(); // init second buffer
738 }
739
740 wxImage screenshotImage;
741
742 if( m_canvas )
743 {
744 // Build image from the 3D buffer
745 wxWindowUpdateLocker noUpdates( this );
746 m_canvas->GetScreenshot( screenshotImage );
747 }
748
749 // Restore highlight setting
750 cfg.highlight_on_rollover = original_highlight;
751
752 return screenshotImage;
753}
754
755
757{
758 wxString fullFileName;
759
761 {
762 if( !getExportFileName( aFormat, fullFileName ) )
763 return;
764 }
765
766 wxImage screenshotImage = captureScreenshot( aSize );
767
768 if( screenshotImage.IsOk() )
769 {
770 saveOrCopyImage( screenshotImage, aFormat, fullFileName );
771 }
772}
773
774
776{
777 // Create combined wildcard for both formats
778 const wxString wildcard = FILEEXT::JpegFileWildcard() + "|" + FILEEXT::PngFileWildcard();
779
782
783 // Set default extension based on current format
784 const wxString defaultExt = ( aFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG ) ?
786 m_defaultSaveScreenshotFileName.SetExt( defaultExt );
787
788 wxFileDialog dlg( this, _( "3D Image File Name" ),
790 m_defaultSaveScreenshotFileName.GetFullName(), wildcard,
791 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
792
794
795 // Set initial filter index based on current format
796 dlg.SetFilterIndex( ( aFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG ) ? 0 : 1 );
797
798 if( dlg.ShowModal() == wxID_CANCEL )
799 return false;
800
801 m_defaultSaveScreenshotFileName = dlg.GetPath();
802
803 // Determine format based on file extension first
804 wxString fileExt = m_defaultSaveScreenshotFileName.GetExt().Lower();
805 EDA_3D_VIEWER_EXPORT_FORMAT detectedFormat;
806 bool formatDetected = false;
807
808 if( fileExt == wxT("jpg") || fileExt == wxT("jpeg") )
809 {
810 detectedFormat = EDA_3D_VIEWER_EXPORT_FORMAT::JPEG;
811 formatDetected = true;
812 }
813 else if( fileExt == wxT("png") )
814 {
815 detectedFormat = EDA_3D_VIEWER_EXPORT_FORMAT::PNG;
816 formatDetected = true;
817 }
818
819 // If format can't be determined from extension, use dropdown selection
820 if( !formatDetected )
821 {
822 int filterIndex = dlg.GetFilterIndex();
823 detectedFormat = ( filterIndex == 0 ) ? EDA_3D_VIEWER_EXPORT_FORMAT::JPEG :
825
826 // Append appropriate extension
827 const wxString ext = ( detectedFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG ) ?
830 }
831
832 // Update the format parameter
833 aFormat = detectedFormat;
834
835 // Check directory permissions using the updated filename
836 wxFileName fn = m_defaultSaveScreenshotFileName;
837
838 if( !fn.IsDirWritable() )
839 {
840 wxString msg;
841 msg.Printf( _( "Insufficient permissions to save file '%s'." ),
842 m_defaultSaveScreenshotFileName.GetFullPath() );
843 wxMessageBox( msg, _( "Error" ), wxOK | wxICON_ERROR, this );
844 return false;
845 }
846
847 fullFileName = m_defaultSaveScreenshotFileName.GetFullPath();
848 return true;
849}
850
851
852wxImage EDA_3D_VIEWER_FRAME::captureScreenshot( const wxSize& aSize )
853{
855 camera.SetCurWindowSize( aSize );
856
858 EDA_3D_VIEWER_SETTINGS* backupCfg = m_boardAdapter.m_Cfg;
859
860 auto configRestorer = std::unique_ptr<void, std::function<void(void*)>>(
861 reinterpret_cast<void*>(1),
862 [&](void*) { m_boardAdapter.m_Cfg = backupCfg; }
863 );
864
865 if( cfg )
866 m_boardAdapter.m_Cfg = cfg;
867
868 if( cfg && cfg->m_Render.engine == RENDER_ENGINE::RAYTRACING )
869 return captureRaytracingScreenshot( m_boardAdapter, camera, aSize );
870 else
871 return captureOpenGLScreenshot( m_boardAdapter, camera, aSize );
872}
873
874
876{
878
879 if( cfg )
880 aAdapter.m_Cfg = cfg;
881}
882
883
884wxImage EDA_3D_VIEWER_FRAME::captureRaytracingScreenshot( BOARD_ADAPTER& aAdapter, TRACK_BALL& aCamera, const wxSize& aSize )
885{
886 BOARD_ADAPTER tempadapter;
887 tempadapter.SetBoard( GetBoard() );
888 tempadapter.m_Cfg = aAdapter.m_Cfg;
889 tempadapter.InitSettings( nullptr, nullptr );
890 tempadapter.CreateLayers( nullptr );
891 tempadapter.Set3dCacheManager( aAdapter.Get3dCacheManager() );
892
893 RENDER_3D_RAYTRACE_RAM raytrace( tempadapter, aCamera );
894 raytrace.SetCurWindowSize( aSize );
895
896 while( raytrace.Redraw( false ) );
897
898 uint8_t* rgbaBuffer = raytrace.GetBuffer();
899 wxSize realSize = raytrace.GetRealBufferSize();
900
901 if( !rgbaBuffer )
902 return wxImage();
903
904 return convertRGBAToImage( rgbaBuffer, realSize );
905}
906
907
908wxImage EDA_3D_VIEWER_FRAME::convertRGBAToImage( uint8_t* aRGBABuffer, const wxSize& aRealSize )
909{
910 const unsigned int wxh = aRealSize.x * aRealSize.y;
911
912 unsigned char* rgbBuffer = (unsigned char*) malloc( wxh * 3 );
913 unsigned char* alphaBuffer = (unsigned char*) malloc( wxh );
914
915 unsigned char* rgbaPtr = aRGBABuffer;
916 unsigned char* rgbPtr = rgbBuffer;
917 unsigned char* alphaPtr = alphaBuffer;
918
919 for( int y = 0; y < aRealSize.y; y++ )
920 {
921 for( int x = 0; x < aRealSize.x; x++ )
922 {
923 rgbPtr[0] = rgbaPtr[0];
924 rgbPtr[1] = rgbaPtr[1];
925 rgbPtr[2] = rgbaPtr[2];
926 alphaPtr[0] = rgbaPtr[3];
927
928 rgbaPtr += 4;
929 rgbPtr += 3;
930 alphaPtr += 1;
931 }
932 }
933
934 wxImage screenshotImage;
935 screenshotImage.Create( aRealSize );
936 screenshotImage.SetData( rgbBuffer );
937 screenshotImage.SetAlpha( alphaBuffer );
938 return screenshotImage.Mirror( false );
939}
940
941
942wxImage EDA_3D_VIEWER_FRAME::captureOpenGLScreenshot( BOARD_ADAPTER& aAdapter, TRACK_BALL& aCamera, const wxSize& aSize )
943{
944 EDA_3D_VIEWER_SETTINGS* cfg = aAdapter.m_Cfg;
945 ANTIALIASING_MODE aaMode = cfg ? static_cast<ANTIALIASING_MODE>( cfg->m_Render.opengl_AA_mode )
947
948 wxFrame temp( this, wxID_ANY, wxEmptyString, wxDefaultPosition, aSize, wxFRAME_NO_TASKBAR );
949 temp.Hide();
950 BOARD_ADAPTER tempadapter;
951 tempadapter.SetBoard( GetBoard() );
952 tempadapter.m_Cfg = aAdapter.m_Cfg;
953 tempadapter.InitSettings( nullptr, nullptr );
954 tempadapter.CreateLayers( nullptr );
955 tempadapter.Set3dCacheManager( aAdapter.Get3dCacheManager() );
956
957 auto canvas = std::make_unique<EDA_3D_CANVAS>( &temp,
958 OGL_ATT_LIST::GetAttributesList( aaMode, true ),
959 tempadapter, aCamera,
960 aAdapter.Get3dCacheManager() );
961
962 canvas->SetSize( aSize );
963 configureCanvas( canvas, cfg );
964 wxWindowUpdateLocker noUpdates( this );
965
966 // Temporarily disable highlight during screenshot
968 bool original_highlight = renderCfg.highlight_on_rollover;
969 bool original_navigator = renderCfg.show_navigator;
970 renderCfg.show_navigator = false;
971 renderCfg.highlight_on_rollover = false;
972
973 std::vector<unsigned char> buffer(aSize.x * aSize.y * 4); // RGBA format
974 canvas->RenderToFrameBuffer( buffer.data(), aSize.x, aSize.y );
975 wxImage result = convertRGBAToImage( buffer.data(), aSize );
976
977 // Restore highlight setting
978 renderCfg.highlight_on_rollover = original_highlight;
979 renderCfg.show_navigator = original_navigator;
980
981 return result;
982}
983
984
985void EDA_3D_VIEWER_FRAME::configureCanvas( std::unique_ptr<EDA_3D_CANVAS>& aCanvas, EDA_3D_VIEWER_SETTINGS* aCfg )
986{
987 if( aCfg )
988 {
989 aCanvas->SetAnimationEnabled( aCfg->m_Camera.animation_enabled );
990 aCanvas->SetMovingSpeedMultiplier( aCfg->m_Camera.moving_speed_multiplier );
991 aCanvas->SetProjectionMode( aCfg->m_Viewer.projection_mode );
992 }
993
994 aCanvas->SetVcSettings( EDA_DRAW_PANEL_GAL::GetVcSettings() );
995}
996
997
998void EDA_3D_VIEWER_FRAME::saveOrCopyImage( const wxImage& aScreenshotImage, EDA_3D_VIEWER_EXPORT_FORMAT aFormat, const wxString& aFullFileName )
999{
1001 {
1002 copyImageToClipboard( aScreenshotImage );
1003 }
1004 else
1005 {
1006 saveImageToFile( aScreenshotImage, aFormat, aFullFileName );
1007 }
1008}
1009
1010
1011void EDA_3D_VIEWER_FRAME::copyImageToClipboard( const wxImage& aScreenshotImage )
1012{
1013 wxBitmap bitmap( aScreenshotImage );
1014 wxLogNull doNotLog;
1015
1016 if( wxTheClipboard->Open() )
1017 {
1018 wxBitmapDataObject* dobjBmp = new wxBitmapDataObject( bitmap );
1019
1020 if( !wxTheClipboard->SetData( dobjBmp ) )
1021 wxMessageBox( _( "Failed to copy image to clipboard" ) );
1022
1023 wxTheClipboard->Flush();
1024 wxTheClipboard->Close();
1025 }
1026}
1027
1028
1029void EDA_3D_VIEWER_FRAME::saveImageToFile( const wxImage& aScreenshotImage, EDA_3D_VIEWER_EXPORT_FORMAT aFormat, const wxString& aFullFileName )
1030{
1031 bool fmt_is_jpeg = ( aFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG );
1032
1033 if( !aScreenshotImage.SaveFile( aFullFileName, fmt_is_jpeg ? wxBITMAP_TYPE_JPEG : wxBITMAP_TYPE_PNG ) )
1034 {
1035 wxMessageBox( _( "Can't save file" ) );
1036 }
1037}
1038
1039
1041{
1042 wxLogTrace( m_logTrace, wxT( "EDA_3D_VIEWER_FRAME::RenderEngineChanged()" ) );
1043
1044 if( m_canvas )
1045 m_canvas->RenderEngineChanged();
1046}
1047
1048
1050{
1051 wxCHECK_RET( m_canvas, wxT( "Cannot load settings to null canvas" ) );
1052
1053 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
1054
1055 // TODO(JE) use all control options
1056 m_boardAdapter.m_MousewheelPanning = settings->m_Input.scroll_modifier_zoom != 0;
1057}
1058
1059
1061{
1062 m_boardAdapter.m_Cfg = cfg;
1063
1064 m_canvas->SetAnimationEnabled( cfg->m_Camera.animation_enabled );
1065 m_canvas->SetMovingSpeedMultiplier( cfg->m_Camera.moving_speed_multiplier );
1066 m_canvas->SetProjectionMode( cfg->m_Viewer.projection_mode );
1067
1068 m_canvas->SetVcSettings( EDA_DRAW_PANEL_GAL::GetVcSettings() );
1069}
@ ID_DISABLE_RAY_TRACING
@ ID_START_COMMAND_3D
@ ID_MENU_COMMAND_END
@ ID_MENU3D_RESET_DEFAULTS
constexpr EDA_IU_SCALE unityScale
Definition base_units.h:131
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition bitmap.cpp:100
#define RANGE_SCALE_3D
This defines the range that all coord will have to be rendered.
Manage TOOL_ACTION objects.
void SetConditions(const TOOL_ACTION &aAction, const ACTION_CONDITIONS &aConditions)
Set the conditions the UI elements for activating a specific tool action should use for determining t...
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Helper class to handle information needed to display 3D board.
void CreateLayers(std::shared_ptr< REPORTER > aStatusReporter, std::stop_token aStop=std::stop_token())
Creates the layers for the board, should be called after InitSettings.
void InitSettings(std::shared_ptr< REPORTER > aStatusReporter, std::shared_ptr< REPORTER > aWarningReporter)
Function to be called by the render when it need to reload the settings for the board.
void SetBoard(BOARD *aBoard) noexcept
Set current board to be rendered.
EDA_3D_VIEWER_SETTINGS * m_Cfg
void Set3dCacheManager(S3D_CACHE *aCacheMgr) noexcept
Update the cache manager pointer.
S3D_CACHE * Get3dCacheManager() const noexcept
bool SetCurWindowSize(const wxSize &aSize)
Update the windows size of the camera.
Definition camera.cpp:567
Handle actions that are shared between different applications.
int ShowModal() override
EDA_3D_ACTIONS.
static TOOL_ACTION showNavigator
static TOOL_ACTION showLayersManager
static TOOL_ACTION showNotInPosFile
static TOOL_ACTION showTHT
static TOOL_ACTION noGrid
static TOOL_ACTION show2_5mmGrid
static TOOL_ACTION show1mmGrid
static TOOL_ACTION showDNP
static TOOL_ACTION toggleOrtho
static TOOL_ACTION toggleShowMissingModels
static TOOL_ACTION show10mmGrid
static TOOL_ACTION toggleRaytacing
static TOOL_ACTION show5mmGrid
static TOOL_ACTION showSMD
static TOOL_ACTION showVirtual
static TOOL_ACTION showBBoxes
Implement a canvas based on a wxGLCanvas.
Handle view actions for various 3D canvases.
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
void saveOrCopyImage(const wxImage &screenshotImage, EDA_3D_VIEWER_EXPORT_FORMAT aFormat, const wxString &fullFileName)
Save image to file or copy to clipboard based on format.
void OnSetFocus(wxFocusEvent &event)
void onDisableRayTracing(wxCommandEvent &aEvent)
void CommonSettingsChanged(int aFlags) override
Notification that common settings are updated.
void applySettings(EDA_3D_VIEWER_SETTINGS *aSettings)
PCB_BASE_FRAME * Parent() const
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Save common frame parameters to a configuration data file.
void NewDisplay(bool aForceImmediateRedraw=false)
Reload and refresh (rebuild) the 3D scene.
void configureCanvas(std::unique_ptr< EDA_3D_CANVAS > &canvas, EDA_3D_VIEWER_SETTINGS *cfg)
Configure canvas settings for screenshot capture.
wxImage captureOpenGLScreenshot(BOARD_ADAPTER &adapter, TRACK_BALL &camera, const wxSize &aSize)
Capture screenshot using OpenGL renderer.
bool getExportFileName(EDA_3D_VIEWER_EXPORT_FORMAT &aFormat, wxString &fullFileName)
Get export filename through file dialog.
void setupUIConditions() override
Setup the UI conditions for the various actions and their controls in this frame.
void OnActivate(wxActivateEvent &event)
bool TryBefore(wxEvent &aEvent) override
void Exit3DFrame(wxCommandEvent &event)
Called when user press the File->Exit.
std::unique_ptr< NL_3D_VIEWER_PLUGIN > m_spaceMouse
void RenderEngineChanged()
RenderEngineChanged - Update toolbar icon and call canvas RenderEngineChanged.
void Process_Special_Functions(wxCommandEvent &event)
wxImage captureCurrentViewScreenshot()
Capture screenshot of the current view using the configured renderer.
void OnCloseWindow(wxCloseEvent &event)
void ShowChangedLanguage() override
wxImage captureScreenshot(const wxSize &aSize)
Capture screenshot using appropriate rendering method.
void setupRenderingConfig(BOARD_ADAPTER &adapter)
Setup rendering configuration for screenshot capture.
void ApplyProjectFileSettings()
Take the saved 3D viewports from the project file, as at startup and after it was reread.
void ReloadRequest()
Request reloading the 3D view.
wxFileName m_defaultSaveScreenshotFileName
wxImage convertRGBAToImage(uint8_t *rgbaBuffer, const wxSize &realSize)
Convert RGBA buffer to wxImage format.
APP_SETTINGS_BASE * config() const override
Return the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
BOARD_ADAPTER & GetAdapter()
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
void saveImageToFile(const wxImage &screenshotImage, EDA_3D_VIEWER_EXPORT_FORMAT aFormat, const wxString &fullFileName)
Save image to file.
void copyImageToClipboard(const wxImage &screenshotImage)
Copy image to system clipboard.
void loadCommonSettings()
Load configuration from common settings.
void ExportImage(EDA_3D_VIEWER_EXPORT_FORMAT aFormat, const wxSize &aSize)
Export 3D viewer image to file or clipboard.
wxImage captureRaytracingScreenshot(BOARD_ADAPTER &adapter, TRACK_BALL &camera, const wxSize &aSize)
Capture screenshot using raytracing renderer.
void TakeScreenshot(EDA_3D_VIEWER_EXPORT_FORMAT aFormat)
Create a Screenshot of the current 3D view.
void handleIconizeEvent(wxIconizeEvent &aEvent) override
Handle a window iconize event.
LAYER_PRESET_3D * FindPreset(const wxString &aName)
std::vector< LAYER_PRESET_3D > m_LayerPresets
Toolbar configuration for the 3D viewer.
Definition toolbars_3d.h:29
virtual void handleIconizeEvent(wxIconizeEvent &aEvent)
Handle a window iconize event.
APPEARANCE_CONTROLS_3D * m_appearancePanel
void CommonSettingsChanged(int aFlags) override
Notification event that some of the common (suite-wide) settings have changed.
void ShowChangedLanguage() override
Redraw the menus and what not in current language.
virtual void setupUIConditions()
Setup the UI conditions for the various actions and their controls in this frame.
wxAuiManager m_auimgr
virtual void RecreateToolbars()
virtual void LoadSettings(APP_SETTINGS_BASE *aCfg)
Load common frame parameters from a configuration file.
virtual void SaveSettings(APP_SETTINGS_BASE *aCfg)
Save common frame parameters to a configuration data file.
static KIGFX::VC_SETTINGS GetVcSettings()
Gets a populated View Controls settings object dervived from our program settings.
Specialization of the wxAuiPaneInfo class for KiCad panels.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
static const wxGLAttributes GetAttributesList(ANTIALIASING_MODE aAntiAliasingMode, bool aAlpha=false)
Get a list of attributes to pass to wxGLCanvas.
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:600
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
std::vector< VIEWPORT3D > m_Viewports3D
List of stored viewports (pos + zoom)
static S3D_CACHE * Get3DCacheManager(PROJECT *aProject, bool updateProjDir=false)
Return a pointer to an instance of the 3D cache manager.
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition project.cpp:271
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
bool Redraw(bool aIsMoving) override
Redraw the view.
void SetCurWindowSize(const wxSize &aSize) override
Before each render, the canvas will tell the render what is the size of its windows,...
T * GetToolbarSettings(const wxString &aFilename)
Return a handle to the given toolbar settings.
TOOL_MANAGER * m_toolManager
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Master controller class:
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
A modified version of the wxInfoBar class that allows us to:
Definition wx_infobar.h:77
#define _(s)
#define GridSizeCheck(x)
EVT_TOOL_RANGE(ID_START_COMMAND_3D, ID_MENU_COMMAND_END, EDA_3D_VIEWER_FRAME::Process_Special_Functions) EDA_3D_VIEWER_FRAME
Declaration of the eda_3d_viewer class.
EDA_3D_VIEWER_EXPORT_FORMAT
@ HOVERED_ITEM
@ RENDER_TIME
#define FOLLOW_PLOT_SETTINGS
#define LEGACY_PRESET_FLAG
#define FOLLOW_PCB
#define QUALIFIED_VIEWER3D_FRAMENAME(parent)
#define VIEWPORT_SWITCH_KEY
#define PRESET_SWITCH_KEY
@ FRAME_PCB_DISPLAY3D
Definition frame_type.h:43
static const std::string JpegFileExtension
static const std::string PngFileExtension
static wxString PngFileWildcard()
static wxString JpegFileWildcard()
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
PROJECT & Prj()
Definition kicad.cpp:730
EVT_MENU(ID_COMPARE_PROJECT_BRANCHES, KICAD_MANAGER_FRAME::OnCompareProjectBranches) KICAD_MANAGER_FRAME
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:741
Declaration of the NL_3D_VIEWER_PLUGIN class.
ANTIALIASING_MODE
Anti-aliasing options.
#define _HKI(x)
Definition page_info.cpp:40
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
T * GetAppSettings(const char *aFilename)
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.
void SetAuiPaneSize(wxAuiManager &aManager, wxAuiPaneInfo &aPane, int aWidth, int aHeight)
Sets the size of an AUI pane, working around http://trac.wxwidgets.org/ticket/13180.