KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ltspice_sch_plugin.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 (C) 2022-2023 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 along
19 * with this program. If not, see <http://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
36const wxString SCH_LTSPICE_PLUGIN::GetName() const
37{
38 return wxT( "LTspice Schematic Importer" );
39}
40
41
43{
44 return 0;
45}
46
47
48SCH_SHEET* SCH_LTSPICE_PLUGIN::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
49 SCH_SHEET* aAppendToMe,
50 const STRING_UTF8_MAP* aProperties )
51{
52 wxASSERT( !aFileName || aSchematic );
53
54 SCH_SHEET* rootSheet = nullptr;
55
56 if( aAppendToMe )
57 {
58 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
59 rootSheet = &aSchematic->Root();
60 }
61 else
62 {
63 rootSheet = new SCH_SHEET( aSchematic );
64 rootSheet->SetFileName( aFileName );
65 aSchematic->SetRoot( rootSheet );
66 }
67
68 if( !rootSheet->GetScreen() )
69 {
70 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
71
72 screen->SetFileName( aFileName );
73 rootSheet->SetScreen( screen );
74 }
75
76 SYMBOL_LIB_TABLE* libTable = PROJECT_SCH::SchSymbolLibTable( &aSchematic->Prj() );
77
78 wxCHECK_MSG( libTable, nullptr, "Could not load symbol lib table." );
79
80 // Windows path: C:\Users\USERNAME\AppData\Local\LTspice\lib
81 wxFileName ltspiceDataDir( KIPLATFORM::ENV::GetUserLocalDataPath(), wxEmptyString );
82 ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
83 ltspiceDataDir.AppendDir( wxS( "lib" ) );
84
85 if( !ltspiceDataDir.DirExists() )
86 {
87 // Mac path
88 ltspiceDataDir = wxFileName( KIPLATFORM::ENV::GetUserDataPath(), wxEmptyString );
89 ltspiceDataDir.RemoveLastDir(); // "kicad"
90 ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
91 ltspiceDataDir.AppendDir( wxS( "lib" ) );
92 }
93
94 if( !ltspiceDataDir.DirExists() )
95 {
96 // See if user has older version of LTspice installed (e.g. C:\Users\USERNAME\Documents\LTspiceXVII\lib
97 wxString foundFile = wxFindFirstFile( KIPLATFORM::ENV::GetDocumentsPath() + wxFileName::GetPathSeparator() + "LTspice*", wxDIR );
98
99 while( !foundFile.empty() )
100 {
101 ltspiceDataDir = wxFileName(foundFile, wxEmptyString);
102 ltspiceDataDir.AppendDir( wxS( "lib" ) );
103
104 if( ltspiceDataDir.DirExists() )
105 break;
106
107 foundFile = wxFindNextFile();
108 }
109 }
110
111 try
112 {
113 LTSPICE_SCHEMATIC ascFile( aFileName, ltspiceDataDir, m_reporter, m_progressReporter );
114 ascFile.Load( aSchematic, rootSheet, aFileName, m_reporter );
115 }
116 catch( IO_ERROR& e )
117 {
119 }
120
122
123 return rootSheet;
124}
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(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxFileName &aLibraryFileName, REPORTER *aReporter)
The main function responsible for loading the .asc and .asy files.
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
Holds all the data relating to one schematic.
Definition: schematic.h:75
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:136
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:113
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:121
SCH_SHEET & Root() const
Definition: schematic.h:105
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:90
int GetModifyHash() const override
Return the modification hash from the library cache.
PROGRESS_REPORTER * m_progressReporter
const wxString GetName() const override
schematic PLUGIN for LTspice (*.asc) and (.asy) format.
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const STRING_UTF8_MAP *aProperties=nullptr) override
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:114
void UpdateAllScreenReferences() const
Update all the symbol references for this sheet path.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
void SetFileName(const wxString &aFilename)
Definition: sch_sheet.h:314
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:110
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:162
A name/value tuple with unique names and optional values.
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_ERROR