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.

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