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 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  wxDialog( 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,100 ) );
83  m_FileTree->SetLabel( wxT( "3D_MODEL_SELECTOR" ) );
84 
85  bSizer2->Add( m_FileTree, 1, wxEXPAND | wxALL, 5 );
86  bSizer1->Add( bSizer2, 1, wxEXPAND, 5 );
87 
88  m_modelViewer = new C3D_MODEL_VIEWER( this,
90  m_cache );
91  m_modelViewer->SetMinSize( wxSize( 512, 384 ) );
92 
93  bSizer1->Add( m_modelViewer, 0, wxCENTER, 5 );
94 
95  // create the filter list
96  if( NULL != m_cache )
97  {
98  std::list< wxString > const* fl = m_cache->GetFileFilters();
99  std::list< wxString >::const_iterator sL = fl->begin();
100  std::list< wxString >::const_iterator eL = fl->end();
101  wxString filter;
102 
103  while( sL != eL )
104  {
105  filter.Append( *sL );
106 
107  ++sL;
108 
109  if( sL != eL )
110  filter.Append( wxT( "|" ) );
111 
112  }
113 
114  if( !filter.empty() )
115  m_FileTree->SetFilter( filter );
116  else
117  m_FileTree->SetFilter( wxT( "*.*" ) );
118 
119  if( prevModelWildcard >= 0 && prevModelWildcard < (int)fl->size() )
120  m_FileTree->SetFilterIndex( prevModelWildcard );
121  else
122  {
123  prevModelWildcard = 0;
124  m_FileTree->SetFilterIndex( 0 );
125  }
126  }
127  else
128  {
129  m_FileTree->SetFilter( wxT( "*.*" ) );
130  prevModelWildcard = 0;
131  m_FileTree->SetFilterIndex( 0 );
132  }
133 
134  // Add the path choice box and config button
135  wxBoxSizer* hboxDirChoice = new wxBoxSizer( wxHORIZONTAL );
136  dirChoices = new wxChoice( this, ID_SET_DIR, wxDefaultPosition, wxSize( 320, 20 ) );
137  dirChoices->SetMinSize( wxSize( 320, 12 ) );
138 
139  wxStaticText* stDirChoice = new wxStaticText( this, -1, _( "Paths:" ) );
140  wxButton* cfgPaths = new wxButton( this, ID_CFG_PATHS, _( "Configure Path" ) );
141  hboxDirChoice->Add( stDirChoice, 0, wxALL | wxCENTER, 5 );
142  hboxDirChoice->Add( dirChoices, 1, wxEXPAND | wxALL, 5 );
143  hboxDirChoice->Add( cfgPaths, 0, wxALL, 5 );
144 
145  wxButton* btn_OK = new wxButton( this, wxID_OK, _( "OK" ) );
146  wxButton* btn_Cancel = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
147 
148  wxStdDialogButtonSizer* hSizer1 = new wxStdDialogButtonSizer();
149  hSizer1->AddButton( btn_OK );
150  hSizer1->AddButton( btn_Cancel );
151  hSizer1->Realize();
152 
153  bSizer0->Add( bSizer1, 1, wxALL | wxEXPAND, 5 );
154  bSizer0->Add( hboxDirChoice, 0, wxALL | wxEXPAND, 5 );
155  bSizer0->Add( hSizer1, 0, wxALL | wxEXPAND, 5 );
156 
157  updateDirChoiceList();
158 
159  SetSizerAndFit( bSizer0 );
160  Layout();
161  Centre( wxBOTH );
162 
163  m_modelViewer->Refresh();
164  m_modelViewer->SetFocus();
165 }
166 
167 
169 {
170  if( NULL == m_model || NULL == m_FileTree )
171  return true;
172 
173  m_model->m_Scale.x = 1.0;
174  m_model->m_Scale.y = 1.0;
175  m_model->m_Scale.z = 1.0;
176 
177  m_model->m_Rotation.x = 0.0;
178  m_model->m_Rotation.y = 0.0;
179  m_model->m_Rotation.z = 0.0;
180 
182  m_model->m_Filename.clear();
183 
184  wxString name = m_FileTree->GetFilePath();
185 
186  if( name.empty() )
187  return true;
188 
189  m_previousDir = m_FileTree->GetPath();
190  m_previousFilterIndex = m_FileTree->GetFilterIndex();
191 
192  // file selection mode: retrieve the filename and specify a
193  // path relative to one of the config paths
194  wxFileName fname = m_FileTree->GetFilePath();
195  fname.Normalize();
196  m_model->m_Filename = m_resolver->ShortenPath( fname.GetFullPath() );
197 
198  return true;
199 }
200 
201 
202 void DLG_SELECT_3DMODEL::OnSelectionChanged( wxTreeEvent& event )
203 {
204  if( m_modelViewer )
205  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
206 
207  event.Skip();
208  return;
209 }
210 
211 
212 void DLG_SELECT_3DMODEL::OnFileActivated( wxTreeEvent& event )
213 {
214  if( m_modelViewer )
215  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
216 
217  event.Skip();
218  SetEscapeId( wxID_OK );
219  Close();
220 
221  return;
222 }
223 
224 
225 void DLG_SELECT_3DMODEL::SetRootDir( wxCommandEvent& event )
226 {
227  if( m_FileTree )
228  m_FileTree->SetPath( dirChoices->GetString( dirChoices->GetSelection() ) );
229 
230  return;
231 }
232 
233 
234 void DLG_SELECT_3DMODEL::Cfg3DPaths( wxCommandEvent& event )
235 {
236  if( S3D::Configure3DPaths( this, m_resolver ) )
238 }
239 
240 
242 {
243  if( NULL == m_FileTree || NULL == m_resolver || NULL == dirChoices )
244  return;
245 
246  std::list< S3D_ALIAS > const* md = m_resolver->GetPaths();
247  std::list< S3D_ALIAS >::const_iterator sL = md->begin();
248  std::list< S3D_ALIAS >::const_iterator eL = md->end();
249  std::set< wxString > cl;
250  wxString prjDir;
251 
252  // extract the current project dir
253  if( sL != eL )
254  {
255  prjDir = sL->m_pathexp;
256  ++sL;
257  }
258 
259  while( sL != eL )
260  {
261  if( !sL->m_pathexp.empty() && sL->m_pathexp.compare( prjDir ) )
262  cl.insert( sL->m_pathexp );
263 
264  ++sL;
265  }
266 
267  if( !cl.empty() )
268  {
269  dirChoices->Clear();
270 
271  if( !prjDir.empty() )
272  dirChoices->Append( prjDir );
273 
274  std::set< wxString >::const_iterator sI = cl.begin();
275  std::set< wxString >::const_iterator eI = cl.end();
276 
277  while( sI != eI )
278  {
279  dirChoices->Append( *sI );
280  ++sI;
281  }
282 
283  dirChoices->Select( 0 );
284  }
285 
286  return;
287 }
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)
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:100
wxBEGIN_EVENT_TABLE(DLG_SELECT_3DMODEL, wxDialog) wxEND_EVENT_TABLE() DLG_SELECT_3DMODEL
#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:101
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:99
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:98
Module description (excepted pads)
MODULE_3D_SETTINGS * m_model