KiCad PCB EDA Suite
S3D_PLUGIN_MANAGER Class Reference

#include <3d_plugin_manager.h>

Public Member Functions

 S3D_PLUGIN_MANAGER ()
 
virtual ~S3D_PLUGIN_MANAGER ()
 
std::list< wxString > const * GetFileFilters (void) const noexcept
 Return the list of file filters; this will contain at least the default "All Files (*.*)|*.*" and the file filters supported by any available plugins. More...
 
SCENEGRAPHLoad3DModel (const wxString &aFileName, std::string &aPluginInfo)
 
void ClosePlugins (void)
 Iterate through all discovered plugins and closes them to reclaim memory. More...
 
bool CheckTag (const char *aTag)
 Check the given tag and returns true if the plugin named in the tag is not loaded or the plugin is loaded and the version matches. More...
 

Private Member Functions

void loadPlugins (void)
 load plugins More...
 
void listPlugins (const wxString &aPath, std::list< wxString > &aPluginList)
 list potential plugins More...
 
void checkPluginName (const wxString &aPath, std::list< wxString > &aPluginList)
 check the existence of a plugin name and add it to the list More...
 
void checkPluginPath (const wxString &aPath, std::list< wxString > &aSearchList)
 check the existence of a path and add it to the path search list More...
 
void addFilterString (const wxString &aFilterString)
 add an entry to the file filter list More...
 
void addExtensionMap (KICAD_PLUGIN_LDR_3D *aPlugin)
 add entries to the extension map More...
 

Private Attributes

std::list< KICAD_PLUGIN_LDR_3D * > m_Plugins
 list of discovered plugins More...
 
std::multimap< const wxString, KICAD_PLUGIN_LDR_3D * > m_ExtMap
 mapping of extensions to available plugins More...
 
std::list< wxString > m_FileFilters
 list of file filters More...
 

Detailed Description

Definition at line 42 of file 3d_plugin_manager.h.

Constructor & Destructor Documentation

◆ S3D_PLUGIN_MANAGER()

S3D_PLUGIN_MANAGER::S3D_PLUGIN_MANAGER ( )

Definition at line 56 of file 3d_plugin_manager.cpp.

57 {
58  // create the initial file filter list entry
59  m_FileFilters.push_back( _( "All Files (*.*)|*.*" ) );
60 
61  // discover and load plugins
62  loadPlugins();
63 
64 #ifdef DEBUG
65  if( !m_ExtMap.empty() )
66  {
67  std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::const_iterator sM = m_ExtMap.begin();
68  std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::const_iterator eM = m_ExtMap.end();
69  wxLogTrace( MASK_3D_PLUGINMGR, " * Extension [plugin name]:\n" );
70 
71  while( sM != eM )
72  {
73  wxLogTrace( MASK_3D_PLUGINMGR, " + '%s' [%s]\n", sM->first.GetData(),
74  sM->second->GetKicadPluginName() );
75  ++sM;
76  }
77 
78  }
79  else
80  {
81  wxLogTrace( MASK_3D_PLUGINMGR, " * No plugins available\n" );
82  }
83 
84 
85  if( !m_FileFilters.empty() )
86  {
88  std::list< wxString >::const_iterator sFF = m_FileFilters.begin();
89  std::list< wxString >::const_iterator eFF = m_FileFilters.end();
90  wxLogTrace( MASK_3D_PLUGINMGR, " * File filters:\n" );
91 
92  while( sFF != eFF )
93  {
94  wxLogTrace( MASK_3D_PLUGINMGR, " + '%s'\n", (*sFF).GetData() );
95  ++sFF;
96  }
97  }
98  else
99  {
100  wxLogTrace( MASK_3D_PLUGINMGR, " * No file filters available\n" );
101  }
102 #endif // DEBUG
103 }
std::list< wxString > m_FileFilters
list of file filters
std::multimap< const wxString, KICAD_PLUGIN_LDR_3D * > m_ExtMap
mapping of extensions to available plugins
#define MASK_3D_PLUGINMGR
Flag to enable 3D plugin manager debug tracing.
#define _(s)
Definition: 3d_actions.cpp:33
void loadPlugins(void)
load plugins

References _, loadPlugins(), m_ExtMap, m_FileFilters, and MASK_3D_PLUGINMGR.

◆ ~S3D_PLUGIN_MANAGER()

S3D_PLUGIN_MANAGER::~S3D_PLUGIN_MANAGER ( )
virtual

Definition at line 106 of file 3d_plugin_manager.cpp.

107 {
108  std::list< KICAD_PLUGIN_LDR_3D* >::iterator sP = m_Plugins.begin();
109  std::list< KICAD_PLUGIN_LDR_3D* >::iterator eP = m_Plugins.end();
110 
111  while( sP != eP )
112  {
113  (*sP)->Close();
114  delete *sP;
115  ++sP;
116  }
117 
118  m_Plugins.clear();
119 }
std::list< KICAD_PLUGIN_LDR_3D * > m_Plugins
list of discovered plugins

References m_Plugins.

Member Function Documentation

◆ addExtensionMap()

void S3D_PLUGIN_MANAGER::addExtensionMap ( KICAD_PLUGIN_LDR_3D aPlugin)
private

add entries to the extension map

Definition at line 373 of file 3d_plugin_manager.cpp.

374 {
375  // add entries to the extension map
376  if( nullptr == aPlugin )
377  return;
378 
379  int nExt = aPlugin->GetNExtensions();
380 
381  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [INFO] adding %d extensions",
382  __FILE__, __FUNCTION__, __LINE__, nExt );
383 
384  for( int i = 0; i < nExt; ++i )
385  {
386  char const* cp = aPlugin->GetModelExtension( i );
387  wxString ws;
388 
389  if( cp )
390  ws = wxString::FromUTF8Unchecked( cp );
391 
392  if( !ws.empty() )
393  {
394  m_ExtMap.insert( std::pair< const wxString, KICAD_PLUGIN_LDR_3D* >( ws, aPlugin ) );
395  }
396 
397  }
398 }
std::multimap< const wxString, KICAD_PLUGIN_LDR_3D * > m_ExtMap
mapping of extensions to available plugins
int GetNExtensions(void)
#define MASK_3D_PLUGINMGR
Flag to enable 3D plugin manager debug tracing.
char const * GetModelExtension(int aIndex)

References KICAD_PLUGIN_LDR_3D::GetModelExtension(), KICAD_PLUGIN_LDR_3D::GetNExtensions(), m_ExtMap, and MASK_3D_PLUGINMGR.

Referenced by loadPlugins().

◆ addFilterString()

void S3D_PLUGIN_MANAGER::addFilterString ( const wxString &  aFilterString)
private

add an entry to the file filter list

Definition at line 351 of file 3d_plugin_manager.cpp.

352 {
353  // add an entry to the file filter list
354  if( aFilterString.empty() )
355  return;
356 
357  std::list< wxString >::iterator sFF = m_FileFilters.begin();
358  std::list< wxString >::iterator eFF = m_FileFilters.end();
359 
360  while( sFF != eFF )
361  {
362  if( 0 == (*sFF).Cmp( aFilterString ) )
363  return;
364 
365  ++sFF;
366  }
367 
368  m_FileFilters.push_back( aFilterString );
369  return;
370 }
std::list< wxString > m_FileFilters
list of file filters

References m_FileFilters.

Referenced by loadPlugins().

◆ checkPluginName()

void S3D_PLUGIN_MANAGER::checkPluginName ( const wxString &  aPath,
std::list< wxString > &  aPluginList 
)
private

check the existence of a plugin name and add it to the list

Definition at line 282 of file 3d_plugin_manager.cpp.

284 {
285  // check the existence of a plugin name and add it to the list
286 
287  if( aPath.empty() || !wxFileName::FileExists( aPath ) )
288  return;
289 
290  wxFileName path( ExpandEnvVarSubstitutions( aPath, nullptr ) );
291 
292  path.Normalize();
293 
294  // determine if the path is already in the list
295  wxString wxpath = path.GetFullPath();
296  std::list< wxString >::iterator bl = aPluginList.begin();
297  std::list< wxString >::iterator el = aPluginList.end();
298 
299  while( bl != el )
300  {
301  if( 0 == (*bl).Cmp( wxpath ) )
302  return;
303 
304  ++bl;
305  }
306 
307  aPluginList.push_back( wxpath );
308 
309  wxLogTrace( MASK_3D_PLUGINMGR, " * [INFO] found 3D plugin '%s'\n", wxpath.GetData() );
310 }
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:273
#define MASK_3D_PLUGINMGR
Flag to enable 3D plugin manager debug tracing.

References ExpandEnvVarSubstitutions(), MASK_3D_PLUGINMGR, and path.

Referenced by listPlugins().

◆ checkPluginPath()

void S3D_PLUGIN_MANAGER::checkPluginPath ( const wxString &  aPath,
std::list< wxString > &  aSearchList 
)
private

check the existence of a path and add it to the path search list

Definition at line 313 of file 3d_plugin_manager.cpp.

315 {
316  // check the existence of a path and add it to the path search list
317  if( aPath.empty() )
318  return;
319 
320  wxLogTrace( MASK_3D_PLUGINMGR, " * [INFO] checking for 3D plugins in '%s'\n", aPath.GetData() );
321 
322  wxFileName path;
323 
324  if( aPath.StartsWith( "${" ) || aPath.StartsWith( "$(" ) )
325  path.Assign( ExpandEnvVarSubstitutions( aPath, nullptr ), "" );
326  else
327  path.Assign( aPath, "" );
328 
329  path.Normalize();
330 
331  if( !wxFileName::DirExists( path.GetFullPath() ) )
332  return;
333 
334  // determine if the directory is already in the list
335  wxString wxpath = path.GetFullPath();
336  std::list< wxString >::iterator bl = aSearchList.begin();
337  std::list< wxString >::iterator el = aSearchList.end();
338 
339  while( bl != el )
340  {
341  if( 0 == (*bl).Cmp( wxpath ) )
342  return;
343 
344  ++bl;
345  }
346 
347  aSearchList.push_back( wxpath );
348 }
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:273
#define MASK_3D_PLUGINMGR
Flag to enable 3D plugin manager debug tracing.

References ExpandEnvVarSubstitutions(), MASK_3D_PLUGINMGR, and path.

Referenced by loadPlugins().

◆ CheckTag()

bool S3D_PLUGIN_MANAGER::CheckTag ( const char *  aTag)

Check the given tag and returns true if the plugin named in the tag is not loaded or the plugin is loaded and the version matches.

Definition at line 460 of file 3d_plugin_manager.cpp.

461 {
462  if( nullptr == aTag || aTag[0] == 0 || m_Plugins.empty() )
463  return false;
464 
465  std::string tname = aTag;
466  std::string pname; // plugin name
467 
468  size_t cpos = tname.find( ':' );
469 
470  // if there is no colon or plugin name then the tag is bad
471  if( cpos == std::string::npos || cpos == 0 )
472  return false;
473 
474  pname = tname.substr( 0, cpos );
475  std::string ptag; // tag from the plugin
476 
477  std::list< KICAD_PLUGIN_LDR_3D* >::iterator pS = m_Plugins.begin();
478  std::list< KICAD_PLUGIN_LDR_3D* >::iterator pE = m_Plugins.end();
479 
480  while( pS != pE )
481  {
482  ptag.clear();
483  (*pS)->GetPluginInfo( ptag );
484 
485  // if the plugin name matches then the version
486  // must also match
487  if( !ptag.compare( 0, pname.size(), pname ) )
488  {
489  if( ptag.compare( tname ) )
490  return false;
491 
492  return true;
493  }
494 
495  ++pS;
496  }
497 
498  return true;
499 }
std::list< KICAD_PLUGIN_LDR_3D * > m_Plugins
list of discovered plugins

References m_Plugins.

Referenced by checkTag().

◆ ClosePlugins()

void S3D_PLUGIN_MANAGER::ClosePlugins ( void  )

Iterate through all discovered plugins and closes them to reclaim memory.

The individual plugins will be automatically reloaded as calls are made to load specific models.

Definition at line 444 of file 3d_plugin_manager.cpp.

445 {
446  std::list< KICAD_PLUGIN_LDR_3D* >::iterator sP = m_Plugins.begin();
447  std::list< KICAD_PLUGIN_LDR_3D* >::iterator eP = m_Plugins.end();
448 
449  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [INFO] closing %d extensions",
450  __FILE__, __FUNCTION__, __LINE__, static_cast<int>( m_Plugins.size() ) );
451 
452  while( sP != eP )
453  {
454  (*sP)->Close();
455  ++sP;
456  }
457 }
std::list< KICAD_PLUGIN_LDR_3D * > m_Plugins
list of discovered plugins
#define MASK_3D_PLUGINMGR
Flag to enable 3D plugin manager debug tracing.

References m_Plugins, and MASK_3D_PLUGINMGR.

Referenced by S3D_CACHE::ClosePlugins().

◆ GetFileFilters()

std::list< wxString > const * S3D_PLUGIN_MANAGER::GetFileFilters ( void  ) const
noexcept

Return the list of file filters; this will contain at least the default "All Files (*.*)|*.*" and the file filters supported by any available plugins.

Returns
a pointer to the internal filter list.

Definition at line 401 of file 3d_plugin_manager.cpp.

402 {
403  return &m_FileFilters;
404 }
std::list< wxString > m_FileFilters
list of file filters

Referenced by S3D_CACHE::GetFileFilters().

◆ listPlugins()

void S3D_PLUGIN_MANAGER::listPlugins ( const wxString &  aPath,
std::list< wxString > &  aPluginList 
)
private

list potential plugins

Definition at line 239 of file 3d_plugin_manager.cpp.

240 {
241  // list potential plugins given a search path
242  wxString nameFilter; // filter for user-loadable libraries (aka footprints)
243  wxString lName; // stores name of enumerated files
244  wxString fName; // full name of file
245  wxDir wd;
246  wd.Open( aPath );
247 
248  if( !wd.IsOpened() )
249  return;
250 
251  nameFilter = wxT( "*" );
252 
253 #ifndef __WXMAC__
254  nameFilter.Append( wxDynamicLibrary::GetDllExt( wxDL_MODULE ) );
255 #else
256  // wxDynamicLibrary::GetDllExt( wxDL_MODULE ) will return ".bundle" on OS X.
257  // This might be correct, but cmake builds a ".so" for a library MODULE.
258  // Per definition a loadable "xxx.bundle" is similar to an "xxx.app" app
259  // bundle being a folder with some special content in it. We obviously don't
260  // want to have that here for our loadable module, so just use ".so".
261  nameFilter.Append( ".so" );
262 #endif
263 
264  wxString lp = wd.GetNameWithSep();
265 
266  if( wd.GetFirst( &lName, nameFilter, wxDIR_FILES ) )
267  {
268  fName = lp + lName;
269  checkPluginName( fName, aPluginList );
270 
271  while( wd.GetNext( &lName ) )
272  {
273  fName = lp + lName;
274  checkPluginName( fName, aPluginList );
275  }
276  }
277 
278  wd.Close();
279 }
void checkPluginName(const wxString &aPath, std::list< wxString > &aPluginList)
check the existence of a plugin name and add it to the list

References checkPluginName().

Referenced by loadPlugins().

◆ Load3DModel()

SCENEGRAPH * S3D_PLUGIN_MANAGER::Load3DModel ( const wxString &  aFileName,
std::string &  aPluginInfo 
)

Definition at line 407 of file 3d_plugin_manager.cpp.

408 {
409  wxFileName raw( aFileName );
410  wxString ext = raw.GetExt();
411 
412 #ifdef _WIN32
413  // note: plugins only have a lowercase filter within Windows; including an uppercase
414  // filter will result in duplicate file entries and should be avoided.
415  ext.LowerCase();
416 #endif
417 
418  std::pair < std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::iterator,
419  std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::iterator > items;
420 
421  items = m_ExtMap.equal_range( ext );
422  std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::iterator sL = items.first;
423 
424  while( sL != items.second )
425  {
426  if( sL->second->CanRender() )
427  {
428  SCENEGRAPH* sp = sL->second->Load( aFileName.ToUTF8() );
429 
430  if( nullptr != sp )
431  {
432  sL->second->GetPluginInfo( aPluginInfo );
433  return sp;
434  }
435  }
436 
437  ++sL;
438  }
439 
440  return nullptr;
441 }
std::multimap< const wxString, KICAD_PLUGIN_LDR_3D * > m_ExtMap
mapping of extensions to available plugins
Define the basic data set required to represent a 3D model.
Definition: scenegraph.h:44

References m_ExtMap.

Referenced by S3D_CACHE::checkCache(), and S3D_CACHE::load().

◆ loadPlugins()

void S3D_PLUGIN_MANAGER::loadPlugins ( void  )
private

load plugins

Definition at line 122 of file 3d_plugin_manager.cpp.

123 {
124  std::list< wxString > searchpaths;
125  std::list< wxString > pluginlist;
126  wxFileName fn;
127 
128 #ifndef __WXMAC__
129 
130 #ifdef DEBUG
131  // set up to work from the build directory
132  fn.Assign( wxStandardPaths::Get().GetExecutablePath() );
133  fn.AppendDir( wxT( ".." ) );
134  fn.AppendDir( wxT( "plugins" ) );
135  fn.AppendDir( wxT( "3d" ) );
136 
137  std::string testpath = std::string( fn.GetPathWithSep().ToUTF8() );
138  checkPluginPath( testpath, searchpaths );
139 
140  // add subdirectories too
141  wxDir debugPluginDir;
142  wxString subdir;
143 
144  debugPluginDir.Open( testpath );
145 
146  if( debugPluginDir.IsOpened() && debugPluginDir.GetFirst( &subdir, wxEmptyString, wxDIR_DIRS ) )
147  {
148  checkPluginPath( testpath + subdir, searchpaths );
149 
150  while( debugPluginDir.GetNext( &subdir ) )
151  checkPluginPath( testpath + subdir, searchpaths );
152  }
153 #endif
154 
155  fn.AssignDir( PATHS::GetStockPlugins3DPath() );
156  checkPluginPath( std::string( fn.GetPathWithSep().ToUTF8() ), searchpaths );
157 
158  // check for per-user third party plugins
159  // note: GetUserDataDir() gives '.pcbnew' rather than '.kicad' since it uses the exe name;
160  fn.AssignDir( PATHS::GetUserPlugins3DPath() );
161  checkPluginPath( fn.GetPathWithSep(), searchpaths );
162 #else
163 
164  // Search path on OS X is
165  // (1) User ~/Library/Application Support/kicad/PlugIns/3d
166  checkPluginPath( PATHS::GetOSXKicadUserDataDir() + wxT( "/PlugIns/3d" ), searchpaths );
167 
168  // (2) Machine /Library/Application Support/kicad/PlugIns/3d
169  checkPluginPath( PATHS::GetOSXKicadMachineDataDir() + wxT( "/PlugIns/3d" ), searchpaths );
170 
171  // (3) Bundle kicad.app/Contents/PlugIns/3d
172  fn.AssignDir( PATHS::GetStockPlugins3DPath() );
173  checkPluginPath( fn.GetPathWithSep(), searchpaths );
174 
175 #endif
176 
177  std::list< wxString >::iterator sPL = searchpaths.begin();
178  std::list< wxString >::iterator ePL = searchpaths.end();
179 
180  while( sPL != ePL )
181  {
182  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [DEBUG] searching path: '%s'",
183  __FILE__, __FUNCTION__, __LINE__, (*sPL).ToUTF8() );
184 
185  listPlugins( *sPL, pluginlist );
186  ++sPL;
187  }
188 
189  if( pluginlist.empty() )
190  return;
191 
192  sPL = pluginlist.begin();
193  ePL = pluginlist.end();
194 
195  while( sPL != ePL )
196  {
198 
199  if( pp->Open( sPL->ToUTF8() ) )
200  {
201  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [DEBUG] adding plugin",
202  __FILE__, __FUNCTION__, __LINE__ );
203 
204  m_Plugins.push_back( pp );
205  int nf = pp->GetNFilters();
206 
207  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [DEBUG] adding %d filters",
208  __FILE__, __FUNCTION__, __LINE__, nf );
209 
210  for( int i = 0; i < nf; ++i )
211  {
212  char const* cp = pp->GetFileFilter( i );
213 
214  if( cp )
215  addFilterString( wxString::FromUTF8Unchecked( cp ) );
216  }
217 
218  addExtensionMap( pp );
219 
220  // close the loaded library
221  pp->Close();
222  }
223  else
224  {
225  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [DEBUG] deleting plugin",
226  __FILE__, __FUNCTION__, __LINE__ );
227 
228  delete pp;
229  }
230 
231  ++sPL;
232  }
233 
234  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [DEBUG] plugins loaded",
235  __FILE__, __FUNCTION__, __LINE__ );
236 }
void Close(void) override
Function Close cleans up and closes/unloads the plugin.
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer pluginspath.
Definition: paths.cpp:217
bool Open(const wxString &aFullFileName) override
Function Open opens a plugin of the given class, performs version compatibility checks,...
Definition: pluginldr3D.cpp:60
void addFilterString(const wxString &aFilterString)
add an entry to the file filter list
void listPlugins(const wxString &aPath, std::list< wxString > &aPluginList)
list potential plugins
static wxString GetUserPlugins3DPath()
Gets the user path for 3d viewer plugin.
Definition: paths.cpp:64
std::list< KICAD_PLUGIN_LDR_3D * > m_Plugins
list of discovered plugins
char const * GetFileFilter(int aIndex)
#define MASK_3D_PLUGINMGR
Flag to enable 3D plugin manager debug tracing.
void checkPluginPath(const wxString &aPath, std::list< wxString > &aSearchList)
check the existence of a path and add it to the path search list
void addExtensionMap(KICAD_PLUGIN_LDR_3D *aPlugin)
add entries to the extension map

References addExtensionMap(), addFilterString(), checkPluginPath(), KICAD_PLUGIN_LDR_3D::Close(), KICAD_PLUGIN_LDR_3D::GetFileFilter(), KICAD_PLUGIN_LDR_3D::GetNFilters(), PATHS::GetStockPlugins3DPath(), PATHS::GetUserPlugins3DPath(), listPlugins(), m_Plugins, MASK_3D_PLUGINMGR, and KICAD_PLUGIN_LDR_3D::Open().

Referenced by S3D_PLUGIN_MANAGER().

Member Data Documentation

◆ m_ExtMap

std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* > S3D_PLUGIN_MANAGER::m_ExtMap
private

mapping of extensions to available plugins

Definition at line 95 of file 3d_plugin_manager.h.

Referenced by addExtensionMap(), Load3DModel(), and S3D_PLUGIN_MANAGER().

◆ m_FileFilters

std::list< wxString > S3D_PLUGIN_MANAGER::m_FileFilters
private

list of file filters

Definition at line 98 of file 3d_plugin_manager.h.

Referenced by addFilterString(), and S3D_PLUGIN_MANAGER().

◆ m_Plugins

std::list< KICAD_PLUGIN_LDR_3D* > S3D_PLUGIN_MANAGER::m_Plugins
private

list of discovered plugins

Definition at line 92 of file 3d_plugin_manager.h.

Referenced by CheckTag(), ClosePlugins(), loadPlugins(), and ~S3D_PLUGIN_MANAGER().


The documentation for this class was generated from the following files: