KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_pcb.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014-2022 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
24#include "project_pcb.h"
25#include <fp_lib_table.h>
26#include <project.h>
27#include <confirm.h>
28#include <pgm_base.h>
29#include <3d_cache/3d_cache.h>
30#include <paths.h>
32
33#include <mutex>
34
35static std::mutex mutex3D_cacheManager;
36
38{
39 // This is a lazy loading function, it loads the project specific table when
40 // that table is asked for, not before.
41
43
44 // its gotta be NULL or a FP_LIB_TABLE, or a bug.
45 wxASSERT( !tbl || tbl->Type() == FP_LIB_TABLE_T );
46
47 if( !tbl )
48 {
49 // Stack the project specific FP_LIB_TABLE overlay on top of the global table.
50 // ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
51 // stack this way, all using the same global fallback table.
52 tbl = new FP_LIB_TABLE( &GFootprintTable );
53
54 aProject->SetElem( PROJECT::ELEM_FPTBL, tbl );
55
56 wxString projectFpLibTableFileName = aProject->FootprintLibTblName();
57
58 try
59 {
60 tbl->Load( projectFpLibTableFileName );
61 }
62 catch( const IO_ERROR& ioe )
63 {
64 DisplayErrorMessage( nullptr, _( "Error loading project footprint libraries." ),
65 ioe.What() );
66 }
67 catch( ... )
68 {
69 DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ) );
70 }
71 }
72
73 return tbl;
74}
75
76
77S3D_CACHE* PROJECT_PCB::Get3DCacheManager( PROJECT* aProject, bool aUpdateProjDir )
78{
79 std::lock_guard<std::mutex> lock( mutex3D_cacheManager );
80
81 // Get the existing cache from the project
82 S3D_CACHE* cache = dynamic_cast<S3D_CACHE*>( aProject->GetElem( PROJECT::ELEM_3DCACHE ) );
83
84 if( !cache )
85 {
86 // Create a cache if there is not one already
87 cache = new S3D_CACHE();
88
89 wxFileName cfgpath;
90 cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
91 cfgpath.AppendDir( wxT( "3d" ) );
92
93 cache->SetProgramBase( &Pgm() );
94 cache->Set3DConfigDir( cfgpath.GetFullPath() );
95
96 aProject->SetElem( PROJECT::ELEM_3DCACHE, cache );
97 aUpdateProjDir = true;
98 }
99
100 if( aUpdateProjDir )
101 cache->SetProject( aProject );
102
103 return cache;
104}
105
106
108{
109 return Get3DCacheManager( aProject )->GetResolver();
110}
111
112
114{
115 std::lock_guard<std::mutex> lock( mutex3D_cacheManager );
116
117 // Get the existing cache from the project
118 S3D_CACHE* cache = dynamic_cast<S3D_CACHE*>( aProject->GetElem( PROJECT::ELEM_3DCACHE ) );
119
120 if( cache )
121 {
122 // We'll delete ".3dc" cache files older than this many days
123 int clearCacheInterval = 0;
124
125 if( Pgm().GetCommonSettings() )
126 clearCacheInterval = Pgm().GetCommonSettings()->m_System.clear_3d_cache_interval;
127
128 // An interval of zero means the user doesn't want to ever clear the cache
129
130 if( clearCacheInterval > 0 )
131 cache->CleanCacheDir( clearCacheInterval );
132 }
133}
Provide an extensible class to resolve 3D model paths.
KICAD_T Type() override
Definition: fp_lib_table.h:101
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: paths.cpp:510
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:678
static FP_LIB_TABLE * PcbFootprintLibs(PROJECT *aProject)
Return the table of footprint libraries without Kiway.
Definition: project_pcb.cpp:37
static void Cleanup3DCache(PROJECT *aProject)
static S3D_CACHE * Get3DCacheManager(PROJECT *aProject, bool updateProjDir=false)
Return a pointer to an instance of the 3D cache manager.
Definition: project_pcb.cpp:77
static FILENAME_RESOLVER * Get3DFilenameResolver(PROJECT *aProject)
Accessor for 3D path resolver.
Container for project specific data.
Definition: project.h:62
virtual const wxString FootprintLibTblName() const
Returns the path and filename of this project's footprint library table.
Definition: project.cpp:165
virtual _ELEM * GetElem(ELEM_T aIndex)
Get and set the elements for this project.
Definition: project.cpp:299
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:309
@ ELEM_3DCACHE
Definition: project.h:227
@ ELEM_FPTBL
Definition: project.h:223
Cache for storing the 3D shapes.
Definition: 3d_cache.h:53
void SetProgramBase(PGM_BASE *aBase)
Set the filename resolver's pointer to the application's PGM_BASE instance.
Definition: 3d_cache.cpp:590
bool Set3DConfigDir(const wxString &aConfigDir)
Sets the configuration directory to be used by the model manager for storing 3D model manager configu...
Definition: 3d_cache.cpp:498
FILENAME_RESOLVER * GetResolver() noexcept
Definition: 3d_cache.cpp:596
bool SetProject(PROJECT *aProject)
Set the current project's working directory; this affects the model search path.
Definition: 3d_cache.cpp:562
void CleanCacheDir(int aNumDaysOld)
Delete up old cache files in cache directory.
Definition: 3d_cache.cpp:660
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: cvpcb.cpp:150
#define _(s)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
static std::mutex mutex3D_cacheManager
Definition: project_pcb.cpp:35
@ FP_LIB_TABLE_T
Definition: typeinfo.h:229