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-2018 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 <pcbnew/class_module.h>
36 
37 #define ID_FILE_TREE ( wxID_LAST + 1 )
38 #define ID_SET_DIR ( ID_FILE_TREE + 1 )
39 #define ID_CFG_PATHS ( ID_SET_DIR + 1 )
40 
41 
43  EVT_DIRCTRL_SELECTIONCHANGED( ID_FILE_TREE, DLG_SELECT_3DMODEL::OnSelectionChanged )
44  EVT_DIRCTRL_FILEACTIVATED( ID_FILE_TREE, DLG_SELECT_3DMODEL::OnFileActivated )
45  EVT_CHOICE( ID_SET_DIR, DLG_SELECT_3DMODEL::SetRootDir )
46  EVT_BUTTON( ID_CFG_PATHS, DLG_SELECT_3DMODEL::Cfg3DPaths )
47 wxEND_EVENT_TABLE()
48 
49 
50 DLG_SELECT_3DMODEL::DLG_SELECT_3DMODEL( wxWindow* aParent, S3D_CACHE* aCacheManager,
51  MODULE_3D_SETTINGS* aModelItem, wxString& prevModelSelectDir, int& prevModelWildcard ) :
52  DIALOG_SHIM( aParent, wxID_ANY, _( "Select 3D Model" ), wxDefaultPosition,
53  wxSize( 500,200 ), wxCAPTION | wxRESIZE_BORDER | wxCLOSE_BOX
54  | wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxSYSTEM_MENU ),
55  m_model( aModelItem ), m_cache( aCacheManager ), m_previousDir( prevModelSelectDir ),
56  m_previousFilterIndex( prevModelWildcard )
57 {
58  SetSizeHints( wxSize( 500,200 ), wxDefaultSize );
59 
60  if( NULL != m_cache )
61  m_resolver = m_cache->GetResolver();
62  else
63  m_resolver = NULL;
64 
65  wxBoxSizer* bSizer0 = new wxBoxSizer( wxVERTICAL );
66 
67  wxBoxSizer* bSizer1 = new wxBoxSizer( wxHORIZONTAL );
68 
69  wxBoxSizer* bSizer2 = new wxBoxSizer( wxVERTICAL );
70 
71  // set to NULL to avoid segfaults when m_FileTree is instantiated
72  // and wxGenericDirCtrl events are posted
73  m_modelViewer = NULL;
74  dirChoices = NULL;
75 
76  m_FileTree = new wxGenericDirCtrl( this, ID_FILE_TREE, prevModelSelectDir, wxDefaultPosition,
77  wxSize( 300,100 ), wxDIRCTRL_3D_INTERNAL | wxDIRCTRL_EDIT_LABELS
78  | wxDIRCTRL_SELECT_FIRST | wxDIRCTRL_SHOW_FILTERS|wxSUNKEN_BORDER, wxEmptyString, 0 );
79 
80 
81  m_FileTree->ShowHidden( false );
82  m_FileTree->SetMinSize( wxSize( 300, 400 ) );
83  m_FileTree->SetLabel( wxT( "3D_MODEL_SELECTOR" ) );
84 
85  bSizer2->Add( m_FileTree, 1, wxEXPAND | wxALL, 5 );
86  bSizer1->Add( bSizer2, 3, wxEXPAND, 5 );
87 
88  m_modelViewer = new C3D_MODEL_VIEWER( this, COGL_ATT_LIST::GetAttributesList( true ), m_cache );
89  m_modelViewer->SetMinSize( wxSize( 500, 400 ) );
90 
91  bSizer1->Add( m_modelViewer, 5, wxEXPAND | wxALL | wxCENTER, 5 );
92 
93  // create the filter list
94  if( NULL != m_cache )
95  {
96  std::list< wxString > const* fl = m_cache->GetFileFilters();
97  std::list< wxString >::const_iterator sL = fl->begin();
98  std::list< wxString >::const_iterator eL = fl->end();
99  wxString filter;
100 
101  while( sL != eL )
102  {
103  filter.Append( *sL );
104 
105  ++sL;
106 
107  if( sL != eL )
108  filter.Append( wxT( "|" ) );
109  }
110 
111  if( !filter.empty() )
112  m_FileTree->SetFilter( filter );
113  else
114  m_FileTree->SetFilter( wxT( "*.*" ) );
115 
116  if( prevModelWildcard >= 0 && prevModelWildcard < (int)fl->size() )
117  m_FileTree->SetFilterIndex( prevModelWildcard );
118  else
119  {
120  prevModelWildcard = 0;
121  m_FileTree->SetFilterIndex( 0 );
122  }
123  }
124  else
125  {
126  m_FileTree->SetFilter( wxT( "*.*" ) );
127  prevModelWildcard = 0;
128  m_FileTree->SetFilterIndex( 0 );
129  }
130 
131  // Add the path choice box and config button
132  wxBoxSizer* hboxDirChoice = new wxBoxSizer( wxHORIZONTAL );
133  dirChoices = new wxChoice( this, ID_SET_DIR, wxDefaultPosition, wxSize( 320, 20 ) );
134  dirChoices->SetMinSize( wxSize( 320, 12 ) );
135 
136  wxStaticText* stDirChoice = new wxStaticText( this, -1, _( "Paths:" ) );
137  wxButton* cfgPaths = new wxButton( this, ID_CFG_PATHS, _( "Configure Paths" ) );
138  hboxDirChoice->Add( stDirChoice, 0, wxALL | wxCENTER, 5 );
139  hboxDirChoice->Add( dirChoices, 1, wxEXPAND | wxALL, 5 );
140  hboxDirChoice->Add( cfgPaths, 0, wxALL, 5 );
141 
142  wxButton* btn_OK = new wxButton( this, wxID_OK, _( "OK" ) );
143  wxButton* btn_Cancel = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
144 
145  wxStdDialogButtonSizer* hSizer1 = new wxStdDialogButtonSizer();
146  hSizer1->AddButton( btn_OK );
147  hSizer1->AddButton( btn_Cancel );
148  hSizer1->Realize();
149 
150  bSizer0->Add( bSizer1, 1, wxALL | wxEXPAND, 5 );
151  bSizer0->Add( hboxDirChoice, 0, wxALL | wxEXPAND, 5 );
152  bSizer0->Add( hSizer1, 0, wxALL | wxEXPAND, 5 );
153 
154  updateDirChoiceList();
155 
156  SetSizerAndFit( bSizer0 );
157  Layout();
158  Centre( wxBOTH );
159 
160  m_modelViewer->Refresh();
161  m_modelViewer->SetFocus();
162 }
163 
164 
166 {
167  if( NULL == m_model || NULL == m_FileTree )
168  return true;
169 
170  m_model->m_Scale.x = 1.0;
171  m_model->m_Scale.y = 1.0;
172  m_model->m_Scale.z = 1.0;
173 
174  m_model->m_Rotation.x = 0.0;
175  m_model->m_Rotation.y = 0.0;
176  m_model->m_Rotation.z = 0.0;
177 
179  m_model->m_Filename.clear();
180 
181  wxString name = m_FileTree->GetFilePath();
182 
183  if( name.empty() )
184  return true;
185 
186  m_previousDir = m_FileTree->GetPath();
187  m_previousFilterIndex = m_FileTree->GetFilterIndex();
188 
189  // file selection mode: retrieve the filename and specify a
190  // path relative to one of the config paths
191  wxFileName fname = m_FileTree->GetFilePath();
192  fname.Normalize();
193  m_model->m_Filename = m_resolver->ShortenPath( fname.GetFullPath() );
194 
195  return true;
196 }
197 
198 
199 void DLG_SELECT_3DMODEL::OnSelectionChanged( wxTreeEvent& event )
200 {
201  if( m_modelViewer )
202  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
203 
204  event.Skip();
205  return;
206 }
207 
208 
209 void DLG_SELECT_3DMODEL::OnFileActivated( wxTreeEvent& event )
210 {
211  if( m_modelViewer )
212  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
213 
214  event.Skip();
215  SetEscapeId( wxID_OK );
216  Close();
217 
218  return;
219 }
220 
221 
222 void DLG_SELECT_3DMODEL::SetRootDir( wxCommandEvent& event )
223 {
224  if( m_FileTree )
225  m_FileTree->SetPath( dirChoices->GetString( dirChoices->GetSelection() ) );
226 
227  return;
228 }
229 
230 
231 void DLG_SELECT_3DMODEL::Cfg3DPaths( wxCommandEvent& event )
232 {
233  if( S3D::Configure3DPaths( this, m_resolver ) )
235 }
236 
237 
239 {
240  if( NULL == m_FileTree || NULL == m_resolver || NULL == dirChoices )
241  return;
242 
243  std::list< S3D_ALIAS > const* md = m_resolver->GetPaths();
244  std::list< S3D_ALIAS >::const_iterator sL = md->begin();
245  std::list< S3D_ALIAS >::const_iterator eL = md->end();
246  std::set< wxString > cl;
247  wxString prjDir;
248 
249  // extract the current project dir
250  if( sL != eL )
251  {
252  prjDir = sL->m_pathexp;
253  ++sL;
254  }
255 
256  while( sL != eL )
257  {
258  if( !sL->m_pathexp.empty() && sL->m_pathexp.compare( prjDir ) )
259  cl.insert( sL->m_pathexp );
260 
261  ++sL;
262  }
263 
264  if( !cl.empty() )
265  {
266  dirChoices->Clear();
267 
268  if( !prjDir.empty() )
269  dirChoices->Append( prjDir );
270 
271  std::set< wxString >::const_iterator sI = cl.begin();
272  std::set< wxString >::const_iterator eI = cl.end();
273 
274  while( sI != eI )
275  {
276  dirChoices->Append( *sI );
277  ++sI;
278  }
279 
280  dirChoices->Select( 0 );
281  }
282 
283  return;
284 }
bool TransferDataFromWindow() override
static const int * GetAttributesList(bool aUseAntiAliasing)
Get a list of attributes to pass to wxGLCanvas.
C3D_MODEL_VIEWER * m_modelViewer
#define ID_FILE_TREE
void Cfg3DPaths(wxCommandEvent &event)
void OnFileActivated(wxTreeEvent &event)
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:70
S3D_FILENAME_RESOLVER * m_resolver
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:101
#define ID_CFG_PATHS
bool Configure3DPaths(wxWindow *aParent, S3D_FILENAME_RESOLVER *aResolver)
wxString ShortenPath(const wxString &aFullPathName)
Function ShortenPath produces a relative path based on the existing search directories or returns the...
Implements a model viewer canvas.
void Set3DModel(const S3DMODEL &a3DModel)
Set3DModel - Set this model to be displayed.
wxGenericDirCtrl * m_FileTree
Declaration of the cogl_att_list class.
const std::list< S3D_ALIAS > * GetPaths(void)
Function GetPaths returns a pointer to the internal path list; the items in:load. ...
#define ID_SET_DIR
wxString m_Filename
The 3D shape filename in 3D library.
Definition: class_module.h:102
void SetRootDir(wxCommandEvent &event)
void OnSelectionChanged(wxTreeEvent &event)
#define D(x)
Definition: ptree.cpp:41
const char * name
Definition: DXF_plotter.cpp:61
VECTOR3D m_Rotation
3D model rotation (degrees)
Definition: class_module.h:100
creates a dialog to select 3D model files
defines the display data cache manager for 3D models
VECTOR3D m_Scale
3D model scaling factor (dimensionless)
Definition: class_module.h:99
Module description (excepted pads)
wxBEGIN_EVENT_TABLE(DLG_SELECT_3DMODEL, DIALOG_SHIM) wxEND_EVENT_TABLE() DLG_SELECT_3DMODEL
MODULE_3D_SETTINGS * m_model