KiCad PCB EDA Suite
eda_3d_viewer.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 <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <wx/colordlg.h>
26 #include <wx/colour.h>
27 #include <wx/filename.h>
28 #include <wx/string.h>
29 #include <wx/wupdlock.h>
30 #include <wx/clipbrd.h>
31 #include "eda_3d_viewer.h"
32 #include <3d_viewer_settings.h>
33 #include <3d_viewer_id.h>
34 #include "../common_ogl/cogl_att_list.h"
37 #include <bitmaps.h>
40 #include <class_board.h>
42 #include <gal/dpi_scaling.h>
43 #include <pgm_base.h>
44 #include <project.h>
47 #include <tool/common_control.h>
48 #include <tool/tool_manager.h>
49 #include <tool/tool_dispatcher.h>
50 #include <tool/action_toolbar.h>
51 #include <widgets/infobar.h>
53 
61 const wxChar * EDA_3D_VIEWER::m_logTrace = wxT( "KI_TRACE_EDA_3D_VIEWER" );
62 
63 
64 BEGIN_EVENT_TABLE( EDA_3D_VIEWER, EDA_BASE_FRAME )
65 
66  EVT_ACTIVATE( EDA_3D_VIEWER::OnActivate )
67  EVT_SET_FOCUS( EDA_3D_VIEWER::OnSetFocus )
68 
71 
73 
74  EVT_MENU( wxID_CLOSE, EDA_3D_VIEWER::Exit3DFrame )
77 
82 
83  EVT_CLOSE( EDA_3D_VIEWER::OnCloseWindow )
84 END_EVENT_TABLE()
85 
86 
87 EDA_3D_VIEWER::EDA_3D_VIEWER( KIWAY *aKiway, PCB_BASE_FRAME *aParent, const wxString &aTitle,
88  long style ) :
89  KIWAY_PLAYER( aKiway, aParent, FRAME_PCB_DISPLAY3D, aTitle, wxDefaultPosition,
90  wxDefaultSize, style, QUALIFIED_VIEWER3D_FRAMENAME( aParent ) ),
91  m_mainToolBar( nullptr ),
92  m_canvas( nullptr ),
93  m_currentCamera( m_trackBallCamera ),
94  m_trackBallCamera( RANGE_SCALE_3D )
95 {
96  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::EDA_3D_VIEWER %s", aTitle );
97 
98  m_disable_ray_tracing = false;
99  m_AboutTitle = "3D Viewer";
100 
101  // Give it an icon
102  wxIcon icon;
103  icon.CopyFromBitmap( KiBitmap( icon_3d_xpm ) );
104  SetIcon( icon );
105 
106  // Create the status line
107  static const int status_dims[4] = { -1, 130, 130, 170 };
108 
109  wxStatusBar *status_bar = CreateStatusBar( arrayDim( status_dims ) );
110  SetStatusWidths( arrayDim( status_dims ), status_dims );
111 
112  m_canvas = new EDA_3D_CANVAS( this, COGL_ATT_LIST::GetAttributesList( m_boardAdapter.AntiAliasingGet() ),
113  aParent->GetBoard(), m_boardAdapter, m_currentCamera,
114  Prj().Get3DCacheManager() );
115 
116  auto config = Pgm().GetSettingsManager().GetAppSettings<EDA_3D_VIEWER_SETTINGS>();
117  LoadSettings( config );
118 
119  // Some settings need the canvas
120  loadCommonSettings();
121 
122  // Create the manager
123  m_toolManager = new TOOL_MANAGER;
124  m_toolManager->SetEnvironment( GetBoard(), nullptr, nullptr, config, this );
125 
126  m_actions = new EDA_3D_ACTIONS();
127  m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager, m_actions );
128  m_canvas->SetEventDispatcher( m_toolDispatcher );
129 
130  // Register tools
131  m_toolManager->RegisterTool( new COMMON_CONTROL );
132  m_toolManager->RegisterTool( new EDA_3D_CONTROLLER );
133  m_toolManager->InitTools();
134 
135  if( EDA_3D_CONTROLLER* ctrlTool = GetToolManager()->GetTool<EDA_3D_CONTROLLER>() )
136  ctrlTool->SetRotationIncrement( config->m_Camera.rotation_increment );
137 
138  // Run the viewer control tool, it is supposed to be always active
139  m_toolManager->InvokeTool( "3DViewer.Control" );
140 
141  CreateMenuBar();
142  ReCreateMainToolbar();
143 
144  // Create the infobar
145  m_infoBar = new WX_INFOBAR( this, &m_auimgr );
146 
147  m_auimgr.SetManagedWindow( this );
148 
149  m_auimgr.AddPane( m_mainToolBar,
150  EDA_PANE().HToolbar().Name( "MainToolbar" ).Top().Layer( 6 ) );
151  m_auimgr.AddPane( m_infoBar,
152  EDA_PANE().InfoBar().Name( "InfoBar" ).Top().Layer(1) );
153  m_auimgr.AddPane( m_canvas,
154  EDA_PANE().Canvas().Name( "DrawFrame" ).Center() );
155 
156  // Call Update() to fix all pane default sizes, especially the "InfoBar" pane before
157  // hidding it.
158  m_auimgr.Update();
159 
160  // We don't want the infobar displayed right away
161  m_auimgr.GetPane( "InfoBar" ).Hide();
162  m_auimgr.Update();
163 
164  if( m_canvas )
165  {
166  m_canvas->SetInfoBar( m_infoBar );
167  m_canvas->SetStatusBar( status_bar );
168  }
169 
170  // Fixes bug in Windows (XP and possibly others) where the canvas requires the focus
171  // in order to receive mouse events. Otherwise, the user has to click somewhere on
172  // the canvas before it will respond to mouse wheel events.
173  if( m_canvas )
174  m_canvas->SetFocus();
175 }
176 
177 
179 {
180  m_canvas->SetEventDispatcher( nullptr );
181 
182  m_auimgr.UnInit();
183 
184  // m_canvas delete will be called by wxWidget manager
185  //delete m_canvas;
186  //m_canvas = nullptr;
187 }
188 
189 
191 {
192  // This will schedule a request to load later
193  if( m_canvas )
194  m_canvas->ReloadRequest( GetBoard(), Prj().Get3DCacheManager() );
195 }
196 
197 
198 void EDA_3D_VIEWER::NewDisplay( bool aForceImmediateRedraw )
199 {
200  ReloadRequest();
201 
202  // After the ReloadRequest call, the refresh often takes a bit of time,
203  // and it is made here only on request.
204  if( aForceImmediateRedraw )
205  m_canvas->Refresh();
206 }
207 
208 
209 void EDA_3D_VIEWER::Exit3DFrame( wxCommandEvent &event )
210 {
211  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::Exit3DFrame" );
212 
213  Close( true );
214 }
215 
216 
217 void EDA_3D_VIEWER::OnCloseWindow( wxCloseEvent &event )
218 {
219  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::OnCloseWindow" );
220 
221  if( m_canvas )
222  m_canvas->Close();
223 
224  // m_canvas delete will be called by wxWidget manager
225  //delete m_canvas;
226  //m_canvas = nullptr;
227 
228  COLOR_SETTINGS* colors = Pgm().GetSettingsManager().GetColorSettings();
229  Pgm().GetSettingsManager().SaveColorSettings( colors, "3d_viewer" );
230 
231  Destroy();
232  event.Skip( true );
233 }
234 
235 
236 void EDA_3D_VIEWER::Process_Special_Functions( wxCommandEvent &event )
237 {
238  int id = event.GetId();
239  bool isChecked = event.IsChecked();
240 
241  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::Process_Special_Functions id %d isChecked %d",
242  id, isChecked );
243 
244  if( m_canvas == NULL )
245  return;
246 
247  switch( id )
248  {
249  case ID_RELOAD3D_BOARD:
250  NewDisplay( true );
251  break;
252 
256  takeScreenshot( event );
257  return;
258 
260  if( Set3DColorFromUser( m_boardAdapter.m_BgColorBot, _( "Background Color, Bottom" ),
261  nullptr ) )
262  {
265  else
266  NewDisplay( true );
267  }
268  return;
269 
271  if( Set3DColorFromUser( m_boardAdapter.m_BgColorTop, _( "Background Color, Top" ), nullptr ) )
272  {
275  else
276  NewDisplay( true );
277  }
278  return;
279 
282  return;
283 
286  return;
287 
290  return;
291 
294  break;
295 
298  break;
299 
302  NewDisplay( true );
303  break;
304 
307  NewDisplay( true );
308  return;
309 
312  NewDisplay( true );
313  return;
314 
317  NewDisplay( true );
318  return;
319 
321  {
322  auto cfg = Pgm().GetSettingsManager().GetAppSettings<EDA_3D_VIEWER_SETTINGS>();
323  cfg->ResetToDefaults();
324  LoadSettings( cfg );
325 
326  // Tell canvas that we (may have) changed the render engine
328 
329  NewDisplay( true );
330  }
331  return;
332 
333  default:
334  wxFAIL_MSG( "Invalid event in EDA_3D_VIEWER::Process_Special_Functions()" );
335  return;
336  }
337 }
338 
339 
340 void EDA_3D_VIEWER::OnRenderEngineSelection( wxCommandEvent &event )
341 {
342  const RENDER_ENGINE old_engine = m_boardAdapter.RenderEngineGet();
343 
344  if( old_engine == RENDER_ENGINE::OPENGL_LEGACY )
346  else
348 
349  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::OnRenderEngineSelection type %s ",
351  "OpenGL Legacy" );
352 
353  if( old_engine != m_boardAdapter.RenderEngineGet() )
355 }
356 
357 
358 void EDA_3D_VIEWER::OnDisableRayTracing( wxCommandEvent& aEvent )
359 {
360  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::%s disabling ray tracing.", __WXFUNCTION__ );
361 
362  m_disable_ray_tracing = true;
364 }
365 
366 
367 void EDA_3D_VIEWER::OnActivate( wxActivateEvent &event )
368 {
369  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::OnActivate" );
370 
371  if( m_canvas )
372  {
373  // Reload data if 3D frame shows a board,
374  // because it can be changed since last frame activation
377 
378  // Activates again the focus of the canvas so it will catch mouse and key events
379  m_canvas->SetFocus();
380  }
381 
382  event.Skip(); // required under wxMAC
383 }
384 
385 
386 void EDA_3D_VIEWER::OnSetFocus(wxFocusEvent &event)
387 {
388  // Activates again the focus of the canvas so it will catch mouse and key events
389  if( m_canvas )
390  m_canvas->SetFocus();
391 
392  event.Skip();
393 }
394 
395 
397 {
399 
400  EDA_3D_VIEWER_SETTINGS* cfg = dynamic_cast<EDA_3D_VIEWER_SETTINGS*>( aCfg );
401  wxASSERT( cfg );
402 
403  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::LoadSettings" );
404 
405  COLOR_SETTINGS* colors = Pgm().GetSettingsManager().GetColorSettings();
406 
407  auto set_color = [] ( const COLOR4D& aColor, SFVEC3D& aTarget )
408  {
409  aTarget.r = aColor.r;
410  aTarget.g = aColor.g;
411  aTarget.b = aColor.b;
412  };
413 
415  set_color( colors->GetColor( LAYER_3D_BACKGROUND_TOP ), m_boardAdapter.m_BgColorTop );
416  set_color( colors->GetColor( LAYER_3D_BOARD ), m_boardAdapter.m_BoardBodyColor );
417  set_color( colors->GetColor( LAYER_3D_COPPER ), m_boardAdapter.m_CopperColor );
423 
424  if( cfg )
425  {
426 #define TRANSFER_SETTING( flag, field ) m_boardAdapter.SetFlag( flag, cfg->m_Render.field )
427 
429  TRANSFER_SETTING( FL_SUBTRACT_MASK_FROM_SILK, subtract_mask_from_silk );
430 
431  // OpenGL options
432  TRANSFER_SETTING( FL_RENDER_OPENGL_COPPER_THICKNESS, opengl_copper_thickness );
433  TRANSFER_SETTING( FL_RENDER_OPENGL_SHOW_MODEL_BBOX, opengl_show_model_bbox );
434  TRANSFER_SETTING( FL_RENDER_OPENGL_AA_DISABLE_ON_MOVE, opengl_AA_disableOnMove );
435  TRANSFER_SETTING( FL_RENDER_OPENGL_THICKNESS_DISABLE_ON_MOVE, opengl_thickness_disableOnMove );
436  TRANSFER_SETTING( FL_RENDER_OPENGL_VIAS_DISABLE_ON_MOVE, opengl_vias_disableOnMove );
437  TRANSFER_SETTING( FL_RENDER_OPENGL_HOLES_DISABLE_ON_MOVE, opengl_holes_disableOnMove );
438 
439  // Raytracing options
440  TRANSFER_SETTING( FL_RENDER_RAYTRACING_SHADOWS, raytrace_shadows );
441  TRANSFER_SETTING( FL_RENDER_RAYTRACING_BACKFLOOR, raytrace_backfloor );
442  TRANSFER_SETTING( FL_RENDER_RAYTRACING_REFRACTIONS, raytrace_refractions );
443  TRANSFER_SETTING( FL_RENDER_RAYTRACING_REFLECTIONS, raytrace_reflections );
444  TRANSFER_SETTING( FL_RENDER_RAYTRACING_POST_PROCESSING, raytrace_post_processing );
445  TRANSFER_SETTING( FL_RENDER_RAYTRACING_ANTI_ALIASING, raytrace_anti_aliasing );
446  TRANSFER_SETTING( FL_RENDER_RAYTRACING_PROCEDURAL_TEXTURES, raytrace_procedural_textures );
447 
448  TRANSFER_SETTING( FL_AXIS, show_axis );
449  TRANSFER_SETTING( FL_MODULE_ATTRIBUTES_NORMAL, show_footprints_normal );
450  TRANSFER_SETTING( FL_MODULE_ATTRIBUTES_NORMAL_INSERT, show_footprints_insert );
451  TRANSFER_SETTING( FL_MODULE_ATTRIBUTES_VIRTUAL, show_footprints_virtual );
452  TRANSFER_SETTING( FL_ZONE, show_zones );
453  TRANSFER_SETTING( FL_ADHESIVE, show_adhesive );
454  TRANSFER_SETTING( FL_SILKSCREEN, show_silkscreen );
455  TRANSFER_SETTING( FL_SOLDERMASK, show_soldermask );
456  TRANSFER_SETTING( FL_SOLDERPASTE, show_solderpaste );
457  TRANSFER_SETTING( FL_COMMENTS, show_comments );
458  TRANSFER_SETTING( FL_ECO, show_eco );
459  TRANSFER_SETTING( FL_SHOW_BOARD_BODY, show_board_body );
460 
461  m_boardAdapter.GridSet( static_cast<GRID3D_TYPE>( cfg->m_Render.grid_type ) );
462  m_boardAdapter.AntiAliasingSet( static_cast<ANTIALIASING_MODE>( cfg->m_Render.opengl_AA_mode ) );
463 
464  // When opening the 3D viewer, we use the opengl mode, not the ray tracing engine
465  // because the ray tracing is very time consumming, and can be seen as not working
466  // (freeze window) with large boards.
467 #if 0
468  RENDER_ENGINE engine = static_cast<RENDER_ENGINE>( cfg->m_Render.engine );
469  wxLogTrace( m_logTrace, engine == RENDER_ENGINE::RAYTRACING ?
470  "EDA_3D_VIEWER::LoadSettings render setting Ray Trace" :
471  "EDA_3D_VIEWER::LoadSettings render setting OpenGL" );
472 #else
474 #endif
475 
476  m_boardAdapter.MaterialModeSet( static_cast<MATERIAL_MODE>( cfg->m_Render.material_mode ) );
477 
480 
481 #undef TRANSFER_SETTING
482  }
483 }
484 
485 
487 {
488  auto cfg = Pgm().GetSettingsManager().GetAppSettings<EDA_3D_VIEWER_SETTINGS>();
489 
491 
492  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::SaveSettings" );
493 
494  COLOR_SETTINGS* colors = Pgm().GetSettingsManager().GetColorSettings();
495 
496  auto save_color = [colors] ( SFVEC3D& aSource, LAYER_3D_ID aTarget )
497  {
498  colors->SetColor( aTarget, COLOR4D( aSource.r, aSource.g, aSource.b, 1.0 ) );
499  };
500 
509 
511  "EDA_3D_VIEWER::SaveSettings render setting Ray Trace" :
512  "EDA_3D_VIEWER::SaveSettings render setting OpenGL" );
513 
514  if( cfg )
515  {
516 #define TRANSFER_SETTING( field, flag ) cfg->m_Render.field = m_boardAdapter.GetFlag( flag )
517 
518  cfg->m_Render.engine = static_cast<int>( m_boardAdapter.RenderEngineGet() );
519  cfg->m_Render.grid_type = static_cast<int>( m_boardAdapter.GridGet() );
520  cfg->m_Render.material_mode = static_cast<int>( m_boardAdapter.MaterialModeGet() );
521  cfg->m_Render.opengl_AA_mode = static_cast<int>( m_boardAdapter.AntiAliasingGet() );
522 
523  cfg->m_Camera.animation_enabled = m_canvas->AnimationEnabledGet();
524  cfg->m_Camera.moving_speed_multiplier = m_canvas->MovingSpeedMultiplierGet();
525 
526  if( EDA_3D_CONTROLLER* ctrlTool = GetToolManager()->GetTool<EDA_3D_CONTROLLER>() )
527  cfg->m_Camera.rotation_increment = ctrlTool->GetRotationIncrement();
528 
529  TRANSFER_SETTING( opengl_AA_disableOnMove, FL_RENDER_OPENGL_AA_DISABLE_ON_MOVE );
530  TRANSFER_SETTING( opengl_copper_thickness, FL_RENDER_OPENGL_COPPER_THICKNESS );
531  TRANSFER_SETTING( opengl_show_model_bbox, FL_RENDER_OPENGL_SHOW_MODEL_BBOX );
532  TRANSFER_SETTING( opengl_thickness_disableOnMove, FL_RENDER_OPENGL_THICKNESS_DISABLE_ON_MOVE );
533  TRANSFER_SETTING( opengl_vias_disableOnMove, FL_RENDER_OPENGL_VIAS_DISABLE_ON_MOVE );
534  TRANSFER_SETTING( opengl_holes_disableOnMove, FL_RENDER_OPENGL_HOLES_DISABLE_ON_MOVE );
535 
536  TRANSFER_SETTING( raytrace_anti_aliasing, FL_RENDER_RAYTRACING_ANTI_ALIASING );
537  TRANSFER_SETTING( raytrace_backfloor, FL_RENDER_RAYTRACING_BACKFLOOR );
538  TRANSFER_SETTING( raytrace_post_processing, FL_RENDER_RAYTRACING_POST_PROCESSING );
539  TRANSFER_SETTING( raytrace_procedural_textures, FL_RENDER_RAYTRACING_PROCEDURAL_TEXTURES );
540  TRANSFER_SETTING( raytrace_reflections, FL_RENDER_RAYTRACING_REFLECTIONS );
541  TRANSFER_SETTING( raytrace_refractions, FL_RENDER_RAYTRACING_REFRACTIONS );
542  TRANSFER_SETTING( raytrace_shadows, FL_RENDER_RAYTRACING_SHADOWS );
543 
545  TRANSFER_SETTING( show_adhesive, FL_ADHESIVE );
546  TRANSFER_SETTING( show_axis, FL_AXIS );
547  TRANSFER_SETTING( show_board_body, FL_SHOW_BOARD_BODY );
548  TRANSFER_SETTING( show_comments, FL_COMMENTS );
549  TRANSFER_SETTING( show_eco, FL_ECO );
550  TRANSFER_SETTING( show_footprints_insert, FL_MODULE_ATTRIBUTES_NORMAL_INSERT );
551  TRANSFER_SETTING( show_footprints_normal, FL_MODULE_ATTRIBUTES_NORMAL );
552  TRANSFER_SETTING( show_footprints_virtual, FL_MODULE_ATTRIBUTES_VIRTUAL );
553  TRANSFER_SETTING( show_silkscreen, FL_SILKSCREEN );
554  TRANSFER_SETTING( show_soldermask, FL_SOLDERMASK );
555  TRANSFER_SETTING( show_solderpaste, FL_SOLDERPASTE );
556  TRANSFER_SETTING( show_zones, FL_ZONE );
557  TRANSFER_SETTING( subtract_mask_from_silk, FL_SUBTRACT_MASK_FROM_SILK );
558 
559 #undef TRANSFER_SETTING
560  }
561 }
562 
563 
565 {
566  BOARD* brd = GetBoard();
567  const FAB_LAYER_COLOR* stdColors = GetColorStandardList();
568  wxColour color;
569  if( brd )
570  {
572 
573  for( BOARD_STACKUP_ITEM* stckpItem : stckp.GetList() )
574  {
575  wxString colorName = stckpItem->GetColor();
576 
577  if( colorName.StartsWith( "#" ) ) // This is a user defined color.
578  {
579  color.Set( colorName );
580  }
581  else
582  {
583  for( int i = 0; i < GetColorStandardListCount(); i++ )
584  {
585  if( stdColors[i].m_ColorName == colorName )
586  {
587  color = stdColors[i].m_Color;
588  break;
589  }
590  }
591  }
592 
593  if( color.IsOk() )
594  {
595  switch( stckpItem->GetBrdLayerId() )
596  {
597  case F_SilkS:
598  m_boardAdapter.m_SilkScreenColorTop.r = color.Red() / 255.0;
599  m_boardAdapter.m_SilkScreenColorTop.g = color.Green() / 255.0;
600  m_boardAdapter.m_SilkScreenColorTop.b = color.Blue() / 255.0;
601  break;
602  case B_SilkS:
603  m_boardAdapter.m_SilkScreenColorBot.r = color.Red() / 255.0;
604  m_boardAdapter.m_SilkScreenColorBot.g = color.Green() / 255.0;
605  m_boardAdapter.m_SilkScreenColorBot.b = color.Blue() / 255.0;
606  break;
607  case F_Mask:
608  m_boardAdapter.m_SolderMaskColorTop.r = color.Red() / 255.0;
609  m_boardAdapter.m_SolderMaskColorTop.g = color.Green() / 255.0;
610  m_boardAdapter.m_SolderMaskColorTop.b = color.Blue() / 255.0;
611  break;
612  case B_Mask:
613  m_boardAdapter.m_SolderMaskColorBot.r = color.Red() / 255.0;
614  m_boardAdapter.m_SolderMaskColorBot.g = color.Green() / 255.0;
615  m_boardAdapter.m_SolderMaskColorBot.b = color.Blue() / 255.0;
616  break;
617  default:
618  break;
619  }
620  }
621  }
622  }
623 }
624 
625 
626 void EDA_3D_VIEWER::CommonSettingsChanged( bool aEnvVarsChanged )
627 {
628  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::CommonSettingsChanged" );
629 
630  // Regen menu bars, etc
631  EDA_BASE_FRAME::CommonSettingsChanged( aEnvVarsChanged );
632 
633  // There is no base class that handles toolbars for this frame
635 
637 
638  NewDisplay( true );
639 }
640 
641 
642 void EDA_3D_VIEWER::takeScreenshot( wxCommandEvent& event )
643 {
644  wxString fullFileName;
645  bool fmt_is_jpeg = false;
646 
647  if( event.GetId() == ID_MENU_SCREENCOPY_JPEG )
648  fmt_is_jpeg = true;
649 
650  if( event.GetId() != ID_TOOL_SCREENCOPY_TOCLIBBOARD )
651  {
652  // Remember path between saves during this session only.
653  const wxString wildcard = fmt_is_jpeg ? JpegFileWildcard() : PngFileWildcard();
654  const wxString ext = fmt_is_jpeg ? JpegFileExtension : PngFileExtension;
655 
656  // First time path is set to the project path.
657  if( !m_defaultSaveScreenshotFileName.IsOk() )
659 
660  m_defaultSaveScreenshotFileName.SetExt( ext );
661 
662  wxFileDialog dlg( this, _( "3D Image File Name" ),
664  m_defaultSaveScreenshotFileName.GetFullName(), wildcard,
665  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
666 
667  if( dlg.ShowModal() == wxID_CANCEL )
668  return;
669 
670  m_defaultSaveScreenshotFileName = dlg.GetPath();
671 
672  if( m_defaultSaveScreenshotFileName.GetExt().IsEmpty() )
673  m_defaultSaveScreenshotFileName.SetExt( ext );
674 
675  fullFileName = m_defaultSaveScreenshotFileName.GetFullPath();
676 
677  wxFileName fn = fullFileName;
678 
679  if( !fn.IsDirWritable() )
680  {
681  wxString msg;
682 
683  msg.Printf( _( "Insufficient permissions required to save file\n%s" ), fullFileName );
684  wxMessageBox( msg, _( "Error" ), wxOK | wxICON_ERROR, this );
685  return;
686  }
687 
688  // Be sure the screen area destroyed by the file dialog is redrawn
689  // before making a screen copy.
690  // Without this call, under Linux the screen refresh is made to late.
691  wxYield();
692  }
693 
694  // Be sure we have the latest 3D view (remember 3D view is buffered)
695  m_canvas->Request_refresh( true );
696  wxYield();
697 
698  // Build image from the 3D buffer
699  wxWindowUpdateLocker noUpdates( this );
700 
701  wxImage screenshotImage;
702 
703  if( m_canvas )
704  m_canvas->GetScreenshot( screenshotImage );
705 
706  if( event.GetId() == ID_TOOL_SCREENCOPY_TOCLIBBOARD )
707  {
708  wxBitmap bitmap( screenshotImage );
709 
710  if( wxTheClipboard->Open() )
711  {
712  wxBitmapDataObject* dobjBmp = new wxBitmapDataObject( bitmap );
713 
714  if( !wxTheClipboard->SetData( dobjBmp ) )
715  wxMessageBox( _( "Failed to copy image to clipboard" ) );
716 
717  wxTheClipboard->Flush(); /* the data in clipboard will stay
718  * available after the application exits */
719  wxTheClipboard->Close();
720  }
721  }
722  else
723  {
724  if( !screenshotImage.SaveFile( fullFileName,
725  fmt_is_jpeg ? wxBITMAP_TYPE_JPEG : wxBITMAP_TYPE_PNG ) )
726  wxMessageBox( _( "Can't save file" ) );
727 
728  screenshotImage.Destroy();
729  }
730 
731 }
732 
733 
735 {
736  wxLogTrace( m_logTrace, "EDA_3D_VIEWER::RenderEngineChanged()" );
737 
738  if( m_canvas )
740 }
741 
742 
743 bool EDA_3D_VIEWER::Set3DColorFromUser( SFVEC3D &aColor, const wxString& aTitle,
744  CUSTOM_COLORS_LIST* aPredefinedColors )
745 {
746  KIGFX::COLOR4D newcolor;
747  KIGFX::COLOR4D oldcolor( aColor.r,aColor.g, aColor.b, 1.0 );
748 
749  DIALOG_COLOR_PICKER picker( this, oldcolor, false, aPredefinedColors );
750 
751  if( picker.ShowModal() != wxID_OK )
752  return false;
753 
754  newcolor = picker.GetColor();
755 
756  if( newcolor == oldcolor )
757  return false;
758 
759  aColor.r = newcolor.r;
760  aColor.g = newcolor.g;
761  aColor.b = newcolor.b;
762 
763  return true;
764 }
765 
766 
768 {
770 
771  colors.push_back( CUSTOM_COLOR_ITEM( 241.0/255.0, 241.0/255.0, 241.0/255.0, "White" ) );
772  colors.push_back( CUSTOM_COLOR_ITEM( 4.0/255.0, 18.0/255.0, 21.0/255.0, "Dark" ) );
773 
774  if( Set3DColorFromUser( m_boardAdapter.m_SilkScreenColorTop, _( "Silkscreen Color" ), &colors ) )
775  {
777  NewDisplay( true );
778  return true;
779  }
780 
781  return false;
782 }
783 
784 
786 {
788 
789  colors.push_back( CUSTOM_COLOR_ITEM( 20/255.0, 51/255.0, 36/255.0, "Green" ) );
790  colors.push_back( CUSTOM_COLOR_ITEM( 91/255.0, 168/255.0, 12/255.0, "Light Green" ) );
791  colors.push_back( CUSTOM_COLOR_ITEM( 13/255.0, 104/255.0, 11/255.0, "Saturated Green" ) );
792  colors.push_back( CUSTOM_COLOR_ITEM( 181/255.0, 19/255.0, 21/255.0, "Red" ) );
793  colors.push_back( CUSTOM_COLOR_ITEM( 239/255.0, 53/255.0, 41/255.0, "Red Light Orange" ) );
794  colors.push_back( CUSTOM_COLOR_ITEM( 210/255.0, 40/255.0, 14/255.0, "Red 2" ) );
795  colors.push_back( CUSTOM_COLOR_ITEM( 2/255.0, 59/255.0, 162/255.0, "Blue" ) );
796  colors.push_back( CUSTOM_COLOR_ITEM( 54/255.0, 79/255.0, 116/255.0, "Light blue 1" ) );
797  colors.push_back( CUSTOM_COLOR_ITEM( 61/255.0, 85/255.0, 130/255.0, "Light blue 2" ) );
798  colors.push_back( CUSTOM_COLOR_ITEM( 21/255.0, 70/255.0, 80/255.0, "Green blue (dark)" ) );
799  colors.push_back( CUSTOM_COLOR_ITEM( 11/255.0, 11/255.0, 11/255.0, "Black" ) );
800  colors.push_back( CUSTOM_COLOR_ITEM( 245/255.0, 245/255.0, 245/255.0, "White" ) );
801  colors.push_back( CUSTOM_COLOR_ITEM( 119/255.0, 31/255.0, 91/255.0, "Purple" ) );
802  colors.push_back( CUSTOM_COLOR_ITEM( 32/255.0, 2/255.0, 53/255.0, "Purple Dark" ) );
803 
804  if( Set3DColorFromUser( m_boardAdapter.m_SolderMaskColorTop, _( "Solder Mask Color" ), &colors ) )
805  {
807  NewDisplay( true );
808  return true;
809  }
810 
811  return false;
812 }
813 
814 
816 {
818 
819  colors.push_back( CUSTOM_COLOR_ITEM( 184/255.0, 115/255.0, 50/255.0, "Copper" ) );
820  colors.push_back( CUSTOM_COLOR_ITEM( 178/255.0, 156/255.0, 0.0, "Gold" ) );
821  colors.push_back( CUSTOM_COLOR_ITEM( 213/255.0, 213/255.0, 213/255.0, "Silver" ) );
822  colors.push_back( CUSTOM_COLOR_ITEM( 160/255.0, 160/255.0, 160/255.0, "Tin" ) );
823 
824  if( Set3DColorFromUser( m_boardAdapter.m_CopperColor, _( "Copper Color" ), &colors ) )
825  {
826  NewDisplay( true );
827  return true;
828  }
829 
830  return false;
831 }
832 
833 
835 {
837 
838  colors.push_back( CUSTOM_COLOR_ITEM( 51/255.0, 43/255.0, 22/255.0, "FR4 natural, dark" ) );
839  colors.push_back( CUSTOM_COLOR_ITEM( 109/255.0, 116/255.0, 75/255.0, "FR4 natural" ) );
840  colors.push_back( CUSTOM_COLOR_ITEM( 78/255.0, 14/255.0, 5/255.0, "brown/red" ) );
841  colors.push_back( CUSTOM_COLOR_ITEM( 146/255.0, 99/255.0, 47/255.0, "brown 1" ) );
842  colors.push_back( CUSTOM_COLOR_ITEM( 160/255.0, 123/255.0, 54/255.0, "brown 2" ) );
843  colors.push_back( CUSTOM_COLOR_ITEM( 146/255.0, 99/255.0, 47/255.0, "brown 3" ) );
844  colors.push_back( CUSTOM_COLOR_ITEM( 63/255.0, 126/255.0, 71/255.0, "green 1" ) );
845  colors.push_back( CUSTOM_COLOR_ITEM( 117/255.0, 122/255.0, 90/255.0, "green 2" ) );
846 
847  if( Set3DColorFromUser( m_boardAdapter.m_BoardBodyColor, _( "Board Body Color" ), &colors ) )
848  {
849  NewDisplay( true );
850  return true;
851  }
852 
853  return false;
854 }
855 
856 
858 {
860 
861  colors.push_back( CUSTOM_COLOR_ITEM( 128/255.0, 128/255.0, 128/255.0, "grey" ) );
862  colors.push_back( CUSTOM_COLOR_ITEM( 213/255.0, 213/255.0, 213/255.0, "Silver" ) );
863  colors.push_back( CUSTOM_COLOR_ITEM( 90/255.0, 90/255.0, 90/255.0, "grey 2" ) );
864 
865  if( Set3DColorFromUser( m_boardAdapter.m_SolderPasteColor, _( "Solder Paste Color" ), &colors ) )
866  {
867  NewDisplay( true );
868  return true;
869  }
870 
871  return false;
872 }
873 
874 
875 void EDA_3D_VIEWER::OnUpdateUIEngine( wxUpdateUIEvent& aEvent )
876 {
878 }
879 
880 
881 void EDA_3D_VIEWER::OnUpdateUIMaterial( wxUpdateUIEvent& aEvent )
882 {
883  // Set the state of toggle menus according to the current display options
884  switch( aEvent.GetId() )
885  {
888  break;
889 
892  break;
893 
896  break;
897 
898  default:
899  wxFAIL_MSG( "Invalid event in EDA_3D_VIEWER::OnUpdateUIMaterial()" );
900  }
901 }
902 
903 
905 {
906  wxCHECK_RET( m_canvas, "Cannot load settings to null canvas" );
907 
908  COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
909 
910  const DPI_SCALING dpi{ settings, this };
911  m_canvas->SetScaleFactor( dpi.GetScaleFactor() );
912  // TODO(JE) use all control options
914 }
Use a gray shading based on diffuse material.
void AnimationEnabledSet(bool aAnimationEnabled)
AnimationEnabledSet - Enable or disable camera animation when switching to a pre-defined view.
void ResetToDefaults()
Resets all parameters to default values.
void GridSet(GRID3D_TYPE aGridType) noexcept
GridSet - set the current grid.
bool Set3DCopperColorFromUser()
Set the copper color from a set of colors.
KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of ...
Definition: kiway_player.h:59
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
void CommonSettingsChanged(bool aEnvVarsChanged) override
Notification that common settings are updated.
std::vector< BOARD_STACKUP_ITEM * > & GetList()
const std::string JpegFileExtension
void Install3DViewOptionDialog(wxCommandEvent &event)
this class manage the layers needed to make a physical board they are solder mask,...
virtual void SaveSettings(APP_SETTINGS_BASE *aCfg)
Saves common frame parameters to a configuration data file.
SFVEC3D m_SolderPasteColor
in realistic mode: solder paste color
bool Set3DSolderMaskColorFromUser()
Set the solder mask color from a set of colors.
void CommonSettingsChanged(bool aEnvVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
PROJECT & Prj()
Definition: kicad.cpp:317
SFVEC3D m_SolderMaskColorBot
in realistic mode: solder mask color ( bot )
void SetFlag(DISPLAY3D_FLG aFlag, bool aState)
SetFlag - set the status of a flag.
int color
Definition: DXF_plotter.cpp:61
EDA_3D_ACTIONS.
Definition: 3d_actions.h:41
LAYER_3D_ID
3D Viewer virtual layers for color settings
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Saves common frame parameters to a configuration data file.
Implement a canvas based on a wxGLCanvas.
Definition: eda_3d_canvas.h:47
bool IsReloadRequestPending() const
IsReloadRequestPending - Query if there is a pending reload request.
Definition: eda_3d_canvas.h:87
int GetColorStandardListCount()
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:551
double g
Green component.
Definition: color4d.h:367
void SynchroniseColoursWithBoard()
a class to handle a custom color (predefined color) for the color picker dialog
wxString JpegFileWildcard()
wxAuiManager m_auimgr
std::vector< CUSTOM_COLOR_ITEM > CUSTOM_COLORS_LIST
void Request_refresh(bool aRedrawImmediately=true)
Request_refresh - Schedule a refresh update of the canvas.
SFVEC3D m_SilkScreenColorBot
in realistic mode: SilkScreen color ( bot )
void ReloadRequest(BOARD *aBoard=NULL, S3D_CACHE *aCachePointer=NULL)
void RenderEngineSet(RENDER_ENGINE aRenderEngine) noexcept
RenderEngineSet.
void Exit3DFrame(wxCommandEvent &event)
Called when user press the File->Exit.
void loadCommonSettings()
Load configuration from common settings.
Class to handle configuration and automatic determination of the DPI scale to use for canvases.
Definition: dpi_scaling.h:36
double b
Blue component.
Definition: color4d.h:368
EDA_3D_CANVAS * m_canvas
void MovingSpeedMultiplierSet(int aMovingSpeedMultiplier)
MovingSpeedMultiplierSet - Set the camera animation moving speed multiplier option.
bool Set3DColorFromUser(SFVEC3D &aColor, const wxString &aTitle, CUSTOM_COLORS_LIST *aPredefinedColors)
Get a SFVEC3D from a wx colour dialog.
SFVEC3D m_BgColorTop
background top color
Use only diffuse material properties.
ID_MENU3D_FL_RENDER_MATERIAL_MODE_CAD_MODE
TOOL_MANAGER.
Definition: tool_manager.h:51
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void OnActivate(wxActivateEvent &event)
BOARD_ADAPTER m_boardAdapter
BOARD_STACKUP & GetStackupDescriptor()
void OnUpdateUIMaterial(wxUpdateUIEvent &aEvent)
void OnDisableRayTracing(wxCommandEvent &aEvent)
void SetEventDispatcher(TOOL_DISPATCHER *aEventDispatcher)
Function SetEventDispatcher() Sets a dispatcher that processes events and forwards them to tools.
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:91
#define NULL
#define TRANSFER_SETTING(flag, field)
const BITMAP_OPAQUE icon_3d_xpm[1]
Definition: icon_3d.cpp:143
Use all material properties from model file.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
void RenderEngineChanged()
RenderEngineChanged - Notify that the render engine was changed.
bool Set3DSilkScreenColorFromUser()
Set the silkscreen color from a set of colors.
void AntiAliasingSet(ANTIALIASING_MODE aAAmode)
AntiAliasingSet - set the current antialiasing mode value.
EDA_3D_CONTROLLER.
Definition: 3d_controller.h:39
void OnUpdateUIEngine(wxUpdateUIEvent &aEvent)
bool AnimationEnabledGet() const
AnimationEnabledGet - Returns whether camera animation is enabled when switching to a pre-defined vie...
Definition of file extensions used in Kicad.
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
RENDER_ENGINE RenderEngineGet() const noexcept
RenderEngineGet.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
glm::dvec3 SFVEC3D
Definition: xv3d_types.h:48
void OnCloseWindow(wxCloseEvent &event)
void ReCreateMainToolbar()
Definition: 3d_toolbar.cpp:39
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:116
COMMON_CONTROL.
bool Set3DSolderPasteColorFromUser()
Set the solder mask color from a set of colors.
this class manage one layer needed to make a physical board it can be a solder mask,...
Specialization of the wxAuiPaneInfo class for KiCad panels.
SFVEC3D m_BgColorBot
background bottom color
TOOL_DISPATCHER.
wxString PngFileWildcard()
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:160
void RenderEngineChanged()
RenderEngineChanged - Update toolbar icon and call canvas RenderEngineChanged.
BOARD * GetBoard()
static const int * GetAttributesList(ANTIALIASING_MODE aAntiAliasingMode)
Get a list of attributes to pass to wxGLCanvas.
SFVEC3D m_SolderMaskColorTop
in realistic mode: solder mask color ( top )
A modified version of the wxInfoBar class that allows us to:
Definition: infobar.h:68
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
void GetScreenshot(wxImage &aDstImage)
Request a screenshot and output it to the aDstImage.
int MovingSpeedMultiplierGet() const
MovingSpeedMultiplierGet - Return the current camera animation moving speed multiplier option.
SFVEC3D m_SilkScreenColorTop
in realistic mode: SilkScreen color ( top )
see class PGM_BASE
Declaration of the eda_3d_viewer class.
Board layer functions and definitions.
PCB_EDIT_FRAME::OnUpdateSelectTrackWidth EVT_UPDATE_UI_RANGE(ID_POPUP_PCB_SELECT_VIASIZE1, ID_POPUP_PCB_SELECT_VIASIZE8, PCB_EDIT_FRAME::OnUpdateSelectViaSize) PCB_EDIT_FRAME
wxFileName m_defaultSaveScreenshotFileName
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Sets the work environment (model, view, view controls and the parent window).
#define _(s)
Definition: 3d_actions.cpp:33
SFVEC3D m_BoardBodyColor
in realistic mode: FR4 board color
The base frame for deriving all KiCad main window classes.
void OnRenderEngineSelection(wxCommandEvent &event)
GERBVIEW_FRAME::OnZipFileHistory GERBVIEW_FRAME::OnToggleShowLayerManager GERBVIEW_FRAME::OnSelectHighlightChoice EVT_UPDATE_UI(ID_TOOLBARH_GERBVIEW_SELECT_ACTIVE_LAYER, GERBVIEW_FRAME::OnUpdateLayerSelectBox) EVT_UPDATE_UI_RANGE(ID_TB_OPTIONS_SHOW_GBR_MODE_0
SFVEC3D m_CopperColor
in realistic mode: copper color
PCB_BASE_FRAME * Parent() const
Definition: eda_3d_viewer.h:76
KIGFX::COLOR4D GetColor()
const FAB_LAYER_COLOR * GetColorStandardList()
const std::string PngFileExtension
void SetScaleFactor(double aFactor)
Set the canvas scale factor, probably for a hi-DPI display.
RENDER_ENGINE
Render engine mode.
Definition: 3d_enums.h:92
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
void OnSetFocus(wxFocusEvent &event)
Color settings are a bit different than most of the settings objects in that there can be more than o...
ANTIALIASING_MODE AntiAliasingGet() const
GridGet - get the current antialiasing mode value.
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
Definition: eda_3d_viewer.h:65
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:74
double r
Red component.
Definition: color4d.h:366
GRID3D_TYPE GridGet() const noexcept
GridGet - get the current grid.
void NewDisplay(bool aForceImmediateRedraw=false)
Reload and refresh (rebuild) the 3D scene.
MATERIAL_MODE MaterialModeGet() const noexcept
MaterialModeGet.
#define RANGE_SCALE_3D
This defines the range that all coord will have to be rendered.
Definition: board_adapter.h:61
#define QUALIFIED_VIEWER3D_FRAMENAME(parent)
Definition: eda_3d_viewer.h:51
static VRML_COLOR colors[VRML_COLOR_LAST]
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
BOARD * GetBoard()
Definition: eda_3d_viewer.h:78
bool m_disable_ray_tracing
bool Set3DBoardBodyColorFromUser()
Set the copper color from a set of colors.
void ReloadRequest()
Request reloading the 3D view.
void MaterialModeSet(MATERIAL_MODE aMaterialMode) noexcept
MaterialModeSet.
virtual void LoadSettings(APP_SETTINGS_BASE *aCfg)
Load common frame parameters from a configuration file.
void Process_Special_Functions(wxCommandEvent &event)
EVT_TOOL_RANGE(ID_START_COMMAND_3D, ID_MENU_COMMAND_END, EDA_3D_VIEWER::Process_Special_Functions) EVT_UPDATE_UI_RANGE(ID_MENU3D_FL_RENDER_MATERIAL_MODE_NORMAL
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99
void takeScreenshot(wxCommandEvent &event)
Create a Screenshot of the current 3D view.