KiCad PCB EDA Suite
Loading...
Searching...
No Matches
nl_schematic_plugin_impl.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) 2022 3Dconnexion
5 * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22
23// KiCAD includes
25#include <sch_base_frame.h>
26#include <bitmaps.h>
28#include <view/view.h>
30#include <tool/action_manager.h>
31#include <tool/tool_action.h>
32#include <tool/tool_manager.h>
33
34// stdlib
35#include <list>
36#include <map>
37#include <memory>
38#include <utility>
39#include <vector>
40#include <cfloat>
41
42#include <wx/log.h>
43#include <wx/mstream.h>
44
45
53const wxChar* NL_SCHEMATIC_PLUGIN_IMPL::m_logTrace = wxT( "KI_TRACE_NL_SCHEMATIC_PLUGIN" );
54
55
57 CNavigation3D( false, false ),
58 m_viewport2D( nullptr ),
59 m_view( nullptr ),
60 m_isMoving( false ),
61 m_viewportWidth( 0.0 )
62{
63 PutProfileHint( "KiCAD Eeschema" );
64}
65
66
68{
69 EnableNavigation( false );
70}
71
72
74{
75 bool init = m_viewport2D == nullptr;
76
77 m_viewport2D = aViewport;
78
79 if( m_viewport2D != nullptr )
80 {
81 m_view = static_cast<KIGFX::SCH_VIEW*>( m_viewport2D->GetView() );
83
84 if( init )
85 {
86 // Use the default settings for the connexion to the 3DMouse navigation
87 // They are use a single-threaded threading model and row vectors.
88 EnableNavigation( true );
89
90 // Use the SpaceMouse internal timing source for the frame rate.
91 PutFrameTimingSource( TimingSource::SpaceMouse );
92
94 }
95 }
96}
97
98
100{
101 wxLogTrace( m_logTrace, wxT( "NL_SCHEMATIC_PLUGIN_IMPL::SetFocus %d" ), aFocus );
102 NAV_3D::Write( navlib::focus_k, aFocus );
103}
104
105// temporary store for the command categories
106typedef std::map<std::string, TDx::CCommandTreeNode*> CATEGORY_STORE;
107
108
118static CATEGORY_STORE::iterator add_category( std::string aCategoryPath,
119 CATEGORY_STORE& aCategoryStore )
120{
121 using TDx::SpaceMouse::CCategory;
122
123 CATEGORY_STORE::iterator parent_iter = aCategoryStore.begin();
124 std::string::size_type pos = aCategoryPath.find_last_of( '.' );
125
126 if( pos != std::string::npos )
127 {
128 std::string parentPath = aCategoryPath.substr( 0, pos );
129 parent_iter = aCategoryStore.find( parentPath );
130
131 if( parent_iter == aCategoryStore.end() )
132 {
133 parent_iter = add_category( parentPath, aCategoryStore );
134 }
135 }
136
137 std::string name = aCategoryPath.substr( pos + 1 );
138 std::unique_ptr<CCategory> categoryNode =
139 std::make_unique<CCategory>( aCategoryPath.c_str(), name.c_str() );
140
141 CATEGORY_STORE::iterator iter = aCategoryStore.insert(
142 aCategoryStore.end(), CATEGORY_STORE::value_type( aCategoryPath, categoryNode.get() ) );
143
144 parent_iter->second->push_back( std::move( categoryNode ) );
145 return iter;
146}
147
148
150{
151 wxLogTrace( m_logTrace, wxT( "NL_SCHEMATIC_PLUGIN_IMPL::exportCommandsAndImages" ) );
152
153 std::list<TOOL_ACTION*> actions = ACTION_MANAGER::GetActionList();
154
155 if( actions.size() == 0 )
156 {
157 return;
158 }
159
160 using TDx::SpaceMouse::CCommand;
161 using TDx::SpaceMouse::CCommandSet;
162
163 // The root action set node
164 CCommandSet commandSet( "SCHEMATIC_EDITOR", "Schematic Editor" );
165
166 // Activate the command set
167 NAV_3D::PutActiveCommands( commandSet.GetId() );
168
169 // temporary store for the categories
170 CATEGORY_STORE categoryStore;
171
172 std::vector<TDx::CImage> vImages;
173
174 // add the action set to the category_store
175 categoryStore.insert( categoryStore.end(), CATEGORY_STORE::value_type( ".", &commandSet ) );
176
177 std::list<TOOL_ACTION*>::const_iterator it;
178
179 for( it = actions.begin(); it != actions.end(); ++it )
180 {
181 const TOOL_ACTION* action = *it;
182 std::string label = action->GetMenuLabel().ToStdString();
183
184 if( label.empty() )
185 {
186 continue;
187 }
188
189 std::string name = action->GetName();
190
191 // Do no export commands for the 3DViewer app.
192
193 if( name.rfind( "3DViewer.", 0 ) == 0 )
194 {
195 continue;
196 }
197
198 std::string strCategory = action->GetToolName();
199 CATEGORY_STORE::iterator iter = categoryStore.find( strCategory );
200
201 if( iter == categoryStore.end() )
202 {
203 iter = add_category( std::move( strCategory ), categoryStore );
204 }
205
206 std::string description = action->GetDescription().ToStdString();
207
208 // Arbitrary 8-bit data stream
209 wxMemoryOutputStream imageStream;
210
211 if( action->GetIcon() != BITMAPS::INVALID_BITMAP )
212 {
213 wxImage image = KiBitmap( action->GetIcon() ).ConvertToImage();
214 image.SaveFile( imageStream, wxBitmapType::wxBITMAP_TYPE_PNG );
215 image.Destroy();
216
217 if( imageStream.GetSize() )
218 {
219 wxStreamBuffer* streamBuffer = imageStream.GetOutputStreamBuffer();
220 TDx::CImage tdxImage = TDx::CImage::FromData( "", 0, name.c_str() );
221 tdxImage.AssignImage( std::string( reinterpret_cast<const char*>(
222 streamBuffer->GetBufferStart() ),
223 streamBuffer->GetBufferSize() ),
224 0 );
225
226 wxLogTrace( m_logTrace, wxT( "Adding image for : %s" ), name );
227 vImages.push_back( std::move( tdxImage ) );
228 }
229 }
230
231 wxLogTrace( m_logTrace, wxT( "Inserting command: %s, description: %s, in category: %s" ),
232 name, description, iter->first );
233
234 iter->second->push_back(
235 CCommand( std::move( name ), std::move( label ), std::move( description ) ) );
236 }
237
238 NAV_3D::AddCommandSet( commandSet );
239 NAV_3D::AddImages( vImages );
240}
241
242
243long NL_SCHEMATIC_PLUGIN_IMPL::GetCameraMatrix( navlib::matrix_t& matrix ) const
244{
245 if( m_view == nullptr )
246 {
247 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
248 }
249
251
252 double x = m_view->IsMirroredX() ? -1 : 1;
253 double y = m_view->IsMirroredY() ? 1 : -1;
254
255 // x * y * z = 1 for a right-handed coordinate system.
256 double z = x * y;
257
258 // Note: the connexion has been configured as row vectors, the coordinate system is defined in
259 // NL_SCHEMATIC_PLUGIN_IMPL::GetCoordinateSystem and the front view in NL_SCHEMATIC_PLUGIN_IMPL::GetFrontView.
260 matrix = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, m_viewPosition.x, m_viewPosition.y, 0, 1 };
261 return 0;
262}
263
264
265long NL_SCHEMATIC_PLUGIN_IMPL::GetPointerPosition( navlib::point_t& position ) const
266{
267 if( m_view == nullptr )
268 {
269 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
270 }
271
273
274 position.x = mouse_pointer.x;
275 position.y = mouse_pointer.y;
276 position.z = 0;
277
278 return 0;
279}
280
281
282long NL_SCHEMATIC_PLUGIN_IMPL::GetViewExtents( navlib::box_t& extents ) const
283{
284 if( m_view == nullptr )
285 {
286 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
287 }
288
289 double scale = m_viewport2D->GetGAL()->GetWorldScale();
290 BOX2D box = m_view->GetViewport();
291
293
294 extents = navlib::box_t{ -box.GetWidth() / 2.0,
295 -box.GetHeight() / 2.0,
297 box.GetWidth() / 2.0,
298 box.GetHeight() / 2.0,
300 return 0;
301}
302
303
304long NL_SCHEMATIC_PLUGIN_IMPL::GetIsViewPerspective( navlib::bool_t& perspective ) const
305{
306 perspective = false;
307
308 return 0;
309}
310
311
312long NL_SCHEMATIC_PLUGIN_IMPL::SetCameraMatrix( const navlib::matrix_t& matrix )
313{
314 if( m_view == nullptr )
315 {
316 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
317 }
318
319 long result = 0;
320 VECTOR2D viewPos( matrix.m4x4[3][0], matrix.m4x4[3][1] );
321
323 static_cast<VECTOR2D::coord_type>( FLT_EPSILON ) ) )
324 {
326 result = navlib::make_result_code( navlib::navlib_errc::error );
327 }
328 else
329 {
330 m_view->SetCenter( viewPos );
331 }
332
333 m_viewPosition = viewPos;
334
335 return result;
336}
337
338
339long NL_SCHEMATIC_PLUGIN_IMPL::SetViewExtents( const navlib::box_t& extents )
340{
341 if( m_view == nullptr )
342 {
343 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
344 }
345
346 long result = 0;
347
349 {
350 result = navlib::make_result_code( navlib::navlib_errc::error );
351 }
352
353 double width = m_viewportWidth;
354 m_viewportWidth = extents.max_x - extents.min_x;
355
356 double scale = width / m_viewportWidth * m_view->GetScale();
358
359 if( !equals( m_view->GetScale(), scale, static_cast<double>( FLT_EPSILON ) ) )
360 {
361 result = navlib::make_result_code( navlib::navlib_errc::error );
362 }
363
364 return result;
365}
366
367
369{
370 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
371}
372
373
374long NL_SCHEMATIC_PLUGIN_IMPL::SetViewFrustum( const navlib::frustum_t& frustum )
375{
376 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
377}
378
379
380long NL_SCHEMATIC_PLUGIN_IMPL::GetModelExtents( navlib::box_t& extents ) const
381{
382 if( m_view == nullptr )
383 {
384 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
385 }
386
387 BOX2I box = static_cast<SCH_BASE_FRAME*>( m_viewport2D->GetParent() )->GetDocumentExtents();
388 box.Normalize();
389
390 double half_depth = 0.1 / m_viewport2D->GetGAL()->GetWorldScale();
391
392 if( box.GetWidth() == 0 && box.GetHeight() == 0 )
393 {
394 half_depth = 0;
395 }
396
397 extents = { static_cast<double>( box.GetOrigin().x ),
398 static_cast<double>( box.GetOrigin().y ),
399 -half_depth,
400 static_cast<double>( box.GetEnd().x ),
401 static_cast<double>( box.GetEnd().y ),
402 half_depth };
403
404 return 0;
405}
406
407
408long NL_SCHEMATIC_PLUGIN_IMPL::GetCoordinateSystem( navlib::matrix_t& matrix ) const
409{
410 // The coordinate system is defined as x to the right, y down and z into the screen.
411 matrix = { 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 };
412 return 0;
413}
414
415
416long NL_SCHEMATIC_PLUGIN_IMPL::GetFrontView( navlib::matrix_t& matrix ) const
417{
418 matrix = { 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 };
419 return 0;
420}
421
422
424{
425 empty = true;
426 return 0;
427}
428
429
430long NL_SCHEMATIC_PLUGIN_IMPL::GetIsViewRotatable( navlib::bool_t& isRotatable ) const
431{
432 isRotatable = false;
433 return 0;
434}
435
436
438{
439 if( commandId.empty() )
440 {
441 return 0;
442 }
443
444 std::list<TOOL_ACTION*> actions = ACTION_MANAGER::GetActionList();
445 TOOL_ACTION* context = nullptr;
446
447 for( std::list<TOOL_ACTION*>::const_iterator it = actions.begin(); it != actions.end(); it++ )
448 {
449 TOOL_ACTION* action = *it;
450 std::string nm = action->GetName();
451
452 if( commandId == nm )
453 {
454 context = action;
455 }
456 }
457
458 if( context != nullptr )
459 {
460 wxWindow* parent = m_viewport2D->GetParent();
461
462 // Only allow command execution if the window is enabled. i.e. there is not a modal dialog
463 // currently active.
464
465 if( parent->IsEnabled() )
466 {
467 TOOL_MANAGER* tool_manager = static_cast<SCH_BASE_FRAME*>( parent )->GetToolManager();
468
469 // Get the selection to use to test if the action is enabled
470 SELECTION& sel = tool_manager->GetToolHolder()->GetCurrentSelection();
471
472 bool runAction = true;
473
474 if( const ACTION_CONDITIONS* aCond =
475 tool_manager->GetActionManager()->GetCondition( *context ) )
476 {
477 runAction = aCond->enableCondition( sel );
478 }
479
480 if( runAction )
481 {
482 tool_manager->RunAction( *context );
483 }
484 }
485 else
486 {
487 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
488 }
489 }
490
491 return 0;
492}
493
494
496{
497 return 0;
498}
499
500
502{
503 m_isMoving = value;
504
505 return 0;
506}
507
508
510{
511 if( value == 0L )
512 {
514 }
515
516 return 0;
517}
518
519
521{
522 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
523}
524
525
526long NL_SCHEMATIC_PLUGIN_IMPL::GetViewFrustum( navlib::frustum_t& frustum ) const
527{
528 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
529}
530
531
532long NL_SCHEMATIC_PLUGIN_IMPL::GetSelectionExtents( navlib::box_t& extents ) const
533{
534 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
535}
536
537
538long NL_SCHEMATIC_PLUGIN_IMPL::GetSelectionTransform( navlib::matrix_t& transform ) const
539{
540 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
541}
542
543
544long NL_SCHEMATIC_PLUGIN_IMPL::SetSelectionTransform( const navlib::matrix_t& matrix )
545{
546 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
547}
548
549
550long NL_SCHEMATIC_PLUGIN_IMPL::GetPivotPosition( navlib::point_t& position ) const
551{
552 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
553}
554
555
556long NL_SCHEMATIC_PLUGIN_IMPL::IsUserPivot( navlib::bool_t& userPivot ) const
557{
558 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
559}
560
561
562long NL_SCHEMATIC_PLUGIN_IMPL::SetPivotPosition( const navlib::point_t& position )
563{
564 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
565}
566
567
568long NL_SCHEMATIC_PLUGIN_IMPL::GetPivotVisible( navlib::bool_t& visible ) const
569{
570 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
571}
572
573
575{
576 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
577}
578
579
580long NL_SCHEMATIC_PLUGIN_IMPL::GetHitLookAt( navlib::point_t& position ) const
581{
582 return navlib::make_result_code( navlib::navlib_errc::no_data_available );
583}
584
585
587{
588 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
589}
590
591
592long NL_SCHEMATIC_PLUGIN_IMPL::SetHitDirection( const navlib::vector_t& direction )
593{
594 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
595}
596
597
598long NL_SCHEMATIC_PLUGIN_IMPL::SetHitLookFrom( const navlib::point_t& eye )
599{
600 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
601}
602
603
605{
606 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
607}
608
609
610long NL_SCHEMATIC_PLUGIN_IMPL::SetCameraTarget( const navlib::point_t& position )
611{
612 return navlib::make_result_code( navlib::navlib_errc::invalid_operation );
613}
const char * name
Definition: DXF_plotter.cpp:57
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:104
const ACTION_CONDITIONS * GetCondition(const TOOL_ACTION &aAction) const
Get the conditions to use for a specific tool action.
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:136
const Vec & GetOrigin() const
Definition: box2.h:200
size_type GetHeight() const
Definition: box2.h:205
size_type GetWidth() const
Definition: box2.h:204
const Vec GetEnd() const
Definition: box2.h:202
KIGFX::VIEW_CONTROLS * GetViewControls() const
Return a pointer to the #VIEW_CONTROLS instance used in the panel.
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
void ForceRefresh()
Force a redraw.
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
double GetMaxDepth() const
double GetMinDepth() const
double GetWorldScale() const
Get the world scale.
void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 }) override
Set the scaling factor, zooming around a given anchor point.
Definition: sch_view.cpp:75
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Return the current mouse pointer position.
double GetScale() const
Definition: view.h:271
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:512
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:341
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition: view.h:245
bool IsMirroredY() const
Return true if view is flipped across the Y axis.
Definition: view.h:253
const BOX2D & GetBoundary() const
Definition: view.h:300
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:578
long GetHitLookAt(navlib::point_t &aPosition) const override
long GetSelectionExtents(navlib::box_t &aExtents) const override
long GetCoordinateSystem(navlib::matrix_t &aMatrix) const override
long SetViewFOV(double aFov) override
long GetSelectionTransform(navlib::matrix_t &aTransform) const override
long GetPivotPosition(navlib::point_t &aPosition) const override
long SetPivotPosition(const navlib::point_t &aPosition) override
long SetViewFrustum(const navlib::frustum_t &aFrustum) override
long GetFrontView(navlib::matrix_t &aMatrix) const override
long GetViewFOV(double &aFov) const override
long SetHitAperture(double aAperture) override
long SetSelectionTransform(const navlib::matrix_t &aMatrix) override
void SetFocus(bool aFocus)
Set the connection to the 3Dconnexion driver to the focus state so that 3DMouse data is routed here.
long SetViewExtents(const navlib::box_t &aExtents) override
void SetCanvas(EDA_DRAW_PANEL_GAL *aViewport)
Sets the viewport controlled by the SpaceMouse.
long GetCameraMatrix(navlib::matrix_t &aMatrix) const override
long GetPointerPosition(navlib::point_t &aPosition) const override
long GetPivotVisible(navlib::bool_t &aVisible) const override
void exportCommandsAndImages()
Export the invocable actions and images to the 3Dconnexion UI.
long SetTransaction(long aValue) override
long GetIsViewPerspective(navlib::bool_t &aPerspective) const override
long SetHitSelectionOnly(bool aSelectionOnly) override
NL_SCHEMATIC_PLUGIN_IMPL()
Initializes a new instance of the NL_SCHEMATIC_PLUGIN_IMPL.
long SetHitLookFrom(const navlib::point_t &aPosition) override
long SetActiveCommand(std::string aCommandId) override
long GetViewExtents(navlib::box_t &aExtents) const override
long SetCameraTarget(const navlib::point_t &aPosition) override
long GetIsSelectionEmpty(navlib::bool_t &aEmpty) const override
long SetCameraMatrix(const navlib::matrix_t &aMatrix) override
long GetIsViewRotatable(navlib::bool_t &isRotatable) const override
long SetMotionFlag(bool aValue) override
long SetHitDirection(const navlib::vector_t &aDirection) override
long GetModelExtents(navlib::box_t &aExtents) const override
long IsUserPivot(navlib::bool_t &aUserPivot) const override
long SetSettingsChanged(long aChangeNumber) override
long GetViewFrustum(navlib::frustum_t &aFrustum) const override
long SetPivotVisible(bool aVisible) override
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
virtual SELECTION & GetCurrentSelection()
Get the current selection from the canvas area.
Definition: tools_holder.h:98
Represent a single user action.
Definition: tool_action.h:269
wxString GetMenuLabel() const
Return the translated label for the action.
BITMAPS GetIcon() const
Return an icon associated with the action.
Definition: tool_action.h:422
std::string GetToolName() const
Return name of the tool associated with the action.
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:302
wxString GetDescription() const
Master controller class:
Definition: tool_manager.h:57
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:145
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:397
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:297
double coord_type
Definition: vector2d.h:73
static bool empty(const wxTextEntryBase *aCtrl)
static const wxChar * m_logTrace
Trace mask used to enable or disable the trace output of this class.
bool equals(glm::mat< L, C, T, Q > const &aFirst, glm::mat< L, C, T, Q > const &aSecond, T aEpsilon=static_cast< T >(FLT_EPSILON *10))
Template to compare two glm::mat<T> values for equality within a required epsilon.
std::map< std::string, TDx::CCommandTreeNode * > CATEGORY_STORE
CATEGORY_STORE::iterator add_category(std::string aCategoryPath, CATEGORY_STORE &aCategoryStore)
Add a category to the store.
std::map< std::string, TDx::CCommandTreeNode * > CATEGORY_STORE
static CATEGORY_STORE::iterator add_category(std::string aCategoryPath, CATEGORY_STORE &aCategoryStore)
Add a category to the store.
Declaration of the NL_SCHEMATIC_PLUGIN_IMPL class.
const int scale
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
WX_VIEW_CONTROLS class definition.