KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_select_3d_model.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) 2016 Mario Luzeiro <[email protected]>
5 * Copyright (C) 2016 Cirilo Bernardo <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22
23#include <set>
25#include "project.h"
26#include "3d_cache/3d_info.h"
27#include "3d_cache/3d_cache.h"
31#include <filename_resolver.h>
32#include <pcbnew/footprint.h>
33#include <wx_filename.h>
34
35#include <wx/filedlg.h>
36
37#include <kiplatform/ui.h>
38
39
41 FP_3DMODEL* aModelItem, wxString& prevModelSelectDir,
42 int& prevModelWildcard ) :
44 m_model( aModelItem ),
45 m_cache( aCacheManager ),
46 m_resolver( aCacheManager ? aCacheManager->GetResolver() : nullptr ),
47 m_previousDir( prevModelSelectDir ),
48 m_modelViewer( nullptr )
49{
52 m_modelViewer->SetMinSize( wxSize( 400, -1 ) );
53 m_Sizer3Dviewer->Add( m_modelViewer, 1, wxEXPAND|wxRIGHT, 5 );
54
55 // create the filter list
56 if( NULL != m_cache )
57 {
58 std::list< wxString > const* fl = m_cache->GetFileFilters();
59 std::list< wxString >::const_iterator sL = fl->begin();
60
61 // The files filter string build from list of loaded plugins:
62 wxString filter;
63
64 // The files filter extensions only build from list of loaded plugins:
65 wxString ext_list;
66
67 while( sL != fl->end() )
68 {
69 if( !filter.IsEmpty() )
70 filter.Append( "|" );
71
72 filter.Append( *sL );
73
74 if( !ext_list.IsEmpty() )
75 ext_list.Append( ";" );
76
77 wxString ext = sL->AfterLast( '|' );
78
79 if( ext.Len() > 3 ) // Skip *.*
80 ext_list.Append( ext );
81
82 ++sL;
83 }
84
85 if( !filter.empty() )
86 {
87 if( !ext_list.empty() )
88 {
89 wxString full_filter;
90 full_filter.Printf( _( "All supported files (%s)" ), ext_list );
91 full_filter << '|' << ext_list << '|' << filter;
92 m_FileTree->SetFilter( full_filter );
93 }
94 else
95 {
96 m_FileTree->SetFilter( filter );
97 }
98 }
99 else
100 {
101 m_FileTree->SetFilter( wxFileSelectorDefaultWildcardStr );
102 }
103
104 if( prevModelWildcard >= 0 && prevModelWildcard < (int)fl->size() )
105 {
106 m_FileTree->SetFilterIndex( prevModelWildcard );
107 }
108 else
109 {
110 prevModelWildcard = 0;
111 m_FileTree->SetFilterIndex( 0 );
112 }
113 }
114 else
115 {
116 m_FileTree->SetFilter( wxFileSelectorDefaultWildcardStr );
117 prevModelWildcard = 0;
118 m_FileTree->SetFilterIndex( 0 );
119 }
120
121 // Fix the filter box on the file selector widget so that it always shows the start of
122 // the filter string in the combobox. Otherwise it will only show the end of the string,
123 // which is empty for all but the all supported filters option.
124 wxChoice* filterBox = m_FileTree->GetFilterListCtrl();
126
127 m_FileTree->SetPath( m_previousDir );
129
130 m_modelViewer->Refresh();
131 m_modelViewer->SetFocus();
132
134}
135
136
138{
139 if( !m_model || !m_FileTree )
140 return true;
141
142 m_model->m_Scale.x = 1.0;
143 m_model->m_Scale.y = 1.0;
144 m_model->m_Scale.z = 1.0;
145
146 m_model->m_Rotation.x = 0.0;
147 m_model->m_Rotation.y = 0.0;
148 m_model->m_Rotation.z = 0.0;
149
150 m_model->m_Offset = m_model->m_Rotation;
151 m_model->m_Filename.clear();
152
153 wxString name = m_FileTree->GetFilePath();
154
155 if( name.empty() )
156 return true;
157
158 m_previousDir = m_FileTree->GetPath();
159
160 // file selection mode: retrieve the filename and specify a
161 // path relative to one of the config paths
162 wxFileName fname = m_FileTree->GetFilePath();
163 fname.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
164 m_model->m_Filename = m_resolver->ShortenPath( fname.GetFullPath() );
165
166 return true;
167}
168
169
170void DIALOG_SELECT_3DMODEL::OnSelectionChanged( wxCommandEvent& event )
171{
172 if( m_modelViewer )
173 m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
174}
175
176
177void DIALOG_SELECT_3DMODEL::OnFileActivated( wxCommandEvent& event )
178{
179 if( m_modelViewer )
180 m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
181
182 EmulateButtonClickIfPresent( wxID_OK );
183}
184
185
186void DIALOG_SELECT_3DMODEL::SetRootDir( wxCommandEvent& event )
187{
188 if( m_FileTree && m_dirChoices->GetSelection() > 0 )
189 {
190 m_FileTree->SetPath( m_dirChoices->GetString( m_dirChoices->GetSelection() ) );
191 m_FileTree->UnselectAll(); // Ensure no unwanted selection
192 }
193}
194
195
196void DIALOG_SELECT_3DMODEL::Cfg3DPaths( wxCommandEvent& event )
197{
198 DIALOG_CONFIGURE_PATHS dlg( this );
199
200 if( dlg.ShowQuasiModal() == wxID_OK )
202}
203
204
206{
207 if( !m_FileTree || !m_resolver || !m_dirChoices )
208 return;
209
210 std::list< SEARCH_PATH > const* md = m_resolver->GetPaths();
211 std::list< SEARCH_PATH >::const_iterator sL = md->begin();
212 std::set< wxString > cl;
213 wxString prjDir;
214
215 // extract the current project dir
216 if( sL != md->end() )
217 {
218 prjDir = sL->m_Pathexp;
219 ++sL;
220 }
221
222 while( sL != md->end() )
223 {
224 if( !sL->m_Pathexp.empty() && sL->m_Pathexp.compare( prjDir ) )
225 cl.insert( sL->m_Pathexp );
226
227 ++sL;
228 }
229
230 if( !cl.empty() )
231 {
232 unsigned int choice = 0;
233 m_dirChoices->Clear();
234 m_dirChoices->Append( "" ); //Insert a blank string at the beginning to allow selection
235
236 if( !prjDir.empty() )
237 {
238 m_dirChoices->Append( prjDir );
239
240 if( prjDir == m_FileTree->GetPath() )
241 choice = 1;
242 }
243
244 std::set< wxString >::const_iterator sI = cl.begin();
245
246 while( sI != cl.end() )
247 {
248 if( *sI == m_FileTree->GetPath() )
249 choice = m_dirChoices->GetCount();
250
251 m_dirChoices->Append( *sI );
252 ++sI;
253 }
254
255 m_dirChoices->SetSelection( choice );
256 }
257
258 return;
259}
defines the basic data associated with a single 3D model.
const char * name
void OnSelectionChanged(wxCommandEvent &event) override
DIALOG_SELECT_3DMODEL(wxWindow *aParent, S3D_CACHE *aCacheManager, FP_3DMODEL *aModelItem, wxString &prevModelSelectDir, int &prevModelWildcard)
void SetRootDir(wxCommandEvent &event) override
FILENAME_RESOLVER * m_resolver
EDA_3D_MODEL_VIEWER * m_modelViewer
void OnFileActivated(wxCommandEvent &event) override
void Cfg3DPaths(wxCommandEvent &event) override
DIALOG_SELECT_3D_MODEL_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Select 3D Model"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Implement a canvas based on a wxGLCanvas.
static const wxGLAttributes GetAttributesList(ANTIALIASING_MODE aAntiAliasingMode, bool aAlpha=false)
Get a list of attributes to pass to wxGLCanvas.
Cache for storing the 3D shapes.
Definition 3d_cache.h:53
#define _(s)
Implements a model viewer canvas.
void EllipsizeChoiceBox(wxChoice *aChoice)
Configure a wxChoice control to ellipsize the shown text in the button with the ellipses placed at th...
Definition wxgtk/ui.cpp:253
Declaration of the cogl_att_list class.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:35