KiCad PCB EDA Suite
S3D_CACHE Class Reference

Cache for storing the 3D shapes. 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)
 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)
 Set the current project's working directory; this affects the model search path. More...
 
void SetProgramBase (PGM_BASE *aBase)
 Set the filename resolver's pointer to the application's PGM_BASE instance. More...
 
SCENEGRAPHLoad (const wxString &aModelFile, const wxString &aBasePath)
 Attempt to load the scene data for a model. More...
 
FILENAME_RESOLVERGetResolver () noexcept
 
std::list< wxString > const * GetFileFilters () const
 Return the list of file filters retrieved from the plugins. More...
 
void FlushCache (bool closePlugins=true)
 Free all data in the cache and by default closes all plugins. More...
 
void ClosePlugins ()
 Unload plugins to free memory. More...
 
S3DMODELGetModel (const wxString &aModelFileName, const wxString &aBasePath)
 Attempt to load the scene data for a model and to translate it into an S3D_MODEL structure for display by a renderer. More...
 
void CleanCacheDir (int aNumDaysOld)
 Delete up old cache files in cache directory. More...
 

Private Member Functions

SCENEGRAPHcheckCache (const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=nullptr)
 Find or create cache entry for file name. More...
 
bool getSHA1 (const wxString &aFileName, unsigned char *aSHA1Sum)
 Calculate the SHA1 hash of the given file. More...
 
bool loadCacheData (S3D_CACHE_ENTRY *aCacheItem)
 
bool saveCacheData (S3D_CACHE_ENTRY *aCacheItem)
 
SCENEGRAPHload (const wxString &aModelFile, const wxString &aBasePath, S3D_CACHE_ENTRY **aCachePtr=nullptr)
 

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

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 52 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}
Provide an extensible class to resolve 3D model paths.
PROJECT * m_project
Definition: 3d_cache.h:180
S3D_PLUGIN_MANAGER * m_Plugins
Definition: 3d_cache.h:178
FILENAME_RESOLVER * m_FNResolver
Definition: 3d_cache.h:176

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}
void FlushCache(bool closePlugins=true)
Free all data in the cache and by default closes all plugins.
Definition: 3d_cache.cpp:609

References FlushCache(), m_FNResolver, and m_Plugins.

Member Function Documentation

◆ checkCache()

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

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
aFileNameis the file name (full or partial path).
aCachePtris an optional return address for cache entry pointer.
Returns
SCENEGRAPH object associated with file name or NULL on error.

Definition at line 282 of file 3d_cache.cpp.

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

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

Referenced by load().

◆ CleanCacheDir()

void S3D_CACHE::CleanCacheDir ( int  aNumDaysOld)

Delete up old cache files in cache directory.

Deletes ".3dc" files in the cache directory that are older than aNumDaysOld.

Parameters
aNumDaysOldis age threshold to delete ".3dc" cache files.

Definition at line 661 of file 3d_cache.cpp.

662{
663 wxDir dir;
664 wxString fileSpec = wxT( "*.3dc" );
665 wxArrayString fileList; // Holds list of ".3dc" files found in cache directory
666 size_t numFilesFound = 0;
667
668 wxFileName thisFile;
669 wxDateTime lastAccess, thresholdDate;
670 wxDateSpan durationInDays;
671
672 // Calc the threshold date above which we delete cache files
673 durationInDays.SetDays( aNumDaysOld );
674 thresholdDate = wxDateTime::Now() - durationInDays;
675
676 // If the cache directory can be found and opened, then we'll try and clean it up
677 if( dir.Open( m_CacheDir ) )
678 {
679 thisFile.SetPath( m_CacheDir ); // Set the base path to the cache folder
680
681 // Get a list of all the ".3dc" files in the cache directory
682 numFilesFound = dir.GetAllFiles( m_CacheDir, &fileList, fileSpec );
683
684 for( unsigned int i = 0; i < numFilesFound; i++ )
685 {
686 // Completes path to specific file so we can get its "last access" date
687 thisFile.SetFullName( fileList[i] );
688
689 // Only get "last access" time to compare against. Don't need the other 2 timestamps.
690 if( thisFile.GetTimes( &lastAccess, nullptr, nullptr ) )
691 {
692 if( lastAccess.IsEarlierThan( thresholdDate ) )
693 {
694 // This file is older than the threshold so delete it
695 wxRemoveFile( thisFile.GetFullPath() );
696 }
697 }
698 }
699 }
700}

References m_CacheDir.

◆ ClosePlugins()

void S3D_CACHE::ClosePlugins ( )

Unload plugins to free memory.

Definition at line 628 of file 3d_cache.cpp.

629{
630 if( m_Plugins )
632}
void ClosePlugins(void)
Iterate through all discovered plugins and closes them to reclaim memory.

References S3D_PLUGIN_MANAGER::ClosePlugins(), and m_Plugins.

Referenced by FlushCache().

◆ FlushCache()

void S3D_CACHE::FlushCache ( bool  closePlugins = true)

Free all data in the cache and by default closes all plugins.

Definition at line 609 of file 3d_cache.cpp.

610{
611 std::list< S3D_CACHE_ENTRY* >::iterator sCL = m_CacheList.begin();
612 std::list< S3D_CACHE_ENTRY* >::iterator eCL = m_CacheList.end();
613
614 while( sCL != eCL )
615 {
616 delete *sCL;
617 ++sCL;
618 }
619
620 m_CacheList.clear();
621 m_CacheMap.clear();
622
623 if( closePlugins )
624 ClosePlugins();
625}
void ClosePlugins()
Unload plugins to free memory.
Definition: 3d_cache.cpp:628

References ClosePlugins(), m_CacheList, and m_CacheMap.

Referenced by ~S3D_CACHE().

◆ GetFileFilters()

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

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

604{
605 return m_Plugins->GetFileFilters();
606}
std::list< wxString > const * GetFileFilters(void) const noexcept
Return the list of file filters; this will contain at least the default "All Files (*....

References S3D_PLUGIN_MANAGER::GetFileFilters(), and m_Plugins.

Referenced by DIALOG_SELECT_3DMODEL::DIALOG_SELECT_3DMODEL().

◆ GetModel()

S3DMODEL * S3D_CACHE::GetModel ( const wxString &  aModelFileName,
const wxString &  aBasePath 
)

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

636{
637 S3D_CACHE_ENTRY* cp = nullptr;
638 SCENEGRAPH* sp = load( aModelFileName, aBasePath,&cp );
639
640 if( !sp )
641 return nullptr;
642
643 if( !cp )
644 {
645 wxLogTrace( MASK_3D_CACHE,
646 wxT( "%s:%s:%d\n * [BUG] model loaded with no associated S3D_CACHE_ENTRY" ),
647 __FILE__, __FUNCTION__, __LINE__ );
648
649 return nullptr;
650 }
651
652 if( cp->renderData )
653 return cp->renderData;
654
655 S3DMODEL* mp = S3D::GetModel( sp );
656 cp->renderData = mp;
657
658 return mp;
659}
S3DMODEL * renderData
Definition: 3d_cache.cpp:137
SCENEGRAPH * load(const wxString &aModelFile, const wxString &aBasePath, S3D_CACHE_ENTRY **aCachePtr=nullptr)
Definition: 3d_cache.cpp:205
Define the basic data set required to represent a 3D model.
Definition: scenegraph.h:45
SGLIB_API S3DMODEL * GetModel(SCENEGRAPH *aNode)
Function GetModel creates an S3DMODEL representation of aNode (raw data, no transforms)
Definition: ifsg_api.cpp:338
Store the a model based on meshes and materials.
Definition: c3dmodel.h:91

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

Referenced by RENDER_3D_RAYTRACE::load3DModels(), RENDER_3D_OPENGL::load3dModels(), and EDA_3D_MODEL_VIEWER::Set3DModel().

◆ GetResolver()

FILENAME_RESOLVER * S3D_CACHE::GetResolver ( )
noexcept

Definition at line 597 of file 3d_cache.cpp.

598{
599 return m_FNResolver;
600}

References m_FNResolver.

Referenced by EXPORTER_PCB_VRML::ExportVrmlFootprint().

◆ getSHA1()

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

Calculate the SHA1 hash of the given file.

Parameters
aFileNamefile name (full path).
aSHA1Suma 20 byte character array to hold the SHA1 hash.
Returns
true on success, otherwise false.

Definition at line 349 of file 3d_cache.cpp.

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

References MASK_3D_CACHE.

Referenced by checkCache(), and load().

◆ Load()

SCENEGRAPH * S3D_CACHE::Load ( const wxString &  aModelFile,
const wxString &  aBasePath 
)

Attempt 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. The model may fail to load if, for example, the plugin does not support rendering of the 3D model.

Parameters
aModelFileis the partial or full path to the model to be loaded.
aBasePathis the path to search for any relative files
Returns
true if the model was successfully loaded, otherwise false.

Definition at line 276 of file 3d_cache.cpp.

277{
278 return load( aModelFile, aBasePath );
279}

References load().

Referenced by EXPORTER_PCB_VRML::ExportVrmlFootprint().

◆ load()

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

Definition at line 205 of file 3d_cache.cpp.

207{
208 if( aCachePtr )
209 *aCachePtr = nullptr;
210
211 wxString full3Dpath = m_FNResolver->ResolvePath( aModelFile, aBasePath );
212
213 if( full3Dpath.empty() )
214 {
215 // the model cannot be found; we cannot proceed
216 wxLogTrace( MASK_3D_CACHE, wxT( "%s:%s:%d\n * [3D model] could not find model '%s'\n" ),
217 __FILE__, __FUNCTION__, __LINE__, aModelFile );
218 return nullptr;
219 }
220
221 // check cache if file is already loaded
222 std::lock_guard<std::mutex> lock( mutex3D_cache );
223
224 std::map< wxString, S3D_CACHE_ENTRY*, rsort_wxString >::iterator mi;
225 mi = m_CacheMap.find( full3Dpath );
226
227 if( mi != m_CacheMap.end() )
228 {
229 wxFileName fname( full3Dpath );
230
231 if( fname.FileExists() ) // Only check if file exists. If not, it will
232 { // use the same model in cache.
234 wxDateTime fmdate = fname.GetModificationTime();
235
236 if( fmdate != mi->second->modTime )
237 {
238 unsigned char hashSum[20];
239 getSHA1( full3Dpath, hashSum );
240 mi->second->modTime = fmdate;
241
242 if( !isSHA1Same( hashSum, mi->second->sha1sum ) )
243 {
244 mi->second->SetSHA1( hashSum );
245 reload = true;
246 }
247 }
248
249 if( reload )
250 {
251 if( nullptr != mi->second->sceneData )
252 {
253 S3D::DestroyNode( mi->second->sceneData );
254 mi->second->sceneData = nullptr;
255 }
256
257 if( nullptr != mi->second->renderData )
258 S3D::Destroy3DModel( &mi->second->renderData );
259
260 mi->second->sceneData = m_Plugins->Load3DModel( full3Dpath,
261 mi->second->pluginInfo );
262 }
263 }
264
265 if( nullptr != aCachePtr )
266 *aCachePtr = mi->second;
267
268 return mi->second->sceneData;
269 }
270
271 // a cache item does not exist; search the Filename->Cachename map
272 return checkCache( full3Dpath, aCachePtr );
273}
static bool isSHA1Same(const unsigned char *shaA, const unsigned char *shaB) noexcept
Definition: 3d_cache.cpp:67
static std::mutex mutex3D_cache
Definition: 3d_cache.cpp:63
bool m_Skip3DModelMemoryCache
Skip reading/writing 3d model memory caches This ensures 3d models are always reloaded from disk even...
wxString ResolvePath(const wxString &aFileName, const wxString &aWorkingPath)
Determines the full path of the given file name.
SCENEGRAPH * checkCache(const wxString &aFileName, S3D_CACHE_ENTRY **aCachePtr=nullptr)
Find or create cache entry for file name.
Definition: 3d_cache.cpp:282
SGLIB_API void DestroyNode(SGNODE *aNode) noexcept
Function DestroyNode deletes the given SG* class node.
Definition: ifsg_api.cpp:149
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:403

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

Referenced by GetModel(), and Load().

◆ loadCacheData()

bool S3D_CACHE::loadCacheData ( S3D_CACHE_ENTRY aCacheItem)
private

Definition at line 405 of file 3d_cache.cpp.

406{
407 wxString bname = aCacheItem->GetCacheBaseName();
408
409 if( bname.empty() )
410 {
411 wxLogTrace( MASK_3D_CACHE,
412 wxT( " * [3D model] cannot load cached model; no file hash available" ) );
413
414 return false;
415 }
416
417 if( m_CacheDir.empty() )
418 {
419 wxLogTrace( MASK_3D_CACHE,
420 wxT( " * [3D model] cannot load cached model; config directory unknown" ) );
421
422 return false;
423 }
424
425 wxString fname = m_CacheDir + bname + wxT( ".3dc" );
426
427 if( !wxFileName::FileExists( fname ) )
428 {
429 wxLogTrace( MASK_3D_CACHE, wxT( " * [3D model] cannot open file '%s'" ), fname.GetData() );
430 return false;
431 }
432
433 if( nullptr != aCacheItem->sceneData )
434 S3D::DestroyNode( (SGNODE*) aCacheItem->sceneData );
435
436 aCacheItem->sceneData = (SCENEGRAPH*)S3D::ReadCache( fname.ToUTF8(), m_Plugins, checkTag );
437
438 if( nullptr == aCacheItem->sceneData )
439 return false;
440
441 return true;
442}
static bool checkTag(const char *aTag, void *aPluginMgrPtr)
Definition: 3d_cache.cpp:79
The base class of all Scene Graph nodes.
Definition: sg_node.h:75
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:221

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

Referenced by checkCache().

◆ saveCacheData()

bool S3D_CACHE::saveCacheData ( S3D_CACHE_ENTRY aCacheItem)
private

Definition at line 445 of file 3d_cache.cpp.

446{
447 if( nullptr == aCacheItem )
448 {
449 wxLogTrace( MASK_3D_CACHE, wxT( "%s:%s:%d\n * NULL passed for aCacheItem" ),
450 __FILE__, __FUNCTION__, __LINE__ );
451
452 return false;
453 }
454
455 if( nullptr == aCacheItem->sceneData )
456 {
457 wxLogTrace( MASK_3D_CACHE, wxT( "%s:%s:%d\n * aCacheItem has no valid scene data" ),
458 __FILE__, __FUNCTION__, __LINE__ );
459
460 return false;
461 }
462
463 wxString bname = aCacheItem->GetCacheBaseName();
464
465 if( bname.empty() )
466 {
467 wxLogTrace( MASK_3D_CACHE,
468 wxT( " * [3D model] cannot load cached model; no file hash available" ) );
469
470 return false;
471 }
472
473 if( m_CacheDir.empty() )
474 {
475 wxLogTrace( MASK_3D_CACHE,
476 wxT( " * [3D model] cannot load cached model; config directory unknown" ) );
477
478 return false;
479 }
480
481 wxString fname = m_CacheDir + bname + wxT( ".3dc" );
482
483 if( wxFileName::Exists( fname ) )
484 {
485 if( !wxFileName::FileExists( fname ) )
486 {
487 wxLogTrace( MASK_3D_CACHE,
488 wxT( " * [3D model] path exists but is not a regular file '%s'" ), fname );
489
490 return false;
491 }
492 }
493
494 return S3D::WriteCache( fname.ToUTF8(), true, (SGNODE*)aCacheItem->sceneData,
495 aCacheItem->pluginInfo.c_str() );
496}
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:157

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

Referenced by checkCache().

◆ Set3DConfigDir()

bool S3D_CACHE::Set3DConfigDir ( const wxString &  aConfigDir)

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

500{
501 if( !m_ConfigDir.empty() )
502 return false;
503
504 wxFileName cfgdir( ExpandEnvVarSubstitutions( aConfigDir, m_project ), wxEmptyString );
505
506 cfgdir.Normalize( FN_NORMALIZE_FLAGS );
507
508 if( !cfgdir.DirExists() )
509 {
510 cfgdir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
511
512 if( !cfgdir.DirExists() )
513 {
514 wxLogTrace( MASK_3D_CACHE,
515 wxT( "%s:%s:%d\n * failed to create 3D configuration directory '%s'" ),
516 __FILE__, __FUNCTION__, __LINE__, cfgdir.GetPath() );
517
518 return false;
519 }
520 }
521
522 m_ConfigDir = cfgdir.GetPath();
523
524 // inform the file resolver of the config directory
526 {
527 wxLogTrace( MASK_3D_CACHE,
528 wxT( "%s:%s:%d\n * could not set 3D Config Directory on filename resolver\n"
529 " * config directory: '%s'" ),
530 __FILE__, __FUNCTION__, __LINE__, m_ConfigDir );
531 }
532
533 // 3D cache data must go to a user's cache directory;
534 // unfortunately wxWidgets doesn't seem to provide
535 // functions to retrieve such a directory.
536 //
537 // 1. OSX: ~/Library/Caches/kicad/3d/
538 // 2. Linux: ${XDG_CACHE_HOME}/kicad/3d ~/.cache/kicad/3d/
539 // 3. MSWin: AppData\Local\kicad\3d
540 wxFileName cacheDir;
541 cacheDir.AssignDir( PATHS::GetUserCachePath() );
542 cacheDir.AppendDir( wxT( "3d" ) );
543
544 if( !cacheDir.DirExists() )
545 {
546 cacheDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
547
548 if( !cacheDir.DirExists() )
549 {
550 wxLogTrace( MASK_3D_CACHE,
551 wxT( "%s:%s:%d\n * failed to create 3D cache directory '%s'" ),
552 __FILE__, __FUNCTION__, __LINE__, cacheDir.GetPath() );
553
554 return false;
555 }
556 }
557
558 m_CacheDir = cacheDir.GetPathWithSep();
559 return true;
560}
bool Set3DConfigDir(const wxString &aConfigDir)
Set the user's configuration directory for 3D models.
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:321
wxString m_ConfigDir
Definition: 3d_cache.h:182
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:299
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:38

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

◆ SetProgramBase()

void S3D_CACHE::SetProgramBase ( PGM_BASE aBase)

Set the filename resolver's pointer to the application's PGM_BASE instance.

The pointer is used to extract the local environment variables.

Definition at line 591 of file 3d_cache.cpp.

592{
594}
void SetProgramBase(PGM_BASE *aBase)
Set a pointer to the application's PGM_BASE instance used to extract the local env vars.

References m_FNResolver, and FILENAME_RESOLVER::SetProgramBase().

◆ SetProject()

bool S3D_CACHE::SetProject ( PROJECT aProject)

Set the current project's working directory; this affects the model search path.

Definition at line 563 of file 3d_cache.cpp.

564{
565 m_project = aProject;
566
567 bool hasChanged = false;
568
569 if( m_FNResolver->SetProject( aProject, &hasChanged ) && hasChanged )
570 {
571 m_CacheMap.clear();
572
573 std::list< S3D_CACHE_ENTRY* >::iterator sL = m_CacheList.begin();
574 std::list< S3D_CACHE_ENTRY* >::iterator eL = m_CacheList.end();
575
576 while( sL != eL )
577 {
578 delete *sL;
579 ++sL;
580 }
581
582 m_CacheList.clear();
583
584 return true;
585 }
586
587 return false;
588}
bool SetProject(PROJECT *aProject, bool *flgChanged=nullptr)
Set the current KiCad project directory as the first entry in the model path list.

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 58 of file 3d_cache.h.

59 {
60 return S3D_CACHE_T;
61 }
@ S3D_CACHE_T
Definition: typeinfo.h:238

References S3D_CACHE_T.

Member Data Documentation

◆ m_CacheDir

wxString S3D_CACHE::m_CacheDir
private

Definition at line 181 of file 3d_cache.h.

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

◆ m_CacheList

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

cache entries

Definition at line 171 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 174 of file 3d_cache.h.

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

◆ m_ConfigDir

wxString S3D_CACHE::m_ConfigDir
private

Definition at line 182 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 180 of file 3d_cache.h.

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


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