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 <class_drawpanel.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>

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 790 of file bus-wire-junction.cpp.

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

Referenced by SCH_EDIT_FRAME::BeginSegment().

791 {
792  SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
793 
794  if( screen->GetCurItem() )
795  {
796  s_wires.DeleteAll(); // Free the list, for a future usage
797  screen->SetCurItem( NULL );
798  aPanel->Refresh();
799  }
800  else
801  {
802  SCH_EDIT_FRAME* parent = ( SCH_EDIT_FRAME* ) aPanel->GetParent();
803  parent->SetRepeatItem( NULL );
804  }
805 
806  // Clear flags used in edit functions.
807  screen->ClearDrawingState();
808 }
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:321
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.
SCH_ITEM * GetCurItem() const
Return the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
Definition: sch_screen.h:174
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:163
void SetCurItem(SCH_ITEM *aItem)
Sets the currently selected object, m_CurrentItem.
Definition: sch_screen.h:182
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:176
void ClearDrawingState()
Clear the state flags of all the items in the screen.
Definition: sch_screen.cpp:582
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 380 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().

381 {
382  wxCHECK_RET( aSegment != nullptr, wxT( "Cannot compute break point of NULL line segment." ) );
383 
384  SCH_LINE* nextSegment = aSegment->Next();
385 
386  wxPoint midPoint;
387  int iDx = aSegment->GetEndPoint().x - aSegment->GetStartPoint().x;
388  int iDy = aSegment->GetEndPoint().y - aSegment->GetStartPoint().y;
389 
390  const SCH_SHEET_PIN* connectedPin = getSheetPin( aScreen, aSegment->GetStartPoint() );
391  auto force = connectedPin ? connectedPin->GetEdge() : SCH_SHEET_PIN::SHEET_UNDEFINED_SIDE;
392 
394  {
395  if( aPosition.x == connectedPin->GetPosition().x ) // push outside sheet boundary
396  {
397  int direction = ( force == SCH_SHEET_PIN::SHEET_LEFT_SIDE ) ? -1 : 1;
398  aPosition.x += aScreen->GetGridSize().x * direction;
399  }
400 
401  midPoint.x = aPosition.x;
402  midPoint.y = aSegment->GetStartPoint().y; // force horizontal
403  }
404  else if( iDy != 0 ) // keep the first segment orientation (vertical)
405  {
406  midPoint.x = aSegment->GetStartPoint().x;
407  midPoint.y = aPosition.y;
408  }
409  else if( iDx != 0 ) // keep the first segment orientation (horizontal)
410  {
411  midPoint.x = aPosition.x;
412  midPoint.y = aSegment->GetStartPoint().y;
413  }
414  else
415  {
416  if( std::abs( aPosition.x - aSegment->GetStartPoint().x ) <
417  std::abs( aPosition.y - aSegment->GetStartPoint().y ) )
418  {
419  midPoint.x = aSegment->GetStartPoint().x;
420  midPoint.y = aPosition.y;
421  }
422  else
423  {
424  midPoint.x = aPosition.x;
425  midPoint.y = aSegment->GetStartPoint().y;
426  }
427  }
428 
429  aSegment->SetEndPoint( midPoint );
430  nextSegment->SetStartPoint( midPoint );
431  nextSegment->SetEndPoint( aPosition );
432 }
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:192
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 121 of file bus-wire-junction.cpp.

References SCH_ITEM::Back(), DLIST< T >::begin(), color, ComputeBreakPoint(), SCH_LINE::Draw(), g_XorMode, DHEAD::GetCount(), EDA_DRAW_FRAME::GetCrossHairPosition(), SCH_EDIT_FRAME::GetForceHVLines(), DLIST< T >::GetLast(), SCH_ITEM::GetLayer(), GetLayerColor(), EDA_DRAW_PANEL::GetParent(), SCH_EDIT_FRAME::GetScreen(), SCH_LINE::IsNull(), and SCH_LINE::Next().

Referenced by SCH_EDIT_FRAME::BeginSegment(), and SCH_EDIT_FRAME::DeleteCurrentSegment().

123 {
124  SCH_LINE* segment;
125 
126  if( s_wires.GetCount() == 0 )
127  return;
128 
129  segment = (SCH_LINE*) s_wires.begin();
130  COLOR4D color = GetLayerColor( segment->GetLayer() );
131 
132  if( aErase )
133  {
134  while( segment )
135  {
136  if( !segment->IsNull() ) // Redraw if segment length != 0
137  segment->Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode, color );
138 
139  segment = segment->Next();
140  }
141  }
142 
143  SCH_EDIT_FRAME* frame = (SCH_EDIT_FRAME*) aPanel->GetParent();
144 
145  wxPoint endpos = frame->GetCrossHairPosition();
146 
147  if( frame->GetForceHVLines() ) /* Coerce the line to vertical or horizontal one: */
148  ComputeBreakPoint( frame->GetScreen(), (SCH_LINE*) s_wires.GetLast()->Back(), endpos );
149  else
150  ( (SCH_LINE*) s_wires.GetLast() )->SetEndPoint( endpos );
151 
152  segment = (SCH_LINE*) s_wires.begin();
153 
154  while( segment )
155  {
156  if( !segment->IsNull() ) // Redraw if segment length != 0
157  segment->Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode, color );
158 
159  segment = segment->Next();
160  }
161 }
GR_DRAWMODE g_XorMode
Definition: gr_basic.cpp:73
bool GetForceHVLines() const
int color
Definition: DXF_plotter.cpp:62
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_LINE * Next() const
Definition: sch_line.h:61
SCH_LAYER_ID GetLayer() const
Function GetLayer returns the layer this item is on.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:174
SCH_ITEM * Back() const
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
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp: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
unsigned GetCount() const
Function GetCount returns the number of elements in the list.
Definition: dlist.h:126
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor=COLOR4D::UNSPECIFIED) override
Function Draw Draw a schematic item.
Definition: sch_line.cpp:306
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
static DLIST< SCH_ITEM > s_wires
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
static const SCH_SHEET_PIN* getSheetPin ( SCH_SCREEN aScreen,
const wxPoint aPosition 
)
static

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

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

Referenced by ComputeBreakPoint().

351 {
352  for( SCH_ITEM* item = aScreen->GetDrawItems(); item; item = item->Next() )
353  {
354  if( item->Type() == SCH_SHEET_T )
355  {
356  SCH_SHEET* sheet = (SCH_SHEET*) item;
357 
358  for( const SCH_SHEET_PIN& pin : sheet->GetPins() )
359  {
360  if( pin.GetPosition() == aPosition )
361  return &pin;
362  }
363  }
364  }
365 
366  return nullptr;
367 }
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:138
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 67 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().

68 {
69  EDA_ITEM* first = aWires.GetFirst();
70  std::vector<SCH_LINE*> last_lines;
71 
72  for( EDA_ITEM* p = first; p; )
73  {
74  SCH_LINE *line = static_cast<SCH_LINE*>( p );
75  p = line->Next();
76 
77  if( line->IsNull() )
78  {
79  delete s_wires.Remove( line );
80  continue;
81  }
82 
83  if( !last_lines.empty() )
84  {
85  SCH_LINE* last_line = last_lines[last_lines.size() - 1];
86  bool contiguous = ( last_line->GetEndPoint() == line->GetStartPoint() );
87  bool backtracks = IsPointOnSegment( last_line->GetStartPoint(),
88  last_line->GetEndPoint(), line->GetEndPoint() );
89  bool total_backtrack = ( last_line->GetStartPoint() == line->GetEndPoint() );
90 
91  if( contiguous && backtracks )
92  {
93  if( total_backtrack )
94  {
95  delete s_wires.Remove( last_line );
96  delete s_wires.Remove( line );
97  last_lines.pop_back();
98  }
99  else
100  {
101  last_line->SetEndPoint( line->GetEndPoint() );
102  delete s_wires.Remove( line );
103  }
104  }
105  else
106  {
107  last_lines.push_back( line );
108  }
109  }
110  else
111  {
112  last_lines.push_back( line );
113  }
114  }
115 }
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 50 of file bus-wire-junction.cpp.