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.

154 lines
4.7 KiB

5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011 Jean-Pierre Charras, <jp.charras@wanadoo.fr>
  5. * Copyright (C) 2013-2016 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU 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, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /*
  26. * Functions to read footprint libraries and fill m_footprints by available footprints names
  27. * and their documentation (comments and keywords)
  28. */
  29. #include <footprint_info.h>
  30. #include <fp_lib_table.h>
  31. #include <dialogs/html_message_box.h>
  32. #include <string_utils.h>
  33. #include <kiface_ids.h>
  34. #include <kiway.h>
  35. #include <lib_id.h>
  36. #include <thread>
  37. #include <utility>
  38. #include <kiface_base.h>
  39. FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aLibNickname,
  40. const wxString& aFootprintName )
  41. {
  42. if( aFootprintName.IsEmpty() )
  43. return nullptr;
  44. for( std::unique_ptr<FOOTPRINT_INFO>& fp : m_list )
  45. {
  46. if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
  47. return fp.get();
  48. }
  49. return nullptr;
  50. }
  51. FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aFootprintName )
  52. {
  53. if( aFootprintName.IsEmpty() )
  54. return nullptr;
  55. LIB_ID fpid;
  56. wxCHECK_MSG( fpid.Parse( aFootprintName ) < 0, nullptr,
  57. wxString::Format( wxT( "'%s' is not a valid LIB_ID." ), aFootprintName ) );
  58. return GetFootprintInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
  59. }
  60. bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
  61. {
  62. return aLibrary == m_nickname;
  63. }
  64. bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs )
  65. {
  66. int retv = StrNumCmp( lhs.m_nickname, rhs.m_nickname, false );
  67. if( retv != 0 )
  68. return retv < 0;
  69. // Technically footprint names are not case sensitive because the file name is used
  70. // as the footprint name. On windows this would be problematic because windows does
  71. // not support case sensitive file names by default. This should not cause any issues
  72. // and allow for a future change to use the name defined in the footprint file.
  73. return StrNumCmp( lhs.m_fpname, rhs.m_fpname, false ) < 0;
  74. }
  75. void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
  76. {
  77. // @todo: go to a more HTML !<table>! ? centric output, possibly with recommendations
  78. // for remedy of errors. Add numeric error codes to PARSE_ERROR, and switch on them for
  79. // remedies, etc. Full access is provided to everything in every exception!
  80. HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
  81. dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
  82. wxString msg;
  83. while( std::unique_ptr<IO_ERROR> error = PopError() )
  84. {
  85. wxString tmp = error->Problem();
  86. // Preserve new lines in error messages so queued errors don't run together.
  87. tmp.Replace( "\n", "<BR>" );
  88. msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
  89. }
  90. dlg.AddHTML_Text( msg );
  91. dlg.ShowModal();
  92. }
  93. static FOOTPRINT_LIST* get_instance_from_id( KIWAY& aKiway, int aId )
  94. {
  95. void* ptr = nullptr;
  96. try
  97. {
  98. ptr = Kiface().IfaceOrAddress( aId );
  99. if( !ptr )
  100. {
  101. KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
  102. ptr = kiface->IfaceOrAddress( aId );
  103. }
  104. return static_cast<FOOTPRINT_LIST*>( ptr );
  105. }
  106. catch( ... )
  107. {
  108. return nullptr;
  109. }
  110. }
  111. FOOTPRINT_LIST* FOOTPRINT_LIST::GetInstance( KIWAY& aKiway )
  112. {
  113. FOOTPRINT_LIST* footprintInfo = get_instance_from_id( aKiway, KIFACE_FOOTPRINT_LIST );
  114. if( !footprintInfo )
  115. return nullptr;
  116. if( !footprintInfo->GetCount() )
  117. footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + "fp-info-cache" );
  118. return footprintInfo;
  119. }