KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ee_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 (C) 2019-2022 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <tools/ee_selection.h>
26#include <sch_item.h>
27#include <sch_reference_list.h>
28#include <sch_sheet_path.h>
29#include <sch_symbol.h>
30#include <sch_sheet.h>
31
32
34 SELECTION()
35{
36 m_screen = aScreen;
37}
38
39
40EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
41{
42 EDA_ITEM* topLeftConnectedItem = nullptr;
43 VECTOR2I topLeftConnectedPos;
44
45 EDA_ITEM* topLeftItem = nullptr;
46 VECTOR2I topLeftPos;
47
48 auto processItem =
49 []( EDA_ITEM* aItem, EDA_ITEM** aCurrent, VECTOR2I* aCurrentPos )
50 {
51 VECTOR2I pos = aItem->GetPosition();
52
53 if( ( *aCurrent == nullptr )
54 || ( pos.x < aCurrentPos->x )
55 || ( pos.x == aCurrentPos->x && pos.y < aCurrentPos->y ) )
56 {
57 *aCurrent = aItem;
58 *aCurrentPos = pos;
59 }
60 };
61
62 // Find the leftmost (smallest x coord) and highest (smallest y with the smallest x) item
63 // in the selection
64
65 for( EDA_ITEM* item : m_items )
66 {
67 SCH_ITEM* sch_item = dynamic_cast<SCH_ITEM*>( item );
68 LIB_PIN* lib_pin = dynamic_cast<LIB_PIN*>( item );
69
70 // Prefer connection points (which should remain on grid)
71
72 if( ( sch_item && sch_item->IsConnectable() ) || lib_pin )
73 processItem( item, &topLeftConnectedItem, &topLeftConnectedPos );
74
75 processItem( item, &topLeftItem, &topLeftPos );
76 }
77
78 if( topLeftConnectedItem )
79 return topLeftConnectedItem;
80 else
81 return topLeftItem;
82}
83
84
86{
87 BOX2I bbox;
88
89 for( EDA_ITEM* item : m_items )
90 {
91 if( item->Type() == SCH_SYMBOL_T )
92 {
93 // Quiet Coverity warning. The LIB_SYMBOL field container is a Boost ptr_vector
94 // so the exception is legit.
95 try
96 {
97 bbox.Merge( static_cast<SCH_SYMBOL*>( item )->GetBoundingBox() );
98 }
99 catch( const boost::bad_pointer& )
100 {
101 wxFAIL_MSG( "Invalid pointer." );
102 }
103 }
104 else if( item->Type() == SCH_SHEET_T )
105 {
106 bbox.Merge( static_cast<SCH_SHEET*>( item )->GetBodyBoundingBox() );
107 }
108 else
109 {
110 bbox.Merge( item->GetBoundingBox() );
111 }
112 }
113
114 return bbox;
115}
116
117
118const std::vector<KIGFX::VIEW_ITEM*> EE_SELECTION::updateDrawList() const
119{
120 std::vector<VIEW_ITEM*> items;
121
122 auto addItem =
123 [&]( EDA_ITEM* item )
124 {
125 items.push_back( item );
126 };
127
128 for( EDA_ITEM* item : m_items )
129 addItem( item );
130
131 return items;
132}
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:589
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:239
BOX2I GetBoundingBox() const override
const std::vector< KIGFX::VIEW_ITEM * > updateDrawList() const override
EDA_ITEM * GetTopLeftItem(bool onlyModules=false) const override
SCH_SCREEN * m_screen
Screen of selected objects.
Definition: ee_selection.h:42
EE_SELECTION(SCH_SCREEN *aScreen=nullptr)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:165
virtual bool IsConnectable() const
Definition: sch_item.h:389
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
Schematic symbol object.
Definition: sch_symbol.h:109
std::deque< EDA_ITEM * > m_items
Definition: selection.h:271
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ SCH_SYMBOL_T
Definition: typeinfo.h:160
@ SCH_SHEET_T
Definition: typeinfo.h:162