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 55 of file 3d_plugin_manager.cpp.

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

106 {
107  std::list< KICAD_PLUGIN_LDR_3D* >::iterator sP = m_Plugins.begin();
108  std::list< KICAD_PLUGIN_LDR_3D* >::iterator eP = m_Plugins.end();
109 
110  while( sP != eP )
111  {
112  (*sP)->Close();
113  delete *sP;
114  ++sP;
115  }
116 
117  m_Plugins.clear();
118 }
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 372 of file 3d_plugin_manager.cpp.

373 {
374  // add entries to the extension map
375  if( nullptr == aPlugin )
376  return;
377 
378  int nExt = aPlugin->GetNExtensions();
379 
380  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [INFO] adding %d extensions",
381  __FILE__, __FUNCTION__, __LINE__, nExt );
382 
383  for( int i = 0; i < nExt; ++i )
384  {
385  char const* cp = aPlugin->GetModelExtension( i );
386  wxString ws;
387 
388  if( cp )
389  ws = wxString::FromUTF8Unchecked( cp );
390 
391  if( !ws.empty() )
392  {
393  m_ExtMap.insert( std::pair< const wxString, KICAD_PLUGIN_LDR_3D* >( ws, aPlugin ) );
394  }
395 
396  }
397 }
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 350 of file 3d_plugin_manager.cpp.

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

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

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

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

444 {
445  std::list< KICAD_PLUGIN_LDR_3D* >::iterator sP = m_Plugins.begin();
446  std::list< KICAD_PLUGIN_LDR_3D* >::iterator eP = m_Plugins.end();
447 
448  wxLogTrace( MASK_3D_PLUGINMGR, "%s:%s:%d * [INFO] closing %d extensions",
449  __FILE__, __FUNCTION__, __LINE__, static_cast<int>( m_Plugins.size() ) );
450 
451  while( sP != eP )
452  {
453  (*sP)->Close();
454  ++sP;
455  }
456 }
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 400 of file 3d_plugin_manager.cpp.

401 {
402  return &m_FileFilters;
403 }
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 238 of file 3d_plugin_manager.cpp.

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

407 {
408  wxFileName raw( aFileName );
409  wxString ext = raw.GetExt();
410 
411 #ifdef _WIN32
412  // note: plugins only have a lowercase filter within Windows; including an uppercase
413  // filter will result in duplicate file entries and should be avoided.
414  ext.LowerCase();
415 #endif
416 
417  std::pair < std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::iterator,
418  std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::iterator > items;
419 
420  items = m_ExtMap.equal_range( ext );
421  std::multimap< const wxString, KICAD_PLUGIN_LDR_3D* >::iterator sL = items.first;
422 
423  while( sL != items.second )
424  {
425  if( sL->second->CanRender() )
426  {
427  SCENEGRAPH* sp = sL->second->Load( aFileName.ToUTF8() );
428 
429  if( nullptr != sp )
430  {
431  sL->second->GetPluginInfo( aPluginInfo );
432  return sp;
433  }
434  }
435 
436  ++sL;
437  }
438 
439  return nullptr;
440 }
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 121 of file 3d_plugin_manager.cpp.

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