26#include <wx/filename.h>
29#include <wx/wupdlock.h>
30#include <wx/clipbrd.h>
31#include <wx/filedlg.h>
92 const wxString& aTitle,
long style ) :
96 m_currentCamera( m_trackBallCamera ),
99 wxLogTrace( m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::EDA_3D_VIEWER_FRAME %s" ), aTitle );
101 m_disable_ray_tracing =
false;
102 m_aboutTitle =
_HKI(
"KiCad 3D Viewer" );
110 static const int status_dims[5] = { -1, 170, 130, 130, 130 };
112 wxStatusBar *status_bar = CreateStatusBar(
arrayDim( status_dims ) );
113 SetStatusWidths(
arrayDim( status_dims ), status_dims );
126 LoadSettings( GetAppSettings<EDA_3D_VIEWER_SETTINGS>(
"3d_viewer" ) );
127 loadCommonSettings();
129 m_appearancePanel->SetUserViewports(
Prj().GetProjectFile().m_Viewports3D );
134 GetAppSettings<EDA_3D_VIEWER_SETTINGS>(
"3d_viewer" ),
this );
138 m_canvas->SetEventDispatcher( m_toolDispatcher );
143 m_toolManager->InitTools();
147 if(
EDA_3D_CONTROLLER* ctrlTool = GetToolManager()->GetTool<EDA_3D_CONTROLLER>() )
151 m_toolManager->InvokeTool(
"3DViewer.Control" );
159 m_infoBar =
new WX_INFOBAR(
this, &m_auimgr );
161 m_auimgr.SetManagedWindow(
this );
163 m_auimgr.AddPane( m_tbTopMain,
EDA_PANE().HToolbar().Name( wxS(
"TopMainToolbar" ) )
165 m_auimgr.AddPane( m_infoBar,
EDA_PANE().InfoBar().Name( wxS(
"InfoBar" ) )
167 m_auimgr.AddPane( m_appearancePanel,
EDA_PANE().Name(
"LayersManager" )
169 .Caption(
_(
"Appearance" ) ).PaneBorder(
false )
170 .MinSize( 180, -1 ).BestSize( 190, -1 ) );
171 m_auimgr.AddPane( m_canvas,
EDA_PANE().Canvas().Name( wxS(
"DrawFrame" ) )
174 wxAuiPaneInfo& layersManager = m_auimgr.GetPane(
"LayersManager" );
187 m_auimgr.GetPane( wxS(
"InfoBar" ) ).Hide();
190 m_canvas->SetInfoBar( m_infoBar );
191 m_canvas->SetStatusBar( status_bar );
195 m_spaceMouse = std::make_unique<NL_3D_VIEWER_PLUGIN>( m_canvas );
197 catch(
const std::system_error& e )
199 wxLogTrace( wxT(
"KI_TRACE_NAVLIB" ), e.what() );
205 m_canvas->SetFocus();
227#define GridSizeCheck( x ) ACTION_CONDITIONS().Check( cond.GridSize( x ) )
249 auto show_NotInPosfile =
264 auto showNavig = [
this](
const SELECTION& aSel )
308 static bool s_presetSwitcherShown =
false;
309 static bool s_viewportSwitcherShown =
false;
316 if( wxGetKeyState( WXK_TAB ) )
318 if( ( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
319 &&
static_cast<wxKeyEvent&
>( aEvent ).GetKeyCode() == WXK_TAB )
330 for( wxString& str : mru )
333 str =
_(
"Follow PCB Editor" );
335 str =
_(
"Follow PCB Plot Settings" );
340 s_presetSwitcherShown =
true;
342 s_presetSwitcherShown =
false;
346 if( idx >= 0 && idx < (
int) mru.size() )
358 if( this->IsActive() )
362 if( viewportMRU.size() > 0 )
366 s_viewportSwitcherShown =
true;
368 s_viewportSwitcherShown =
false;
372 if( idx >= 0 && idx < (
int) viewportMRU.size() )
381 return wxFrame::TryBefore( aEvent );
413 if(
m_canvas && aForceImmediateRedraw )
437 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::Exit3DFrame" ) );
445 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::OnCloseWindow" ) );
449 if(
m_auimgr.GetPane( wxS(
"LayersManager" ) ).IsShown() )
450 m_auimgr.GetPane( wxS(
"LayersManager" ) ).Show(
false );
465 switch( event.GetId() )
472 cfg->ResetToDefaults();
474 LoadSettings( GetAppSettings<EDA_3D_VIEWER_SETTINGS>(
"3d_viewer" ) );
483 wxFAIL_MSG( wxT(
"Invalid event in EDA_3D_VIEWER_FRAME::Process_Special_Functions()" ) );
491 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::%s disabling ray tracing." ), __WXFUNCTION__ );
500 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::OnActivate" ) );
502 if( aEvent.GetActive() &&
m_canvas )
537 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::LoadSettings" ) );
552 wxString legacyColorsPresetName =
_(
"legacy colors" );
556 if( !cfg->
FindPreset( legacyColorsPresetName ) )
587 cfg->m_Camera.rotation_increment = ctrlTool->GetRotationIncrement();
594 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::CommonSettingsChanged" ) );
600 applySettings( GetAppSettings<EDA_3D_VIEWER_SETTINGS>(
"3d_viewer" ) );
612 SetTitle(
_(
"3D Viewer" ) );
618 lm_pane_info.Caption(
_(
"Appearance" ) );
621 SetStatusText( wxEmptyString,
ACTIVITY );
628 wxAuiPaneInfo& layersManager =
m_auimgr.GetPane(
"LayersManager" );
633 cfg->m_AuiPanels.show_layer_manager = !cfg->m_AuiPanels.show_layer_manager;
635 layersManager.Show( cfg->m_AuiPanels.show_layer_manager );
637 if( cfg->m_AuiPanels.show_layer_manager )
659 wxString fullFileName;
661 if( aFormat != EDA_3D_VIEWER_EXPORT_FORMAT::CLIPBOARD )
669 if( screenshotImage.IsOk() )
687 wxImage screenshotImage;
692 wxWindowUpdateLocker noUpdates(
this );
699 return screenshotImage;
705 wxString fullFileName;
707 if( aFormat != EDA_3D_VIEWER_EXPORT_FORMAT::CLIPBOARD )
715 if( screenshotImage.IsOk() )
731 const wxString defaultExt = ( aFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG ) ?
735 wxFileDialog dlg(
this,
_(
"3D Image File Name" ),
738 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
741 dlg.SetFilterIndex( ( aFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG ) ? 0 : 1 );
743 if( dlg.ShowModal() == wxID_CANCEL )
751 bool formatDetected =
false;
753 if( fileExt == wxT(
"jpg") || fileExt == wxT(
"jpeg") )
755 detectedFormat = EDA_3D_VIEWER_EXPORT_FORMAT::JPEG;
756 formatDetected =
true;
758 else if( fileExt == wxT(
"png") )
760 detectedFormat = EDA_3D_VIEWER_EXPORT_FORMAT::PNG;
761 formatDetected =
true;
765 if( !formatDetected )
767 int filterIndex = dlg.GetFilterIndex();
768 detectedFormat = ( filterIndex == 0 ) ? EDA_3D_VIEWER_EXPORT_FORMAT::JPEG :
769 EDA_3D_VIEWER_EXPORT_FORMAT::PNG;
772 const wxString ext = ( detectedFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG ) ?
778 aFormat = detectedFormat;
783 if( !fn.IsDirWritable() )
786 msg.Printf(
_(
"Insufficient permissions to save file '%s'." ),
788 wxMessageBox( msg,
_(
"Error" ), wxOK | wxICON_ERROR,
this );
805 auto configRestorer = std::unique_ptr<void, std::function<void(
void*)>>(
806 reinterpret_cast<void*
>(1),
825 aAdapter.
m_Cfg = cfg;
840 while( raytrace.
Redraw(
false,
nullptr,
nullptr ) );
842 uint8_t* rgbaBuffer = raytrace.
GetBuffer();
854 const unsigned int wxh = aRealSize.x * aRealSize.y;
856 unsigned char* rgbBuffer = (
unsigned char*) malloc( wxh * 3 );
857 unsigned char* alphaBuffer = (
unsigned char*) malloc( wxh );
859 unsigned char* rgbaPtr = aRGBABuffer;
860 unsigned char* rgbPtr = rgbBuffer;
861 unsigned char* alphaPtr = alphaBuffer;
863 for(
int y = 0; y < aRealSize.y; y++ )
865 for(
int x = 0; x < aRealSize.x; x++ )
867 rgbPtr[0] = rgbaPtr[0];
868 rgbPtr[1] = rgbaPtr[1];
869 rgbPtr[2] = rgbaPtr[2];
870 alphaPtr[0] = rgbaPtr[3];
878 wxImage screenshotImage;
879 screenshotImage.Create( aRealSize );
880 screenshotImage.SetData( rgbBuffer );
881 screenshotImage.SetAlpha( alphaBuffer );
882 return screenshotImage.Mirror(
false );
890 : ANTIALIASING_MODE::AA_NONE;
892 wxFrame temp(
this, wxID_ANY, wxEmptyString, wxDefaultPosition, aSize, wxFRAME_NO_TASKBAR );
900 auto canvas = std::make_unique<EDA_3D_CANVAS>( &temp,
902 tempadapter, aCamera,
905 canvas->SetSize( aSize );
907 wxWindowUpdateLocker noUpdates(
this );
916 std::vector<unsigned char> buffer(aSize.x * aSize.y * 4);
917 canvas->RenderToFrameBuffer( buffer.data(), aSize.x, aSize.y );
943 if( aFormat == EDA_3D_VIEWER_EXPORT_FORMAT::CLIPBOARD )
956 wxBitmap bitmap( aScreenshotImage );
959 if( wxTheClipboard->Open() )
961 wxBitmapDataObject* dobjBmp =
new wxBitmapDataObject( bitmap );
963 if( !wxTheClipboard->SetData( dobjBmp ) )
964 wxMessageBox(
_(
"Failed to copy image to clipboard" ) );
966 wxTheClipboard->Flush();
967 wxTheClipboard->Close();
974 bool fmt_is_jpeg = ( aFormat == EDA_3D_VIEWER_EXPORT_FORMAT::JPEG );
976 if( !aScreenshotImage.SaveFile( aFullFileName, fmt_is_jpeg ? wxBITMAP_TYPE_JPEG : wxBITMAP_TYPE_PNG ) )
978 wxMessageBox(
_(
"Can't save file" ) );
985 wxLogTrace(
m_logTrace, wxT(
"EDA_3D_VIEWER_FRAME::RenderEngineChanged()" ) );
994 wxCHECK_RET(
m_canvas, wxT(
"Cannot load settings to null canvas" ) );
@ ID_MENU3D_RESET_DEFAULTS
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
constexpr EDA_IU_SCALE unityScale
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
#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...
void CommonSettingsChanged()
std::vector< VIEWPORT3D > GetUserViewports() const
Return a list of viewports created by the user.
void ApplyViewport(const wxString &aPresetName)
const wxArrayString & GetViewportsMRU()
void ApplyLayerPreset(const wxString &aPresetName)
const wxArrayString & GetLayerPresetsMRU()
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 InitSettings(REPORTER *aStatusReporter, 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.
void SetLayerColors(const std::map< int, COLOR4D > &aColors)
EDA_3D_VIEWER_SETTINGS * m_Cfg
std::map< int, COLOR4D > GetDefaultColors() const
void Set3dCacheManager(S3D_CACHE *aCacheMgr) noexcept
Update the cache manager pointer.
S3D_CACHE * Get3dCacheManager() const noexcept
PROJECTION_TYPE GetProjection()
bool SetCurWindowSize(const wxSize &aSize)
Update the windows size of the camera.
Handle actions that are shared between different applications.
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 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.
int GetMovingSpeedMultiplier() const
void SetProjectionMode(int aMode)
void SetAnimationEnabled(bool aEnable)
Enable or disable camera animation when switching to a pre-defined view.
int GetProjectionMode() const
void DoRePaint()
The actual function to repaint the canvas.
void SetEventDispatcher(TOOL_DISPATCHER *aEventDispatcher)
Set a dispatcher that processes events and forwards them to tools.
bool IsReloadRequestPending() const
Query if there is a pending reload request.
bool GetAnimationEnabled() const
void ReloadRequest(BOARD *aBoard=nullptr, S3D_CACHE *aCachePointer=nullptr)
void GetScreenshot(wxImage &aDstImage)
Request a screenshot and output it to the aDstImage.
void SetMovingSpeedMultiplier(int aMultiplier)
Set the camera animation moving speed multiplier option.
void RenderEngineChanged()
Notify that the render engine was changed.
void Request_refresh(bool aRedrawImmediately=true)
Schedule a refresh update of the canvas.
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.
bool m_disable_ray_tracing
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.
BOARD_ADAPTER m_boardAdapter
void ToggleAppearanceManager()
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 ReloadRequest()
Request reloading the 3D view.
wxFileName m_defaultSaveScreenshotFileName
wxImage convertRGBAToImage(uint8_t *rgbaBuffer, const wxSize &realSize)
Convert RGBA buffer to wxImage format.
TRACK_BALL m_trackBallCamera
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
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.
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.
void SetVcSettings(const KIGFX::VC_SETTINGS &aVcSettings)
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...
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
virtual SETTINGS_MANAGER & GetSettingsManager() const
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.
virtual PROJECT_FILE & GetProjectFile() const
bool Redraw(bool aIsMoving, REPORTER *aStatusReporter, REPORTER *aWarningReporter) override
Redraw the view.
wxSize GetRealBufferSize()
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.
A modified version of the wxInfoBar class that allows us to:
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
#define FOLLOW_PLOT_SETTINGS
#define LEGACY_PRESET_FLAG
#define QUALIFIED_VIEWER3D_FRAMENAME(parent)
#define VIEWPORT_SWITCH_KEY
#define PRESET_SWITCH_KEY
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.
Declaration of the NL_3D_VIEWER_PLUGIN class.
ANTIALIASING_MODE
Anti-aliasing options.
PGM_BASE & Pgm()
The global program "get" accessor.
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
ACTION_CONDITIONS & Check(const SELECTION_CONDITION &aCondition)
double rotation_increment
int moving_speed_multiplier
bool show_footprints_normal
ANTIALIASING_MODE opengl_AA_mode
bool show_footprints_virtual
bool show_footprints_not_in_posfile
bool highlight_on_rollover
bool show_footprints_insert
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.