From 4f52c69724bd1b565945f44933887c61db684450 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 26 Jun 2023 20:51:16 +0100 Subject: [PATCH] Better path handling for importing LTspice files. --- .../ltspice/ltspice_sch_parser.cpp | 32 ++++++----------- .../sch_plugins/ltspice/ltspice_sch_parser.h | 14 -------- .../ltspice/ltspice_sch_plugin.cpp | 15 ++++++-- .../sch_plugins/ltspice/ltspice_schematic.cpp | 19 +++------- .../sch_plugins/ltspice/ltspice_schematic.h | 35 ++++++++----------- libs/kiplatform/gtk/environment.cpp | 6 ++++ .../include/kiplatform/environment.h | 7 ++++ libs/kiplatform/msw/environment.cpp | 14 ++++++++ libs/kiplatform/osx/environment.mm | 6 ++++ 9 files changed, 74 insertions(+), 74 deletions(-) diff --git a/eeschema/sch_plugins/ltspice/ltspice_sch_parser.cpp b/eeschema/sch_plugins/ltspice/ltspice_sch_parser.cpp index 152f5d14b6..1eeeac57a7 100644 --- a/eeschema/sch_plugins/ltspice/ltspice_sch_parser.cpp +++ b/eeschema/sch_plugins/ltspice/ltspice_sch_parser.cpp @@ -27,8 +27,6 @@ #include #include #include -#include -#include #include #include #include @@ -38,7 +36,6 @@ #include #include #include -#include void LTSPICE_SCH_PARSER::Parse( SCH_SHEET_PATH* aSheet, @@ -77,8 +74,7 @@ void LTSPICE_SCH_PARSER::Parse( SCH_SHEET_PATH* aSheet, void LTSPICE_SCH_PARSER::readIncludes( std::vector& outLT_ASCs ) { - static wxString ltSubDir = KIPLATFORM::ENV::GetDocumentsPath() + wxS( "/LTspiceXVII/lib/sub/" ); - + wxString ltSubDir = m_lt_schematic->GetLTspiceDataDir().GetFullPath() + wxS( "lib/sub/" ); wxString path; for( const LTSPICE_SCHEMATIC::LT_ASC& asc : outLT_ASCs ) @@ -349,22 +345,14 @@ VECTOR2I LTSPICE_SCH_PARSER::ToKicadFontSize( int aLTFontSize ) return VECTOR2I( schIUScale.MilsToIU( mils ), schIUScale.MilsToIU( mils ) ); }; - if( aLTFontSize == 1 ) - return MILS_SIZE( 36 ); - else if( aLTFontSize == 2 ) - return MILS_SIZE( 42 ); - else if( aLTFontSize == 3 ) - return MILS_SIZE( 50 ); - else if( aLTFontSize == 4 ) - return MILS_SIZE( 60 ); - else if( aLTFontSize == 5 ) - return MILS_SIZE( 72 ); - else if( aLTFontSize == 6 ) - return MILS_SIZE( 88 ); - else if( aLTFontSize == 7 ) - return MILS_SIZE( 108 ); - else - return ToKicadFontSize( 2 ); + if( aLTFontSize == 1 ) return MILS_SIZE( 36 ); + else if( aLTFontSize == 2 ) return MILS_SIZE( 42 ); + else if( aLTFontSize == 3 ) return MILS_SIZE( 50 ); + else if( aLTFontSize == 4 ) return MILS_SIZE( 60 ); + else if( aLTFontSize == 5 ) return MILS_SIZE( 72 ); + else if( aLTFontSize == 6 ) return MILS_SIZE( 88 ); + else if( aLTFontSize == 7 ) return MILS_SIZE( 108 ); + else return ToKicadFontSize( 2 ); } @@ -903,7 +891,7 @@ SCH_LABEL_BASE* LTSPICE_SCH_PARSER::CreateSCH_LABEL( KICAD_T aType, const VECTOR void LTSPICE_SCH_PARSER::CreateFields( LTSPICE_SCHEMATIC::LT_SYMBOL& aLTSymbol, SCH_SYMBOL* aSymbol, SCH_SHEET_PATH* aSheet ) { - wxString libPath = KIPLATFORM::ENV::GetDocumentsPath() + wxS( "/LTspiceXVII/lib/" ); + wxString libPath = m_lt_schematic->GetLTspiceDataDir().GetFullPath() + wxS( "lib/" ); wxString symbolName = aLTSymbol.Name.Upper(); wxString type = aLTSymbol.SymAttributes[ wxS( "TYPE" ) ].Upper(); wxString prefix = aLTSymbol.SymAttributes[ wxS( "PREFIX" ) ].Upper(); diff --git a/eeschema/sch_plugins/ltspice/ltspice_sch_parser.h b/eeschema/sch_plugins/ltspice/ltspice_sch_parser.h index 248413cde2..ed1534e429 100644 --- a/eeschema/sch_plugins/ltspice/ltspice_sch_parser.h +++ b/eeschema/sch_plugins/ltspice/ltspice_sch_parser.h @@ -66,9 +66,6 @@ class SCH_SHAPE; class LTSPICE_SCH_PARSER { public: - // Size of tiny net labels which none present in original design - const int SMALL_LABEL_SIZE = KiROUND( (double) SCH_IU_PER_MM * 0.4 ); - explicit LTSPICE_SCH_PARSER( const wxString& aFilename, LTSPICE_SCHEMATIC* aLTSchematic ) : m_lt_schematic( aLTSchematic ), m_powerSymbolIndex( 0 ) @@ -279,18 +276,8 @@ public: void CreateCircle( LTSPICE_SCHEMATIC::LT_SYMBOL& aLTSymbol, int aIndex, SCH_SHEET_PATH* aSheet ); private: - /** - * Method for setting Text Justification. - * - * @param aLineWidth object representing line width from ltspice. - */ int getLineWidth( const LTSPICE_SCHEMATIC::LINEWIDTH& aLineWidth ); - /** - * Method to get line Style For kicad. - * - * @param aLineStyle lineStyle from ltspice. - */ PLOT_DASH_TYPE getLineStyle( const LTSPICE_SCHEMATIC::LINESTYLE& aLineStyle ); STROKE_PARAMS getStroke( const LTSPICE_SCHEMATIC::LINEWIDTH& aLineWidth, @@ -300,7 +287,6 @@ private: private: LTSPICE_SCHEMATIC* m_lt_schematic; - wxFileName m_libraryFileName; VECTOR2I m_originOffset; std::map m_includes; int m_powerSymbolIndex; diff --git a/eeschema/sch_plugins/ltspice/ltspice_sch_plugin.cpp b/eeschema/sch_plugins/ltspice/ltspice_sch_plugin.cpp index 69205c82f1..5267e23035 100644 --- a/eeschema/sch_plugins/ltspice/ltspice_sch_plugin.cpp +++ b/eeschema/sch_plugins/ltspice/ltspice_sch_plugin.cpp @@ -26,8 +26,7 @@ #include #include #include -#include -#include +#include /** * @brief schematic PLUGIN for Ltspice (*.asc) and (.asy) format. @@ -88,7 +87,17 @@ SCH_SHEET* SCH_LTSPICE_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSche wxCHECK_MSG( libTable, nullptr, "Could not load symbol lib table." ); - LTSPICE_SCHEMATIC ascFile( aFileName ); + wxFileName ltspiceDataDir( KIPLATFORM::ENV::GetUserDataPath(), wxEmptyString ); + ltspiceDataDir.RemoveLastDir(); // "kicad" + ltspiceDataDir.AppendDir( wxS( "LTspice" ) ); + + if( !ltspiceDataDir.DirExists() ) + { + ltspiceDataDir = wxFileName( KIPLATFORM::ENV::GetDocumentsPath(), wxEmptyString ); + ltspiceDataDir.AppendDir( wxS( "LTspiceXVII" ) ); + } + + LTSPICE_SCHEMATIC ascFile( aFileName, ltspiceDataDir, nullptr, nullptr ); SCH_PLUGIN::SCH_PLUGIN_RELEASER sch_plugin; sch_plugin.set( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) ); diff --git a/eeschema/sch_plugins/ltspice/ltspice_schematic.cpp b/eeschema/sch_plugins/ltspice/ltspice_schematic.cpp index 7c3669fd0e..2f28403e2a 100644 --- a/eeschema/sch_plugins/ltspice/ltspice_schematic.cpp +++ b/eeschema/sch_plugins/ltspice/ltspice_schematic.cpp @@ -25,11 +25,9 @@ #include #include -#include #include #include #include -#include #include #include #include @@ -164,9 +162,6 @@ void LTSPICE_SCHEMATIC::Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet, curSheet->GetScreen()->SetFileName( m_schematic->Prj().GetProjectPath() + sheetName + ".kicad_sch" ); - - // JEY TODO: do we need this? - //curSheet->addInstance( sheetPath ); } else { @@ -205,10 +200,10 @@ void LTSPICE_SCHEMATIC::GetAscAndAsyFilePaths( std::map& aMa std::map& aMapOfAsyFiles, const wxFileName& parentFileName ) { - wxString ltSpiceFolder = KIPLATFORM::ENV::GetDocumentsPath() + wxS( "/LTspiceXVII" ); - wxString cmpFolder = ltSpiceFolder + wxS( "/lib/cmp/" ); - wxString subFolder = ltSpiceFolder + wxS( "/lib/sub/" ); - wxString symFolder = ltSpiceFolder + wxS( "/lib/sym/" ); + wxString ltSpiceFolder = m_ltspiceDataDir.GetFullPath(); + wxString cmpFolder = ltSpiceFolder + wxS( "lib/cmp/" ); + wxString subFolder = ltSpiceFolder + wxS( "lib/sub/" ); + wxString symFolder = ltSpiceFolder + wxS( "lib/sym/" ); wxArrayString fileList; wxDir::GetAllFiles( ltSpiceFolder, &fileList ); @@ -329,10 +324,7 @@ void LTSPICE_SCHEMATIC::tokensSizeRangeCheck( size_t aActualSize, int aExpectedM void LTSPICE_SCHEMATIC::aggregateAttributeValue( wxArrayString& aTokens, int aIndex ) { - /** - * Merges a value which is across multiple tokens into one token appended with spaces in - * between. - */ + // Merges a value which is across multiple tokens into one token with spaces in between. for( int i = aIndex + 1; i < (int) aTokens.GetCount(); i++ ) aTokens[ aIndex ] += " " + aTokens[i]; } @@ -1038,7 +1030,6 @@ std::vector LTSPICE_SCHEMATIC::StructureBuilder() else if( element == "VERSION" ) { wxString versionNumber = tokens[1]; - ascFile.Version = integerCheck( versionNumber, lineNumber, fileName ); } diff --git a/eeschema/sch_plugins/ltspice/ltspice_schematic.h b/eeschema/sch_plugins/ltspice/ltspice_schematic.h index 51dc667f35..e14b3a8c94 100644 --- a/eeschema/sch_plugins/ltspice/ltspice_schematic.h +++ b/eeschema/sch_plugins/ltspice/ltspice_schematic.h @@ -45,7 +45,6 @@ struct LTSPICE_FILE ParentIndex( 0 ), Sheet( nullptr ), Screen( nullptr ) - { } bool operator<( const LTSPICE_FILE& t ) const @@ -276,17 +275,13 @@ public: BOX2I BoundingBox; }; - explicit LTSPICE_SCHEMATIC( const wxString& aFilename, REPORTER* aReporter = nullptr, - PROGRESS_REPORTER* aProgressReporter = nullptr ) - { - m_schematic = nullptr; - m_rootSheet = nullptr; - m_plugin = nullptr; - m_designCenter.x = 0; - m_designCenter.y = 0; - m_reporter = aReporter; - m_progressReporter = aProgressReporter; - } + explicit LTSPICE_SCHEMATIC( const wxString& aFilename, const wxFileName& aLTspiceDataDir, + REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter ) : + m_reporter( aReporter ), + m_schematic( nullptr ), + m_ltspiceDataDir( aLTspiceDataDir ), + m_progressReporter( aProgressReporter ) + {} ~LTSPICE_SCHEMATIC() {} @@ -352,6 +347,9 @@ public: LT_SYMBOL SymbolBuilder( const wxString& aAscFileName, const wxString& aAsyFileContent, LT_ASC& aAscFile ); + + const wxFileName& GetLTspiceDataDir() { return m_ltspiceDataDir; } + private: /** * Join value present across multiple tokens into one @@ -432,15 +430,10 @@ private: static SYMBOLTYPE getSymbolType( const wxString& aValue ); private: - REPORTER* m_reporter; - SCHEMATIC* m_schematic; - SCH_SHEET* m_rootSheet; - SCH_PLUGIN::SCH_PLUGIN_RELEASER* m_plugin; - wxFileName m_libraryFileName; - wxPoint m_designCenter; //< Used for calculating the required - //< offset to apply to the LTspice design - //< so that it fits in KiCad canvas - PROGRESS_REPORTER* m_progressReporter; // optional; may be nullptr + REPORTER* m_reporter; + SCHEMATIC* m_schematic; + wxFileName m_ltspiceDataDir; + PROGRESS_REPORTER* m_progressReporter; // optional; may be nullptr std::map> m_fileCache; }; diff --git a/libs/kiplatform/gtk/environment.cpp b/libs/kiplatform/gtk/environment.cpp index 8450b7e265..f1f40c1802 100644 --- a/libs/kiplatform/gtk/environment.cpp +++ b/libs/kiplatform/gtk/environment.cpp @@ -109,6 +109,12 @@ wxString KIPLATFORM::ENV::GetUserConfigPath() } +wxString KIPLATFORM::ENV::GetUserDataPath() +{ + return g_get_user_data_dir(); +} + + wxString KIPLATFORM::ENV::GetUserCachePath() { return g_get_user_cache_dir(); diff --git a/libs/kiplatform/include/kiplatform/environment.h b/libs/kiplatform/include/kiplatform/environment.h index 8cfc7e22bc..005ecafec3 100644 --- a/libs/kiplatform/include/kiplatform/environment.h +++ b/libs/kiplatform/include/kiplatform/environment.h @@ -65,6 +65,13 @@ namespace KIPLATFORM */ wxString GetUserConfigPath(); + /** + * Retrieves the operating system specific path for a user's data store + * + * @return User config path + */ + wxString GetUserDataPath(); + /** * Retrieves the operating system specific path for user's application cache * diff --git a/libs/kiplatform/msw/environment.cpp b/libs/kiplatform/msw/environment.cpp index 4c00cb3a68..b96c94abbd 100644 --- a/libs/kiplatform/msw/environment.cpp +++ b/libs/kiplatform/msw/environment.cpp @@ -102,6 +102,20 @@ wxString KIPLATFORM::ENV::GetUserConfigPath() } +wxString KIPLATFORM::ENV::GetUserDataPath() +{ + // If called by a python script in stand-alone (outside KiCad), wxStandardPaths::Get() + // complains about not existing app. so use a dummy app + if( wxTheApp == nullptr ) + { + wxApp dummy; + return wxStandardPaths::Get().GetUserDataDir(); + } + + return wxStandardPaths::Get().GetUserDataDir(); +} + + wxString KIPLATFORM::ENV::GetUserCachePath() { // Unfortunately AppData/Local is the closest analog to "Cache" directories of other platforms diff --git a/libs/kiplatform/osx/environment.mm b/libs/kiplatform/osx/environment.mm index 8483de812d..3ea07ae557 100644 --- a/libs/kiplatform/osx/environment.mm +++ b/libs/kiplatform/osx/environment.mm @@ -85,6 +85,12 @@ wxString KIPLATFORM::ENV::GetUserConfigPath() } +wxString KIPLATFORM::ENV::GetUserDataPath() +{ + return wxStandardPaths::Get().GetUserDataDir(); +} + + wxString KIPLATFORM::ENV::GetUserCachePath() { NSURL* url = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory