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 <schematic.h>
28#include <sch_sheet.h>
29#include <sch_screen.h>
31
35
37{
38 return 0;
39}
40
41
42SCH_SHEET* SCH_IO_LTSPICE::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic, SCH_SHEET* aAppendToMe,
43 const std::map<std::string, UTF8>* aProperties )
44{
45 wxASSERT( !aFileName || aSchematic );
46
47 SCH_SHEET* rootSheet = nullptr;
48
49 if( aAppendToMe )
50 {
51 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
52 rootSheet = aAppendToMe;
53 }
54 else
55 {
56 rootSheet = new SCH_SHEET( aSchematic );
57 rootSheet->SetFileName( aFileName );
58 aSchematic->SetTopLevelSheets( { rootSheet } );
59 }
60
61 if( !rootSheet->GetScreen() )
62 {
63 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
64
65 screen->SetFileName( aFileName );
66 rootSheet->SetScreen( screen );
67
68 // Virtual root sheet UUID must be the same as the schematic file UUID.
69 rootSheet->SyncUuidToScreen();
70 }
71
72 // Windows path: C:\Users\USERNAME\AppData\Local\LTspice\lib
73 wxFileName ltspiceDataDir( KIPLATFORM::ENV::GetUserLocalDataPath(), wxEmptyString );
74 ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
75 ltspiceDataDir.AppendDir( wxS( "lib" ) );
76
77 if( !ltspiceDataDir.DirExists() )
78 {
79 // Mac path
80 ltspiceDataDir = wxFileName( KIPLATFORM::ENV::GetUserDataPath(), wxEmptyString );
81 ltspiceDataDir.RemoveLastDir(); // "kicad"
82 ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
83 ltspiceDataDir.AppendDir( wxS( "lib" ) );
84 }
85
86#ifdef __APPLE__
87 if( !ltspiceDataDir.DirExists() )
88 {
89 // LTspice 26 Mac path
90 ltspiceDataDir = wxFileName( "/Applications/LTspice.app/Contents/SharedSupport/ltspice/support/ltspice/drive_c/"
91 "users/crossover/AppData/Local/LTspice/lib",
92 wxEmptyString );
93 }
94#endif
95
96 if( !ltspiceDataDir.DirExists() )
97 {
98 // See if user has older version of LTspice installed
99 // (e.g. C:\Users\USERNAME\Documents\LTspiceXVII\lib
100 wxString foundFile = wxFindFirstFile( KIPLATFORM::ENV::GetDocumentsPath() +
101 wxFileName::GetPathSeparator() + "LTspice*", wxDIR );
102
103 while( !foundFile.empty() )
104 {
105 ltspiceDataDir = wxFileName(foundFile, wxEmptyString);
106 ltspiceDataDir.AppendDir( wxS( "lib" ) );
107
108 if( ltspiceDataDir.DirExists() )
109 break;
110
111 foundFile = wxFindNextFile();
112 }
113 }
114
115 if( !ltspiceDataDir.DirExists() )
116 {
117 wxFileName fn( aFileName );
118 fn.SetFullName( wxEmptyString );
119 fn.AppendDir( "lib" );
120
121 wxString localLibPath = fn.GetFullPath();
122
123 if( wxDirExists( localLibPath ) )
124 {
125 ltspiceDataDir = localLibPath;
126 }
127 else
128 {
129 m_reporter->Report( wxString::Format( _( "Unable to find LTspice symbols.\nInstall "
130 "LTspice or put its library files into %s" ),
131 localLibPath ),
133 }
134 }
135
136 try
137 {
138 LTSPICE_SCHEMATIC ascFile( aFileName, ltspiceDataDir, m_reporter, m_progressReporter );
139 ascFile.Load( aSchematic, rootSheet, aFileName, m_reporter );
140 }
141 catch( IO_ERROR& e )
142 {
143 m_reporter->Report( e.What(), RPT_SEVERITY_ERROR );
144 }
145
146 return rootSheet;
147}
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition io_base.h:242
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:245
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:149
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:297
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,...
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:48
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:398
void SyncUuidToScreen()
Take the identity of the screen this sheet owns.
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:151
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