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, 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 <tool/actions.h>
26#include <tool/tool_manager.h>
27#include <eda_3d_canvas.h>
28#include <eda_3d_viewer_frame.h>
29#include <id.h>
30#include <kiface_base.h>
36
37
39{
40 CONDITIONAL_MENU& ctxMenu = m_menu->GetMenu();
41
44
45 ctxMenu.AddSeparator();
48
49 ctxMenu.AddSeparator();
52
53 ctxMenu.AddSeparator();
56
57 ctxMenu.AddSeparator();
59
60 ctxMenu.AddSeparator();
65
66 return true;
67}
68
69
71{
72 m_canvas = nullptr;
73 m_boardAdapter = nullptr;
74 m_camera = nullptr;
75
77
78 wxCHECK( holder, /* void */ );
79 wxCHECK( holder->GetToolCanvas()->GetId() == EDA_3D_CANVAS_ID, /* void */ );
80
81 m_canvas = static_cast<EDA_3D_CANVAS*>( holder->GetToolCanvas() );
82
83 if( EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( holder ) )
84 {
85 wxCHECK( frame->GetFrameType() == FRAME_PCB_DISPLAY3D, /* void */ );
86
87 m_boardAdapter = &static_cast<EDA_3D_VIEWER_FRAME*>( frame )->GetAdapter();
88 m_camera = &static_cast<EDA_3D_VIEWER_FRAME*>( frame )->GetCurrentCamera();
89 }
90 else if( wxWindow* previewWindow = dynamic_cast<wxWindow*>( holder ) )
91 {
92 wxCHECK( previewWindow->GetId() == PANEL_PREVIEW_3D_MODEL_ID, /* void */ );
93
94 m_boardAdapter = &static_cast<PANEL_PREVIEW_3D_MODEL*>( holder )->GetAdapter();
95 m_camera = &static_cast<PANEL_PREVIEW_3D_MODEL*>( holder )->GetCurrentCamera();
96 }
97}
98
99
101{
102 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
103 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
104 SELECTION dummySel;
105
106 if( conditionalMenu )
107 conditionalMenu->Evaluate( dummySel );
108
109 if( actionMenu )
110 actionMenu->UpdateAll();
111
112 return 0;
113}
114
115
117{
118 // Main loop: keep receiving events
119 while( TOOL_EVENT* evt = Wait() )
120 {
121 if( evt->IsCancelInteractive() )
122 {
123 wxWindow* canvas = m_toolMgr->GetToolHolder()->GetToolCanvas();
124 KIWAY_HOLDER* parent = dynamic_cast<KIWAY_HOLDER*>( wxGetTopLevelParent( canvas ) );
125
126 if( parent && parent->GetType() == KIWAY_HOLDER::DIALOG )
127 {
128 DIALOG_SHIM* dialog = static_cast<DIALOG_SHIM*>( parent );
129
130 if( dialog->IsQuasiModal() )
131 dialog->EndQuasiModal( wxID_CANCEL );
132 else
133 dialog->EndModal( wxID_CANCEL );
134 }
135 else
136 {
137 evt->SetPassEvent();
138 }
139 }
140 else if( evt->IsClick( BUT_RIGHT ) )
141 {
142 m_menu->ShowContextMenu();
143 }
144 else
145 {
146 evt->SetPassEvent();
147 }
148 }
149
150 return 0;
151}
152
153
155{
157
158 return 0;
159}
160
161
163{
164 switch( aEvent.Parameter<ACTIONS::CURSOR_EVENT_TYPE>() )
165 {
166 case ACTIONS::CURSOR_UP: m_canvas->SetView3D( VIEW3D_TYPE::VIEW3D_PAN_UP ); break;
167 case ACTIONS::CURSOR_DOWN: m_canvas->SetView3D( VIEW3D_TYPE::VIEW3D_PAN_DOWN ); break;
168 case ACTIONS::CURSOR_LEFT: m_canvas->SetView3D( VIEW3D_TYPE::VIEW3D_PAN_LEFT ); break;
169 case ACTIONS::CURSOR_RIGHT: m_canvas->SetView3D( VIEW3D_TYPE::VIEW3D_PAN_RIGHT ); break;
170 default: wxFAIL; break;
171 }
172
173 return 0;
174}
175
176
178{
179 double rotIncrement = glm::radians( m_rotationIncrement );
180
181 switch( aEvent.Parameter<ROTATION_DIR>() )
182 {
183 case ROTATION_DIR::X_CW: m_camera->RotateX( -rotIncrement ); break;
184 case ROTATION_DIR::X_CCW: m_camera->RotateX( rotIncrement ); break;
185
187 case ROTATION_DIR::Y_CW: m_camera->RotateY( rotIncrement ); break;
188 case ROTATION_DIR::Y_CCW: m_camera->RotateY( -rotIncrement ); break;
189 case ROTATION_DIR::Z_CW: m_camera->RotateZ( -rotIncrement ); break;
190 case ROTATION_DIR::Z_CCW: m_camera->RotateZ( rotIncrement ); break;
191 default: wxFAIL; break;
192 }
193
194 if( m_boardAdapter->m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
196 else
198
199 return 0;
200}
201
202
204{
206
207 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
208
209 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
210 static_cast<EDA_3D_VIEWER_FRAME*>( frame )->NewDisplay( true );
211 else
213
214 return 0;
215}
216
217
219{
221
222 if( m_boardAdapter->m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
224 else
226
227 return 0;
228}
229
231{
232 std::bitset<LAYER_3D_END> visibilityFlags = m_boardAdapter->GetVisibleLayers();
233 APPEARANCE_CONTROLS_3D* appearanceManager = nullptr;
234
235 auto flipLayer =
236 [&]( int layer )
237 {
238 appearanceManager->OnLayerVisibilityChanged( layer,
239 !visibilityFlags.test( layer ) );
240 };
241
242 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
243
244 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
245 appearanceManager = static_cast<EDA_3D_VIEWER_FRAME*>( frame )->GetAppearanceManager();
246
247 if( appearanceManager )
248 {
249 if( aEvent.IsAction( &EDA_3D_ACTIONS::showTHT ) )
250 flipLayer( LAYER_3D_TH_MODELS );
251 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showSMD ) )
252 flipLayer( LAYER_3D_SMD_MODELS );
253 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showVirtual ) )
254 flipLayer( LAYER_3D_VIRTUAL_MODELS );
255 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showNotInPosFile ) )
256 flipLayer( LAYER_3D_MODELS_NOT_IN_POS );
257 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showDNP ) )
258 flipLayer( LAYER_3D_MODELS_MARKED_DNP );
259 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showAxis ) )
260 flipLayer( LAYER_3D_AXES );
261 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showBBoxes ) )
262 flipLayer( LAYER_3D_BOUNDING_BOXES );
263 }
264
265 return 0;
266}
267
268
270{
271 EDA_BASE_FRAME* frame = dynamic_cast<EDA_BASE_FRAME*>( m_toolMgr->GetToolHolder() );
272
273 if( frame && frame->GetFrameType() == FRAME_PCB_DISPLAY3D )
274 static_cast<EDA_3D_VIEWER_FRAME*>( frame )->ToggleAppearanceManager();
275
276 return 0;
277}
278
279
281{
283
284 if( m_canvas )
286
287 return 0;
288}
289
290
292{
294 return 0;
295}
296
297
299{
300 bool direction = aEvent.IsAction( &ACTIONS::zoomIn );
301 return doZoomInOut( direction, true );
302}
303
304
306{
307 bool direction = aEvent.IsAction( &ACTIONS::zoomInCenter );
308 return doZoomInOut( direction, false );
309}
310
311
312int EDA_3D_CONTROLLER::doZoomInOut( bool aDirection, bool aCenterOnCursor )
313{
314 if( m_canvas )
315 {
316 m_canvas->SetView3D( aDirection ? VIEW3D_TYPE::VIEW3D_ZOOM_IN
317 : VIEW3D_TYPE::VIEW3D_ZOOM_OUT );
319 }
320
321 return 0;
322}
323
324
326{
327 if( m_canvas )
328 {
329 m_canvas->SetView3D( VIEW3D_TYPE::VIEW3D_FIT_SCREEN );
331 }
332
333 return 0;
334}
335
336
338{
341
342 // Pan control
351
352 // View rotation
368
369 // Zoom control
374 // zoom in/out at cursor does not exist in 3D viewer but because F1 and F2 keys generate
375 // a zoomIn/zoomOut event, these events must be captured to use these hot keys. The actual
376 // zoom is the same as ZoomInOutCenter
379
380 // Grid
386
387 // Material
391
392 // Visibility
400}
401
402
ROTATION_DIR
Rotation direction for the 3d canvas.
Definition: 3d_enums.h:35
GRID3D_TYPE
Grid types.
Definition: 3d_enums.h:54
MATERIAL_MODE
Render 3d model shape materials mode.
Definition: 3d_enums.h:71
VIEW3D_TYPE
Definition: 3d_enums.h:78
static TOOL_ACTION zoomRedraw
Definition: actions.h:124
static TOOL_ACTION zoomOutCenter
Definition: actions.h:128
static TOOL_ACTION zoomIn
Definition: actions.h:125
static TOOL_ACTION zoomOut
Definition: actions.h:126
static TOOL_ACTION panDown
Definition: actions.h:174
CURSOR_EVENT_TYPE
Definition: actions.h:250
@ CURSOR_RIGHT
Definition: actions.h:258
@ CURSOR_LEFT
Definition: actions.h:256
@ CURSOR_UP
Definition: actions.h:252
@ CURSOR_DOWN
Definition: actions.h:254
static TOOL_ACTION panLeft
Definition: actions.h:175
static TOOL_ACTION updateMenu
Definition: actions.h:220
static TOOL_ACTION zoomFitScreen
Definition: actions.h:134
static TOOL_ACTION panUp
Definition: actions.h:173
static TOOL_ACTION zoomInCenter
Definition: actions.h:127
static TOOL_ACTION panRight
Definition: actions.h:176
Define the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
void UpdateAll()
Run update handlers for the menu and its submenus.
void OnLayerVisibilityChanged(int aLayer, bool isVisible)
std::bitset< LAYER_3D_END > GetVisibleLayers() const
EDA_3D_VIEWER_SETTINGS * m_Cfg
void RotateY(float aAngleInRadians)
Definition: camera.cpp:666
void RotateX(float aAngleInRadians)
Definition: camera.cpp:659
void RotateZ(float aAngleInRadians)
Definition: camera.cpp:673
void ToggleProjection()
Definition: camera.cpp:560
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.
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:88
bool IsQuasiModal() const
Definition: dialog_shim.h:113
void EndQuasiModal(int retCode)
static TOOL_ACTION showLayersManager
static TOOL_ACTION rotateXCCW
static TOOL_ACTION showNotInPosFile
static TOOL_ACTION showTHT
static TOOL_ACTION noGrid
static TOOL_ACTION show2_5mmGrid
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 viewLeft
static TOOL_ACTION show10mmGrid
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 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
static TOOL_ACTION showAxis
Implement a canvas based on a wxGLCanvas.
Definition: eda_3d_canvas.h:51
void DisplayStatus()
Update the status bar with the position information.
void RenderRaytracingRequest()
Request to render the current view in Raytracing mode.
bool SetView3D(VIEW3D_TYPE aRequestedView)
Select a specific 3D view or operation.
void Request_refresh(bool aRedrawImmediately=true)
Schedule a refresh update of the canvas.
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 doZoomInOut(bool aDirection, bool aCenterOnCursor)
int ToggleLayersManager(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 SetMaterial(const TOOL_EVENT &aEvent)
int UpdateMenu(const TOOL_EVENT &aEvent)
int ViewControl(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.
void NewDisplay(bool aForceImmediateRedraw=false)
Reload and refresh (rebuild) the 3D scene.
APPEARANCE_CONTROLS_3D * GetAppearanceManager()
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.
Definition: kiway_holder.h:39
HOLDER_TYPE GetType() const
Definition: kiway_holder.h:48
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:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:82
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:465
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.
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:402
#define EDA_3D_CANVAS_ID
Definition: eda_3d_canvas.h:45
Declaration of the eda_3d_viewer class.
@ FRAME_PCB_DISPLAY3D
Definition: frame_type.h:47
Common command IDs shared by more than one of the KiCad applications.
@ LAYER_3D_BOUNDING_BOXES
Definition: layer_ids.h:523
@ LAYER_3D_SMD_MODELS
Definition: layer_ids.h:518
@ LAYER_3D_TH_MODELS
Definition: layer_ids.h:517
@ LAYER_3D_AXES
Definition: layer_ids.h:522
@ LAYER_3D_VIRTUAL_MODELS
Definition: layer_ids.h:519
@ LAYER_3D_MODELS_MARKED_DNP
Definition: layer_ids.h:521
@ LAYER_3D_MODELS_NOT_IN_POS
Definition: layer_ids.h:520
#define PANEL_PREVIEW_3D_MODEL_ID
@ BUT_RIGHT
Definition: tool_event.h:133