KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_ltspice.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) 2022 Chetan Subhash Shinde<[email protected]>
5 * Copyright (C) 2023 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
25
26#include <project_sch.h>
27#include <richio.h>
28#include <schematic.h>
29#include <sch_sheet.h>
30#include <sch_screen.h>
32
36
38{
39 return 0;
40}
41
42
43bool SCH_IO_LTSPICE::CanReadSchematicFile( const wxString& aFileName ) const
44{
45 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
46 return false;
47
48 // PADS uses .asc too, and LTspice files can be UTF-16 so the header needs decoding
49 try
50 {
51 return SafeReadFile( aFileName, wxS( "r" ) ).Left( 7 ).Upper() == wxS( "VERSION" );
52 }
53 catch( const IO_ERROR& )
54 {
55 return false;
56 }
57}
58
59
60SCH_SHEET* SCH_IO_LTSPICE::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic, SCH_SHEET* aAppendToMe,
61 const std::map<std::string, UTF8>* aProperties )
62{
63 wxASSERT( !aFileName || aSchematic );
64
65 SCH_SHEET* rootSheet = nullptr;
66
67 if( aAppendToMe )
68 {
69 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
70 rootSheet = aAppendToMe;
71 }
72 else
73 {
74 rootSheet = new SCH_SHEET( aSchematic );
75 rootSheet->SetFileName( aFileName );
76 aSchematic->SetTopLevelSheets( { rootSheet } );
77 }
78
79 if( !rootSheet->GetScreen() )
80 {
81 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
82
83 screen->SetFileName( aFileName );
84 rootSheet->SetScreen( screen );
85
86 // Virtual root sheet UUID must be the same as the schematic file UUID.
87 rootSheet->SyncUuidToScreen();
88 }
89
90 // An explicit library location wins over the places LTspice installs itself
91 wxString libOverride;
92 wxFileName ltspiceDataDir;
93
94 if( wxGetEnv( wxS( "KICAD_LTSPICE_LIB" ), &libOverride ) && wxDirExists( libOverride ) )
95 ltspiceDataDir = wxFileName( libOverride, wxEmptyString );
96
97 // Windows path: C:\Users\USERNAME\AppData\Local\LTspice\lib
98 if( !ltspiceDataDir.IsOk() )
99 {
100 ltspiceDataDir = wxFileName( KIPLATFORM::ENV::GetUserLocalDataPath(), wxEmptyString );
101 ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
102 ltspiceDataDir.AppendDir( wxS( "lib" ) );
103 }
104
105 if( !ltspiceDataDir.DirExists() )
106 {
107 // Mac path
108 ltspiceDataDir = wxFileName( KIPLATFORM::ENV::GetUserDataPath(), wxEmptyString );
109 ltspiceDataDir.RemoveLastDir(); // "kicad"
110 ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
111 ltspiceDataDir.AppendDir( wxS( "lib" ) );
112 }
113
114#ifdef __APPLE__
115 if( !ltspiceDataDir.DirExists() )
116 {
117 // LTspice 26 Mac path
118 ltspiceDataDir = wxFileName( "/Applications/LTspice.app/Contents/SharedSupport/ltspice/support/ltspice/drive_c/"
119 "users/crossover/AppData/Local/LTspice/lib",
120 wxEmptyString );
121 }
122#endif
123
124 if( !ltspiceDataDir.DirExists() && wxDirExists( KIPLATFORM::ENV::GetDocumentsPath() ) )
125 {
126 // See if user has older version of LTspice installed
127 // (e.g. C:\Users\USERNAME\Documents\LTspiceXVII\lib
128 wxString foundFile = wxFindFirstFile( KIPLATFORM::ENV::GetDocumentsPath() +
129 wxFileName::GetPathSeparator() + "LTspice*", wxDIR );
130
131 while( !foundFile.empty() )
132 {
133 ltspiceDataDir = wxFileName(foundFile, wxEmptyString);
134 ltspiceDataDir.AppendDir( wxS( "lib" ) );
135
136 if( ltspiceDataDir.DirExists() )
137 break;
138
139 foundFile = wxFindNextFile();
140 }
141 }
142
143 if( !ltspiceDataDir.DirExists() )
144 {
145 wxFileName fn( aFileName );
146 fn.SetFullName( wxEmptyString );
147 fn.AppendDir( "lib" );
148
149 wxString localLibPath = fn.GetFullPath();
150
151 if( wxDirExists( localLibPath ) )
152 {
153 ltspiceDataDir = localLibPath;
154 }
155 else
156 {
157 m_reporter->Report( wxString::Format( _( "Unable to find LTspice symbols.\nInstall "
158 "LTspice or put its library files into %s" ),
159 localLibPath ),
161 }
162 }
163
164 try
165 {
166 LTSPICE_SCHEMATIC ascFile( aFileName, ltspiceDataDir, m_reporter, m_progressReporter );
167 ascFile.Load( aSchematic, rootSheet, aFileName, m_reporter );
168 }
169 catch( IO_ERROR& e )
170 {
171 m_reporter->Report( e.What(), RPT_SEVERITY_ERROR );
172 }
173
174 return rootSheet;
175}
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition io_base.h:264
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:267
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
void Load(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxFileName &aLibraryFileName, REPORTER *aReporter)
The main function responsible for loading the .asc and .asy files.
Holds all the data relating to one schematic.
Definition schematic.h:155
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:384
void SetTopLevelSheets(const std::vector< SCH_SHEET * > &aSheets)
Replace the top level sheets, rebuilding the hierarchy and connectivity around them.
int GetModifyHash() const override
schematic PLUGIN for LTspice (*.asc) and (.asy) format.
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_IO can read the specified schematic file.
Definition sch_io.cpp:47
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:434
void SyncUuidToScreen()
Take the identity of the screen this sheet owns.
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:187
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
#define _(s)
wxString GetUserDataPath()
Retrieves the operating system specific path for a user's data store.
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
wxString GetUserLocalDataPath()
Retrieves the operating system specific path for a user's local data store.
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
wxString SafeReadFile(const wxString &aFilePath, const wxString &aReadType)
Nominally opens a file and reads it into a string.
Definition richio.cpp:51