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.

205 lines
5.2 KiB

  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-2017 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 <common.h>
  30. #include <fctsys.h>
  31. #include <footprint_info.h>
  32. #include <fp_lib_table.h>
  33. #include <html_messagebox.h>
  34. #include <io_mgr.h>
  35. #include <kiface_ids.h>
  36. #include <kiway.h>
  37. #include <lib_id.h>
  38. #include <macros.h>
  39. #include <pgm_base.h>
  40. #include <thread>
  41. #include <utility>
  42. #include <wildcards_and_files_ext.h>
  43. FOOTPRINT_INFO* FOOTPRINT_LIST::GetModuleInfo( const wxString& aLibNickname,
  44. const wxString& aFootprintName )
  45. {
  46. if( aFootprintName.IsEmpty() )
  47. return NULL;
  48. for( auto& fp : m_list )
  49. {
  50. if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
  51. return &*fp;
  52. }
  53. return NULL;
  54. }
  55. FOOTPRINT_INFO* FOOTPRINT_LIST::GetModuleInfo( const wxString& aFootprintName )
  56. {
  57. if( aFootprintName.IsEmpty() )
  58. return NULL;
  59. LIB_ID fpid;
  60. wxCHECK_MSG( fpid.Parse( aFootprintName, LIB_ID::ID_PCB ) < 0, NULL,
  61. wxString::Format( wxT( "\"%s\" is not a valid LIB_ID." ), aFootprintName ) );
  62. return GetModuleInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
  63. }
  64. bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
  65. {
  66. return aLibrary == m_nickname;
  67. }
  68. void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
  69. {
  70. // @todo: go to a more HTML !<table>! ? centric output, possibly with
  71. // recommendations for remedy of errors. Add numeric error codes
  72. // to PARSE_ERROR, and switch on them for remedies, etc. Full
  73. // access is provided to everything in every exception!
  74. HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
  75. dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
  76. wxString msg;
  77. while( auto error = PopError() )
  78. {
  79. wxString tmp = error->Problem();
  80. // Preserve new lines in error messages so queued errors don't run together.
  81. tmp.Replace( "\n", "<BR>" );
  82. msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
  83. }
  84. dlg.AddHTML_Text( msg );
  85. dlg.ShowModal();
  86. }
  87. static FOOTPRINT_LIST* get_instance_from_id( KIWAY& aKiway, int aId )
  88. {
  89. void* ptr = nullptr;
  90. try
  91. {
  92. KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
  93. if( !kiface )
  94. return nullptr;
  95. ptr = kiface->IfaceOrAddress( aId );
  96. if( !ptr )
  97. return nullptr;
  98. }
  99. catch( ... )
  100. {
  101. return nullptr;
  102. }
  103. return static_cast<FOOTPRINT_LIST*>( ptr );
  104. }
  105. FOOTPRINT_LIST* FOOTPRINT_LIST::GetInstance( KIWAY& aKiway )
  106. {
  107. FOOTPRINT_LIST* footprintInfo = get_instance_from_id( aKiway, KIFACE_FOOTPRINT_LIST );
  108. if( ! footprintInfo )
  109. return nullptr;
  110. if( !footprintInfo->GetCount() )
  111. {
  112. wxTextFile footprintInfoCache( aKiway.Prj().GetProjectPath() + "fp-info-cache" );
  113. footprintInfo->ReadCacheFromFile( &footprintInfoCache );
  114. }
  115. return footprintInfo;
  116. }
  117. FOOTPRINT_ASYNC_LOADER::FOOTPRINT_ASYNC_LOADER() : m_list( nullptr )
  118. {
  119. m_total_libs = 0;
  120. }
  121. FOOTPRINT_ASYNC_LOADER::~FOOTPRINT_ASYNC_LOADER()
  122. {
  123. // This is NOP if the load has finished
  124. Abort();
  125. }
  126. void FOOTPRINT_ASYNC_LOADER::SetList( FOOTPRINT_LIST* aList )
  127. {
  128. m_list = aList;
  129. }
  130. void FOOTPRINT_ASYNC_LOADER::Start(
  131. FP_LIB_TABLE* aTable, wxString const* aNickname, unsigned aNThreads )
  132. {
  133. // Capture the FP_LIB_TABLE into m_last_table. Formatting it as a string instead of storing the
  134. // raw data avoids having to pull in the FP-specific parts.
  135. STRING_FORMATTER sof;
  136. aTable->Format( &sof, 0 );
  137. m_last_table = sof.GetString();
  138. m_list->StartWorkers( aTable, aNickname, this, aNThreads );
  139. }
  140. bool FOOTPRINT_ASYNC_LOADER::Join()
  141. {
  142. if( m_list )
  143. {
  144. bool rv = m_list->JoinWorkers();
  145. m_list = nullptr;
  146. return rv;
  147. }
  148. else
  149. return true;
  150. }
  151. void FOOTPRINT_ASYNC_LOADER::Abort()
  152. {
  153. if( m_list )
  154. {
  155. m_list->StopWorkers();
  156. m_list = nullptr;
  157. }
  158. }