← Back to team overview

kicad-developers team mailing list archive

[PATCH] Remove shorted wire (fixes lp:1678449)

 

​When laying out a schematic, it is often necessary to ​add resistors or
capacitors to existing wires.  Currently, placing a resistor on top of a
wire in Eeschema will connect both ends, resulting in a short across the
resistor that needs to be removed.

The attached patch removes the shorted wire when a two-ended component is
placed directly on a wire.

The associated bug report is  https://bugs.launchpad.net/kicad/+bug/1678449

-Seth
From 052be727642ba8e0afbd82ed45f9ccf29c107bf4 Mon Sep 17 00:00:00 2001
From: Seth Hillbrand <hillbrand@xxxxxxxxxxx>
Date: Fri, 1 Dec 2017 14:53:23 -0800
Subject: [PATCH] Eeschema: Remove wire under a two-ended component

CHANGE: In Eeschema, when placing a two-terminal component directly
over an existing wire, the wire is automatically removed to the
endpoints.

This helps workflow in Eeschema, allowing one to quickly add resistors,
capacitors and diodes over existing lines.

Fixes: lp:1678449
* https://bugs.launchpad.net/kicad/+bug/1678449
---
 eeschema/bus-wire-junction.cpp | 45 +++++++++++++++++++++++++++++++++++++++++-
 eeschema/schframe.cpp          |  3 +++
 eeschema/schframe.h            | 14 ++++++++++++-
 3 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/eeschema/bus-wire-junction.cpp b/eeschema/bus-wire-junction.cpp
index c5882810a..05a521afd 100644
--- a/eeschema/bus-wire-junction.cpp
+++ b/eeschema/bus-wire-junction.cpp
@@ -437,6 +437,45 @@ void SCH_EDIT_FRAME::SaveWireImage()
 }
 
 
+void SCH_EDIT_FRAME::TrimWire( const wxPoint& aStart, const wxPoint& aEnd, bool aAppend )
+{
+    SCH_LINE* line;
+
+    if( aStart == aEnd )
+        return;
+
+    for( SCH_ITEM* item = GetScreen()->GetDrawItems(); item; item = item->Next() )
+    {
+        if( item->GetFlags() & STRUCT_DELETED )
+            continue;
+
+        if( item->Type() != SCH_LINE_T || item->GetLayer() != LAYER_WIRE )
+            continue;
+
+        line = (SCH_LINE*) item;
+        if( !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aStart ) ||
+                !IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aEnd ) )
+            continue;
+
+        // Step 1: break the segment on one end.  return_line remains line if not broken.
+        // Ensure that *line points to the segment containing aEnd
+        SCH_LINE* return_line = line;
+        aAppend |= BreakSegment( line, aStart, aAppend, &return_line );
+        if( IsPointOnSegment( return_line->GetStartPoint(), return_line->GetEndPoint(), aEnd ) )
+            line = return_line;
+
+        // Step 2: break the remaining segment.  return_line remains line if not broken.
+        // Ensure that *line _also_ contains aStart.  This is our overlapping segment
+        aAppend |= BreakSegment( line, aEnd, aAppend, &return_line );
+        if( IsPointOnSegment( return_line->GetStartPoint(), return_line->GetEndPoint(), aStart ) )
+            line = return_line;
+
+        SaveCopyInUndoList( (SCH_ITEM*)line, UR_DELETED, aAppend );
+        GetScreen()->Remove( (SCH_ITEM*)line );
+    }
+}
+
+
 bool SCH_EDIT_FRAME::SchematicCleanUp( bool aAppend )
 {
     SCH_ITEM*           item = NULL;
@@ -531,7 +570,8 @@ bool SCH_EDIT_FRAME::SchematicCleanUp( bool aAppend )
 }
 
 
-bool SCH_EDIT_FRAME::BreakSegment( SCH_LINE *aSegment, const wxPoint& aPoint, bool aAppend )
+bool SCH_EDIT_FRAME::BreakSegment( SCH_LINE* aSegment, const wxPoint& aPoint, bool aAppend,
+        SCH_LINE** aNewSegment )
 {
     if( !IsPointOnSegment( aSegment->GetStartPoint(), aSegment->GetEndPoint(), aPoint )
             || aSegment->IsEndPoint( aPoint ) )
@@ -545,6 +585,9 @@ bool SCH_EDIT_FRAME::BreakSegment( SCH_LINE *aSegment, const wxPoint& aPoint, bo
     aSegment->SetEndPoint( aPoint );
     GetScreen()->Append( newSegment );
 
+    if( aNewSegment )
+        *aNewSegment = newSegment;
+
     return true;
 }
 
diff --git a/eeschema/schframe.cpp b/eeschema/schframe.cpp
index f2f28d51b..828ed64f6 100644
--- a/eeschema/schframe.cpp
+++ b/eeschema/schframe.cpp
@@ -1446,6 +1446,9 @@ void SCH_EDIT_FRAME::addCurrentItemToList( bool aRedraw )
     {
         std::vector< wxPoint > pts;
         item->GetConnectionPoints( pts );
+        if( pts.size() == 2 )
+            TrimWire( pts[0], pts[1], true );
+
         for( auto i : pts )
         {
             if( screen->IsJunctionNeeded( i, true ) )
diff --git a/eeschema/schframe.h b/eeschema/schframe.h
index 90366584d..e08082d7f 100644
--- a/eeschema/schframe.h
+++ b/eeschema/schframe.h
@@ -471,9 +471,11 @@ public:
      * @param aSegment Line segment to break
      * @param aPoint Point at which to break the segment
      * @param aAppend Add the changes to the previous undo state
+     * @param aNewSegment Pointer to the newly created segment (if given and created)
      * @return True if any wires or buses were broken.
      */
-    bool BreakSegment( SCH_LINE* aSegment, const wxPoint& aPoint, bool aAppend = false );
+    bool BreakSegment( SCH_LINE* aSegment, const wxPoint& aPoint, bool aAppend = false,
+            SCH_LINE** aNewSegment = NULL );
 
     /**
      * Checks every wire and bus for a intersection at \a aPoint and break into two segments
@@ -958,6 +960,16 @@ private:
     bool SchematicCleanUp( bool aAppend = false );
 
     /**
+     * If any single wire passes through _both points_, remove the portion between the two points,
+     * potentially splitting the wire into two.
+     *
+     * @param aStart The starting point for trimmming
+     * @param aEnd The ending point for trimming
+     * @param aAppend Should the line changes be appended to a previous undo state
+     */
+    void TrimWire( const wxPoint& aStart, const wxPoint& aEnd, bool aAppend = true );
+
+    /**
      * Start moving \a aItem using the mouse.
      *
      * @param aItem A pointer to an SCH_ITEM to move.
-- 
2.11.0


Follow ups