KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_3d_controller.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) 2023 CERN
5 * Copyright The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <3d_rendering/opengl/render_3d_opengl.h> // Must be included before any GL header
22
23#include <tool/actions.h>
24#include <tool/tool_manager.h>
25#include <eda_3d_canvas.h>
26#include <eda_3d_viewer_frame.h>
29#include <id.h>
30#include <kiface_base.h>
36
37
39{
40 std::shared_ptr<ACTION_MENU> rotateSubmenu = std::make_shared<ACTION_MENU>( true, this );
41 rotateSubmenu->SetUntranslatedTitle( _HKI( "Rotate Board" ) );
42 rotateSubmenu->SetIcon( BITMAPS::rotate_cw );
43 m_menu->RegisterSubMenu( rotateSubmenu );
44
45 rotateSubmenu->Add( EDA_3D_ACTIONS::rotateXCW );
46 rotateSubmenu->Add( EDA_3D_ACTIONS::rotateXCCW );
47 rotateSubmenu->AppendSeparator();
48 rotateSubmenu->Add( EDA_3D_ACTIONS::rotateYCW );
49 rotateSubmenu->Add( EDA_3D_ACTIONS::rotateYCCW );
50 rotateSubmenu->AppendSeparator();
51 rotateSubmenu->Add( EDA_3D_ACTIONS::rotateZCW );
52 rotateSubmenu->Add( EDA_3D_ACTIONS::rotateZCCW );
53
54 std::shared_ptr<ACTION_MENU> moveSubmenu = std::make_shared<ACTION_MENU>( true, this );
55 moveSubmenu->SetUntranslatedTitle( _HKI( "Move Board" ) );
56 moveSubmenu->SetIcon( BITMAPS::move );
57 m_menu->RegisterSubMenu( moveSubmenu );
58
59 moveSubmenu->Add( EDA_3D_ACTIONS::moveLeft );
60 moveSubmenu->Add( EDA_3D_ACTIONS::moveRight );
61 moveSubmenu->Add( EDA_3D_ACTIONS::moveUp );
62 moveSubmenu->Add( EDA_3D_ACTIONS::moveDown );
63
64 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
65
68
69 ctxMenu.AddSeparator();
76
77 ctxMenu.AddSeparator();
78 ctxMenu.AddMenu( rotateSubmenu.get(), SELECTION_CONDITIONS::ShowAlways );
80 ctxMenu.AddMenu( moveSubmenu.get(), SELECTION_CONDITIONS::ShowAlways );
81
82 return true;
83}
84
85
87{
88 m_canvas = nullptr;
89 m_boardAdapter = nullptr;
90 m_camera = nullptr;
91
92 TOOLS_HOLDER* holder = m_toolMgr->GetToolHolder();
93
94 wxCHECK( holder, /* void */ );
95 wxCHECK( holder->GetToolCanvas()->GetId() == EDA_3D_CANVAS_ID, /* void */ );
96
97 m_canvas = static_cast<EDA_3D_CANVAS*>( holder->GetToolCanvas() );
98
99 if( EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( holder ) )
100 {
101 wxCHECK( frame->GetFrameType() == FRAME_PCB_DISPLAY3D, /* void */ );
102
103 m_boardAdapter = &static_cast<EDA_3D_VIEWER_FRAME*>( frame )->GetAdapter();
104 m_camera = &static_cast<EDA_3D_VIEWER_FRAME*>( frame )->GetCurrentCamera();
105 }
106 else if( wxWindow* previewWindow = dynamic_cast<wxWindow*>( holder ) )
107 {
108 wxCHECK( previewWindow->GetId() == PANEL_PREVIEW_3D_MODEL_ID, /* void */ );
109
110 m_boardAdapter = &static_cast<PANEL_PREVIEW_3D_MODEL*>( holder )->GetAdapter();
111 m_camera = &static_cast<PANEL_PREVIEW_3D_MODEL*>( holder )->GetCurrentCamera();
112 }
113}
114
115
117{
118 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
119 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
120 SELECTION dummySel;
121
122 if( conditionalMenu )
123 conditionalMenu->Evaluate( dummySel );
124
125 if( actionMenu )
126 actionMenu->UpdateAll();
127
128 return 0;
129}
130
131
133{
134 // Main loop: keep receiving events
135 while( TOOL_EVENT* evt = Wait() )
136 {
137 if( evt->IsCancelInteractive() )
138 {
139 wxWindow* canvas = m_toolMgr->GetToolHolder()->GetToolCanvas();
140 KIWAY_HOLDER* parent = dynamic_cast<KIWAY_HOLDER*>( wxGetTopLevelParent( canvas ) );
141
142 if( parent && parent->GetType() == KIWAY_HOLDER::DIALOG )
143 {
144 DIALOG_SHIM* dialog = static_cast<DIALOG_SHIM*>( parent );
145
146 if( dialog->IsQuasiModal() )
147 dialog->EndQuasiModal( wxID_CANCEL );
148 else
149 dialog->EndModal( wxID_CANCEL );
150 }
151 else
152 {
153 evt->SetPassEvent();
154 }
155 }
156 else if( evt->IsMouseDown() )
157 {
158 }
159 else if( evt->IsClick() && ( evt->Buttons() & BUT_RIGHT ) )
160 {
161
162 if( !m_canvas->m_mouse_was_moved )
163 m_menu->ShowContextMenu();
164 }
165 else
166 {
167 evt->SetPassEvent();
168 }
169 }
170
171 return 0;
172}
173
174
176{
177 m_canvas->SetView3D( aEvent.Parameter<VIEW3D_TYPE>() );
178
179 return 0;
180}
181
182
184{
185 switch( aEvent.Parameter<ACTIONS::CURSOR_EVENT_TYPE>() )
186 {
187 case ACTIONS::CURSOR_UP: m_canvas->SetView3D( VIEW3D_TYPE::VIEW3D_PAN_UP ); break;
191 default: wxFAIL; break;
192 }
193
194 return 0;
195}
196
197
199{
200 double rotIncrement = glm::radians( m_rotationIncrement );
201
202 switch( aEvent.Parameter<ROTATION_DIR>() )
203 {
204 case ROTATION_DIR::X_CW: m_camera->RotateX( -rotIncrement ); break;
205 case ROTATION_DIR::X_CCW: m_camera->RotateX( rotIncrement ); break;
206
208 case ROTATION_DIR::Y_CW: m_camera->RotateY( rotIncrement ); break;
209 case ROTATION_DIR::Y_CCW: m_camera->RotateY( -rotIncrement ); break;
210 case ROTATION_DIR::Z_CW: m_camera->RotateZ( -rotIncrement ); break;
211 case ROTATION_DIR::Z_CCW: m_camera->RotateZ( rotIncrement ); break;
212 default: wxFAIL; break;
213 }
214
215 if( m_boardAdapter->m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
216 m_canvas->Request_refresh();
217 else
218 m_canvas->RenderRaytracingRequest();
219
220 return 0;
221}
222
223
225{
226 m_boardAdapter->m_Cfg->m_Render.material_mode = aEvent.Parameter<MATERIAL_MODE>();
227
228 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
229
230 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
231 static_cast<EDA_3D_VIEWER_FRAME*>( frame )->NewDisplay( true );
232 else
233 m_canvas->Request_refresh();
234
235 return 0;
236}
237
238
240{
241 m_camera->ToggleProjection();
242
243 if( m_boardAdapter->m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
244 m_canvas->Request_refresh();
245 else
246 m_canvas->RenderRaytracingRequest();
247
248 return 0;
249}
250
252{
253 std::bitset<LAYER_3D_END> visibilityFlags = m_boardAdapter->GetVisibleLayers();
254 APPEARANCE_CONTROLS_3D* appearanceManager = nullptr;
255
256 auto flipLayer =
257 [&]( int layer )
258 {
259 appearanceManager->OnLayerVisibilityChanged( layer, !visibilityFlags.test( layer ) );
260 };
261
262 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
263
264 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
265 appearanceManager = static_cast<EDA_3D_VIEWER_FRAME*>( frame )->GetAppearanceManager();
266
267 if( appearanceManager )
268 {
269 if( aEvent.IsAction( &EDA_3D_ACTIONS::showTHT ) ) flipLayer( LAYER_3D_TH_MODELS );
270 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showSMD ) ) flipLayer( LAYER_3D_SMD_MODELS );
271 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showVirtual ) ) flipLayer( LAYER_3D_VIRTUAL_MODELS );
273 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showDNP ) ) flipLayer( LAYER_3D_MODELS_MARKED_DNP );
274 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showNavigator ) ) flipLayer( LAYER_3D_NAVIGATOR );
275 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showBBoxes ) ) flipLayer( LAYER_3D_BOUNDING_BOXES );
276 }
277
278 return 0;
279}
280
281
283{
284 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
285
286 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
287 static_cast<EDA_3D_VIEWER_FRAME*>( frame )->ToggleAppearanceManager();
288
289 return 0;
290}
291
292
294{
295 m_boardAdapter->m_Cfg->m_Render.grid_type = aEvent.Parameter<GRID3D_TYPE>();
296
297 if( m_canvas )
298 m_canvas->Request_refresh();
299
300 return 0;
301}
302
303
305{
306 m_canvas->Request_refresh();
307 return 0;
308}
309
310
312{
313 bool direction = aEvent.IsAction( &ACTIONS::zoomIn );
314 return doZoomInOut( direction, true );
315}
316
317
319{
320 bool direction = aEvent.IsAction( &ACTIONS::zoomInCenter );
321 return doZoomInOut( direction, false );
322}
323
324
325int EDA_3D_CONTROLLER::doZoomInOut( bool aDirection, bool aCenterOnCursor )
326{
327 if( m_canvas )
328 {
330 m_canvas->DisplayStatus();
331 }
332
333 return 0;
334}
335
336
338{
339 if( m_canvas )
340 {
342 m_canvas->DisplayStatus();
343 }
344
345 return 0;
346}
347
348
350{
351 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
352
353 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
354 static_cast<EDA_3D_VIEWER_FRAME*>( frame )->NewDisplay( true );
355
356 return 0;
357}
358
359
361{
362 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
363
364 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
365 {
366 EDA_3D_VIEWER_FRAME* frame3d = static_cast<EDA_3D_VIEWER_FRAME*>( frame );
367
368 RENDER_ENGINE& engine = frame3d->GetAdapter().m_Cfg->m_Render.engine;
369 RENDER_ENGINE old_engine = engine;
370
371 if( old_engine == RENDER_ENGINE::OPENGL )
373 else
374 engine = RENDER_ENGINE::OPENGL;
375
376 // Directly tell the canvas the rendering engine changed
377 if( old_engine != engine )
378 frame3d->GetCanvas()->RenderEngineChanged();
379 }
380
381 return 0;
382}
383
384
386{
387 m_boardAdapter->m_Cfg->m_Render.show_missing_models = !m_boardAdapter->m_Cfg->m_Render.show_missing_models;
388 m_canvas->ReloadRequest();
389 m_canvas->Request_refresh();
390 return 0;
391}
392
393
395{
396 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
397
398 if( !frame || frame->GetFrameType() != FRAME_PCB_DISPLAY3D )
399 return 0;
400
401 EDA_3D_VIEWER_FRAME* viewer = static_cast<EDA_3D_VIEWER_FRAME*>( frame );
403
404 wxSize currentSize = viewer->GetCanvas()->GetClientSize();
405
407 {
408 viewer->ExportImage( fmt, currentSize );
409 return 0;
410 }
411
413 DIALOG_EXPORT_3D_IMAGE dlg( viewer, currentSize, cfg ? &cfg->m_ExportImage : nullptr );
414
415 if( dlg.ShowModal() == wxID_OK )
416 viewer->ExportImage( fmt, dlg.GetSize() );
417
418 return 0;
419}
420
421
423{
426
427 // Miscellaneous control
433
434 // Pan control
443
444 // View rotation
460
461 // Zoom control
466 // zoom in/out at cursor does not exist in 3D viewer but because F1 and F2 keys generate
467 // a zoomIn/zoomOut event, these events must be captured to use these hot keys. The actual
468 // zoom is the same as ZoomInOutCenter
471
472 // Grid
478
479 // Material
483
484 // Visibility
493}
494
495
ROTATION_DIR
Rotation direction for the 3d canvas.
Definition 3d_enums.h:31
GRID3D_TYPE
Grid types.
Definition 3d_enums.h:50
MATERIAL_MODE
Render 3d model shape materials mode.
Definition 3d_enums.h:67
RENDER_ENGINE
Render engine mode.
Definition 3d_enums.h:60
VIEW3D_TYPE
Definition 3d_enums.h:74
@ VIEW3D_ZOOM_OUT
Definition 3d_enums.h:90
@ VIEW3D_PAN_LEFT
Definition 3d_enums.h:87
@ VIEW3D_FIT_SCREEN
Definition 3d_enums.h:94
@ VIEW3D_ZOOM_IN
Definition 3d_enums.h:89
@ VIEW3D_PAN_UP
Definition 3d_enums.h:85
@ VIEW3D_PAN_DOWN
Definition 3d_enums.h:86
@ VIEW3D_PAN_RIGHT
Definition 3d_enums.h:88
static TOOL_ACTION zoomRedraw
Definition actions.h:128
static TOOL_ACTION zoomOutCenter
Definition actions.h:132
static TOOL_ACTION zoomIn
Definition actions.h:129
static TOOL_ACTION zoomOut
Definition actions.h:130
static TOOL_ACTION panDown
Definition actions.h:181
CURSOR_EVENT_TYPE
Definition actions.h:299
@ CURSOR_RIGHT
Definition actions.h:307
@ CURSOR_LEFT
Definition actions.h:305
@ CURSOR_UP
Definition actions.h:301
@ CURSOR_DOWN
Definition actions.h:303
static TOOL_ACTION panLeft
Definition actions.h:182
static TOOL_ACTION updateMenu
Definition actions.h:266
static TOOL_ACTION zoomFitScreen
Definition actions.h:138
static TOOL_ACTION panUp
Definition actions.h:180
static TOOL_ACTION zoomInCenter
Definition actions.h:131
static TOOL_ACTION panRight
Definition actions.h:183
Define the structure of a menu based on ACTIONs.
Definition action_menu.h:43
void UpdateAll()
Run update handlers for the menu and its submenus.
void OnLayerVisibilityChanged(int aLayer, bool isVisible)
EDA_3D_VIEWER_SETTINGS * m_Cfg
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
void AddMenu(ACTION_MENU *aMenu, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Add a submenu to the menu.
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition dialog_shim.h:65
bool IsQuasiModal() const
Definition dialog_shim.h:90
void EndQuasiModal(int retCode)
int ShowModal() override
static TOOL_ACTION showNavigator
static TOOL_ACTION showLayersManager
static TOOL_ACTION rotateXCCW
static TOOL_ACTION showNotInPosFile
static TOOL_ACTION exportImage
static TOOL_ACTION showTHT
static TOOL_ACTION noGrid
static TOOL_ACTION show2_5mmGrid
static TOOL_ACTION reloadBoard
static TOOL_ACTION rotateZCCW
static TOOL_ACTION rotateZCW
static TOOL_ACTION rotateYCCW
static TOOL_ACTION rotateXCW
static TOOL_ACTION viewTop
static TOOL_ACTION show1mmGrid
static TOOL_ACTION showDNP
static TOOL_ACTION toggleOrtho
static TOOL_ACTION moveLeft
static TOOL_ACTION toggleShowMissingModels
static TOOL_ACTION viewLeft
static TOOL_ACTION show10mmGrid
static TOOL_ACTION toggleRaytacing
static TOOL_ACTION viewBack
static TOOL_ACTION show5mmGrid
static TOOL_ACTION viewRight
static TOOL_ACTION showSMD
static TOOL_ACTION homeView
static TOOL_ACTION moveUp
static TOOL_ACTION flipView
static TOOL_ACTION moveDown
static TOOL_ACTION viewBottom
static TOOL_ACTION copyToClipboard
static TOOL_ACTION moveRight
static TOOL_ACTION materialDiffuse
static TOOL_ACTION pivotCenter
static TOOL_ACTION controlActivate
static TOOL_ACTION showVirtual
static TOOL_ACTION rotateYCW
static TOOL_ACTION materialCAD
static TOOL_ACTION viewFront
static TOOL_ACTION showBBoxes
static TOOL_ACTION materialNormal
Implement a canvas based on a wxGLCanvas.
void RenderEngineChanged()
Notify that the render engine was changed.
int On3DGridSelection(const TOOL_EVENT &aEvent)
double m_rotationIncrement
Rotation increment for the rotate actions (degrees)
int ZoomFitScreen(const TOOL_EVENT &aEvent)
EDA_3D_CANVAS * m_canvas
int ExportImage(const TOOL_EVENT &aEvent)
int doZoomInOut(bool aDirection, bool aCenterOnCursor)
int ToggleLayersManager(const TOOL_EVENT &aEvent)
int ToggleRaytracing(const TOOL_EVENT &aEvent)
int Main(const TOOL_EVENT &aEvent)
BOARD_ADAPTER * m_boardAdapter
int ZoomRedraw(const TOOL_EVENT &aEvent)
int ToggleOrtho(const TOOL_EVENT &aEvent)
int PanControl(const TOOL_EVENT &aEvent)
bool Init() override
Init() is called once upon a registration of the tool.
int ToggleVisibility(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void setTransitions() override
Set up handlers for various events.
int RotateView(const TOOL_EVENT &aEvent)
int ToggleShowMissingModels(const TOOL_EVENT &aEvent)
int SetMaterial(const TOOL_EVENT &aEvent)
int UpdateMenu(const TOOL_EVENT &aEvent)
int ViewControl(const TOOL_EVENT &aEvent)
int ReloadBoard(const TOOL_EVENT &aEvent)
int ZoomInOutCenter(const TOOL_EVENT &aEvent)
int ZoomInOut(const TOOL_EVENT &aEvent)
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
EDA_3D_CANVAS * GetCanvas()
BOARD_ADAPTER & GetAdapter()
void ExportImage(EDA_3D_VIEWER_EXPORT_FORMAT aFormat, const wxSize &aSize)
Export 3D viewer image to file or clipboard.
EXPORT_IMAGE_SETTINGS m_ExportImage
The base frame for deriving all KiCad main window classes.
FRAME_T GetFrameType() const
A mix in class which holds the location of a wxWindow's KIWAY.
HOLDER_TYPE GetType() const
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
virtual wxWindow * GetToolCanvas() const =0
Canvas access.
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
Generic, UI-independent tool event.
Definition tool_event.h:167
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:469
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
std::unique_ptr< TOOL_MENU > m_menu
The functions below are not yet implemented - their interface may change.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
#define EDA_3D_CANVAS_ID
Declaration of the eda_3d_viewer class.
EDA_3D_VIEWER_EXPORT_FORMAT
@ FRAME_PCB_DISPLAY3D
Definition frame_type.h:43
@ LAYER_3D_NAVIGATOR
Definition layer_ids.h:615
@ LAYER_3D_BOUNDING_BOXES
Definition layer_ids.h:616
@ LAYER_3D_SMD_MODELS
Definition layer_ids.h:611
@ LAYER_3D_TH_MODELS
Definition layer_ids.h:610
@ LAYER_3D_VIRTUAL_MODELS
Definition layer_ids.h:612
@ LAYER_3D_MODELS_MARKED_DNP
Definition layer_ids.h:614
@ LAYER_3D_MODELS_NOT_IN_POS
Definition layer_ids.h:613
#define _HKI(x)
Definition page_info.cpp:40
#define PANEL_PREVIEW_3D_MODEL_ID
T * GetAppSettings(const char *aFilename)
@ BUT_RIGHT
Definition tool_event.h:129