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

672 {
673  SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
674 
675  if( screen->GetCurItem() )
676  {
677  s_wires.DeleteAll(); // Free the list, for a future usage
678  screen->SetCurItem( NULL );
679  aPanel->Refresh();
680  }
681  else
682  {
683  SCH_EDIT_FRAME* parent = ( SCH_EDIT_FRAME* ) aPanel->GetParent();
684  parent->SetRepeatItem( NULL );
685  }
686 
687  // Clear flags used in edit functions.
688  screen->ClearDrawingState();
689 }
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
Schematic editor (Eeschema) main window.
Definition: schframe.h:118
void DeleteAll()
Function DeleteAll deletes all items on the list and leaves the list empty.
Definition: dlist.cpp:41
void SetRepeatItem(SCH_ITEM *aItem)
Clone aItem and owns that clone in this container.
Definition: schframe.cpp:471
SCH_ITEM * GetCurItem() const
Return the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:174
void SetCurItem(SCH_ITEM *aItem)
Sets the currently selected object, m_CurrentItem.
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:187
void ClearDrawingState()
Clear the state flags of all the items in the screen.
Definition: sch_screen.cpp:567
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 355 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().

356 {
357  wxCHECK_RET( aSegment != NULL, wxT( "Cannot compute break point of NULL line segment." ) );
358 
359  SCH_LINE* nextSegment = aSegment->Next();
360  wxPoint midPoint = aPosition;
361 
362  wxCHECK_RET( nextSegment != NULL,
363  wxT( "Cannot compute break point of NULL second line segment." ) );
364 
365 #if 0
366  if( ABS( midPoint.x - aSegment->GetStartPoint().x ) <
367  ABS( midPoint.y - aSegment->GetStartPoint().y ) )
368  midPoint.x = aSegment->GetStartPoint().x;
369  else
370  midPoint.y = aSegment->GetStartPoint().y;
371 #else
372  int iDx = aSegment->GetEndPoint().x - aSegment->GetStartPoint().x;
373  int iDy = aSegment->GetEndPoint().y - aSegment->GetStartPoint().y;
374 
375  if( iDy != 0 ) // keep the first segment orientation (currently horizontal)
376  {
377  midPoint.x = aSegment->GetStartPoint().x;
378  }
379  else if( iDx != 0 ) // keep the first segment orientation (currently vertical)
380  {
381  midPoint.y = aSegment->GetStartPoint().y;
382  }
383  else
384  {
385  if( std::abs( midPoint.x - aSegment->GetStartPoint().x ) <
386  std::abs( midPoint.y - aSegment->GetStartPoint().y ) )
387  midPoint.x = aSegment->GetStartPoint().x;
388  else
389  midPoint.y = aSegment->GetStartPoint().y;
390  }
391 #endif
392 
393  aSegment->SetEndPoint( midPoint );
394  nextSegment->SetStartPoint( midPoint );
395  nextSegment->SetEndPoint( aPosition );
396 }
wxPoint GetEndPoint() const
Definition: sch_line.h:80
SCH_LINE * Next() const
Definition: sch_line.h:61
#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
Segment description base class to describe items which have 2 end points (track, wire, draw line ...)
Definition: sch_line.h:41
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_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( (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:51
bool GetForceHVLines() const
Definition: schframe.h:236
T * begin() const
Definition: dlist.h:218
Schematic editor (Eeschema) main window.
Definition: schframe.h:118
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:167
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:174
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 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(), 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:178
static DLIST< SCH_ITEM > s_wires

Variable Documentation

DLIST< SCH_ITEM > s_wires
static

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