KiCad PCB EDA Suite
Loading...
Searching...
No Matches
bus-wire-junction.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) 2004 Jean-Pierre Charras, [email protected]
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 <core/kicad_algo.h>
22#include <general.h>
23#include <sch_bus_entry.h>
24#include <sch_edit_frame.h>
25#include <sch_junction.h>
26#include <sch_line.h>
27#include <sch_no_connect.h>
28#include <sch_commit.h>
29#include <tool/tool_manager.h>
32#include <trigo.h>
33
34
36{
37 std::function<void( SCH_ITEM* )> changeHandler =
38 [&]( SCH_ITEM* aChangedItem ) -> void
39 {
40 GetCanvas()->GetView()->Update( aChangedItem, KIGFX::REPAINT );
41 };
42
43 GetScreen()->TestDanglingEnds( nullptr, &changeHandler );
44}
45
46
47bool SCH_EDIT_FRAME::TrimWire( SCH_COMMIT* aCommit, const VECTOR2I& aStart, const VECTOR2I& aEnd )
48{
49 if( aStart == aEnd )
50 return false;
51
52 SCH_SCREEN* screen = GetScreen();
53 std::vector<SCH_LINE*> wires;
54 BOX2I bb( aStart );
55
57 bb.Merge( aEnd );
58
59 // We cannot modify the RTree while iterating, so push the possible
60 // wires into a separate structure.
61 for( EDA_ITEM* item : screen->Items().Overlapping( bb ) )
62 {
63 SCH_LINE* line = static_cast<SCH_LINE*>( item );
64
65 if( item->Type() == SCH_LINE_T && line->GetLayer() == LAYER_WIRE )
66 wires.push_back( line );
67 }
68
69 for( SCH_LINE* line : wires )
70 {
71 // Don't remove wires that are already deleted or are currently being dragged
72 if( line->GetEditFlags() & ( STRUCT_DELETED | IS_MOVING | SKIP_STRUCT ) )
73 continue;
74
75 if( !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aStart ) ||
76 !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aEnd ) )
77 {
78 continue;
79 }
80
81 // Don't remove entire wires
82 if( ( line->GetStartPoint() == aStart && line->GetEndPoint() == aEnd )
83 || ( line->GetStartPoint() == aEnd && line->GetEndPoint() == aStart ) )
84 {
85 continue;
86 }
87
88 // Step 1: break the segment on one end.
89 // Ensure that *line points to the segment containing aEnd
90 SCH_LINE* new_line;
91 lwbTool->BreakSegment( aCommit, line, aStart, &new_line, screen );
92
93 if( IsPointOnSegment( new_line->GetStartPoint(), new_line->GetEndPoint(), aEnd ) )
94 line = new_line;
95
96 // Step 2: break the remaining segment.
97 // Ensure that *line _also_ contains aStart. This is our overlapping segment
98 lwbTool->BreakSegment( aCommit, line, aEnd, &new_line, screen );
99
100 if( IsPointOnSegment( new_line->GetStartPoint(), new_line->GetEndPoint(), aStart ) )
101 line = new_line;
102
103 RemoveFromScreen( line, screen );
104 aCommit->Removed( line, screen );
105
106 return true;
107 }
108
109 return false;
110}
111
112
114{
115 SCH_SCREEN* screen = GetScreen();
116 PICKED_ITEMS_LIST undoList;
117 SCH_SELECTION_TOOL* selectionTool = m_toolManager->GetTool<SCH_SELECTION_TOOL>();
118
119 aJunction->SetFlags( STRUCT_DELETED );
120 RemoveFromScreen( aJunction, screen );
121 aCommit->Removed( aJunction, screen );
122
125 std::list<SCH_LINE*> lines;
126
127 for( SCH_ITEM* item : screen->Items().Overlapping( SCH_LINE_T, aJunction->GetPosition() ) )
128 {
129 SCH_LINE* line = static_cast<SCH_LINE*>( item );
130
131 if( ( line->IsWire() || line->IsBus() )
132 && line->IsEndPoint( aJunction->GetPosition() )
133 && !( line->GetEditFlags() & STRUCT_DELETED ) )
134 {
135 lines.push_back( line );
136 }
137 }
138
139 alg::for_all_pairs( lines.begin(), lines.end(),
140 [&]( SCH_LINE* firstLine, SCH_LINE* secondLine )
141 {
142 if( ( firstLine->GetEditFlags() & STRUCT_DELETED )
143 || ( secondLine->GetEditFlags() & STRUCT_DELETED )
144 || !secondLine->IsParallel( firstLine ) )
145 {
146 return;
147 }
148
149 // Remove identical lines
150 if( firstLine->IsEndPoint( secondLine->GetStartPoint() )
151 && firstLine->IsEndPoint( secondLine->GetEndPoint() ) )
152 {
153 firstLine->SetFlags( STRUCT_DELETED );
154 return;
155 }
156
157 // Try to merge the remaining lines
158 if( SCH_LINE* new_line = secondLine->MergeOverlap( screen, firstLine, false ) )
159 {
160 firstLine->SetFlags( STRUCT_DELETED );
161 secondLine->SetFlags( STRUCT_DELETED );
162 AddToScreen( new_line, screen );
163 aCommit->Added( new_line, screen );
164
165 if( new_line->IsSelected() )
166 selectionTool->AddItemToSel( new_line, true /*quiet mode*/ );
167
168 lines.push_back( new_line );
169 }
170 } );
171
172 for( SCH_LINE* line : lines )
173 {
174 if( line->GetEditFlags() & STRUCT_DELETED )
175 {
176 if( line->IsSelected() )
177 selectionTool->RemoveItemFromSel( line, true /*quiet mode*/ );
178
179 RemoveFromScreen( line, screen );
180 aCommit->Removed( line, screen );
181 }
182 }
183}
184
186{
187 std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> items;
188
189 GetCanvas()->GetView()->Query( aItem->GetBoundingBox(), items );
190
191 for( const auto& it : items )
192 {
193 if( !it.first->IsSCH_ITEM() )
194 continue;
195
196 SCH_ITEM* item = static_cast<SCH_ITEM*>( it.first );
197
198 if( item == aItem )
199 continue;
200
201 if( item->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
202 {
203 GetCanvas()->GetView()->Update( item );
204 }
205 }
206}
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
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Definition commit.h:92
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
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:135
EDA_ITEM_FLAGS GetEditFlags() const
Definition eda_item.h:158
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:152
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition sch_rtree.h:226
void Update(const KIGFX::VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition sch_view.cpp:73
int Query(const BOX2I &aRect, std::vector< LAYER_ITEM_PAIR > &aResult) const
Find all visible items that touch or are within the rectangle aRect.
Definition view.cpp:487
A holder to handle information on schematic or board items.
void RemoveFromScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen) override
Remove an item from the screen (and view) aScreen is the screen the item is located on,...
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
bool TrimWire(SCH_COMMIT *aCommit, const VECTOR2I &aStart, const VECTOR2I &aEnd)
If any single wire passes through both points, remove the portion between the two points,...
void TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
void DeleteJunction(SCH_COMMIT *aCommit, SCH_ITEM *aItem)
Remove a given junction and heals any wire segments under the junction.
void UpdateHopOveredWires(SCH_ITEM *aItem)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:338
bool IsType(const std::vector< KICAD_T > &aScanTypes) const override
Check whether the item is one of the listed types.
Definition sch_item.h:177
Tool responsible for drawing/placing items (symbols, wires, buses, labels, etc.)
void BreakSegment(SCH_COMMIT *aCommit, SCH_LINE *aSegment, const VECTOR2I &aPoint, SCH_LINE **aNewSegment, SCH_SCREEN *aScreen)
Break a single segment into two at the specified point.
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
bool IsWire() const
Return true if the line is a wire.
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:135
SCH_LINE * MergeOverlap(SCH_SCREEN *aScreen, SCH_LINE *aLine, bool aCheckJunctions)
Check line against aLine to see if it overlaps and merge if it does.
Definition sch_line.cpp:495
bool IsBus() const
Return true if the line is a bus.
bool IsEndPoint(const VECTOR2I &aPoint) const override
Test if aPt is an end point of this schematic object.
Definition sch_line.h:87
void TestDanglingEnds(const SCH_SHEET_PATH *aPath=nullptr, std::function< void(SCH_ITEM *)> *aChangedHandler=nullptr) const
Test all of the connectable objects in the schematic for unused connection points.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
TOOL_MANAGER * m_toolManager
#define STRUCT_DELETED
flag indication structures to be erased
#define SKIP_STRUCT
flag indicating that the structure should be ignored
#define IS_MOVING
Item being moved.
@ LAYER_WIRE
Definition layer_ids.h:450
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:54
void for_all_pairs(_InputIterator __first, _InputIterator __last, _Function __f)
Apply a function to every possible pair of elements of a sequence.
Definition kicad_algo.h:80
bool IsPointOnSegment(const VECTOR2I &aSegStart, const VECTOR2I &aSegEnd, const VECTOR2I &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
Definition trigo.cpp:85
@ SCH_LINE_T
Definition typeinfo.h:160
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683