KiCad PCB EDA Suite
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, jean-pierre.charras@gipsa-lab.inpg.fr
5  * Copyright (C) 2004-2019 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 <eeschema_id.h>
27 #include <general.h>
28 #include <lib_item.h>
29 #include <sch_bus_entry.h>
30 #include <sch_component.h>
31 #include <sch_edit_frame.h>
32 #include <sch_junction.h>
33 #include <sch_line.h>
34 #include <sch_no_connect.h>
35 #include <sch_screen.h>
36 #include <sch_sheet.h>
37 #include <sch_view.h>
38 #include <tool/tool_manager.h>
39 #include <tools/ee_actions.h>
41 #include <trigo.h>
42 
43 
45 {
46  std::vector<wxPoint> retval;
47 
48  for( auto item : GetScreen()->Items() )
49  {
50  // Avoid items that are changing
51  if( !( item->GetEditFlags() & ( IS_DRAGGED | IS_MOVED | IS_DELETED ) ) )
52  {
53  std::vector<wxPoint> pts = item->GetConnectionPoints();
54  retval.insert( retval.end(), pts.begin(), pts.end() );
55  }
56  }
57 
58  // We always have some overlapping connection points. Drop duplicates here
59  std::sort( retval.begin(), retval.end(),
60  []( const wxPoint& a, const wxPoint& b ) -> bool
61  { return a.x < b.x || (a.x == b.x && a.y < b.y); } );
62  retval.erase(
63  std::unique( retval.begin(), retval.end() ), retval.end() );
64 
65  return retval;
66 }
67 
68 
70 {
71  std::vector<DANGLING_END_ITEM> endPoints;
72  bool hasStateChanged = false;
73 
74  for( auto item : GetScreen()->Items() )
75  item->GetEndPoints( endPoints );
76 
77  for( auto item : GetScreen()->Items() )
78  {
79  if( item->UpdateDanglingState( endPoints ) )
80  {
81  GetCanvas()->GetView()->Update( item, KIGFX::REPAINT );
82  hasStateChanged = true;
83  }
84  item->GetEndPoints( endPoints );
85  }
86 
87  return hasStateChanged;
88 }
89 
90 
91 bool SCH_EDIT_FRAME::TrimWire( const wxPoint& aStart, const wxPoint& aEnd )
92 {
93  SCH_SCREEN* screen = GetScreen();
94  bool retval = false;
95 
96  if( aStart == aEnd )
97  return retval;
98 
99  for( EDA_ITEM* item : screen->Items().OfType( SCH_LINE_T ) )
100  {
101  SCH_LINE* line = static_cast<SCH_LINE*>( item );
102 
103  if( line->GetLayer() != LAYER_WIRE )
104  continue;
105 
106  // Don't remove wires that are already deleted or are currently being dragged
107  if( line->GetEditFlags() & ( STRUCT_DELETED | IS_DRAGGED | IS_MOVED | SKIP_STRUCT ) )
108  continue;
109 
110  if( !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aStart ) ||
111  !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aEnd ) )
112  {
113  continue;
114  }
115 
116  // Don't remove entire wires
117  if( ( line->GetStartPoint() == aStart && line->GetEndPoint() == aEnd )
118  || ( line->GetStartPoint() == aEnd && line->GetEndPoint() == aStart ) )
119  {
120  continue;
121  }
122 
123  // Step 1: break the segment on one end. return_line remains line if not broken.
124  // Ensure that *line points to the segment containing aEnd
125  SCH_LINE* return_line = line;
126  BreakSegment( line, aStart, &return_line );
127 
128  if( IsPointOnSegment( return_line->GetStartPoint(), return_line->GetEndPoint(), aEnd ) )
129  line = return_line;
130 
131  // Step 2: break the remaining segment. return_line remains line if not broken.
132  // Ensure that *line _also_ contains aStart. This is our overlapping segment
133  BreakSegment( line, aEnd, &return_line );
134 
135  if( IsPointOnSegment( return_line->GetStartPoint(), return_line->GetEndPoint(), aStart ) )
136  line = return_line;
137 
138  SaveCopyInUndoList( screen, line, UNDO_REDO::DELETED, true );
139  RemoveFromScreen( line, screen );
140 
141  retval = true;
142  }
143 
144  return retval;
145 }
146 
147 
149 {
150  PICKED_ITEMS_LIST itemList;
152  std::vector<SCH_ITEM*> deletedItems;
153  std::vector<SCH_LINE*> lines;
154  std::vector<SCH_JUNCTION*> junctions;
155  std::vector<SCH_NO_CONNECT*> ncs;
156  bool changed = true;
157 
158  if( aScreen == nullptr )
159  aScreen = GetScreen();
160 
161  auto remove_item = [&]( SCH_ITEM* aItem ) -> void
162  {
163  changed = true;
164  aItem->SetFlags( STRUCT_DELETED );
165  itemList.PushItem( ITEM_PICKER( aScreen, aItem, UNDO_REDO::DELETED ) );
166  deletedItems.push_back( aItem );
167  };
168 
169  BreakSegmentsOnJunctions( aScreen );
170 
171  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_JUNCTION_T ) )
172  {
173  if( !aScreen->IsJunctionNeeded( item->GetPosition() ) )
174  remove_item( item );
175  else
176  junctions.push_back( static_cast<SCH_JUNCTION*>( item ) );
177  }
178 
179  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_NO_CONNECT_T ) )
180  {
181  ncs.push_back( static_cast<SCH_NO_CONNECT*>( item ) );
182  }
183 
184  alg::for_all_pairs( junctions.begin(), junctions.end(),
185  [&]( SCH_JUNCTION* aFirst, SCH_JUNCTION* aSecond )
186  {
187  if( ( aFirst->GetEditFlags() & STRUCT_DELETED )
188  || ( aSecond->GetEditFlags() & STRUCT_DELETED ) )
189  {
190  return;
191  }
192 
193  if( aFirst->GetPosition() == aSecond->GetPosition() )
194  remove_item( aSecond );
195  } );
196 
197  alg::for_all_pairs( ncs.begin(), ncs.end(),
198  [&]( SCH_NO_CONNECT* aFirst, SCH_NO_CONNECT* aSecond )
199  {
200  if( ( aFirst->GetEditFlags() & STRUCT_DELETED )
201  || ( aSecond->GetEditFlags() & STRUCT_DELETED ) )
202  {
203  return;
204  }
205 
206  if( aFirst->GetPosition() == aSecond->GetPosition() )
207  remove_item( aSecond );
208  } );
209 
210 
211  while( changed )
212  {
213  changed = false;
214  lines.clear();
215 
216  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_LINE_T ) )
217  {
218  if( item->GetLayer() == LAYER_WIRE || item->GetLayer() == LAYER_BUS )
219  lines.push_back( static_cast<SCH_LINE*>( item ) );
220  }
221 
222  for( auto it1 = lines.begin(); it1 != lines.end(); ++it1 )
223  {
224  SCH_LINE* firstLine = *it1;
225 
226  if( firstLine->GetEditFlags() & STRUCT_DELETED )
227  continue;
228 
229  if( firstLine->IsNull() )
230  {
231  remove_item( firstLine );
232  continue;
233  }
234 
235  auto it2 = it1;
236 
237  for( ++it2; it2 != lines.end(); ++it2 )
238  {
239  SCH_LINE* secondLine = *it2;
240 
241  if( secondLine->GetFlags() & STRUCT_DELETED )
242  continue;
243 
244  if( !secondLine->IsParallel( firstLine )
245  || !secondLine->IsStrokeEquivalent( firstLine )
246  || secondLine->GetLayer() != firstLine->GetLayer() )
247  {
248  continue;
249  }
250 
251  // Remove identical lines
252  if( firstLine->IsEndPoint( secondLine->GetStartPoint() )
253  && firstLine->IsEndPoint( secondLine->GetEndPoint() ) )
254  {
255  remove_item( secondLine );
256  continue;
257  }
258 
259  // See if we can merge an overlap (or two colinear touching segments with
260  // no junction where they meet).
261  SCH_LINE* mergedLine = secondLine->MergeOverlap( aScreen, firstLine, true );
262 
263  if( mergedLine != nullptr )
264  {
265  remove_item( firstLine );
266  remove_item( secondLine );
267  itemList.PushItem( ITEM_PICKER( aScreen, mergedLine, UNDO_REDO::NEWITEM ) );
268 
269  AddToScreen( mergedLine, aScreen );
270 
271  if( firstLine->IsSelected() )
272  selectionTool->AddItemToSel( mergedLine, true /*quiet mode*/ );
273 
274  break;
275  }
276  }
277  }
278  }
279 
280  for( auto item : deletedItems )
281  {
282  if( item->IsSelected() )
283  selectionTool->RemoveItemFromSel( item, true /*quiet mode*/ );
284 
285  RemoveFromScreen( item, aScreen );
286  }
287 
288  if( itemList.GetCount() )
289  SaveCopyInUndoList( itemList, UNDO_REDO::DELETED, true );
290 
291  return itemList.GetCount() > 0;
292 }
293 
294 
295 bool SCH_EDIT_FRAME::BreakSegment( SCH_LINE* aSegment, const wxPoint& aPoint,
296  SCH_LINE** aNewSegment, SCH_SCREEN* aScreen )
297 {
298  if( !IsPointOnSegment( aSegment->GetStartPoint(), aSegment->GetEndPoint(), aPoint )
299  || aSegment->IsEndPoint( aPoint ) )
300  {
301  return false;
302  }
303 
304  if( aScreen == nullptr )
305  aScreen = GetScreen();
306 
307  SCH_LINE* newSegment = new SCH_LINE( *aSegment );
308 
309  newSegment->SetStartPoint( aPoint );
310  AddToScreen( newSegment, aScreen );
311 
312  SaveCopyInUndoList( aScreen, newSegment, UNDO_REDO::NEWITEM, true );
313  SaveCopyInUndoList( aScreen, aSegment, UNDO_REDO::CHANGED, true );
314 
315  UpdateItem( aSegment );
316  aSegment->SetEndPoint( aPoint );
317 
318  if( aNewSegment )
319  *aNewSegment = newSegment;
320 
321  return true;
322 }
323 
324 
325 bool SCH_EDIT_FRAME::BreakSegments( const wxPoint& aPoint, SCH_SCREEN* aScreen )
326 {
327  static const KICAD_T wiresAndBuses[] = { SCH_LINE_LOCATE_WIRE_T, SCH_LINE_LOCATE_BUS_T, EOT };
328 
329  if( aScreen == nullptr )
330  aScreen = GetScreen();
331 
332  bool brokenSegments = false;
333  std::vector<SCH_LINE*> wires;
334  EDA_RECT bbox( aPoint, wxSize( 2, 2 ) );
335 
336  for( auto item : aScreen->Items().Overlapping( SCH_LINE_T, aPoint ) )
337  {
338  if( item->IsType( wiresAndBuses ) )
339  wires.push_back( static_cast<SCH_LINE*>( item ) );
340  }
341 
342  for( auto wire : wires )
343  brokenSegments |= BreakSegment( wire, aPoint, NULL, aScreen );
344 
345  return brokenSegments;
346 }
347 
348 
350 {
351  if( aScreen == nullptr )
352  aScreen = GetScreen();
353 
354  bool brokenSegments = false;
355 
356  std::set<wxPoint> point_set;
357 
358  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_JUNCTION_T ) )
359  point_set.insert( item->GetPosition() );
360 
361  for( SCH_ITEM* item : aScreen->Items().OfType( SCH_BUS_WIRE_ENTRY_T ) )
362  {
363  SCH_BUS_WIRE_ENTRY* entry = static_cast<SCH_BUS_WIRE_ENTRY*>( item );
364  point_set.insert( entry->GetPosition() );
365  point_set.insert( entry->GetEnd() );
366  }
367 
368 
369  for( auto pt : point_set )
370  brokenSegments |= BreakSegments( pt, aScreen );
371 
372  return brokenSegments;
373 }
374 
375 
376 void SCH_EDIT_FRAME::DeleteJunction( SCH_ITEM* aJunction, bool aAppend )
377 {
378  SCH_SCREEN* screen = GetScreen();
379  PICKED_ITEMS_LIST undoList;
382 
383  auto remove_item = [ & ]( SCH_ITEM* aItem ) -> void
384  {
385  aItem->SetFlags( STRUCT_DELETED );
386  undoList.PushItem( ITEM_PICKER( screen, aItem, UNDO_REDO::DELETED ) );
387  };
388 
389  remove_item( aJunction );
390  RemoveFromScreen( aJunction, screen );
391 
394  std::list<SCH_LINE*> lines;
395 
396  for( SCH_ITEM* item : screen->Items().Overlapping( SCH_LINE_T, aJunction->GetPosition() ) )
397  {
398  SCH_LINE* line = static_cast<SCH_LINE*>( item );
399 
400  if( line->IsType( wiresAndBuses ) && line->IsEndPoint( aJunction->GetPosition() )
401  && !( line->GetEditFlags() & STRUCT_DELETED ) )
402  lines.push_back( line );
403  }
404 
405  alg::for_all_pairs( lines.begin(), lines.end(),
406  [&]( SCH_LINE* firstLine, SCH_LINE* secondLine )
407  {
408  if( ( firstLine->GetEditFlags() & STRUCT_DELETED )
409  || ( secondLine->GetEditFlags() & STRUCT_DELETED )
410  || !secondLine->IsParallel( firstLine ) )
411  {
412  return;
413  }
414 
415  // Remove identical lines
416  if( firstLine->IsEndPoint( secondLine->GetStartPoint() )
417  && firstLine->IsEndPoint( secondLine->GetEndPoint() ) )
418  {
419  remove_item( firstLine );
420  return;
421  }
422 
423  // Try to merge the remaining lines
424  if( SCH_LINE* line = secondLine->MergeOverlap( screen, firstLine, false ) )
425  {
426  remove_item( firstLine );
427  remove_item( secondLine );
428  undoList.PushItem( ITEM_PICKER( screen, line, UNDO_REDO::NEWITEM ) );
429  AddToScreen( line, screen );
430 
431  if( line->IsSelected() )
432  selectionTool->AddItemToSel( line, true /*quiet mode*/ );
433 
434  lines.push_back( line );
435  }
436  } );
437 
438  SaveCopyInUndoList( undoList, UNDO_REDO::DELETED, aAppend );
439 
440 
441  for( auto line : lines )
442  {
443  if( line->GetEditFlags() & STRUCT_DELETED )
444  {
445  if( line->IsSelected() )
446  selectionTool->RemoveItemFromSel( line, true /*quiet mode*/ );
447 
448  RemoveFromScreen( line, screen );
449  }
450  }
451 }
452 
453 
454 SCH_JUNCTION* SCH_EDIT_FRAME::AddJunction( SCH_SCREEN* aScreen, const wxPoint& aPos,
455  bool aUndoAppend, bool aFinal )
456 {
457  SCH_JUNCTION* junction = new SCH_JUNCTION( aPos );
458 
459  AddToScreen( junction, aScreen );
460  SaveCopyInUndoList( aScreen, junction, UNDO_REDO::NEWITEM, aUndoAppend );
461  BreakSegments( aPos );
462 
463  if( aFinal )
464  {
466 
468  OnModify();
469 
470  auto view = GetCanvas()->GetView();
471  view->ClearPreview();
472  view->ShowPreview( false );
473  view->ClearHiddenFlags();
474  }
475 
476  return junction;
477 }
478 
479 
KIGFX::SCH_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
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:403
bool SchematicCleanUp(SCH_SCREEN *aScreen=nullptr)
Performs routine schematic cleaning including breaking wire and buses and deleting identical objects ...
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
bool IsStrokeEquivalent(const SCH_LINE *aLine)
Definition: sch_line.h:131
wxPoint GetStartPoint() const
Definition: sch_line.h:94
bool IsSelected() const
Definition: eda_item.h:192
wxPoint GetPosition() const override
bool IsJunctionNeeded(const wxPoint &aPosition, bool aNew=false)
Test if a junction is required for the items at aPosition on the screen.
Definition: sch_screen.cpp:371
#define IS_DELETED
Definition: eda_item.h:109
bool BreakSegments(const wxPoint &aPoint, SCH_SCREEN *aScreen=nullptr)
Checks every wire and bus for a intersection at aPoint and break into two segments at aPoint if an in...
bool BreakSegment(SCH_LINE *aSegment, const wxPoint &aPoint, SCH_LINE **aNewSegment=NULL, SCH_SCREEN *aScreen=nullptr)
Breaks a single segment into two at the specified point.
bool IsParallel(SCH_LINE *aLine)
Definition: sch_line.cpp:390
EE_TYPE OfType(KICAD_T aType)
Definition: sch_rtree.h:219
EE_TYPE Overlapping(const EDA_RECT &aRect)
Definition: sch_rtree.h:224
bool IsPointOnSegment(const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
Definition: trigo.cpp:42
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,...
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
bool TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
virtual wxPoint GetPosition() const
Definition: eda_item.h:326
unsigned GetCount() const
Function GetCount.
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
static const TOOL_EVENT SelectedItemsModified
Definition: actions.h:213
void SetEndPoint(const wxPoint &aPosition)
Definition: sch_line.h:98
SCH_JUNCTION * AddJunction(SCH_SCREEN *aScreen, const wxPoint &aPos, bool aAppendToUndo, bool aFinal=true)
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
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:65
Item is being added to the view.
Definition: view_item.h:62
#define NULL
wxPoint GetPosition() const override
Definition: sch_junction.h:95
STATUS_FLAGS GetEditFlags() const
Definition: eda_item.h:226
bool IsNull() const
Definition: sch_line.h:92
void SetStartPoint(const wxPoint &aPosition)
Definition: sch_line.h:95
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void ClearPreview()
Definition: view.cpp:1535
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
std::vector< wxPoint > GetSchematicConnections()
Collects a unique list of all possible connection points in the schematic.
bool IsEndPoint(const wxPoint &aPoint) const
Definition: sch_line.h:87
#define STRUCT_DELETED
flag indication structures to be erased
Definition: eda_item.h:115
PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
#define IS_MOVED
Item being moved.
Definition: eda_item.h:105
#define IS_DRAGGED
Item being dragged.
Definition: eda_item.h:108
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:279
bool BreakSegmentsOnJunctions(SCH_SCREEN *aScreen=nullptr)
Tests all junctions and bus entries in the schematic for intersections with wires and buses and break...
int RemoveItemFromSel(const TOOL_EVENT &aEvent)
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:48
int AddItemToSel(const TOOL_EVENT &aEvent)
void AddToScreen(EDA_ITEM *aItem, SCH_SCREEN *aScreen)
Add an item to the screen (and view) aScreen is the screen the item is located on,...
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:37
void DeleteJunction(SCH_ITEM *aItem, bool aAppend=false)
Removes a given junction and heals any wire segments under the junction.
EE_RTREE & Items()
Definition: sch_screen.h:159
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: eda_item.h:148
Class for a wire to bus entry.
bool TrimWire(const wxPoint &aStart, const wxPoint &aEnd)
If any single wire passes through both points, remove the portion between the two points,...
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current screen and u...
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: eda_item.h:117
STATUS_FLAGS GetFlags() const
Definition: eda_item.h:223
void PostEvent(const TOOL_EVENT &aEvent)
Puts an event to the event queue to be processed at the end of event processing cycle.
Definition: tool_manager.h:274
bool IsType(const KICAD_T aScanTypes[]) const override
Function IsType Checks whether the item is one of the listed types.
Definition: sch_line.h:69
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1513
wxPoint GetEnd() const
wxPoint GetPosition() const override
wxPoint GetEndPoint() const
Definition: sch_line.h:97