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.

174 lines
5.3 KiB

5 years ago
3 years ago
3 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 The 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 <wx/tokenzr.h>
  39. #include <kiface_base.h>
  40. FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aLibNickname,
  41. const wxString& aFootprintName )
  42. {
  43. if( aFootprintName.IsEmpty() )
  44. return nullptr;
  45. for( std::unique_ptr<FOOTPRINT_INFO>& fp : m_list )
  46. {
  47. if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
  48. return fp.get();
  49. }
  50. return nullptr;
  51. }
  52. FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aFootprintName )
  53. {
  54. if( aFootprintName.IsEmpty() )
  55. return nullptr;
  56. LIB_ID fpid;
  57. wxCHECK_MSG( fpid.Parse( aFootprintName ) < 0, nullptr,
  58. wxString::Format( wxT( "'%s' is not a valid LIB_ID." ), aFootprintName ) );
  59. return GetFootprintInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
  60. }
  61. std::vector<SEARCH_TERM> FOOTPRINT_INFO::GetSearchTerms()
  62. {
  63. std::vector<SEARCH_TERM> terms;
  64. terms.emplace_back( SEARCH_TERM( GetName(), 8 ) );
  65. wxStringTokenizer keywordTokenizer( GetKeywords(), wxS( " " ), wxTOKEN_STRTOK );
  66. while( keywordTokenizer.HasMoreTokens() )
  67. terms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
  68. // Also include keywords as one long string, just in case
  69. terms.emplace_back( SEARCH_TERM( GetKeywords(), 1 ) );
  70. terms.emplace_back( SEARCH_TERM( GetDesc(), 1 ) );
  71. return terms;
  72. }
  73. bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
  74. {
  75. return aLibrary == m_nickname;
  76. }
  77. bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs )
  78. {
  79. int retv = StrNumCmp( lhs.m_nickname, rhs.m_nickname, false );
  80. if( retv != 0 )
  81. return retv < 0;
  82. // Technically footprint names are not case sensitive because the file name is used
  83. // as the footprint name. On windows this would be problematic because windows does
  84. // not support case sensitive file names by default. This should not cause any issues
  85. // and allow for a future change to use the name defined in the footprint file.
  86. return StrNumCmp( lhs.m_fpname, rhs.m_fpname, false ) < 0;
  87. }
  88. void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
  89. {
  90. // @todo: go to a more HTML !<table>! ? centric output, possibly with recommendations
  91. // for remedy of errors. Add numeric error codes to PARSE_ERROR, and switch on them for
  92. // remedies, etc. Full access is provided to everything in every exception!
  93. HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
  94. dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
  95. wxString msg;
  96. while( std::unique_ptr<IO_ERROR> error = PopError() )
  97. {
  98. wxString tmp = EscapeHTML( error->Problem() );
  99. // Preserve new lines in error messages so queued errors don't run together.
  100. tmp.Replace( wxS( "\n" ), wxS( "<BR>" ) );
  101. msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
  102. }
  103. dlg.AddHTML_Text( msg );
  104. dlg.ShowModal();
  105. }
  106. static FOOTPRINT_LIST* get_instance_from_id( KIWAY& aKiway, int aId )
  107. {
  108. void* ptr = nullptr;
  109. try
  110. {
  111. ptr = Kiface().IfaceOrAddress( aId );
  112. if( !ptr )
  113. {
  114. KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
  115. ptr = kiface->IfaceOrAddress( aId );
  116. }
  117. return static_cast<FOOTPRINT_LIST*>( ptr );
  118. }
  119. catch( ... )
  120. {
  121. return nullptr;
  122. }
  123. }
  124. FOOTPRINT_LIST* FOOTPRINT_LIST::GetInstance( KIWAY& aKiway )
  125. {
  126. FOOTPRINT_LIST* footprintInfo = get_instance_from_id( aKiway, KIFACE_FOOTPRINT_LIST );
  127. if( !footprintInfo )
  128. return nullptr;
  129. if( !footprintInfo->GetCount() )
  130. footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + wxS( "fp-info-cache" ) );
  131. return footprintInfo;
  132. }