KiCad PCB EDA Suite
dlg_select_3dmodel.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 <mrluzeiro@ua.pt>
5  * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
6  * Copyright (C) 2017-2021 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, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 
27 #include <set>
28 #include "dlg_select_3dmodel.h"
29 #include "project.h"
30 #include "3d_cache/3d_info.h"
31 #include "3d_cache/3d_cache.h"
32 #include "3d_cache_dialogs.h"
35 #include <filename_resolver.h>
36 #include <pcbnew/footprint.h>
37 
38 #include <wx/filedlg.h>
39 
40 #include <kiplatform/ui.h>
41 
42 
43 DLG_SELECT_3DMODEL::DLG_SELECT_3DMODEL( wxWindow* aParent, S3D_CACHE* aCacheManager,
44  FP_3DMODEL* aModelItem, wxString& prevModelSelectDir,
45  int& prevModelWildcard ) :
46  DLG_SELECT_3D_MODELE_BASE( aParent ),
47  m_model( aModelItem ), m_cache( aCacheManager ), m_previousDir( prevModelSelectDir ),
48  m_previousFilterIndex( prevModelWildcard )
49 {
50  if( NULL != m_cache )
52  else
53  m_resolver = NULL;
54 
55  // set to NULL to avoid segfaults when m_FileTree is instantiated
56  // and wxGenericDirCtrl events are posted
58 
61  m_cache );
62  m_modelViewer->SetMinSize( wxSize( 400, -1 ) );
63  m_Sizer3Dviewer->Add( m_modelViewer, 1, wxEXPAND|wxRIGHT, 5 );
64 
65  // create the filter list
66  if( NULL != m_cache )
67  {
68  std::list< wxString > const* fl = m_cache->GetFileFilters();
69  std::list< wxString >::const_iterator sL = fl->begin();
70 
71  // The files filter string build from list of loaded plugins:
72  wxString filter;
73 
74  // The files filter extensions only build from list of loaded plugins:
75  wxString ext_list;
76 
77  while( sL != fl->end() )
78  {
79  if( !filter.IsEmpty() )
80  filter.Append( "|" );
81 
82  filter.Append( *sL );
83 
84  if( !ext_list.IsEmpty() )
85  ext_list.Append( ";" );
86 
87  wxString ext = sL->AfterLast( '|' );
88 
89  if( ext.Len() > 3 ) // Skip *.*
90  ext_list.Append( ext );
91 
92  ++sL;
93  }
94 
95  if( !filter.empty() )
96  {
97  if( !ext_list.empty() )
98  {
99  wxString full_filter;
100  full_filter.Printf( _( "All supported files (%s)" ), ext_list );
101  full_filter << '|' << ext_list << '|' << filter;
102  m_FileTree->SetFilter( full_filter );
103  }
104  else
105  m_FileTree->SetFilter( filter );
106  }
107  else
108  m_FileTree->SetFilter( wxFileSelectorDefaultWildcardStr );
109 
110  if( prevModelWildcard >= 0 && prevModelWildcard < (int)fl->size() )
111  m_FileTree->SetFilterIndex( prevModelWildcard );
112  else
113  {
114  prevModelWildcard = 0;
115  m_FileTree->SetFilterIndex( 0 );
116  }
117  }
118  else
119  {
120  m_FileTree->SetFilter( wxFileSelectorDefaultWildcardStr );
121  prevModelWildcard = 0;
122  m_FileTree->SetFilterIndex( 0 );
123  }
124 
125  // Fix the filter box on the file selector widget so that it always shows the start of
126  // the filter string in the combobox. Otherwise it will only show the end of the string,
127  // which is empty for all but the all supported filters option.
128  wxChoice* filterBox = m_FileTree->GetFilterListCtrl();
130 
131  m_FileTree->SetPath( m_previousDir );
133 
134  m_modelViewer->Refresh();
135  m_modelViewer->SetFocus();
136 
138 }
139 
140 
142 {
143  if( !m_model || !m_FileTree )
144  return true;
145 
146  m_model->m_Scale.x = 1.0;
147  m_model->m_Scale.y = 1.0;
148  m_model->m_Scale.z = 1.0;
149 
150  m_model->m_Rotation.x = 0.0;
151  m_model->m_Rotation.y = 0.0;
152  m_model->m_Rotation.z = 0.0;
153 
155  m_model->m_Filename.clear();
156 
157  wxString name = m_FileTree->GetFilePath();
158 
159  if( name.empty() )
160  return true;
161 
162  m_previousDir = m_FileTree->GetPath();
163  m_previousFilterIndex = m_FileTree->GetFilterIndex();
164 
165  // file selection mode: retrieve the filename and specify a
166  // path relative to one of the config paths
167  wxFileName fname = m_FileTree->GetFilePath();
168  fname.Normalize();
169  m_model->m_Filename = m_resolver->ShortenPath( fname.GetFullPath() );
170 
171  return true;
172 }
173 
174 
175 void DLG_SELECT_3DMODEL::OnSelectionChanged( wxCommandEvent& event )
176 {
177  if( m_modelViewer )
178  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
179 }
180 
181 
182 void DLG_SELECT_3DMODEL::OnFileActivated( wxCommandEvent& event )
183 {
184  if( m_modelViewer )
185  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
186 
187  EmulateButtonClickIfPresent( wxID_OK );
188 }
189 
190 
191 void DLG_SELECT_3DMODEL::SetRootDir( wxCommandEvent& event )
192 {
193  if( m_FileTree && m_dirChoices->GetSelection() > 0 )
194  m_FileTree->SetPath( m_dirChoices->GetString( m_dirChoices->GetSelection() ) );
195 }
196 
197 
198 void DLG_SELECT_3DMODEL::Cfg3DPaths( wxCommandEvent& event )
199 {
200  if( S3D::Configure3DPaths( this, m_resolver ) )
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 }
bool TransferDataFromWindow() override
C3D_MODEL_VIEWER * m_modelViewer
std::list< wxString > const * GetFileFilters() const
Return the list of file filters retrieved from the plugins.
Definition: 3d_cache.cpp:614
void SetRootDir(wxCommandEvent &event) override
Declaration of the cogl_att_list class.
Cache for storing the 3D shapes.
Definition: 3d_cache.h:52
Implement a canvas based on a wxGLCanvas.
defines the basic data associated with a single 3D model.
static const int * GetAttributesList(ANTIALIASING_MODE aAntiAliasingMode)
Get a list of attributes to pass to wxGLCanvas.
VECTOR3D m_Offset
3D model offset (mm)
Definition: footprint.h:95
#define NULL
wxString ShortenPath(const wxString &aFullPathName)
Produce a relative path based on the existing search directories or returns the same path if the path...
void Set3DModel(const S3DMODEL &a3DModel)
Set this model to be displayed.
void OnFileActivated(wxCommandEvent &event) override
VECTOR3D m_Scale
3D model scaling factor (dimensionless)
Definition: footprint.h:93
const std::list< SEARCH_PATH > * GetPaths() const
Return a pointer to the internal path list; the items in:load.
void EllipsizeChoiceBox(wxChoice *aChoice)
Configure a wxChoice control to ellipsize the shown text in the button with the ellipses placed at th...
Definition: gtk/ui.cpp:91
wxString m_Filename
The 3D shape filename in 3D library.
Definition: footprint.h:97
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
bool Configure3DPaths(wxWindow *aParent, FILENAME_RESOLVER *aResolver)
VECTOR3D m_Rotation
3D model rotation (degrees)
Definition: footprint.h:94
const char * name
Definition: DXF_plotter.cpp:59
#define _(s)
Definition: 3d_actions.cpp:33
creates a dialog to select 3D model files
FILENAME_RESOLVER * GetResolver() noexcept
Definition: 3d_cache.cpp:608
FILENAME_RESOLVER * m_resolver
void Cfg3DPaths(wxCommandEvent &event) override
DLG_SELECT_3DMODEL(wxWindow *aParent, S3D_CACHE *aCacheManager, FP_3DMODEL *aModelItem, wxString &prevModelSelectDir, int &prevModelWildcard)
void OnSelectionChanged(wxCommandEvent &event) override
Class DLG_SELECT_3D_MODELE_BASE.