KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_selection.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019 CERN
5 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <tools/sch_selection.h>
22#include <sch_item.h>
23#include <sch_reference_list.h>
24#include <sch_sheet_path.h>
25#include <sch_symbol.h>
26#include <sch_sheet.h>
27
28
30 SELECTION()
31{
32 m_screen = aScreen;
33}
34
35
36EDA_ITEM* SCH_SELECTION::GetTopLeftItem( bool onlyModules ) const
37{
38 EDA_ITEM* topLeftConnectedItem = nullptr;
39 VECTOR2I topLeftConnectedPos;
40
41 EDA_ITEM* topLeftItem = nullptr;
42 VECTOR2I topLeftPos;
43
44 auto processItem =
45 []( EDA_ITEM* aItem, EDA_ITEM** aCurrent, VECTOR2I* aCurrentPos )
46 {
47 VECTOR2I pos = aItem->GetPosition();
48
49 if( ( *aCurrent == nullptr )
50 || ( pos.x < aCurrentPos->x )
51 || ( pos.x == aCurrentPos->x && pos.y < aCurrentPos->y ) )
52 {
53 *aCurrent = aItem;
54 *aCurrentPos = pos;
55 }
56 };
57
58 // Find the leftmost (smallest x coord) and highest (smallest y with the smallest x) item
59 // in the selection
60
61 for( EDA_ITEM* item : m_items )
62 {
63 SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( item );
64 SCH_PIN* pin = dynamic_cast<SCH_PIN*>( item );
65
66 // Prefer connection points (which should remain on grid)
67
68 if( ( sch_item && sch_item->IsConnectable() ) || pin )
69 processItem( item, &topLeftConnectedItem, &topLeftConnectedPos );
70
71 processItem( item, &topLeftItem, &topLeftPos );
72 }
73
74 if( topLeftConnectedItem )
75 return topLeftConnectedItem;
76 else
77 return topLeftItem;
78}
79
80
82{
83 BOX2I bbox;
84
85 for( EDA_ITEM* item : m_items )
86 {
87 if( item->Type() == SCH_SYMBOL_T )
88 {
89 // Quiet Coverity warning. The LIB_SYMBOL field container is a Boost ptr_vector
90 // so the exception is legit.
91 try
92 {
93 bbox.Merge( static_cast<SCH_SYMBOL*>( item )->GetBoundingBox() );
94 }
95 catch( const boost::bad_pointer& )
96 {
97 wxFAIL_MSG( "Invalid pointer." );
98 }
99 }
100 else if( item->Type() == SCH_SHEET_T )
101 {
102 bbox.Merge( static_cast<SCH_SHEET*>( item )->GetBodyBoundingBox() );
103 }
104 else
105 {
106 bbox.Merge( item->GetBoundingBox() );
107 }
108 }
109
110 return bbox;
111}
112
113
114const std::vector<KIGFX::VIEW_ITEM*> SCH_SELECTION::updateDrawList() const
115{
116 std::vector<VIEW_ITEM*> items;
117
118 auto addItem =
119 [&]( EDA_ITEM* item )
120 {
121 items.push_back( item );
122 };
123
124 for( EDA_ITEM* item : m_items )
125 addItem( item );
126
127 return items;
128}
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:654
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
virtual bool IsConnectable() const
Definition sch_item.h:524
const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const override
SCH_SELECTION(SCH_SCREEN *aScreen=nullptr)
BOX2I GetBoundingBox() const override
SCH_SCREEN * m_screen
Screen of selected objects.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
Schematic symbol object.
Definition sch_symbol.h:69
std::deque< EDA_ITEM * > m_items
Definition selection.h:236
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_SHEET_T
Definition typeinfo.h:172
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683