KiCad PCB EDA Suite
CACHE_WRAPPER Class Reference

#include <3d_cache_wrapper.h>

Inheritance diagram for CACHE_WRAPPER:
S3D_CACHE PROJECT::_ELEM

Public Member Functions

 CACHE_WRAPPER ()
 
virtual ~CACHE_WRAPPER ()
 
bool Set3DConfigDir (const wxString &aConfigDir)
 Function Set3DConfigDir Sets the configuration directory to be used by the model manager for storing 3D model manager configuration data and the model cache. More...
 
wxString Get3DConfigDir (bool createDefault=false)
 Function Get3DConfigDir returns the current 3D configuration directory on success, otherwise it returns wxEmptyString. More...
 
bool SetProjectDir (const wxString &aProjDir)
 Function SetProjectDir sets the current project's working directory; this affects the model search path. More...
 
void SetProgramBase (PGM_BASE *aBase)
 Function SetProgramBase sets the filename resolver's pointer to the application's PGM_BASE instance; the pointer is used to extract the local env vars. More...
 
wxString GetProjectDir (void)
 Function GetProjectDir returns the current project's working directory. More...
 
SCENEGRAPHLoad (const wxString &aModelFile)
 Function Load attempts to load the scene data for a model; it will consult the internal cache list and load from cache if possible before invoking the load() function of the available plugins. More...
 
S3D_FILENAME_RESOLVERGetResolver (void)
 
std::list< wxString > const * GetFileFilters (void) const
 Function GetFileFilters returns the list of file filters retrieved from the plugins; this will contain at least the default "All Files (*.*)|*.*". More...
 
void FlushCache (bool closePlugins=true)
 Function FlushCache frees all data in the cache and by default closes all plugins. More...
 
void ClosePlugins (void)
 Function ClosePlugins unloads plugins to free memory. More...
 
S3DMODELGetModel (const wxString &aModelFileName)
 Function GetModel attempts to load the scene data for a model and to translate it into an S3D_MODEL structure for display by a renderer. More...
 
wxString GetModelHash (const wxString &aModelFileName)
 

Detailed Description

Definition at line 30 of file 3d_cache_wrapper.h.

Constructor & Destructor Documentation

CACHE_WRAPPER::CACHE_WRAPPER ( )

Definition at line 31 of file 3d_cache_wrapper.cpp.

32 {
33  return;
34 }
CACHE_WRAPPER::~CACHE_WRAPPER ( )
virtual

Definition at line 37 of file 3d_cache_wrapper.cpp.

38 {
39  return;
40 }

Member Function Documentation

void S3D_CACHE::ClosePlugins ( void  )
inherited

Function ClosePlugins unloads plugins to free memory.

Definition at line 772 of file 3d_cache.cpp.

References S3D_PLUGIN_MANAGER::ClosePlugins(), and S3D_CACHE::m_Plugins.

Referenced by S3D_CACHE::FlushCache().

773 {
774  if( NULL != m_Plugins )
776 
777  return;
778 }
void ClosePlugins(void)
Function ClosePlugins iterates through all discovered plugins and closes them to reclaim memory...
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:63
void S3D_CACHE::FlushCache ( bool  closePlugins = true)
inherited

Function FlushCache frees all data in the cache and by default closes all plugins.

Definition at line 751 of file 3d_cache.cpp.

References S3D_CACHE::ClosePlugins(), S3D_CACHE::m_CacheList, and S3D_CACHE::m_CacheMap.

Referenced by S3D_CACHE::~S3D_CACHE().

752 {
753  std::list< S3D_CACHE_ENTRY* >::iterator sCL = m_CacheList.begin();
754  std::list< S3D_CACHE_ENTRY* >::iterator eCL = m_CacheList.end();
755 
756  while( sCL != eCL )
757  {
758  delete *sCL;
759  ++sCL;
760  }
761 
762  m_CacheList.clear();
763  m_CacheMap.clear();
764 
765  if( closePlugins )
766  ClosePlugins();
767 
768  return;
769 }
std::map< wxString, S3D_CACHE_ENTRY *, S3D::rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:57
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
Definition: 3d_cache.h:54
void ClosePlugins(void)
Function ClosePlugins unloads plugins to free memory.
Definition: 3d_cache.cpp:772
wxString S3D_CACHE::Get3DConfigDir ( bool  createDefault = false)
inherited

Function Get3DConfigDir returns the current 3D configuration directory on success, otherwise it returns wxEmptyString.

If the directory was not previously set via Set3DConfigDir() then a default is used which is based on kicad's configuration directory code as of September 2015.

Definition at line 642 of file 3d_cache.cpp.

References ExpandEnvVarSubstitutions(), S3D_CACHE::m_ConfigDir, MASK_3D_CACHE, and S3D_CACHE::Set3DConfigDir().

643 {
644  if( !m_ConfigDir.empty() || !createDefault )
645  return m_ConfigDir;
646 
647  // note: duplicated from common/common.cpp GetKicadConfigPath() to avoid
648  // code coupling; ideally the instantiating code should call
649  // Set3DConfigDir() to set the directory rather than relying on this
650  // directory remaining the same in future KiCad releases.
651  wxFileName cfgpath;
652 
653  // From the wxWidgets wxStandardPaths::GetUserConfigDir() help:
654  // Unix: ~ (the home directory)
655  // Windows: "C:\Documents and Settings\username\Application Data"
656  // Mac: ~/Library/Preferences
657  cfgpath.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
658 
659 #if !defined( __WINDOWS__ ) && !defined( __WXMAC__ )
660  wxString envstr = ExpandEnvVarSubstitutions( "${XDG_CONFIG_HOME}" );
661 
662  if( envstr.IsEmpty() || envstr == "${XDG_CONFIG_HOME}" )
663  {
664  // XDG_CONFIG_HOME is not set, so use the fallback
665  cfgpath.AppendDir( wxT( ".config" ) );
666  }
667  else
668  {
669  // Override the assignment above with XDG_CONFIG_HOME
670  cfgpath.AssignDir( envstr );
671  }
672 #endif
673 
674  cfgpath.AppendDir( wxT( "kicad" ) );
675  cfgpath.AppendDir( wxT( "3d" ) );
676 
677  if( !cfgpath.DirExists() )
678  {
679  cfgpath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
680  }
681 
682  if( !cfgpath.DirExists() )
683  {
684  std::ostringstream ostr;
685  wxString errmsg = "failed to create 3D configuration directory";
686  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
687  ostr << " * " << errmsg.ToUTF8();
688  wxLogTrace( MASK_3D_CACHE, "%s\n", ostr.str().c_str() );
689 
690  return wxT( "" );
691  }
692 
693  if( Set3DConfigDir( cfgpath.GetPath() ) )
694  return m_ConfigDir;
695 
696  return wxEmptyString;
697 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:51
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Function ExpandEnvVarSubstitutions replaces any environment variable references with their values...
Definition: common.cpp:254
bool Set3DConfigDir(const wxString &aConfigDir)
Function Set3DConfigDir Sets the configuration directory to be used by the model manager for storing ...
Definition: 3d_cache.cpp:542
wxString m_ConfigDir
base configuration path for 3D items
Definition: 3d_cache.h:72
std::list< wxString > const * S3D_CACHE::GetFileFilters ( void  ) const
inherited

Function GetFileFilters returns the list of file filters retrieved from the plugins; this will contain at least the default "All Files (*.*)|*.*".

Returns
a pointer to the filter list

Definition at line 745 of file 3d_cache.cpp.

References S3D_PLUGIN_MANAGER::GetFileFilters(), and S3D_CACHE::m_Plugins.

746 {
747  return m_Plugins->GetFileFilters();
748 }
std::list< wxString > const * GetFileFilters(void) const
Function GetFileFilters returns the list of file filters; this will contain at least the default "All...
S3D_PLUGIN_MANAGER * m_Plugins
plugin manager
Definition: 3d_cache.h:63
S3DMODEL * S3D_CACHE::GetModel ( const wxString &  aModelFileName)
inherited

Function GetModel attempts to load the scene data for a model and to translate it into an S3D_MODEL structure for display by a renderer.

Parameters
aModelFileNameis the full path to the model to be loaded
Returns
is a pointer to the render data or NULL if not available

Definition at line 781 of file 3d_cache.cpp.

References S3D::GetModel(), S3D_CACHE::load(), MASK_3D_CACHE, and S3D_CACHE_ENTRY::renderData.

Referenced by C3D_RENDER_OGL_LEGACY::load_3D_models(), C3D_RENDER_RAYTRACING::load_3D_models(), and C3D_MODEL_VIEWER::Set3DModel().

782 {
783  S3D_CACHE_ENTRY* cp = NULL;
784  SCENEGRAPH* sp = load( aModelFileName, &cp );
785 
786  if( !sp )
787  return NULL;
788 
789  if( !cp )
790  {
791  #ifdef DEBUG
792  do {
793  std::ostringstream ostr;
794  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
795  ostr << " * [BUG] model loaded with no associated S3D_CACHE_ENTRY";
796  wxLogTrace( MASK_3D_CACHE, "%s\n", ostr.str().c_str() );
797  } while( 0 );
798  #endif
799 
800  return NULL;
801  }
802 
803  if( cp->renderData )
804  return cp->renderData;
805 
806  S3DMODEL* mp = S3D::GetModel( sp );
807  cp->renderData = mp;
808 
809  return mp;
810 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:51
SCENEGRAPH * load(const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
Definition: 3d_cache.cpp:203
S3DMODEL * renderData
Definition: 3d_cache.cpp:128
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
Definition: 3d_cache.cpp:108
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms) ...
Definition: ifsg_api.cpp:478
wxString S3D_CACHE::GetModelHash ( const wxString &  aModelFileName)
inherited

Definition at line 813 of file 3d_cache.cpp.

References S3D_CACHE::checkCache(), S3D_CACHE_ENTRY::GetCacheBaseName(), S3D_CACHE::m_CacheMap, S3D_CACHE::m_FNResolver, and S3D_FILENAME_RESOLVER::ResolvePath().

814 {
815  wxString full3Dpath = m_FNResolver->ResolvePath( aModelFileName );
816 
817  if( full3Dpath.empty() || !wxFileName::FileExists( full3Dpath ) )
818  return wxEmptyString;
819 
820  // check cache if file is already loaded
821  std::map< wxString, S3D_CACHE_ENTRY*, S3D::rsort_wxString >::iterator mi;
822  mi = m_CacheMap.find( full3Dpath );
823 
824  if( mi != m_CacheMap.end() )
825  return mi->second->GetCacheBaseName();
826 
827  // a cache item does not exist; search the Filename->Cachename map
828  S3D_CACHE_ENTRY* cp = NULL;
829  checkCache( full3Dpath, &cp );
830 
831  if( NULL != cp )
832  return cp->GetCacheBaseName();
833 
834  return wxEmptyString;
835 }
const wxString GetCacheBaseName(void)
Definition: 3d_cache.cpp:171
std::map< wxString, S3D_CACHE_ENTRY *, S3D::rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:57
SCENEGRAPH * checkCache(const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=NULL)
Function checkCache searches the cache list for the given filename and retrieves the cache data; a ca...
Definition: 3d_cache.cpp:277
wxString ResolvePath(const wxString &aFileName)
Function ResolvePath determines the full path of the given file name.
S3D_FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:60
Definition: 3d_cache.cpp:108
wxString S3D_CACHE::GetProjectDir ( void  )
inherited

Function GetProjectDir returns the current project's working directory.

Definition at line 733 of file 3d_cache.cpp.

References S3D_FILENAME_RESOLVER::GetProjectDir(), and S3D_CACHE::m_FNResolver.

734 {
735  return m_FNResolver->GetProjectDir();
736 }
S3D_FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:60
S3D_FILENAME_RESOLVER * S3D_CACHE::GetResolver ( void  )
inherited

Definition at line 739 of file 3d_cache.cpp.

References S3D_CACHE::m_FNResolver.

Referenced by export_vrml_module(), and PANEL_PREV_3D::PANEL_PREV_3D().

740 {
741  return m_FNResolver;
742 }
S3D_FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:60
SCENEGRAPH * S3D_CACHE::Load ( const wxString &  aModelFile)
inherited

Function Load attempts to load the scene data for a model; it will consult the internal cache list and load from cache if possible before invoking the load() function of the available plugins.

Parameters
aModelFile[in] is the partial or full path to the model to be loaded
Returns
true if the model was successfully loaded, otherwise false. The model may fail to load if, for example, the plugin does not support rendering of the 3D model.

Definition at line 271 of file 3d_cache.cpp.

References S3D_CACHE::load().

Referenced by export_vrml_module().

272 {
273  return load( aModelFile );
274 }
SCENEGRAPH * load(const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
Definition: 3d_cache.cpp:203
bool S3D_CACHE::Set3DConfigDir ( const wxString &  aConfigDir)
inherited

Function Set3DConfigDir Sets the configuration directory to be used by the model manager for storing 3D model manager configuration data and the model cache.

The config directory may only be set once in the lifetime of the object.

Parameters
aConfigDiris the configuration directory to use for 3D model manager data
Returns
true on success

Definition at line 542 of file 3d_cache.cpp.

References ExpandEnvVarSubstitutions(), S3D_CACHE::m_CacheDir, S3D_CACHE::m_ConfigDir, S3D_CACHE::m_FNResolver, MASK_3D_CACHE, and S3D_FILENAME_RESOLVER::Set3DConfigDir().

Referenced by S3D_CACHE::Get3DConfigDir().

543 {
544  if( !m_ConfigDir.empty() )
545  return false;
546 
547  wxFileName cfgdir;
548 
549  if( aConfigDir.StartsWith( "${" ) || aConfigDir.StartsWith( "$(" ) )
550  cfgdir.Assign( ExpandEnvVarSubstitutions( aConfigDir ), "" );
551  else
552  cfgdir.Assign( aConfigDir, "" );
553 
554  cfgdir.Normalize();
555 
556  if( !cfgdir.DirExists() )
557  {
558  cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
559 
560  if( !cfgdir.DirExists() )
561  {
562  std::ostringstream ostr;
563  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
564  wxString errmsg = _( "failed to create 3D configuration directory" );
565  ostr << " * " << errmsg.ToUTF8() << "\n";
566  errmsg = _( "config directory" );
567  ostr << " * " << errmsg.ToUTF8() << " '";
568  ostr << cfgdir.GetPath().ToUTF8() << "'";
569  wxLogTrace( MASK_3D_CACHE, "%s\n", ostr.str().c_str() );
570 
571  return false;
572  }
573  }
574 
575  m_ConfigDir = cfgdir.GetPath();
576 
577  // inform the file resolver of the config directory
579  {
580  #ifdef DEBUG
581  do {
582  std::ostringstream ostr;
583  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
584  ostr << " * could not set 3D Config Directory on filename resolver\n";
585  ostr << " * config directory: '" << m_ConfigDir.ToUTF8() << "'";
586  wxLogTrace( MASK_3D_CACHE, "%s\n", ostr.str().c_str() );
587  } while( 0 );
588  #endif
589  }
590 
591  // 3D cache data must go to a user's cache directory;
592  // unfortunately wxWidgets doesn't seem to provide
593  // functions to retrieve such a directory.
594  //
595  // 1. OSX: ~/Library/Caches/kicad/3d/
596  // 2. Linux: ${XDG_CACHE_HOME}/kicad/3d ~/.cache/kicad/3d/
597  // 3. MSWin: AppData\Local\kicad\3d
598  wxString cacheDir;
599 
600  #if defined(_WIN32)
601  wxStandardPaths::Get().UseAppInfo( wxStandardPaths::AppInfo_None );
602  cacheDir = wxStandardPaths::Get().GetUserLocalDataDir();
603  cacheDir.append( "\\kicad\\3d" );
604  #elif defined(__APPLE)
605  cacheDir = "${HOME}/Library/Caches/kicad/3d";
606  #else // assume Linux
607  cacheDir = ExpandEnvVarSubstitutions( "${XDG_CACHE_HOME}" );
608 
609  if( cacheDir.empty() || cacheDir == "${XDG_CACHE_HOME}" )
610  cacheDir = "${HOME}/.cache";
611 
612  cacheDir.append( "/kicad/3d" );
613  #endif
614 
615  cacheDir = ExpandEnvVarSubstitutions( cacheDir );
616  cfgdir.Assign( cacheDir, "" );
617 
618  if( !cfgdir.DirExists() )
619  {
620  cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
621 
622  if( !cfgdir.DirExists() )
623  {
624  std::ostringstream ostr;
625  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
626  wxString errmsg = "failed to create 3D cache directory";
627  ostr << " * " << errmsg.ToUTF8() << "\n";
628  errmsg = "cache directory";
629  ostr << " * " << errmsg.ToUTF8() << " '";
630  ostr << cfgdir.GetPath().ToUTF8() << "'";
631  wxLogTrace( MASK_3D_CACHE, "%s\n", ostr.str().c_str() );
632 
633  return false;
634  }
635  }
636 
637  m_CacheDir = cfgdir.GetPathWithSep();
638  return true;
639 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:51
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Function ExpandEnvVarSubstitutions replaces any environment variable references with their values...
Definition: common.cpp:254
wxString m_CacheDir
3D cache directory
Definition: 3d_cache.h:69
wxString m_ConfigDir
base configuration path for 3D items
Definition: 3d_cache.h:72
S3D_FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:60
bool Set3DConfigDir(const wxString &aConfigDir)
Function Set3DConfigDir sets the user's configuration directory for 3D models.
void S3D_CACHE::SetProgramBase ( PGM_BASE aBase)
inherited

Function SetProgramBase sets the filename resolver's pointer to the application's PGM_BASE instance; the pointer is used to extract the local env vars.

Definition at line 726 of file 3d_cache.cpp.

References S3D_CACHE::m_FNResolver, and S3D_FILENAME_RESOLVER::SetProgramBase().

727 {
728  m_FNResolver->SetProgramBase( aBase );
729  return;
730 }
void SetProgramBase(PGM_BASE *aBase)
Function SetProgramBase sets a pointer to the application's PGM_BASE instance; the pointer is used to...
S3D_FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:60
bool S3D_CACHE::SetProjectDir ( const wxString &  aProjDir)
inherited

Function SetProjectDir sets the current project's working directory; this affects the model search path.

Definition at line 700 of file 3d_cache.cpp.

References S3D_CACHE::m_CacheList, S3D_CACHE::m_CacheMap, S3D_CACHE::m_FNResolver, and S3D_FILENAME_RESOLVER::SetProjectDir().

701 {
702  bool hasChanged = false;
703 
704  if( m_FNResolver->SetProjectDir( aProjDir, &hasChanged ) && hasChanged )
705  {
706  m_CacheMap.clear();
707 
708  std::list< S3D_CACHE_ENTRY* >::iterator sL = m_CacheList.begin();
709  std::list< S3D_CACHE_ENTRY* >::iterator eL = m_CacheList.end();
710 
711  while( sL != eL )
712  {
713  delete *sL;
714  ++sL;
715  }
716 
717  m_CacheList.clear();
718 
719  return true;
720  }
721 
722  return false;
723 }
std::map< wxString, S3D_CACHE_ENTRY *, S3D::rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:57
bool SetProjectDir(const wxString &aProjDir, bool *flgChanged=NULL)
Function SetProjectDir sets the current KiCad project directory as the first entry in the model path ...
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
Definition: 3d_cache.h:54
S3D_FILENAME_RESOLVER * m_FNResolver
object to resolve file names
Definition: 3d_cache.h:60

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