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 (C) 2004-2023 KiCad Developers, see change_log.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 <core/kicad_algo.h>
26#include <general.h>
27#include <sch_bus_entry.h>
28#include <sch_edit_frame.h>
29#include <sch_junction.h>
30#include <sch_line.h>
31#include <sch_no_connect.h>
32#include <sch_screen.h>
33#include <sch_view.h>
34#include <sch_commit.h>
35#include <tool/tool_manager.h>
36#include <tools/ee_actions.h>
38#include <trigo.h>
39
40
42{
43 std::function<void( SCH_ITEM* )> changeHandler =
44 [&]( SCH_ITEM* aChangedItem ) -> void
45 {
46 GetCanvas()->GetView()->Update( aChangedItem, KIGFX::REPAINT );
47 };
48
49 GetScreen()->TestDanglingEnds( nullptr, &changeHandler );
50}
51
52
53bool SCH_EDIT_FRAME::TrimWire( SCH_COMMIT* aCommit, const VECTOR2I& aStart, const VECTOR2I& aEnd )
54{
55 if( aStart == aEnd )
56 return false;
57
58 SCH_SCREEN* screen = GetScreen();
59 std::vector<SCH_LINE*> wires;
60 BOX2I bb( aStart );
61
62 bb.Merge( aEnd );
63
64 // We cannot modify the RTree while iterating, so push the possible
65 // wires into a separate structure.
66 for( EDA_ITEM* item : screen->Items().Overlapping( bb ) )
67 {
68 SCH_LINE* line = static_cast<SCH_LINE*>( item );
69
70 if( item->Type() == SCH_LINE_T && line->GetLayer() == LAYER_WIRE )
71 wires.push_back( line );
72 }
73
74 for( SCH_LINE* line : wires )
75 {
76 // Don't remove wires that are already deleted or are currently being dragged
77 if( line->GetEditFlags() & ( STRUCT_DELETED | IS_MOVING | SKIP_STRUCT ) )
78 continue;
79
80 if( !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aStart ) ||
81 !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aEnd ) )
82 {
83 continue;
84 }
85
86 // Don't remove entire wires
87 if( ( line->GetStartPoint() == aStart && line->GetEndPoint() == aEnd )
88 || ( line->GetStartPoint() == aEnd && line->GetEndPoint() == aStart ) )
89 {
90 continue;
91 }
92
93 // Step 1: break the segment on one end.
94 // Ensure that *line points to the segment containing aEnd
95 SCH_LINE* new_line;
96 BreakSegment( aCommit, line, aStart, &new_line, screen );
97
98 if( IsPointOnSegment( new_line->GetStartPoint(), new_line->GetEndPoint(), aEnd ) )
99 line = new_line;
100
101 // Step 2: break the remaining segment.
102 // Ensure that *line _also_ contains aStart. This is our overlapping segment
103 BreakSegment( aCommit, line, aEnd, &new_line, screen );
104
105 if( IsPointOnSegment( new_line->GetStartPoint(), new_line->GetEndPoint(), aStart ) )
106 line = new_line;
107
108 RemoveFromScreen( line, screen );
109 aCommit->Removed( line, screen );
110
111 return true;
112 }
113
114 return false;
115}
116
117
119{
121 std::vector<SCH_LINE*> lines;
122 std::vector<SCH_JUNCTION*> junctions;
123 std::vector<SCH_NO_CONNECT*> ncs;
124 std::vector<SCH_ITEM*> items_to_remove;
125 bool changed = true;
126
127 if( aScreen == nullptr )
128 aScreen = GetScreen();
129
130 auto remove_item = [&]( SCH_ITEM* aItem ) -> void
131 {
132 changed = true;
133
134 if( !( aItem->GetFlags() & STRUCT_DELETED ) )
135 {
136 aItem->SetFlags( STRUCT_DELETED );
137
138 if( aItem->IsSelected() )
139 selectionTool->RemoveItemFromSel( aItem, true /*quiet mode*/ );
140
141 RemoveFromScreen( aItem, aScreen );
142 aCommit->Removed( aItem, aScreen );
143 }
144 };
145
146 BreakSegmentsOnJunctions( aCommit, aScreen );
147
148 for( SCH_ITEM* item : aScreen->Items().OfType( SCH_JUNCTION_T ) )
149 {
150 if( !aScreen->IsExplicitJunction( item->GetPosition() ) )
151 items_to_remove.push_back( item );
152 else
153 junctions.push_back( static_cast<SCH_JUNCTION*>( item ) );
154 }
155
156 for( SCH_ITEM* item : items_to_remove )
157 remove_item( item );
158
159 for( SCH_ITEM* item : aScreen->Items().OfType( SCH_NO_CONNECT_T ) )
160 ncs.push_back( static_cast<SCH_NO_CONNECT*>( item ) );
161
162 alg::for_all_pairs( junctions.begin(), junctions.end(),
163 [&]( SCH_JUNCTION* aFirst, SCH_JUNCTION* aSecond )
164 {
165 if( ( aFirst->GetEditFlags() & STRUCT_DELETED )
166 || ( aSecond->GetEditFlags() & STRUCT_DELETED ) )
167 {
168 return;
169 }
170
171 if( aFirst->GetPosition() == aSecond->GetPosition() )
172 remove_item( aSecond );
173 } );
174
175 alg::for_all_pairs( ncs.begin(), ncs.end(),
176 [&]( SCH_NO_CONNECT* aFirst, SCH_NO_CONNECT* aSecond )
177 {
178 if( ( aFirst->GetEditFlags() & STRUCT_DELETED )
179 || ( aSecond->GetEditFlags() & STRUCT_DELETED ) )
180 {
181 return;
182 }
183
184 if( aFirst->GetPosition() == aSecond->GetPosition() )
185 remove_item( aSecond );
186 } );
187
188
189 while( changed )
190 {
191 changed = false;
192 lines.clear();
193
194 for( SCH_ITEM* item : aScreen->Items().OfType( SCH_LINE_T ) )
195 {
196 if( item->GetLayer() == LAYER_WIRE || item->GetLayer() == LAYER_BUS )
197 lines.push_back( static_cast<SCH_LINE*>( item ) );
198 }
199
200 for( auto it1 = lines.begin(); it1 != lines.end(); ++it1 )
201 {
202 SCH_LINE* firstLine = *it1;
203
204 if( firstLine->GetEditFlags() & STRUCT_DELETED )
205 continue;
206
207 if( firstLine->IsNull() )
208 {
209 remove_item( firstLine );
210 continue;
211 }
212
213 auto it2 = it1;
214
215 for( ++it2; it2 != lines.end(); ++it2 )
216 {
217 SCH_LINE* secondLine = *it2;
218
219 if( secondLine->GetFlags() & STRUCT_DELETED )
220 continue;
221
222 if( !secondLine->IsParallel( firstLine )
223 || !secondLine->IsStrokeEquivalent( firstLine )
224 || secondLine->GetLayer() != firstLine->GetLayer() )
225 {
226 continue;
227 }
228
229 // Remove identical lines
230 if( firstLine->IsEndPoint( secondLine->GetStartPoint() )
231 && firstLine->IsEndPoint( secondLine->GetEndPoint() ) )
232 {
233 remove_item( secondLine );
234 continue;
235 }
236
237 // See if we can merge an overlap (or two colinear touching segments with
238 // no junction where they meet).
239 SCH_LINE* mergedLine = secondLine->MergeOverlap( aScreen, firstLine, true );
240
241 if( mergedLine != nullptr )
242 {
243 remove_item( firstLine );
244 remove_item( secondLine );
245
246 AddToScreen( mergedLine, aScreen );
247 aCommit->Added( mergedLine, aScreen );
248
249 if( firstLine->IsSelected() || secondLine->IsSelected() )
250 selectionTool->AddItemToSel( mergedLine, true /*quiet mode*/ );
251
252 break;
253 }
254 }
255 }
256 }
257}
258
259
260void SCH_EDIT_FRAME::BreakSegment( SCH_COMMIT* aCommit, SCH_LINE* aSegment, const VECTOR2I& aPoint,
261 SCH_LINE** aNewSegment, SCH_SCREEN* aScreen )
262{
263 // Save the copy of aSegment before breaking it
264 aCommit->Modify( aSegment, aScreen );
265
266 SCH_LINE* newSegment = aSegment->BreakAt( aPoint );
267
268 aSegment->SetFlags( IS_CHANGED | IS_BROKEN );
269 newSegment->SetFlags( IS_NEW | IS_BROKEN );
270
271 AddToScreen( newSegment, aScreen );
272 aCommit->Added( newSegment, aScreen );
273
274 *aNewSegment = newSegment;
275}
276
277
278bool SCH_EDIT_FRAME::BreakSegments( SCH_COMMIT* aCommit, const VECTOR2I& aPos, SCH_SCREEN* aScreen )
279{
280 bool brokenSegments = false;
281 SCH_LINE* new_line;
282
283 for( SCH_LINE* wire : aScreen->GetBusesAndWires( aPos, true ) )
284 {
285 BreakSegment( aCommit, wire, aPos, &new_line, aScreen );
286 brokenSegments = true;
287 }
288
289 return brokenSegments;
290}
291
292
294{
295 bool brokenSegments = false;
296
297 std::set<VECTOR2I> point_set;
298
299 for( SCH_ITEM* item : aScreen->Items().OfType( SCH_JUNCTION_T ) )
300 point_set.insert( item->GetPosition() );
301
302 for( SCH_ITEM* item : aScreen->Items().OfType( SCH_BUS_WIRE_ENTRY_T ) )
303 {
304 SCH_BUS_WIRE_ENTRY* entry = static_cast<SCH_BUS_WIRE_ENTRY*>( item );
305 point_set.insert( entry->GetPosition() );
306 point_set.insert( entry->GetEnd() );
307 }
308
309 for( const VECTOR2I& pt : point_set )
310 {
311 BreakSegments( aCommit, pt, aScreen );
312 brokenSegments = true;
313 }
314
315 return brokenSegments;
316}
317
318
320{
321 SCH_SCREEN* screen = GetScreen();
322 PICKED_ITEMS_LIST undoList;
324
325 aJunction->SetFlags( STRUCT_DELETED );
326 RemoveFromScreen( aJunction, screen );
327 aCommit->Removed( aJunction, screen );
328
331 std::list<SCH_LINE*> lines;
332
333 for( SCH_ITEM* item : screen->Items().Overlapping( SCH_LINE_T, aJunction->GetPosition() ) )
334 {
335 SCH_LINE* line = static_cast<SCH_LINE*>( item );
336
337 if( line->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } )
338 && line->IsEndPoint( aJunction->GetPosition() )
339 && !( line->GetEditFlags() & STRUCT_DELETED ) )
340 {
341 lines.push_back( line );
342 }
343 }
344
345 alg::for_all_pairs( lines.begin(), lines.end(),
346 [&]( SCH_LINE* firstLine, SCH_LINE* secondLine )
347 {
348 if( ( firstLine->GetEditFlags() & STRUCT_DELETED )
349 || ( secondLine->GetEditFlags() & STRUCT_DELETED )
350 || !secondLine->IsParallel( firstLine ) )
351 {
352 return;
353 }
354
355 // Remove identical lines
356 if( firstLine->IsEndPoint( secondLine->GetStartPoint() )
357 && firstLine->IsEndPoint( secondLine->GetEndPoint() ) )
358 {
359 firstLine->SetFlags( STRUCT_DELETED );
360 return;
361 }
362
363 // Try to merge the remaining lines
364 if( SCH_LINE* new_line = secondLine->MergeOverlap( screen, firstLine, false ) )
365 {
366 firstLine->SetFlags( STRUCT_DELETED );
367 secondLine->SetFlags( STRUCT_DELETED );
368 AddToScreen( new_line, screen );
369 aCommit->Added( new_line, screen );
370
371 if( new_line->IsSelected() )
372 selectionTool->AddItemToSel( new_line, true /*quiet mode*/ );
373
374 lines.push_back( new_line );
375 }
376 } );
377
378 for( SCH_LINE* line : lines )
379 {
380 if( line->GetEditFlags() & STRUCT_DELETED )
381 {
382 if( line->IsSelected() )
383 selectionTool->RemoveItemFromSel( line, true /*quiet mode*/ );
384
385 RemoveFromScreen( line, screen );
386 aCommit->Removed( line, screen );
387 }
388 }
389}
390
391
393 const VECTOR2I& aPos )
394{
395 SCH_JUNCTION* junction = new SCH_JUNCTION( aPos );
396
397 AddToScreen( junction, aScreen );
398 aCommit->Added( junction, aScreen );
399
400 BreakSegments( aCommit, aPos, aScreen );
401
402 return junction;
403}
404
405
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:589
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
COMMIT & Added(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition: commit.h:84
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:96
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
EDA_ITEM_FLAGS GetEditFlags() const
Definition: eda_item.h:129
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:123
bool IsSelected() const
Definition: eda_item.h:106
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:126
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition: sch_rtree.h:243
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1607
A holder to handle information on schematic or board items.
void AddToScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen=nullptr)
Add an item to 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.
void RemoveFromScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen)
Remove an item from the screen (and view) aScreen is the screen the item is located on,...
VECTOR2I GetPosition() const override
VECTOR2I GetEnd() const
Class for a wire to bus entry.
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 BreakSegmentsOnJunctions(SCH_COMMIT *aCommit, SCH_SCREEN *aScreen)
Test all junctions and bus entries in the schematic for intersections with wires and buses and breaks...
void SchematicCleanUp(SCH_COMMIT *aCommit, SCH_SCREEN *aScreen=nullptr)
Perform routine schematic cleaning including breaking wire and buses and deleting identical objects s...
bool BreakSegments(SCH_COMMIT *aCommit, const VECTOR2I &aPoint, SCH_SCREEN *aScreen)
Check every wire and bus for a intersection at aPoint and break into two segments at aPoint if an int...
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.
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)
Removes a given junction and heals any wire segments under the junction.
SCH_JUNCTION * AddJunction(SCH_COMMIT *aCommit, SCH_SCREEN *aScreen, const VECTOR2I &aPos)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:150
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:257
VECTOR2I GetPosition() const override
Definition: sch_junction.h:106
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:40
bool IsParallel(const SCH_LINE *aLine) const
Definition: sch_line.cpp:457
VECTOR2I GetEndPoint() const
Definition: sch_line.h:145
VECTOR2I GetStartPoint() const
Definition: sch_line.h:140
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:470
bool IsNull() const
Definition: sch_line.h:138
bool IsEndPoint(const VECTOR2I &aPoint) const
Definition: sch_line.h:94
bool IsStrokeEquivalent(const SCH_LINE *aLine)
Definition: sch_line.h:183
SCH_LINE * BreakAt(const VECTOR2I &aPoint)
Break this segment into two at the specified point.
Definition: sch_line.cpp:591
bool IsType(const std::vector< KICAD_T > &aScanTypes) const override
Check whether the item is one of the listed types.
Definition: sch_line.h:74
VECTOR2I GetPosition() const override
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.
std::vector< SCH_LINE * > GetBusesAndWires(const VECTOR2I &aPosition, bool aIgnoreEndpoints=false) const
Return buses and wires passing through aPosition.
bool IsExplicitJunction(const VECTOR2I &aPosition) const
Indicates that a junction dot is necessary at the given location.
Definition: sch_screen.cpp:488
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
int RemoveItemFromSel(const TOOL_EVENT &aEvent)
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:165
#define IS_CHANGED
Item was edited, and modified.
#define IS_NEW
New item, just created.
#define IS_BROKEN
Is a segment just broken by BreakSegment.
#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:349
@ LAYER_BUS
Definition: layer_ids.h:350
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:57
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:83
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:42
@ SCH_LINE_T
Definition: typeinfo.h:145
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:142
@ SCH_BUS_WIRE_ENTRY_T
Definition: typeinfo.h:143
@ SCH_JUNCTION_T
Definition: typeinfo.h:141