You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 Chetan Subhash Shinde<chetanshinde2001@gmail.com>
  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 along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <sch_io/ltspice/ltspice_schematic.h>
  22. #include <sch_io/ltspice/sch_io_ltspice.h>
  23. #include <sch_io/ltspice/sch_io_ltspice_parser.h>
  24. #include <project_sch.h>
  25. #include <schematic.h>
  26. #include <sch_sheet.h>
  27. #include <sch_screen.h>
  28. #include <symbol_lib_table.h>
  29. #include <kiplatform/environment.h>
  30. /**
  31. * @brief schematic PLUGIN for LTspice (*.asc) and (.asy) format.
  32. */
  33. int SCH_IO_LTSPICE::GetModifyHash() const
  34. {
  35. return 0;
  36. }
  37. SCH_SHEET* SCH_IO_LTSPICE::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
  38. SCH_SHEET* aAppendToMe,
  39. const std::map<std::string, UTF8>* aProperties )
  40. {
  41. wxASSERT( !aFileName || aSchematic );
  42. SCH_SHEET* rootSheet = nullptr;
  43. if( aAppendToMe )
  44. {
  45. wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
  46. rootSheet = &aSchematic->Root();
  47. }
  48. else
  49. {
  50. rootSheet = new SCH_SHEET( aSchematic );
  51. rootSheet->SetFileName( aFileName );
  52. aSchematic->SetRoot( rootSheet );
  53. }
  54. if( !rootSheet->GetScreen() )
  55. {
  56. SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
  57. screen->SetFileName( aFileName );
  58. rootSheet->SetScreen( screen );
  59. // Virtual root sheet UUID must be the same as the schematic file UUID.
  60. const_cast<KIID&>( rootSheet->m_Uuid ) = screen->GetUuid();
  61. }
  62. SYMBOL_LIB_TABLE* libTable = PROJECT_SCH::SchSymbolLibTable( &aSchematic->Prj() );
  63. wxCHECK_MSG( libTable, nullptr, "Could not load symbol lib table." );
  64. // Windows path: C:\Users\USERNAME\AppData\Local\LTspice\lib
  65. wxFileName ltspiceDataDir( KIPLATFORM::ENV::GetUserLocalDataPath(), wxEmptyString );
  66. ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
  67. ltspiceDataDir.AppendDir( wxS( "lib" ) );
  68. if( !ltspiceDataDir.DirExists() )
  69. {
  70. // Mac path
  71. ltspiceDataDir = wxFileName( KIPLATFORM::ENV::GetUserDataPath(), wxEmptyString );
  72. ltspiceDataDir.RemoveLastDir(); // "kicad"
  73. ltspiceDataDir.AppendDir( wxS( "LTspice" ) );
  74. ltspiceDataDir.AppendDir( wxS( "lib" ) );
  75. }
  76. if( !ltspiceDataDir.DirExists() )
  77. {
  78. // See if user has older version of LTspice installed
  79. // (e.g. C:\Users\USERNAME\Documents\LTspiceXVII\lib
  80. wxString foundFile = wxFindFirstFile( KIPLATFORM::ENV::GetDocumentsPath() +
  81. wxFileName::GetPathSeparator() + "LTspice*", wxDIR );
  82. while( !foundFile.empty() )
  83. {
  84. ltspiceDataDir = wxFileName(foundFile, wxEmptyString);
  85. ltspiceDataDir.AppendDir( wxS( "lib" ) );
  86. if( ltspiceDataDir.DirExists() )
  87. break;
  88. foundFile = wxFindNextFile();
  89. }
  90. }
  91. try
  92. {
  93. LTSPICE_SCHEMATIC ascFile( aFileName, ltspiceDataDir, m_reporter, m_progressReporter );
  94. ascFile.Load( aSchematic, rootSheet, aFileName, m_reporter );
  95. }
  96. catch( IO_ERROR& e )
  97. {
  98. m_reporter->Report( e.What(), RPT_SEVERITY_ERROR );
  99. }
  100. aSchematic->CurrentSheet().UpdateAllScreenReferences();
  101. return rootSheet;
  102. }