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 <schframe.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_LINE *aSegment, const 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...
 

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 452 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().

453 {
454  SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
455 
456  if( screen->GetCurItem() )
457  {
458  s_wires.DeleteAll(); // Free the list, for a future usage
459  screen->SetCurItem( NULL );
460  aPanel->Refresh();
461  }
462  else
463  {
464  SCH_EDIT_FRAME* parent = ( SCH_EDIT_FRAME* ) aPanel->GetParent();
465  parent->SetRepeatItem( NULL );
466  }
467 
468  // Clear flags used in edit functions.
469  screen->ClearDrawingState();
470 }
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
Schematic editor (Eeschema) main window.
Definition: schframe.h:117
void DeleteAll()
Function DeleteAll deletes all items on the list and leaves the list empty.
Definition: dlist.cpp:41
void SetRepeatItem(SCH_ITEM *aItem)
Function SetRepeatItem clones aItem and owns that clone in this container.
Definition: schframe.cpp:460
SCH_ITEM * GetCurItem() const
Function GetCurItem returns the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:175
void SetCurItem(SCH_ITEM *aItem)
Function SetCurItem sets the currently selected object, m_CurrentItem.
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:188
void ClearDrawingState()
Clear the state flags of all the items in the screen.
Definition: sch_screen.cpp:617
static DLIST< SCH_ITEM > s_wires
static void ComputeBreakPoint ( SCH_LINE aSegment,
const 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 353 of file bus-wire-junction.cpp.

References abs, SCH_LINE::GetEndPoint(), SCH_LINE::GetStartPoint(), SCH_LINE::Next(), SCH_LINE::SetEndPoint(), SCH_LINE::SetStartPoint(), wxPoint::x, and wxPoint::y.

Referenced by DrawSegment().

354 {
355  wxCHECK_RET( aSegment != NULL, wxT( "Cannot compute break point of NULL line segment." ) );
356 
357  SCH_LINE* nextSegment = aSegment->Next();
358  wxPoint midPoint = aPosition;
359 
360  wxCHECK_RET( nextSegment != NULL,
361  wxT( "Cannot compute break point of NULL second line segment." ) );
362 
363 #if 0
364  if( ABS( midPoint.x - aSegment->GetStartPoint().x ) <
365  ABS( midPoint.y - aSegment->GetStartPoint().y ) )
366  midPoint.x = aSegment->GetStartPoint().x;
367  else
368  midPoint.y = aSegment->GetStartPoint().y;
369 #else
370  int iDx = aSegment->GetEndPoint().x - aSegment->GetStartPoint().x;
371  int iDy = aSegment->GetEndPoint().y - aSegment->GetStartPoint().y;
372 
373  if( iDy != 0 ) // keep the first segment orientation (currently horizontal)
374  {
375  midPoint.x = aSegment->GetStartPoint().x;
376  }
377  else if( iDx != 0 ) // keep the first segment orientation (currently vertical)
378  {
379  midPoint.y = aSegment->GetStartPoint().y;
380  }
381  else
382  {
383  if( std::abs( midPoint.x - aSegment->GetStartPoint().x ) <
384  std::abs( midPoint.y - aSegment->GetStartPoint().y ) )
385  midPoint.x = aSegment->GetStartPoint().x;
386  else
387  midPoint.y = aSegment->GetStartPoint().y;
388  }
389 #endif
390 
391  aSegment->SetEndPoint( midPoint );
392  nextSegment->SetStartPoint( midPoint );
393  nextSegment->SetEndPoint( aPosition );
394 }
wxPoint GetEndPoint() const
Definition: sch_line.h:75
SCH_LINE * Next() const
Definition: sch_line.h:56
#define abs(a)
Definition: auxiliary.h:84
void SetEndPoint(const wxPoint &aPosition)
Definition: sch_line.h:77
wxPoint GetStartPoint() const
Definition: sch_line.h:71
void SetStartPoint(const wxPoint &aPosition)
Definition: sch_line.h:73
Class SCH_LINE is a segment description base class to describe items which have 2 end points (track...
Definition: sch_line.h:42
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 115 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_LINE::IsNull(), and SCH_LINE::Next().

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

117 {
118  SCH_LINE* segment;
119 
120  if( s_wires.GetCount() == 0 )
121  return;
122 
123  segment = (SCH_LINE*) s_wires.begin();
124  COLOR4D color = GetLayerColor( segment->GetLayer() );
125 
126  if( aErase )
127  {
128  while( segment )
129  {
130  if( !segment->IsNull() ) // Redraw if segment length != 0
131  segment->Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode, color );
132 
133  segment = segment->Next();
134  }
135  }
136 
137  SCH_EDIT_FRAME* frame = (SCH_EDIT_FRAME*) aPanel->GetParent();
138 
139  wxPoint endpos = frame->GetCrossHairPosition();
140 
141  if( frame->GetForceHVLines() ) /* Coerce the line to vertical or horizontal one: */
142  ComputeBreakPoint( (SCH_LINE*) s_wires.GetLast()->Back(), endpos );
143  else
144  ( (SCH_LINE*) s_wires.GetLast() )->SetEndPoint( endpos );
145 
146  segment = (SCH_LINE*) s_wires.begin();
147 
148  while( segment )
149  {
150  if( !segment->IsNull() ) // Redraw if segment length != 0
151  segment->Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode, color );
152 
153  segment = segment->Next();
154  }
155 }
GR_DRAWMODE g_XorMode
Definition: gr_basic.cpp:51
bool GetForceHVLines() const
Definition: schframe.h:239
T * begin() const
Definition: dlist.h:218
Schematic editor (Eeschema) main window.
Definition: schframe.h:117
SCH_LINE * Next() const
Definition: sch_line.h:56
SCH_LAYER_ID GetLayer() const
Function GetLayer returns the layer this item is on.
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:166
SCH_ITEM * Back() const
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:175
bool IsNull() const
Definition: sch_line.h:69
Class SCH_LINE is a segment description base class to describe items which have 2 end points (track...
Definition: sch_line.h:42
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:214
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
static void ComputeBreakPoint(SCH_LINE *segment, const wxPoint &new_pos)
Function ComputeBreakPoint computes the middle coordinate for 2 segments from the start point to aPos...
static DLIST< SCH_ITEM > s_wires
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
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(), 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( !last_lines.empty() )
78  {
79  SCH_LINE* last_line = last_lines[last_lines.size() - 1];
80  bool contiguous = ( last_line->GetEndPoint() == line->GetStartPoint() );
81  bool backtracks = IsPointOnSegment( last_line->GetStartPoint(),
82  last_line->GetEndPoint(), line->GetEndPoint() );
83  bool total_backtrack = ( last_line->GetStartPoint() == line->GetEndPoint() );
84 
85  if( contiguous && backtracks )
86  {
87  if( total_backtrack )
88  {
89  delete s_wires.Remove( last_line );
90  delete s_wires.Remove( line );
91  last_lines.pop_back();
92  }
93  else
94  {
95  last_line->SetEndPoint( line->GetEndPoint() );
96  delete s_wires.Remove( line );
97  }
98  }
99  else
100  {
101  last_lines.push_back( line );
102  }
103  }
104  else
105  {
106  last_lines.push_back( line );
107  }
108  }
109 }
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:75
SCH_LINE * Next() const
Definition: sch_line.h:56
void SetEndPoint(const wxPoint &aPosition)
Definition: sch_line.h:77
wxPoint GetStartPoint() const
Definition: sch_line.h:71
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
Class SCH_LINE is a segment description base class to describe items which have 2 end points (track...
Definition: sch_line.h:42
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.