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.

212 lines
6.2 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-2020 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_component.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_COMPONENT_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::ComponentsOnly[] = {
  56. SCH_COMPONENT_T,
  57. EOT
  58. };
  59. const KICAD_T EE_COLLECTOR::SheetsOnly[] = {
  60. SCH_SHEET_T,
  61. EOT
  62. };
  63. SEARCH_RESULT EE_COLLECTOR::Inspect( EDA_ITEM* aItem, void* aTestData )
  64. {
  65. if( aItem->Type() == LIB_PIN_T )
  66. {
  67. // Special selection rules apply to pins of different units when edited in
  68. // synchronized pins mode. Leave it to EE_SELECTION_TOOL::Selectable() to
  69. // decide what to do with them.
  70. }
  71. else if( m_Unit || m_Convert )
  72. {
  73. LIB_ITEM* lib_item = dynamic_cast<LIB_ITEM*>( aItem );
  74. if( m_Unit && lib_item && lib_item->GetUnit() && lib_item->GetUnit() != m_Unit )
  75. return SEARCH_RESULT::CONTINUE;
  76. if( m_Convert && lib_item && lib_item->GetConvert() && lib_item->GetConvert() != m_Convert )
  77. return SEARCH_RESULT::CONTINUE;
  78. }
  79. if( aItem->HitTest( m_RefPos, m_Threshold ) )
  80. Append( aItem );
  81. return SEARCH_RESULT::CONTINUE;
  82. }
  83. void EE_COLLECTOR::Collect( SCH_SCREEN* aScreen, const KICAD_T aFilterList[], const wxPoint& aPos,
  84. int aUnit, int aConvert )
  85. {
  86. Empty(); // empty the collection just in case
  87. SetScanTypes( aFilterList );
  88. m_Unit = aUnit;
  89. m_Convert = aConvert;
  90. // remember where the snapshot was taken from and pass refPos to the Inspect() function.
  91. SetRefPos( aPos );
  92. if( aScreen )
  93. {
  94. // Components and sheets own their own children so have to be visited even if
  95. // they're not in the filter list
  96. bool componentsVisited = false;
  97. bool sheetsVisited = false;
  98. for( const KICAD_T* filter = aFilterList; *filter != EOT; ++filter )
  99. {
  100. for( SCH_ITEM* item : aScreen->Items().OfType( *filter ) )
  101. {
  102. if( *filter == SCH_COMPONENT_T || *filter == SCH_LOCATE_ANY_T )
  103. componentsVisited = true;
  104. if( *filter == SCH_SHEET_T || *filter == SCH_LOCATE_ANY_T )
  105. sheetsVisited = true;
  106. item->Visit( m_inspector, nullptr, m_ScanTypes );
  107. }
  108. }
  109. if( !componentsVisited )
  110. {
  111. for( SCH_ITEM* item : aScreen->Items().OfType( SCH_COMPONENT_T ) )
  112. item->Visit( m_inspector, nullptr, m_ScanTypes );
  113. }
  114. if( !sheetsVisited )
  115. {
  116. for( SCH_ITEM* item : aScreen->Items().OfType( SCH_SHEET_T ) )
  117. item->Visit( m_inspector, nullptr, m_ScanTypes );
  118. }
  119. }
  120. }
  121. void EE_COLLECTOR::Collect( LIB_ITEMS_CONTAINER& aItems, const KICAD_T aFilterList[],
  122. const wxPoint& aPos, int aUnit, int aConvert )
  123. {
  124. Empty(); // empty the collection just in case
  125. SetScanTypes( aFilterList );
  126. m_Unit = aUnit;
  127. m_Convert = aConvert;
  128. // remember where the snapshot was taken from and pass refPos to the Inspect() function.
  129. SetRefPos( aPos );
  130. for( auto& item : aItems )
  131. {
  132. if( item.Visit( m_inspector, nullptr, m_ScanTypes ) == SEARCH_RESULT::QUIT )
  133. break;
  134. }
  135. }
  136. bool EE_COLLECTOR::IsCorner() const
  137. {
  138. if( GetCount() != 2 )
  139. return false;
  140. bool is_busentry0 = (dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_List[0] ) != NULL);
  141. bool is_busentry1 = (dynamic_cast<SCH_BUS_ENTRY_BASE*>( m_List[1] ) != NULL);
  142. if( (m_List[0]->Type() == SCH_LINE_T) && (m_List[1]->Type() == SCH_LINE_T) )
  143. return ( ( SCH_LINE* ) m_List[0])->GetLayer() == ( ( SCH_LINE* ) m_List[1])->GetLayer();
  144. if( (m_List[0]->Type() == SCH_LINE_T) && is_busentry1 )
  145. return true;
  146. if( is_busentry0 && (m_List[1]->Type() == SCH_LINE_T) )
  147. return true;
  148. return false;
  149. }
  150. void CollectOtherUnits( SCH_SHEET_PATH& aSheet, SCH_COMPONENT* aUnit,
  151. std::vector<SCH_COMPONENT*>* otherUnits )
  152. {
  153. // Obviously, one can collect other units only if aUnit is annotated.
  154. if( aUnit->GetUnitCount() > 1 && aUnit->IsAnnotated( &aSheet ) )
  155. {
  156. const LIB_ID thisLibId = aUnit->GetLibId();
  157. const wxString thisRef = aUnit->GetRef( &aSheet );
  158. int thisUnit = aUnit->GetUnit();
  159. SCH_REFERENCE_LIST components;
  160. aSheet.GetComponents( components );
  161. for( unsigned i = 0; i < components.GetCount(); i++ )
  162. {
  163. SCH_REFERENCE component = components[i];
  164. if( component.GetRef() == thisRef && component.GetUnit() != thisUnit )
  165. otherUnits->push_back( component.GetComp() );
  166. }
  167. }
  168. }