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-2020 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/class_module.h>
37 
38 
39 DLG_SELECT_3DMODEL::DLG_SELECT_3DMODEL( wxWindow* aParent, S3D_CACHE* aCacheManager,
40  MODULE_3D_SETTINGS* aModelItem, wxString& prevModelSelectDir, int& prevModelWildcard ) :
41  DLG_SELECT_3D_MODELE_BASE( aParent ),
42  m_model( aModelItem ), m_cache( aCacheManager ), m_previousDir( prevModelSelectDir ),
43  m_previousFilterIndex( prevModelWildcard )
44 {
45  if( NULL != m_cache )
47  else
48  m_resolver = NULL;
49 
50  // set to NULL to avoid segfaults when m_FileTree is instantiated
51  // and wxGenericDirCtrl events are posted
53 
56  m_cache );
57  m_modelViewer->SetMinSize( wxSize( 300, -1 ) );
58  m_Sizer3Dviewer->Add( m_modelViewer, 1, wxEXPAND, 5 );
59 
60  // create the filter list
61  if( NULL != m_cache )
62  {
63  std::list< wxString > const* fl = m_cache->GetFileFilters();
64  std::list< wxString >::const_iterator sL = fl->begin();
65 
66  // The files filter string build from list of loaded plugins:
67  wxString filter;
68 
69  // The files filter extensions only build from list of loaded plugins:
70  wxString ext_list;
71 
72  while( sL != fl->end() )
73  {
74  if( !filter.IsEmpty() )
75  filter.Append( "|" );
76 
77  filter.Append( *sL );
78 
79  if( !ext_list.IsEmpty() )
80  ext_list.Append( ";" );
81 
82  wxString ext = sL->AfterLast( '|' );
83 
84  if( ext.Len() > 3 ) // Skip *.*
85  ext_list.Append( ext );
86 
87  ++sL;
88  }
89 
90  if( !filter.empty() )
91  {
92  if( !ext_list.empty() )
93  {
94  wxString full_filter;
95  full_filter.Printf( _( "All supported files (%s)" ), ext_list );
96  full_filter << '|' << ext_list << '|' << filter;
97  m_FileTree->SetFilter( full_filter );
98  }
99  else
100  m_FileTree->SetFilter( filter );
101  }
102  else
103  m_FileTree->SetFilter( wxFileSelectorDefaultWildcardStr );
104 
105  if( prevModelWildcard >= 0 && prevModelWildcard < (int)fl->size() )
106  m_FileTree->SetFilterIndex( prevModelWildcard );
107  else
108  {
109  prevModelWildcard = 0;
110  m_FileTree->SetFilterIndex( 0 );
111  }
112  }
113  else
114  {
115  m_FileTree->SetFilter( wxFileSelectorDefaultWildcardStr );
116  prevModelWildcard = 0;
117  m_FileTree->SetFilterIndex( 0 );
118  }
119 
120  m_FileTree->SetPath( m_previousDir );
122 
123  m_modelViewer->Refresh();
124  m_modelViewer->SetFocus();
125 
127 }
128 
129 
131 {
132  if( !m_model || !m_FileTree )
133  return true;
134 
135  m_model->m_Scale.x = 1.0;
136  m_model->m_Scale.y = 1.0;
137  m_model->m_Scale.z = 1.0;
138 
139  m_model->m_Rotation.x = 0.0;
140  m_model->m_Rotation.y = 0.0;
141  m_model->m_Rotation.z = 0.0;
142 
144  m_model->m_Filename.clear();
145 
146  wxString name = m_FileTree->GetFilePath();
147 
148  if( name.empty() )
149  return true;
150 
151  m_previousDir = m_FileTree->GetPath();
152  m_previousFilterIndex = m_FileTree->GetFilterIndex();
153 
154  // file selection mode: retrieve the filename and specify a
155  // path relative to one of the config paths
156  wxFileName fname = m_FileTree->GetFilePath();
157  fname.Normalize();
158  m_model->m_Filename = m_resolver->ShortenPath( fname.GetFullPath() );
159 
160  return true;
161 }
162 
163 
164 void DLG_SELECT_3DMODEL::OnSelectionChanged( wxCommandEvent& event )
165 {
166  if( m_modelViewer )
167  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
168 }
169 
170 
171 void DLG_SELECT_3DMODEL::OnFileActivated( wxCommandEvent& event )
172 {
173  if( m_modelViewer )
174  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
175 
176  SetEscapeId( wxID_OK );
177  Close();
178 }
179 
180 
181 void DLG_SELECT_3DMODEL::SetRootDir( wxCommandEvent& event )
182 {
183  if( m_FileTree && m_dirChoices->GetSelection() > 0 )
184  m_FileTree->SetPath( m_dirChoices->GetString( m_dirChoices->GetSelection() ) );
185 }
186 
187 
188 void DLG_SELECT_3DMODEL::Cfg3DPaths( wxCommandEvent& event )
189 {
190  if( S3D::Configure3DPaths( this, m_resolver ) )
192 }
193 
194 
196 {
197  if( !m_FileTree || !m_resolver || !m_dirChoices )
198  return;
199 
200  std::list< SEARCH_PATH > const* md = m_resolver->GetPaths();
201  std::list< SEARCH_PATH >::const_iterator sL = md->begin();
202  std::set< wxString > cl;
203  wxString prjDir;
204 
205  // extract the current project dir
206  if( sL != md->end() )
207  {
208  prjDir = sL->m_pathexp;
209  ++sL;
210  }
211 
212  while( sL != md->end() )
213  {
214  if( !sL->m_pathexp.empty() && sL->m_pathexp.compare( prjDir ) )
215  cl.insert( sL->m_pathexp );
216 
217  ++sL;
218  }
219 
220  if( !cl.empty() )
221  {
222  unsigned int choice = 0;
223  m_dirChoices->Clear();
224  m_dirChoices->Append( "" ); //Insert a blank string at the beginning to allow selection
225 
226  if( !prjDir.empty() )
227  {
228  m_dirChoices->Append( prjDir );
229 
230  if( prjDir == m_FileTree->GetPath() )
231  choice = 1;
232  }
233 
234  std::set< wxString >::const_iterator sI = cl.begin();
235 
236  while( sI != cl.end() )
237  {
238  if( *sI == m_FileTree->GetPath() )
239  choice = m_dirChoices->GetCount();
240 
241  m_dirChoices->Append( *sI );
242  ++sI;
243  }
244 
245  m_dirChoices->SetSelection( choice );
246  }
247 
248  return;
249 }
bool TransferDataFromWindow() override
C3D_MODEL_VIEWER * m_modelViewer
std::list< wxString > const * GetFileFilters() const
Function GetFileFilters returns the list of file filters retrieved from the plugins; this will contai...
Definition: 3d_cache.cpp:622
void SetRootDir(wxCommandEvent &event) override
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
provides an extensible class to resolve 3D model paths.
const std::list< SEARCH_PATH > * GetPaths(void)
Function GetPaths returns a pointer to the internal path list; the items in:load.
S3D_CACHE.
Definition: 3d_cache.h:54
Class C3D_MODEL_VIEWER Implement a canvas based on a wxGLCanvas.
defines the basic data associated with a single 3D model.
VECTOR3D m_Offset
3D model offset (mm)
Definition: class_module.h:94
#define NULL
Implements a model viewer canvas.
wxString ShortenPath(const wxString &aFullPathName)
Function ShortenPath produces a relative path based on the existing search directories or returns the...
void Set3DModel(const S3DMODEL &a3DModel)
Set3DModel - Set this model to be displayed.
void OnFileActivated(wxCommandEvent &event) override
Declaration of the cogl_att_list class.
wxString m_Filename
The 3D shape filename in 3D library.
Definition: class_module.h:96
static const int * GetAttributesList(ANTIALIASING_MODE aAntiAliasingMode)
Get a list of attributes to pass to wxGLCanvas.
bool Configure3DPaths(wxWindow *aParent, FILENAME_RESOLVER *aResolver)
const char * name
Definition: DXF_plotter.cpp:60
VECTOR3D m_Rotation
3D model rotation (degrees)
Definition: class_module.h:93
#define _(s)
Definition: 3d_actions.cpp:33
DLG_SELECT_3DMODEL(wxWindow *aParent, S3D_CACHE *aCacheManager, MODULE_3D_SETTINGS *aModelItem, wxString &prevModelSelectDir, int &prevModelWildcard)
creates a dialog to select 3D model files
FILENAME_RESOLVER * GetResolver() noexcept
Definition: 3d_cache.cpp:616
FILENAME_RESOLVER * m_resolver
defines the display data cache manager for 3D models
void Cfg3DPaths(wxCommandEvent &event) override
VECTOR3D m_Scale
3D model scaling factor (dimensionless)
Definition: class_module.h:92
MODULE_3D_SETTINGS * m_model
void OnSelectionChanged(wxCommandEvent &event) override
Class DLG_SELECT_3D_MODELE_BASE.