KiCad PCB EDA Suite
bus-wire-junction.cpp File Reference

Code for editing buses, wires, and junctions. More...

#include <fctsys.h>
#include <gr_basic.h>
#include <sch_draw_panel.h>
#include <sch_edit_frame.h>
#include <lib_draw_item.h>
#include <lib_pin.h>
#include <general.h>
#include <sch_bus_entry.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <sch_no_connect.h>
#include <sch_text.h>
#include <sch_component.h>
#include <sch_sheet.h>
#include <sch_view.h>
#include <view/view_group.h>

Go to the source code of this file.

Functions

static void AbortCreateNewLine (EDA_DRAW_PANEL *aPanel, wxDC *aDC)
 
static void ComputeBreakPoint (SCH_SCREEN *aScreen, SCH_LINE *aSegment, wxPoint &aPosition)
 Function ComputeBreakPoint computes the middle coordinate for 2 segments from the start point to aPosition with the segments kept in the horizontal or vertical axis only. More...
 
static void RemoveBacktracks (DLIST< SCH_ITEM > &aWires)
 In a contiguous list of wires, remove wires that backtrack over the previous wire. More...
 
static void DrawSegment (EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
 Mouse capture callback for drawing line segments. More...
 
static const SCH_SHEET_PINgetSheetPin (SCH_SCREEN *aScreen, const wxPoint &aPosition)
 

Variables

static DLIST< SCH_ITEMs_wires
 

Detailed Description

Code for editing buses, wires, and junctions.

Definition in file bus-wire-junction.cpp.

Function Documentation

static void AbortCreateNewLine ( EDA_DRAW_PANEL aPanel,
wxDC *  aDC 
)
static

Definition at line 838 of file bus-wire-junction.cpp.

References SCH_SCREEN::ClearDrawingState(), DHEAD::DeleteAll(), SCH_SCREEN::GetCurItem(), EDA_DRAW_PANEL::GetParent(), EDA_DRAW_PANEL::GetScreen(), SCH_SCREEN::SetCurItem(), and SCH_EDIT_FRAME::SetRepeatItem().

Referenced by SCH_EDIT_FRAME::BeginSegment().

839 {
840  SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
841  SCH_EDIT_FRAME* parent = ( SCH_EDIT_FRAME* ) aPanel->GetParent();
842 
843  if( screen->GetCurItem() )
844  {
845  s_wires.DeleteAll(); // Free the list, for a future usage
846  screen->SetCurItem( NULL );
847  }
848  else
849  {
850  parent->SetRepeatItem( NULL );
851  }
852 
853  auto view = static_cast<SCH_DRAW_PANEL*>(aPanel)->GetView();
854  view->ClearPreview();
855  view->ShowPreview( false );
856  view->ClearHiddenFlags();
857 
858  // Clear flags used in edit functions.
859  screen->ClearDrawingState();
860 }
virtual BASE_SCREEN * GetScreen()=0
Schematic editor (Eeschema) main window.
void DeleteAll()
Function DeleteAll deletes all items on the list and leaves the list empty.
Definition: dlist.cpp:44
void SetRepeatItem(SCH_ITEM *aItem)
Clone aItem and owns that clone in this container.
virtual EDA_DRAW_FRAME * GetParent() const =0
SCH_ITEM * GetCurItem() const
Return the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
Definition: sch_screen.h:196
void SetCurItem(SCH_ITEM *aItem)
Sets the currently selected object, m_CurrentItem.
Definition: sch_screen.h:204
void ClearDrawingState()
Clear the state flags of all the items in the screen.
Definition: sch_screen.cpp:561
static DLIST< SCH_ITEM > s_wires
static void ComputeBreakPoint ( SCH_SCREEN aScreen,
SCH_LINE aSegment,
wxPoint aPosition 
)
static

Function ComputeBreakPoint computes the middle coordinate for 2 segments from the start point to aPosition with the segments kept in the horizontal or vertical axis only.

Parameters
aSegmentA pointer to a SCH_LINE object containing the first line break point to compute.
aPositionA reference to a wxPoint object containing the coordinates of the position used to calculate the line break point.

Definition at line 413 of file bus-wire-junction.cpp.

References abs, SCH_SHEET_PIN::GetEdge(), SCH_LINE::GetEndPoint(), BASE_SCREEN::GetGridSize(), SCH_TEXT::GetPosition(), getSheetPin(), SCH_LINE::GetStartPoint(), SCH_LINE::Next(), SCH_LINE::SetEndPoint(), SCH_LINE::SetStartPoint(), SCH_SHEET_PIN::SHEET_LEFT_SIDE, SCH_SHEET_PIN::SHEET_RIGHT_SIDE, SCH_SHEET_PIN::SHEET_UNDEFINED_SIDE, wxPoint::x, and wxPoint::y.

Referenced by DrawSegment().

414 {
415  wxCHECK_RET( aSegment != nullptr, wxT( "Cannot compute break point of NULL line segment." ) );
416 
417  SCH_LINE* nextSegment = aSegment->Next();
418 
419  wxPoint midPoint;
420  int iDx = aSegment->GetEndPoint().x - aSegment->GetStartPoint().x;
421  int iDy = aSegment->GetEndPoint().y - aSegment->GetStartPoint().y;
422 
423  const SCH_SHEET_PIN* connectedPin = getSheetPin( aScreen, aSegment->GetStartPoint() );
424  auto force = connectedPin ? connectedPin->GetEdge() : SCH_SHEET_PIN::SHEET_UNDEFINED_SIDE;
425 
427  {
428  if( aPosition.x == connectedPin->GetPosition().x ) // push outside sheet boundary
429  {
430  int direction = ( force == SCH_SHEET_PIN::SHEET_LEFT_SIDE ) ? -1 : 1;
431  aPosition.x += aScreen->GetGridSize().x * direction;
432  }
433 
434  midPoint.x = aPosition.x;
435  midPoint.y = aSegment->GetStartPoint().y; // force horizontal
436  }
437  else if( iDy != 0 ) // keep the first segment orientation (vertical)
438  {
439  midPoint.x = aSegment->GetStartPoint().x;
440  midPoint.y = aPosition.y;
441  }
442  else if( iDx != 0 ) // keep the first segment orientation (horizontal)
443  {
444  midPoint.x = aPosition.x;
445  midPoint.y = aSegment->GetStartPoint().y;
446  }
447  else
448  {
449  if( std::abs( aPosition.x - aSegment->GetStartPoint().x ) <
450  std::abs( aPosition.y - aSegment->GetStartPoint().y ) )
451  {
452  midPoint.x = aSegment->GetStartPoint().x;
453  midPoint.y = aPosition.y;
454  }
455  else
456  {
457  midPoint.x = aPosition.x;
458  midPoint.y = aSegment->GetStartPoint().y;
459  }
460  }
461 
462  aSegment->SetEndPoint( midPoint );
463  nextSegment->SetStartPoint( midPoint );
464  nextSegment->SetEndPoint( aPosition );
465 }
SHEET_SIDE GetEdge() const
wxPoint GetEndPoint() const
Definition: sch_line.h:80
SCH_LINE * Next() const
Definition: sch_line.h:61
static const SCH_SHEET_PIN * getSheetPin(SCH_SCREEN *aScreen, const wxPoint &aPosition)
#define abs(a)
Definition: auxiliary.h:84
void SetEndPoint(const wxPoint &aPosition)
Definition: sch_line.h:82
wxPoint GetStartPoint() const
Definition: sch_line.h:76
void SetStartPoint(const wxPoint &aPosition)
Definition: sch_line.h:78
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:61
virtual wxPoint GetPosition() const override
Function GetPosition.
Definition: sch_text.h:194
Segment description base class to describe items which have 2 end points (track, wire, draw line ...)
Definition: sch_line.h:41
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
Definition: base_screen.h:410
static void DrawSegment ( EDA_DRAW_PANEL aPanel,
wxDC *  aDC,
const wxPoint aPosition,
bool  aErase 
)
static

Mouse capture callback for drawing line segments.

Definition at line 124 of file bus-wire-junction.cpp.

References SCH_ITEM::Back(), DLIST< T >::begin(), ComputeBreakPoint(), DHEAD::GetCount(), EDA_DRAW_FRAME::GetCrossHairPosition(), SCH_EDIT_FRAME::GetForceHVLines(), DLIST< T >::GetLast(), EDA_DRAW_PANEL::GetParent(), SCH_EDIT_FRAME::GetScreen(), and SCH_ITEM::Next().

Referenced by SCH_EDIT_FRAME::BeginSegment().

125 {
126  if( s_wires.GetCount() == 0 )
127  return;
128 
129  SCH_EDIT_FRAME* frame = (SCH_EDIT_FRAME*) aPanel->GetParent();
130 
131  wxPoint endpos = frame->GetCrossHairPosition();
132 
133  if( frame->GetForceHVLines() ) /* Coerce the line to vertical or horizontal one: */
134  ComputeBreakPoint( frame->GetScreen(), (SCH_LINE*) s_wires.GetLast()->Back(), endpos );
135  else
136  ( (SCH_LINE*) s_wires.GetLast() )->SetEndPoint( endpos );
137 
138  auto view = static_cast<SCH_DRAW_PANEL*>( aPanel )->GetView();
139 
140  view->ClearPreview();
141 
142  for( SCH_LINE* segment = (SCH_LINE*) s_wires.begin(); segment; segment = segment->Next() )
143  {
144  if( !segment->IsNull() ) // Add to preview if segment length != 0
145  view->AddToPreview( segment->Clone() );
146  }
147 }
bool GetForceHVLines() const
static void ComputeBreakPoint(SCH_SCREEN *aScreen, SCH_LINE *aSegment, wxPoint &new_pos)
Function ComputeBreakPoint computes the middle coordinate for 2 segments from the start point to aPos...
T * begin() const
Definition: dlist.h:218
Schematic editor (Eeschema) main window.
SCH_ITEM * Next() const
SCH_ITEM * Back() const
virtual EDA_DRAW_FRAME * GetParent() const =0
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
T * GetLast() const
Function GetLast returns the last T* in the list without removing it, or NULL if the list is empty...
Definition: dlist.h:170
Segment description base class to describe items which have 2 end points (track, wire, draw line ...)
Definition: sch_line.h:41
unsigned GetCount() const
Function GetCount returns the number of elements in the list.
Definition: dlist.h:126
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
static DLIST< SCH_ITEM > s_wires
static const SCH_SHEET_PIN* getSheetPin ( SCH_SCREEN aScreen,
const wxPoint aPosition 
)
static

Definition at line 383 of file bus-wire-junction.cpp.

References SCH_SCREEN::GetDrawItems(), SCH_SHEET::GetPins(), SCH_ITEM::Next(), and SCH_SHEET_T.

Referenced by ComputeBreakPoint().

384 {
385  for( SCH_ITEM* item = aScreen->GetDrawItems(); item; item = item->Next() )
386  {
387  if( item->Type() == SCH_SHEET_T )
388  {
389  SCH_SHEET* sheet = (SCH_SHEET*) item;
390 
391  for( const SCH_SHEET_PIN& pin : sheet->GetPins() )
392  {
393  if( pin.GetPosition() == aPosition )
394  return &pin;
395  }
396  }
397  }
398 
399  return nullptr;
400 }
SCH_SHEET_PINS & GetPins()
Definition: sch_sheet.h:334
SCH_ITEM * Next() const
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:61
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
SCH_ITEM * GetDrawItems() const
Definition: sch_screen.h:160
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
static void RemoveBacktracks ( DLIST< SCH_ITEM > &  aWires)
static

In a contiguous list of wires, remove wires that backtrack over the previous wire.

Example:

Wire is added: -------------------------------------—>

A second wire backtracks over it: ----------------—<====================>

RemoveBacktracks is called: ----------------—>

Definition at line 70 of file bus-wire-junction.cpp.

References SCH_LINE::GetEndPoint(), DLIST< T >::GetFirst(), SCH_LINE::GetStartPoint(), SCH_LINE::IsNull(), IsPointOnSegment(), SCH_LINE::Next(), DLIST< T >::Remove(), and SCH_LINE::SetEndPoint().

Referenced by SCH_EDIT_FRAME::EndSegment().

71 {
72  EDA_ITEM* first = aWires.GetFirst();
73  std::vector<SCH_LINE*> last_lines;
74 
75  for( EDA_ITEM* p = first; p; )
76  {
77  SCH_LINE *line = static_cast<SCH_LINE*>( p );
78  p = line->Next();
79 
80  if( line->IsNull() )
81  {
82  delete s_wires.Remove( line );
83  continue;
84  }
85 
86  if( !last_lines.empty() )
87  {
88  SCH_LINE* last_line = last_lines[last_lines.size() - 1];
89  bool contiguous = ( last_line->GetEndPoint() == line->GetStartPoint() );
90  bool backtracks = IsPointOnSegment( last_line->GetStartPoint(),
91  last_line->GetEndPoint(), line->GetEndPoint() );
92  bool total_backtrack = ( last_line->GetStartPoint() == line->GetEndPoint() );
93 
94  if( contiguous && backtracks )
95  {
96  if( total_backtrack )
97  {
98  delete s_wires.Remove( last_line );
99  delete s_wires.Remove( line );
100  last_lines.pop_back();
101  }
102  else
103  {
104  last_line->SetEndPoint( line->GetEndPoint() );
105  delete s_wires.Remove( line );
106  }
107  }
108  else
109  {
110  last_lines.push_back( line );
111  }
112  }
113  else
114  {
115  last_lines.push_back( line );
116  }
117  }
118 }
bool IsPointOnSegment(const wxPoint &aSegStart, const wxPoint &aSegEnd, const wxPoint &aTestPoint)
Function IsPointOnSegment.
Definition: trigo.cpp:39
T * Remove(T *aElement)
Function Remove removes aElement from the list, but does not delete it.
Definition: dlist.h:211
wxPoint GetEndPoint() const
Definition: sch_line.h:80
SCH_LINE * Next() const
Definition: sch_line.h:61
void SetEndPoint(const wxPoint &aPosition)
Definition: sch_line.h:82
wxPoint GetStartPoint() const
Definition: sch_line.h:76
T * GetFirst() const
Function GetFirst returns the first T* in the list without removing it, or NULL if the list is empty...
Definition: dlist.h:163
bool IsNull() const
Definition: sch_line.h:74
Segment description base class to describe items which have 2 end points (track, wire, draw line ...)
Definition: sch_line.h:41
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
static DLIST< SCH_ITEM > s_wires

Variable Documentation

DLIST< SCH_ITEM > s_wires
static

Definition at line 53 of file bus-wire-junction.cpp.