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.

252 lines
6.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. * Copyright (C) 2019 CERN
  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. #include <macros.h>
  26. #include <trace_helpers.h>
  27. #include <ee_collectors.h>
  28. #include <lib_item.h>
  29. #include <sch_bus_entry.h>
  30. #include <sch_symbol.h>
  31. #include <sch_line.h>
  32. #include <sch_screen.h>
  33. #include <sch_sheet_path.h>
  34. #include <transform.h>
  35. #include "sch_reference_list.h"
  36. const KICAD_T EE_COLLECTOR::AllItems[] = {
  37. SCH_LOCATE_ANY_T,
  38. EOT
  39. };
  40. const KICAD_T EE_COLLECTOR::EditableItems[] = {
  41. SCH_TEXT_T,
  42. SCH_LABEL_T,
  43. SCH_GLOBAL_LABEL_T,
  44. SCH_HIER_LABEL_T,
  45. SCH_FIELD_T,
  46. SCH_SYMBOL_T,
  47. SCH_SHEET_PIN_T,
  48. SCH_SHEET_T,
  49. SCH_BITMAP_T,
  50. SCH_LINE_T,
  51. SCH_BUS_WIRE_ENTRY_T,
  52. SCH_JUNCTION_T,
  53. EOT
  54. };
  55. const KICAD_T EE_COLLECTOR::SymbolsOnly[] = {
  56. SCH_SYMBOL_T,
  57. EOT
  58. };
  59. const KICAD_T EE_COLLECTOR::SheetsOnly[] = {
  60. SCH_SHEET_T,
  61. EOT
  62. };
  63. const KICAD_T EE_COLLECTOR::MovableItems[] =
  64. {
  65. SCH_MARKER_T,
  66. SCH_JUNCTION_T,
  67. SCH_NO_CONNECT_T,
  68. SCH_BUS_BUS_ENTRY_T,
  69. SCH_BUS_WIRE_ENTRY_T,
  70. SCH_LINE_T,
  71. SCH_BITMAP_T,
  72. SCH_TEXT_T,
  73. SCH_LABEL_T,
  74. SCH_GLOBAL_LABEL_T,
  75. SCH_HIER_LABEL_T,
  76. SCH_FIELD_T,
  77. SCH_SYMBOL_T,
  78. SCH_SHEET_PIN_T,
  79. SCH_SHEET_T,
  80. EOT
  81. };
  82. const KICAD_T EE_COLLECTOR::WiresOnly[] = {
  83. SCH_LINE_T,
  84. EOT
  85. };
  86. const KICAD_T EE_COLLECTOR::FieldOwners[] = {
  87. SCH_SYMBOL_T,
  88. SCH_SHEET_T,
  89. SCH_GLOBAL_LABEL_T,
  90. EOT
  91. };
  92. SEARCH_RESULT EE_COLLECTOR::Inspect( EDA_ITEM* aItem, void* aTestData )
  93. {
  94. if( aItem->Type() == LIB_PIN_T )
  95. {
  96. // Special selection rules apply to pins of different units when edited in
  97. // synchronized pins mode. Leave it to EE_SELECTION_TOOL::Selectable() to
  98. // decide what to do with them.
  99. }
  100. else if( m_Unit || m_Convert )
  101. {
  102. LIB_ITEM* lib_item = dynamic_cast<LIB_ITEM*>( aItem );
  103. if( m_Unit && lib_item && lib_item->GetUnit() && lib_item->GetUnit() != m_Unit )
  104. return SEARCH_RESULT::CONTINUE;
  105. if( m_Convert && lib_item && lib_item->GetConvert() && lib_item->GetConvert() != m_Convert )
  106. return SEARCH_RESULT::CONTINUE;
  107. }
  108. if( aItem->HitTest( m_refPos, m_Threshold ) )
  109. Append( aItem );
  110. return SEARCH_RESULT::CONTINUE;
  111. }
  112. void EE_COLLECTOR::Collect( SCH_SCREEN* aScreen, const KICAD_T aFilterList[], const wxPoint& aPos,
  113. int aUnit, int aConvert )
  114. {
  115. Empty(); // empty the collection just in case
  116. SetScanTypes( aFilterList );
  117. m_Unit = aUnit;
  118. m_Convert = aConvert;
  119. // remember where the snapshot was taken from and pass refPos to the Inspect() function.
  120. SetRefPos( aPos );
  121. if( aScreen )
  122. {
  123. // Symbols and sheets own their own children so have to be visited even if
  124. // they're not in the filter list
  125. bool symbolsVisited = false;
  126. bool sheetsVisited = false;
  127. bool globalLabelsVisited = false;
  128. EE_RTREE& items = aScreen->Items();
  129. for( const KICAD_T *filter = aFilterList; *filter != EOT; ++filter )
  130. {
  131. for( SCH_ITEM *item : items.Overlapping( *filter, aPos, m_Threshold ) )
  132. {
  133. if( *filter == SCH_SYMBOL_T || *filter == SCH_LOCATE_ANY_T )
  134. symbolsVisited = true;
  135. if( *filter == SCH_SHEET_T || *filter == SCH_LOCATE_ANY_T )
  136. sheetsVisited = true;
  137. if( *filter == SCH_GLOBAL_LABEL_T || *filter == SCH_LOCATE_ANY_T )
  138. globalLabelsVisited = true;
  139. item->Visit( m_inspector, nullptr, m_scanTypes );
  140. }
  141. }
  142. if( !symbolsVisited )
  143. {
  144. for( SCH_ITEM *item : items.Overlapping( SCH_SYMBOL_T, aPos, m_Threshold ) )
  145. item->Visit( m_inspector, nullptr, m_scanTypes );
  146. }
  147. if( !sheetsVisited )
  148. {
  149. for( SCH_ITEM *item : items.Overlapping( SCH_SHEET_T, aPos, m_Threshold ) )
  150. item->Visit( m_inspector, nullptr, m_scanTypes );
  151. }
  152. if( !globalLabelsVisited )
  153. {
  154. for( SCH_ITEM *item : items.Overlapping( SCH_GLOBAL_LABEL_T, aPos, m_Threshold ) )
  155. item->Visit( m_inspector, nullptr, m_scanTypes );
  156. }
  157. }
  158. }
  159. void EE_COLLECTOR::Collect( LIB_ITEMS_CONTAINER& aItems, const KICAD_T aFilterList[],
  160. const wxPoint& aPos, int aUnit, int aConvert )
  161. {
  162. Empty(); // empty the collection just in case
  163. SetScanTypes( aFilterList );
  164. m_Unit = aUnit;
  165. m_Convert = aConvert;
  166. // remember where the snapshot was taken from and pass refPos to the Inspect() function.
  167. SetRefPos( aPos );
  168. for( LIB_ITEM& item : aItems )
  169. {
  170. if( item.Visit( m_inspector, nullptr, m_scanTypes ) == SEARCH_RESULT::QUIT )
  171. break;
  172. }
  173. }
  174. bool EE_COLLECTOR::IsCorner() const
  175. {
  176. if( GetCount() != 2 )
  177. return false;
  178. bool is_busentry0 = ( dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_list[0] ) != nullptr );
  179. bool is_busentry1 = ( dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_list[1] ) != nullptr );
  180. if(( m_list[0]->Type() == SCH_LINE_T) && ( m_list[1]->Type() == SCH_LINE_T) )
  181. return ( ( SCH_LINE* ) m_list[0])->GetLayer() == ( ( SCH_LINE* ) m_list[1])->GetLayer();
  182. if(( m_list[0]->Type() == SCH_LINE_T) && is_busentry1 )
  183. return true;
  184. if( is_busentry0 && ( m_list[1]->Type() == SCH_LINE_T) )
  185. return true;
  186. return false;
  187. }
  188. void CollectOtherUnits( const wxString& aRef, int aUnit, const LIB_ID& aLibId,
  189. SCH_SHEET_PATH& aSheet, std::vector<SCH_SYMBOL*>* otherUnits )
  190. {
  191. SCH_REFERENCE_LIST symbols;
  192. aSheet.GetSymbols( symbols );
  193. for( unsigned i = 0; i < symbols.GetCount(); i++ )
  194. {
  195. SCH_REFERENCE symbol = symbols[i];
  196. if( symbol.GetRef() == aRef && symbol.GetSymbol()->GetLibId() == aLibId
  197. && symbol.GetUnit() != aUnit )
  198. otherUnits->push_back( symbol.GetSymbol() );
  199. }
  200. }