KiCad PCB EDA Suite
S3D_CACHE Class Reference

S3D_CACHE. More...

#include <3d_cache.h>

Inheritance diagram for S3D_CACHE:
PROJECT::_ELEM

Public Member Functions

 S3D_CACHE ()
 
virtual ~S3D_CACHE ()
 
KICAD_T Type () noexcept override
 
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...
 
bool SetProject (PROJECT *aProject)
 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...
 
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...
 
FILENAME_RESOLVERGetResolver () noexcept
 
std::list< wxString > const * GetFileFilters () 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 ()
 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...
 

Private Member Functions

SCENEGRAPHcheckCache (const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=NULL)
 base configuration path for 3D items More...
 
bool getSHA1 (const wxString &aFileName, unsigned char *aSHA1Sum)
 Function getSHA1 calculates the SHA1 hash of the given file. More...
 
bool loadCacheData (S3D_CACHE_ENTRY *aCacheItem)
 
bool saveCacheData (S3D_CACHE_ENTRY *aCacheItem)
 
SCENEGRAPHload (const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
 

Private Attributes

std::list< S3D_CACHE_ENTRY * > m_CacheList
 cache entries More...
 
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxStringm_CacheMap
 mapping of file names to cache names and data More...
 
FILENAME_RESOLVERm_FNResolver
 
S3D_PLUGIN_MANAGERm_Plugins
 
PROJECTm_project
 
wxString m_CacheDir
 
wxString m_ConfigDir
 

Detailed Description

S3D_CACHE.

Cache for storing the 3D shapes. This cache is able to be stored as a project element (since it inherits from PROJECT::_ELEM).

Definition at line 54 of file 3d_cache.h.

Constructor & Destructor Documentation

◆ S3D_CACHE()

S3D_CACHE::S3D_CACHE ( )

Definition at line 188 of file 3d_cache.cpp.

189 {
191  m_project = nullptr;
193 }
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:63
PROJECT * m_project
Definition: 3d_cache.h:67
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:65

References m_FNResolver, m_Plugins, and m_project.

◆ ~S3D_CACHE()

S3D_CACHE::~S3D_CACHE ( )
virtual

Definition at line 196 of file 3d_cache.cpp.

197 {
198  FlushCache();
199 
200  delete m_FNResolver;
201  delete m_Plugins;
202 }
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:63
void FlushCache(bool closePlugins=true)
Function FlushCache frees all data in the cache and by default closes all plugins.
Definition: 3d_cache.cpp:628
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:65

References FlushCache(), m_FNResolver, and m_Plugins.

Member Function Documentation

◆ checkCache()

SCENEGRAPH * S3D_CACHE::checkCache ( const wxString &  aFileName,
S3D_CACHE_ENTRY **  aCachePtr = NULL 
)
private

base configuration path for 3D items

Find or create cache entry for file name

Searches the cache list for the given filename and retrieves the cache data; a cache entry is created if one does not already exist.

Parameters
[in]aFileNamefile name (full or partial path)
[out]aCachePtroptional return address for cache entry pointer
Returns
SCENEGRAPH object associated with file name
Return values
NULLon error

Definition at line 280 of file 3d_cache.cpp.

281 {
282  if( aCachePtr )
283  *aCachePtr = NULL;
284 
285  unsigned char sha1sum[20];
286 
287  if( !getSHA1( aFileName, sha1sum ) || m_CacheDir.empty() )
288  {
289  // just in case we can't get a hash digest (for example, on access issues)
290  // or we do not have a configured cache file directory, we create an
291  // entry to prevent further attempts at loading the file
293  m_CacheList.push_back( ep );
294  wxFileName fname( aFileName );
295  ep->modTime = fname.GetModificationTime();
296 
297  if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* >
298  ( aFileName, ep ) ).second == false )
299  {
300  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [BUG] duplicate entry in map file; key = '%s'",
301  __FILE__, __FUNCTION__, __LINE__, aFileName );
302 
303  m_CacheList.pop_back();
304  delete ep;
305  }
306  else
307  {
308  if( aCachePtr )
309  *aCachePtr = ep;
310 
311  }
312 
313  return NULL;
314  }
315 
317  m_CacheList.push_back( ep );
318  wxFileName fname( aFileName );
319  ep->modTime = fname.GetModificationTime();
320 
321  if( m_CacheMap.insert( std::pair< wxString, S3D_CACHE_ENTRY* >
322  ( aFileName, ep ) ).second == false )
323  {
324  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [BUG] duplicate entry in map file; key = '%s'",
325  __FILE__, __FUNCTION__, __LINE__, aFileName );
326 
327  m_CacheList.pop_back();
328  delete ep;
329  return NULL;
330  }
331 
332  if( aCachePtr )
333  *aCachePtr = ep;
334 
335  ep->SetSHA1( sha1sum );
336 
337  wxString bname = ep->GetCacheBaseName();
338  wxString cachename = m_CacheDir + bname + wxT( ".3dc" );
339 
340  if( wxFileName::FileExists( cachename ) && loadCacheData( ep ) )
341  return ep->sceneData;
342 
343  ep->sceneData = m_Plugins->Load3DModel( aFileName, ep->pluginInfo );
344 
345  if( NULL != ep->sceneData )
346  saveCacheData( ep );
347 
348  return ep->sceneData;
349 }
std::string pluginInfo
Definition: 3d_cache.cpp:142
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
bool loadCacheData(S3D_CACHE_ENTRY *aCacheItem)
Definition: 3d_cache.cpp:408
SCENEGRAPH * Load3DModel(const wxString &aFileName, std::string &aPluginInfo)
const wxString GetCacheBaseName()
Definition: 3d_cache.cpp:179
bool saveCacheData(S3D_CACHE_ENTRY *aCacheItem)
Definition: 3d_cache.cpp:449
wxDateTime modTime
Definition: 3d_cache.cpp:140
#define NULL
wxString m_CacheDir
Definition: 3d_cache.h:68
void SetSHA1(const unsigned char *aSHA1Sum)
Definition: 3d_cache.cpp:165
bool getSHA1(const wxString &aFileName, unsigned char *aSHA1Sum)
Function getSHA1 calculates the SHA1 hash of the given file.
Definition: 3d_cache.cpp:352
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
Definition: 3d_cache.h:58
Definition: 3d_cache.cpp:124
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:65

References S3D_CACHE_ENTRY::GetCacheBaseName(), getSHA1(), S3D_PLUGIN_MANAGER::Load3DModel(), loadCacheData(), m_CacheDir, m_CacheList, m_CacheMap, m_Plugins, MASK_3D_CACHE, S3D_CACHE_ENTRY::modTime, NULL, S3D_CACHE_ENTRY::pluginInfo, saveCacheData(), S3D_CACHE_ENTRY::sceneData, and S3D_CACHE_ENTRY::SetSHA1().

Referenced by load().

◆ ClosePlugins()

void S3D_CACHE::ClosePlugins ( )

Function ClosePlugins unloads plugins to free memory.

Definition at line 647 of file 3d_cache.cpp.

648 {
649  if( m_Plugins )
651 }
void ClosePlugins(void)
Function ClosePlugins iterates through all discovered plugins and closes them to reclaim memory.
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:65

References S3D_PLUGIN_MANAGER::ClosePlugins(), and m_Plugins.

Referenced by FlushCache().

◆ FlushCache()

void S3D_CACHE::FlushCache ( bool  closePlugins = true)

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

Definition at line 628 of file 3d_cache.cpp.

629 {
630  std::list< S3D_CACHE_ENTRY* >::iterator sCL = m_CacheList.begin();
631  std::list< S3D_CACHE_ENTRY* >::iterator eCL = m_CacheList.end();
632 
633  while( sCL != eCL )
634  {
635  delete *sCL;
636  ++sCL;
637  }
638 
639  m_CacheList.clear();
640  m_CacheMap.clear();
641 
642  if( closePlugins )
643  ClosePlugins();
644 }
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
void ClosePlugins()
Function ClosePlugins unloads plugins to free memory.
Definition: 3d_cache.cpp:647
std::list< S3D_CACHE_ENTRY * > m_CacheList
cache entries
Definition: 3d_cache.h:58

References ClosePlugins(), m_CacheList, and m_CacheMap.

Referenced by ~S3D_CACHE().

◆ GetFileFilters()

std::list< wxString > const * S3D_CACHE::GetFileFilters ( ) const

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 622 of file 3d_cache.cpp.

623 {
624  return m_Plugins->GetFileFilters();
625 }
std::list< wxString > const * GetFileFilters(void) const noexcept
Function GetFileFilters returns the list of file filters; this will contain at least the default "All...
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:65

References S3D_PLUGIN_MANAGER::GetFileFilters(), and m_Plugins.

Referenced by DLG_SELECT_3DMODEL::DLG_SELECT_3DMODEL().

◆ GetModel()

S3DMODEL * S3D_CACHE::GetModel ( 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.

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 654 of file 3d_cache.cpp.

655 {
656  S3D_CACHE_ENTRY* cp = NULL;
657  SCENEGRAPH* sp = load( aModelFileName, &cp );
658 
659  if( !sp )
660  return NULL;
661 
662  if( !cp )
663  {
664  wxLogTrace( MASK_3D_CACHE,
665  "%s:%s:%d\n * [BUG] model loaded with no associated S3D_CACHE_ENTRY",
666  __FILE__, __FUNCTION__, __LINE__ );
667 
668  return NULL;
669  }
670 
671  if( cp->renderData )
672  return cp->renderData;
673 
674  S3DMODEL* mp = S3D::GetModel( sp );
675  cp->renderData = mp;
676 
677  return mp;
678 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
#define NULL
SCENEGRAPH * load(const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
Definition: 3d_cache.cpp:205
S3DMODEL * renderData
Definition: 3d_cache.cpp:144
Store the a model based on meshes and materials.
Definition: c3dmodel.h:90
Definition: 3d_cache.cpp:124
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms)
Definition: ifsg_api.cpp:471

References S3D::GetModel(), load(), MASK_3D_CACHE, NULL, 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().

◆ GetResolver()

FILENAME_RESOLVER * S3D_CACHE::GetResolver ( )
noexcept

Definition at line 616 of file 3d_cache.cpp.

617 {
618  return m_FNResolver;
619 }
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:63

References m_FNResolver.

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

◆ getSHA1()

bool S3D_CACHE::getSHA1 ( const wxString &  aFileName,
unsigned char *  aSHA1Sum 
)
private

Function getSHA1 calculates the SHA1 hash of the given file.

Parameters
[in]aFileNamefile name (full path)
[out]aSHA1Suma 20 byte character array to hold the SHA1 hash
Return values
truesuccess
falsefailure

Definition at line 352 of file 3d_cache.cpp.

353 {
354  if( aFileName.empty() )
355  {
356  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [BUG] empty filename",
357  __FILE__, __FUNCTION__, __LINE__ );
358 
359  return false;
360  }
361 
362  if( NULL == aSHA1Sum )
363  {
364  wxLogTrace( MASK_3D_CACHE, "%s\n * [BUG] NULL pointer passed for aMD5Sum",
365  __FILE__, __FUNCTION__, __LINE__ );
366 
367  return false;
368  }
369 
370  #ifdef _WIN32
371  FILE* fp = _wfopen( aFileName.wc_str(), L"rb" );
372  #else
373  FILE* fp = fopen( aFileName.ToUTF8(), "rb" );
374  #endif
375 
376  if( NULL == fp )
377  return false;
378 
379  boost::uuids::detail::sha1 dblock;
380  unsigned char block[4096];
381  size_t bsize = 0;
382 
383  while( ( bsize = fread( &block, 1, 4096, fp ) ) > 0 )
384  dblock.process_bytes( block, bsize );
385 
386  fclose( fp );
387  unsigned int digest[5];
388  dblock.get_digest( digest );
389 
390  // ensure MSB order
391  for( int i = 0; i < 5; ++i )
392  {
393  int idx = i << 2;
394  unsigned int tmp = digest[i];
395  aSHA1Sum[idx+3] = tmp & 0xff;
396  tmp >>= 8;
397  aSHA1Sum[idx+2] = tmp & 0xff;
398  tmp >>= 8;
399  aSHA1Sum[idx+1] = tmp & 0xff;
400  tmp >>= 8;
401  aSHA1Sum[idx] = tmp & 0xff;
402  }
403 
404  return true;
405 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
#define NULL

References MASK_3D_CACHE, and NULL.

Referenced by checkCache(), and load().

◆ load()

SCENEGRAPH * S3D_CACHE::load ( const wxString &  aModelFile,
S3D_CACHE_ENTRY **  aCachePtr = NULL 
)
private

Definition at line 205 of file 3d_cache.cpp.

206 {
207  if( aCachePtr )
208  *aCachePtr = NULL;
209 
210  wxString full3Dpath = m_FNResolver->ResolvePath( aModelFile );
211 
212  if( full3Dpath.empty() )
213  {
214  // the model cannot be found; we cannot proceed
215  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * [3D model] could not find model '%s'\n",
216  __FILE__, __FUNCTION__, __LINE__, aModelFile );
217  return NULL;
218  }
219 
220  // check cache if file is already loaded
221  std::lock_guard<std::mutex> lock( mutex3D_cache );
222 
223  std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString >::iterator mi;
224  mi = m_CacheMap.find( full3Dpath );
225 
226  if( mi != m_CacheMap.end() )
227  {
228  wxFileName fname( full3Dpath );
229 
230  if( fname.FileExists() ) // Only check if file exists. If not, it will
231  { // use the same model in cache.
232  bool reload = false;
233  wxDateTime fmdate = fname.GetModificationTime();
234 
235  if( fmdate != mi->second->modTime )
236  {
237  unsigned char hashSum[20];
238  getSHA1( full3Dpath, hashSum );
239  mi->second->modTime = fmdate;
240 
241  if( !isSHA1Same( hashSum, mi->second->sha1sum ) )
242  {
243  mi->second->SetSHA1( hashSum );
244  reload = true;
245  }
246  }
247 
248  if( reload )
249  {
250  if( NULL != mi->second->sceneData )
251  {
252  S3D::DestroyNode( mi->second->sceneData );
253  mi->second->sceneData = NULL;
254  }
255 
256  if( NULL != mi->second->renderData )
257  S3D::Destroy3DModel( &mi->second->renderData );
258 
259  mi->second->sceneData = m_Plugins->Load3DModel( full3Dpath, mi->second->pluginInfo );
260  }
261  }
262 
263  if( NULL != aCachePtr )
264  *aCachePtr = mi->second;
265 
266  return mi->second->sceneData;
267  }
268 
269  // a cache item does not exist; search the Filename->Cachename map
270  return checkCache( full3Dpath, aCachePtr );
271 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
static std::mutex mutex3D_cache
Definition: 3d_cache.cpp:65
SGLIB_API void Destroy3DModel(S3DMODEL **aModel)
Function Destroy3DModel frees memory used by an S3DMODEL structure and sets the pointer to the struct...
Definition: ifsg_api.cpp:536
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
SCENEGRAPH * Load3DModel(const wxString &aFileName, std::string &aPluginInfo)
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:63
SGLIB_API void DestroyNode(SGNODE *aNode) noexcept
Function DestroyNode deletes the given SG* class node.
Definition: ifsg_api.cpp:210
#define NULL
bool getSHA1(const wxString &aFileName, unsigned char *aSHA1Sum)
Function getSHA1 calculates the SHA1 hash of the given file.
Definition: 3d_cache.cpp:352
SCENEGRAPH * checkCache(const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=NULL)
base configuration path for 3D items
Definition: 3d_cache.cpp:280
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
wxString ResolvePath(const wxString &aFileName)
Function ResolvePath determines the full path of the given file name.
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:65
static bool isSHA1Same(const unsigned char *shaA, const unsigned char *shaB) noexcept
Definition: 3d_cache.cpp:69

References checkCache(), S3D::Destroy3DModel(), S3D::DestroyNode(), getSHA1(), isSHA1Same(), S3D_PLUGIN_MANAGER::Load3DModel(), m_CacheMap, m_FNResolver, m_Plugins, MASK_3D_CACHE, mutex3D_cache, NULL, FILENAME_RESOLVER::ResolvePath(), and S3D_CACHE_ENTRY::sceneData.

Referenced by GetModel(), and Load().

◆ Load()

SCENEGRAPH * S3D_CACHE::Load ( 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.

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 274 of file 3d_cache.cpp.

275 {
276  return load( aModelFile );
277 }
SCENEGRAPH * load(const wxString &aModelFile, S3D_CACHE_ENTRY **aCachePtr=NULL)
Definition: 3d_cache.cpp:205

References load().

Referenced by export_vrml_module().

◆ loadCacheData()

bool S3D_CACHE::loadCacheData ( S3D_CACHE_ENTRY aCacheItem)
private

Definition at line 408 of file 3d_cache.cpp.

409 {
410  wxString bname = aCacheItem->GetCacheBaseName();
411 
412  if( bname.empty() )
413  {
414  wxLogTrace( MASK_3D_CACHE,
415  " * [3D model] cannot load cached model; no file hash available" );
416 
417  return false;
418  }
419 
420  if( m_CacheDir.empty() )
421  {
422  wxLogTrace( MASK_3D_CACHE,
423  " * [3D model] cannot load cached model; config directory unknown" );
424 
425  return false;
426  }
427 
428  wxString fname = m_CacheDir + bname + wxT( ".3dc" );
429 
430  if( !wxFileName::FileExists( fname ) )
431  {
432  wxString errmsg = "cannot open file";
433  wxLogTrace( MASK_3D_CACHE, " * [3D model] %s '%s'", errmsg.GetData(), fname.GetData() );
434  return false;
435  }
436 
437  if( NULL != aCacheItem->sceneData )
438  S3D::DestroyNode( (SGNODE*) aCacheItem->sceneData );
439 
440  aCacheItem->sceneData = (SCENEGRAPH*)S3D::ReadCache( fname.ToUTF8(), m_Plugins, checkTag );
441 
442  if( NULL == aCacheItem->sceneData )
443  return false;
444 
445  return true;
446 }
SGLIB_API SGNODE * ReadCache(const char *aFileName, void *aPluginMgr, bool(*aTagCheck)(const char *, void *))
Function ReadCache reads a binary cache file and creates an SGNODE tree.
Definition: ifsg_api.cpp:321
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
const wxString GetCacheBaseName()
Definition: 3d_cache.cpp:179
SGLIB_API void DestroyNode(SGNODE *aNode) noexcept
Function DestroyNode deletes the given SG* class node.
Definition: ifsg_api.cpp:210
#define NULL
wxString m_CacheDir
Definition: 3d_cache.h:68
static bool checkTag(const char *aTag, void *aPluginMgrPtr)
Definition: 3d_cache.cpp:79
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:65

References checkTag(), S3D::DestroyNode(), S3D_CACHE_ENTRY::GetCacheBaseName(), m_CacheDir, m_Plugins, MASK_3D_CACHE, NULL, S3D::ReadCache(), and S3D_CACHE_ENTRY::sceneData.

Referenced by checkCache().

◆ saveCacheData()

bool S3D_CACHE::saveCacheData ( S3D_CACHE_ENTRY aCacheItem)
private

Definition at line 449 of file 3d_cache.cpp.

450 {
451  if( NULL == aCacheItem )
452  {
453  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * NULL passed for aCacheItem",
454  __FILE__, __FUNCTION__, __LINE__ );
455 
456  return false;
457  }
458 
459  if( NULL == aCacheItem->sceneData )
460  {
461  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * aCacheItem has no valid scene data",
462  __FILE__, __FUNCTION__, __LINE__ );
463 
464  return false;
465  }
466 
467  wxString bname = aCacheItem->GetCacheBaseName();
468 
469  if( bname.empty() )
470  {
471  wxLogTrace( MASK_3D_CACHE,
472  " * [3D model] cannot load cached model; no file hash available" );
473 
474  return false;
475  }
476 
477  if( m_CacheDir.empty() )
478  {
479  wxLogTrace( MASK_3D_CACHE,
480  " * [3D model] cannot load cached model; config directory unknown" );
481 
482  return false;
483  }
484 
485  wxString fname = m_CacheDir + bname + wxT( ".3dc" );
486 
487  if( wxFileName::Exists( fname ) )
488  {
489  if( !wxFileName::FileExists( fname ) )
490  {
491  wxLogTrace( MASK_3D_CACHE, " * [3D model] path exists but is not a regular file '%s'",
492  fname );
493 
494  return false;
495  }
496  }
497 
498  return S3D::WriteCache( fname.ToUTF8(), true, (SGNODE*)aCacheItem->sceneData,
499  aCacheItem->pluginInfo.c_str() );
500 }
std::string pluginInfo
Definition: 3d_cache.cpp:142
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
const wxString GetCacheBaseName()
Definition: 3d_cache.cpp:179
#define NULL
wxString m_CacheDir
Definition: 3d_cache.h:68
SCENEGRAPH * sceneData
Definition: 3d_cache.cpp:143
SGLIB_API bool WriteCache(const char *aFileName, bool overwrite, SGNODE *aNode, const char *aPluginInfo)
Function WriteCache writes the SGNODE tree to a binary cache file.
Definition: ifsg_api.cpp:232

References S3D_CACHE_ENTRY::GetCacheBaseName(), m_CacheDir, MASK_3D_CACHE, NULL, S3D_CACHE_ENTRY::pluginInfo, S3D_CACHE_ENTRY::sceneData, and S3D::WriteCache().

Referenced by checkCache().

◆ Set3DConfigDir()

bool S3D_CACHE::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.

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 503 of file 3d_cache.cpp.

504 {
505  if( !m_ConfigDir.empty() )
506  return false;
507 
508  wxFileName cfgdir( ExpandEnvVarSubstitutions( aConfigDir, m_project ), "" );
509 
510  cfgdir.Normalize();
511 
512  if( !cfgdir.DirExists() )
513  {
514  cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
515 
516  if( !cfgdir.DirExists() )
517  {
518  wxLogTrace( MASK_3D_CACHE,
519  "%s:%s:%d\n * failed to create 3D configuration directory '%s'",
520  __FILE__, __FUNCTION__, __LINE__, cfgdir.GetPath() );
521 
522  return false;
523  }
524  }
525 
526  m_ConfigDir = cfgdir.GetPath();
527 
528  // inform the file resolver of the config directory
530  {
531  wxLogTrace( MASK_3D_CACHE,
532  "%s:%s:%d\n * could not set 3D Config Directory on filename resolver\n"
533  " * config directory: '%s'",
534  __FILE__, __FUNCTION__, __LINE__, m_ConfigDir );
535  }
536 
537  // 3D cache data must go to a user's cache directory;
538  // unfortunately wxWidgets doesn't seem to provide
539  // functions to retrieve such a directory.
540  //
541  // 1. OSX: ~/Library/Caches/kicad/3d/
542  // 2. Linux: ${XDG_CACHE_HOME}/kicad/3d ~/.cache/kicad/3d/
543  // 3. MSWin: AppData\Local\kicad\3d
544  wxString cacheDir;
545 
546 #if defined(_WIN32)
547  wxStandardPaths::Get().UseAppInfo( wxStandardPaths::AppInfo_None );
548  cacheDir = wxStandardPaths::Get().GetUserLocalDataDir();
549  cacheDir.append( "\\kicad\\3d" );
550 #elif defined(__APPLE)
551  cacheDir = "${HOME}/Library/Caches/kicad/3d";
552 #else // assume Linux
553  cacheDir = ExpandEnvVarSubstitutions( "${XDG_CACHE_HOME}", nullptr );
554 
555  if( cacheDir.empty() || cacheDir == "${XDG_CACHE_HOME}" )
556  cacheDir = "${HOME}/.cache";
557 
558  cacheDir.append( "/kicad/3d" );
559 #endif
560 
561  cacheDir = ExpandEnvVarSubstitutions( cacheDir, m_project );
562  cfgdir.Assign( cacheDir, "" );
563 
564  if( !cfgdir.DirExists() )
565  {
566  cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
567 
568  if( !cfgdir.DirExists() )
569  {
570  wxLogTrace( MASK_3D_CACHE, "%s:%s:%d\n * failed to create 3D cache directory '%s'",
571  __FILE__, __FUNCTION__, __LINE__, cfgdir.GetPath() );
572 
573  return false;
574  }
575  }
576 
577  m_CacheDir = cfgdir.GetPathWithSep();
578  return true;
579 }
#define MASK_3D_CACHE
Definition: 3d_cache.cpp:63
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:574
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:63
wxString m_CacheDir
Definition: 3d_cache.h:68
wxString m_ConfigDir
Definition: 3d_cache.h:69
PROJECT * m_project
Definition: 3d_cache.h:67
bool Set3DConfigDir(const wxString &aConfigDir)
Function Set3DConfigDir sets the user's configuration directory for 3D models.

References ExpandEnvVarSubstitutions(), m_CacheDir, m_ConfigDir, m_FNResolver, m_project, MASK_3D_CACHE, and FILENAME_RESOLVER::Set3DConfigDir().

◆ SetProgramBase()

void S3D_CACHE::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.

Definition at line 610 of file 3d_cache.cpp.

611 {
612  m_FNResolver->SetProgramBase( aBase );
613 }
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:63
void SetProgramBase(PGM_BASE *aBase)
Function SetProgramBase sets a pointer to the application's PGM_BASE instance; the pointer is used to...

References m_FNResolver, and FILENAME_RESOLVER::SetProgramBase().

◆ SetProject()

bool S3D_CACHE::SetProject ( PROJECT aProject)

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

Definition at line 582 of file 3d_cache.cpp.

583 {
584  m_project = aProject;
585 
586  bool hasChanged = false;
587 
588  if( m_FNResolver->SetProject( aProject, &hasChanged ) && hasChanged )
589  {
590  m_CacheMap.clear();
591 
592  std::list< S3D_CACHE_ENTRY* >::iterator sL = m_CacheList.begin();
593  std::list< S3D_CACHE_ENTRY* >::iterator eL = m_CacheList.end();
594 
595  while( sL != eL )
596  {
597  delete *sL;
598  ++sL;
599  }
600 
601  m_CacheList.clear();
602 
603  return true;
604  }
605 
606  return false;
607 }
std::map< wxString, S3D_CACHE_ENTRY *, rsort_wxString > m_CacheMap
mapping of file names to cache names and data
Definition: 3d_cache.h:61
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:63
bool SetProject(PROJECT *aProject, 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:58
PROJECT * m_project
Definition: 3d_cache.h:67

References m_CacheList, m_CacheMap, m_FNResolver, m_project, and FILENAME_RESOLVER::SetProject().

◆ Type()

KICAD_T S3D_CACHE::Type ( )
inlineoverridevirtualnoexcept

Implements PROJECT::_ELEM.

Definition at line 108 of file 3d_cache.h.

109  {
110  return S3D_CACHE_T;
111  }

References S3D_CACHE_T.

Member Data Documentation

◆ m_CacheDir

wxString S3D_CACHE::m_CacheDir
private

Definition at line 68 of file 3d_cache.h.

Referenced by checkCache(), loadCacheData(), saveCacheData(), and Set3DConfigDir().

◆ m_CacheList

std::list< S3D_CACHE_ENTRY* > S3D_CACHE::m_CacheList
private

cache entries

Definition at line 58 of file 3d_cache.h.

Referenced by checkCache(), FlushCache(), and SetProject().

◆ m_CacheMap

std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString > S3D_CACHE::m_CacheMap
private

mapping of file names to cache names and data

Definition at line 61 of file 3d_cache.h.

Referenced by checkCache(), FlushCache(), load(), and SetProject().

◆ m_ConfigDir

wxString S3D_CACHE::m_ConfigDir
private

Definition at line 69 of file 3d_cache.h.

Referenced by Set3DConfigDir().

◆ m_FNResolver

FILENAME_RESOLVER* S3D_CACHE::m_FNResolver
private

◆ m_Plugins

S3D_PLUGIN_MANAGER* S3D_CACHE::m_Plugins
private

◆ m_project

PROJECT* S3D_CACHE::m_project
private

Definition at line 67 of file 3d_cache.h.

Referenced by S3D_CACHE(), Set3DConfigDir(), and SetProject().


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