From fff54d759e5a231c003dbe052ee2d2cc3a5326b2 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sat, 8 Feb 2014 11:44:55 +0100 Subject: [PATCH] Add the environment variable KYSYS3DMOD to define a default path for 3D models. --- 3d-viewer/3d_read_mesh.cpp | 36 ++++++++++++++++- 3d-viewer/3d_struct.h | 8 ++++ 3d-viewer/3d_viewer.h | 3 ++ common/edaappl.cpp | 66 ++++++++++++++++++++++++++++++++ cvpcb/cvpcb.cpp | 4 ++ include/appl_wxstruct.h | 20 ++++++++++ pcbnew/exporters/export_vrml.cpp | 3 +- pcbnew/pcbnew.cpp | 10 +++-- 8 files changed, 143 insertions(+), 7 deletions(-) diff --git a/3d-viewer/3d_read_mesh.cpp b/3d-viewer/3d_read_mesh.cpp index 24046f9e9c..1863b339b2 100644 --- a/3d-viewer/3d_read_mesh.cpp +++ b/3d-viewer/3d_read_mesh.cpp @@ -59,6 +59,39 @@ S3D_MODEL_PARSER* S3D_MODEL_PARSER::Create( S3D_MASTER* aMaster, } } +const wxString S3D_MASTER::GetShape3DFullFilename() +{ + + wxString shapeName; + + // Expand any environment variables embedded in footprint's m_Shape3DName field. + // To ensure compatibility with most of footprint's m_Shape3DName field, + // if the m_Shape3DName is not an absolute path the default path + // given by the environment variable KISYS3DMOD will be used + + if( m_Shape3DName.StartsWith( wxT("${") ) ) + shapeName = wxExpandEnvVars( m_Shape3DName ); + else + shapeName = m_Shape3DName; + + wxFileName fn( shapeName ); + + if( fn.IsAbsolute() || shapeName.StartsWith( wxT(".") ) ) + return shapeName; + + wxString default_path; + wxGetEnv( wxT( KISYS3DMOD ), &default_path ); + + if( default_path.IsEmpty() ) + return shapeName; + + if( !default_path.EndsWith( wxT("/") ) && !default_path.EndsWith( wxT("\\") ) ) + default_path += wxT("/"); + + default_path += shapeName; + + return default_path; +} int S3D_MASTER::ReadData() { @@ -67,8 +100,7 @@ int S3D_MASTER::ReadData() return 1; } - // Expand any environment variables embedded in footprint's m_Shape3DName field. - wxString filename = wxExpandEnvVars( m_Shape3DName ); + wxString filename = GetShape3DFullFilename(); #ifdef __WINDOWS__ filename.Replace( wxT( "/" ), wxT( "\\" ) ); diff --git a/3d-viewer/3d_struct.h b/3d-viewer/3d_struct.h index ebbe54e874..d53327f7e9 100644 --- a/3d-viewer/3d_struct.h +++ b/3d-viewer/3d_struct.h @@ -144,6 +144,14 @@ public: return m_Shape3DName; } + /** + * Function GetShape3DFullFilename + * @return the full filename of the 3D shape, + * expanding environment variable (if any ) and/or adding default 3D path + * given by environment variable KISYS3DMOD + */ + const wxString GetShape3DFullFilename(); + void SetShape3DName( const wxString& aShapeName ); }; diff --git a/3d-viewer/3d_viewer.h b/3d-viewer/3d_viewer.h index 24d76d6aa7..dc94f933ef 100644 --- a/3d-viewer/3d_viewer.h +++ b/3d-viewer/3d_viewer.h @@ -48,6 +48,9 @@ # include #endif + +#define KISYS3DMOD "KISYS3DMOD" + #include <3d_struct.h> class EDA_3D_CANVAS; diff --git a/common/edaappl.cpp b/common/edaappl.cpp index d01535c76c..c3595b0f8b 100644 --- a/common/edaappl.cpp +++ b/common/edaappl.cpp @@ -1196,3 +1196,69 @@ bool EDA_APP::SetFootprintLibTablePath() return false; } +/** + * Function Set3DShapesPath + * attempts set the environment variable given by aKiSys3Dmod to a valid path. + * (typically "KISYS3DMOD" ) + * If the environment variable is already set, + * then it left as is to respect the wishes of the user. + * + * The path is determined by attempting to find the path modules/packages3d + * files in kicad tree. + * This may or may not be the best path but it provides the best solution for + * backwards compatibility with the previous 3D shapes search path implementation. + * + * @note This must be called after #SetBinDir() is called at least on Windows. + * Otherwise, the kicad path is not known (Windows specific) + * + * @param aKiSys3Dmod = the value of environment variable, typically "KISYS3DMOD" + * @return false if the aKiSys3Dmod path is not valid. + */ +bool EDA_APP::Set3DShapesPath( const wxString& aKiSys3Dmod ) +{ + wxString path; + + // Set the KISYS3DMOD environment variable for the current process, + // if it is not already defined in the user's environment and valid. + if( wxGetEnv( aKiSys3Dmod, &path ) && wxFileName::DirExists( path ) ) + return true; + + // Attempt to determine where the 3D shape libraries were installed using the + // legacy path: + // on Unix: /usr/local/kicad/share/modules/packages3d + // or /usr/share/kicad/modules/packages3d + // On Windows: bin../share/modules/packages3d + wxString relpath( wxT( "modules/packages3d" ) ); + +// Apple MacOSx +#ifdef __APPLE__ + // TO DO + +#elif defined(__UNIX__) // Linux and non-Apple Unix + path = wxT("/usr/local/kicad/share/") + relpath; + if( wxFileName::DirExists( path ) ) + { + wxSetEnv( aKiSys3Dmod, path ); + return true; + } + + path = wxT("/usr/share/kicad/") + relpath; + if( wxFileName::DirExists( path ) ) + { + wxSetEnv( aKiSys3Dmod, path ); + return true; + } + +#else // Windows + path = m_BinDir + wxT("../share/") + relpath; + + if( wxFileName::DirExists( path ) ) + { + wxSetEnv( aKiSys3Dmod, path ); + return true; + } +#endif + + return false; +} + diff --git a/cvpcb/cvpcb.cpp b/cvpcb/cvpcb.cpp index 84a1ae8b73..b2f307fb32 100644 --- a/cvpcb/cvpcb.cpp +++ b/cvpcb/cvpcb.cpp @@ -33,6 +33,7 @@ #include #include +#include <3d_viewer.h> #include #include #include @@ -102,6 +103,9 @@ bool EDA_APP::OnInit() SetFootprintLibTablePath(); + // Set 3D shape path from environment variable KISYS3DMOD + Set3DShapesPath( wxT(KISYS3DMOD) ); + if( m_Checker && m_Checker->IsAnotherRunning() ) { if( !IsOK( NULL, _( "CvPcb is already running, Continue?" ) ) ) diff --git a/include/appl_wxstruct.h b/include/appl_wxstruct.h index a4305a4d28..afb2b91ed8 100644 --- a/include/appl_wxstruct.h +++ b/include/appl_wxstruct.h @@ -456,6 +456,26 @@ public: */ bool SetFootprintLibTablePath(); + /** + * Function Set3DShapesPath + * attempts set the environment variable given by aKiSys3Dmod to a valid path. + * (typically "KISYS3DMOD" ) + * If the environment variable is already set, + * then it left as is to respect the wishes of the user. + * + * The path is determined by attempting to find the path modules/packages3d + * files in kicad tree. + * This may or may not be the best path but it provides the best solution for + * backwards compatibility with the previous 3D shapes search path implementation. + * + * @note This must be called after #SetBinDir() is called at least on Windows. + * Otherwise, the kicad path is not known (Windows specific) + * + * @param aKiSys3Dmod = the value of environment variable, typically "KISYS3DMOD" + * @return false if the aKiSys3Dmod path is not valid. + */ + bool Set3DShapesPath( const wxString& aKiSys3Dmod ); + const wxString& GetModuleLibraryNickname() { return m_module_nickname; } void SetModuleLibraryNickname( const wxString& aNickname ) { m_module_nickname = aNickname; } }; diff --git a/pcbnew/exporters/export_vrml.cpp b/pcbnew/exporters/export_vrml.cpp index 9002657d19..83965bfbf5 100644 --- a/pcbnew/exporters/export_vrml.cpp +++ b/pcbnew/exporters/export_vrml.cpp @@ -1154,8 +1154,7 @@ static void export_vrml_module( MODEL_VRML& aModel, BOARD* aPcb, MODULE* aModule if( !vrmlm->Is3DType( S3D_MASTER::FILE3D_VRML ) ) continue; - // expand environment variables - wxString fname = wxExpandEnvVars( vrmlm->GetShape3DName() ); + wxString fname = vrmlm->GetShape3DFullFilename(); fname.Replace( wxT( "\\" ), wxT( "/" ) ); wxString source_fname = fname; diff --git a/pcbnew/pcbnew.cpp b/pcbnew/pcbnew.cpp index 2d695c8f44..49cae3dea4 100644 --- a/pcbnew/pcbnew.cpp +++ b/pcbnew/pcbnew.cpp @@ -42,6 +42,7 @@ #include #include #include +#include <3d_viewer.h> #include #include @@ -168,10 +169,10 @@ bool EDA_APP::OnInit() bundledir.RemoveLastDir(); // Prepend in PYTHONPATH the content of the bundle libraries ! - wxSetEnv("PYTHONPATH",((wxGetenv("PYTHONPATH") != NULL ) ? (wxString(wxGetenv("PYTHONPATH")) + ":") : wxString("")) - + bundledir.GetPath() + + wxSetEnv("PYTHONPATH",((wxGetenv("PYTHONPATH") != NULL ) ? (wxString(wxGetenv("PYTHONPATH")) + ":") : wxString("")) + + bundledir.GetPath() + "/Frameworks/wxPython/lib/python2.6/site-packages/wx-3.0-osx_cocoa" ); -#endif +#endif #endif // On linux and osx, 2 others paths are // [HOME]/.kicad_plugins/ @@ -233,6 +234,9 @@ bool EDA_APP::OnInit() // Set any environment variables before loading FP_LIB_TABLE SetFootprintLibTablePath(); + // Set 3D shape path from environment variable KISYS3DMOD + Set3DShapesPath( wxT(KISYS3DMOD) ); + frame = new PCB_EDIT_FRAME( NULL, wxT( "Pcbnew" ), wxPoint( 0, 0 ), wxSize( 600, 400 ) ); #ifdef KICAD_SCRIPTING