You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include "project_pcb.h"
  24. #include <fp_lib_table.h>
  25. #include <project.h>
  26. #include <confirm.h>
  27. #include <pgm_base.h>
  28. #include <3d_cache/3d_cache.h>
  29. #include <paths.h>
  30. #include <settings/common_settings.h>
  31. #include <mutex>
  32. static std::mutex mutex3D_cacheManager;
  33. FP_LIB_TABLE* PROJECT_PCB::PcbFootprintLibs( PROJECT* aProject )
  34. {
  35. // This is a lazy loading function, it loads the project specific table when
  36. // that table is asked for, not before.
  37. FP_LIB_TABLE* tbl = (FP_LIB_TABLE*) aProject->GetElem( PROJECT::ELEM::FPTBL );
  38. // its gotta be NULL or a FP_LIB_TABLE, or a bug.
  39. wxASSERT( !tbl || tbl->ProjectElementType() == PROJECT::ELEM::FPTBL );
  40. if( !tbl )
  41. {
  42. // Stack the project specific FP_LIB_TABLE overlay on top of the global table.
  43. // ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
  44. // stack this way, all using the same global fallback table.
  45. tbl = new FP_LIB_TABLE( &GFootprintTable );
  46. aProject->SetElem( PROJECT::ELEM::FPTBL, tbl );
  47. wxString projectFpLibTableFileName = aProject->FootprintLibTblName();
  48. try
  49. {
  50. tbl->Load( projectFpLibTableFileName );
  51. }
  52. catch( const IO_ERROR& ioe )
  53. {
  54. DisplayErrorMessage( nullptr, _( "Error loading project footprint libraries." ),
  55. ioe.What() );
  56. }
  57. catch( ... )
  58. {
  59. DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ) );
  60. }
  61. }
  62. return tbl;
  63. }
  64. S3D_CACHE* PROJECT_PCB::Get3DCacheManager( PROJECT* aProject, bool aUpdateProjDir )
  65. {
  66. std::lock_guard<std::mutex> lock( mutex3D_cacheManager );
  67. // Get the existing cache from the project
  68. S3D_CACHE* cache = static_cast<S3D_CACHE*>( aProject->GetElem( PROJECT::ELEM::S3DCACHE ) );
  69. if( !cache )
  70. {
  71. // Create a cache if there is not one already
  72. cache = new S3D_CACHE();
  73. wxFileName cfgpath;
  74. cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
  75. cfgpath.AppendDir( wxT( "3d" ) );
  76. cache->SetProgramBase( &Pgm() );
  77. cache->Set3DConfigDir( cfgpath.GetFullPath() );
  78. aProject->SetElem( PROJECT::ELEM::S3DCACHE, cache );
  79. aUpdateProjDir = true;
  80. }
  81. if( aUpdateProjDir )
  82. cache->SetProject( aProject );
  83. return cache;
  84. }
  85. FILENAME_RESOLVER* PROJECT_PCB::Get3DFilenameResolver( PROJECT* aProject )
  86. {
  87. return Get3DCacheManager( aProject )->GetResolver();
  88. }
  89. void PROJECT_PCB::Cleanup3DCache( PROJECT* aProject )
  90. {
  91. std::lock_guard<std::mutex> lock( mutex3D_cacheManager );
  92. // Get the existing cache from the project
  93. S3D_CACHE* cache = static_cast<S3D_CACHE*>( aProject->GetElem( PROJECT::ELEM::S3DCACHE ) );
  94. if( cache )
  95. {
  96. // We'll delete ".3dc" cache files older than this many days
  97. int clearCacheInterval = 0;
  98. if( Pgm().GetCommonSettings() )
  99. clearCacheInterval = Pgm().GetCommonSettings()->m_System.clear_3d_cache_interval;
  100. // An interval of zero means the user doesn't want to ever clear the cache
  101. if( clearCacheInterval > 0 )
  102. cache->CleanCacheDir( clearCacheInterval );
  103. }
  104. }