← Back to team overview

kicad-developers team mailing list archive

[patch] [eeschema] restore junctions if optimized away

 

Hi all,

Here's a patch that restores missing junctions in schematic documents
that have been saved with missing libraries/cache (see [1] for an
example). In this case, some connection points on wires can be optimized
away, resulting in

a lot of ERC errors.

@JP/Wayne, is this OK for you or would you prefer to fix it in another
way (I'm asking because I'm not very proficient with eeschema internals).

Cheers,

Tom

[1] https://github.com/yetifrisstlama/Marble/tree/symbol_cleanup

>From b145efafc388ea39a66b668d53f2a792fcb68570 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomasz=20W=C5=82ostowski?= <tomasz.wlostowski@xxxxxxx>
Date: Sun, 23 Jun 2019 17:12:25 +0200
Subject: [PATCH] eeschema: automatically insert junctions at pin connections
 if needed during file load

EEschema optimizes wires by merging colinear segments. If a schematic opened without a valid
cache library or missing installed libraries and later saved, this optimization can cause connectivity
errors. In order to fix that we check each pin-wire connection and junctions if necessary.
---
 eeschema/files-io.cpp       |  7 +++++++
 eeschema/sch_edit_frame.cpp | 35 +++++++++++++++++++++++++++++++++++
 eeschema/sch_edit_frame.h   |  2 ++
 3 files changed, 44 insertions(+)

diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp
index 48d57eaf9..0889dfcc5 100644
--- a/eeschema/files-io.cpp
+++ b/eeschema/files-io.cpp
@@ -384,6 +384,13 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
     GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
     m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
     SetSheetNumberAndCount();
+
+    // re-create junctions if needed. EEschema optimizes wires by merging
+    // colinear segments. If a schematic is saved without a valid
+    // cache library or missing installed libraries, this can cause connectivity errors
+    // unless junctions are added.
+    FixupJunctions();
+
     SyncView();
     GetScreen()->ClearDrawingState();
 
diff --git a/eeschema/sch_edit_frame.cpp b/eeschema/sch_edit_frame.cpp
index e3010f36e..56f8b2d34 100644
--- a/eeschema/sch_edit_frame.cpp
+++ b/eeschema/sch_edit_frame.cpp
@@ -1139,3 +1139,38 @@ const BOX2I SCH_EDIT_FRAME::GetDocumentExtents() const
 
     return BOX2I( VECTOR2I(0, 0), VECTOR2I( sizeX, sizeY ) );
 }
+
+void SCH_EDIT_FRAME::FixupJunctions()
+{
+    SCH_SHEET_LIST sheetList;
+
+    sheetList.BuildSheetList( g_RootSheet );
+
+    for( unsigned i = 0; i < sheetList.size();  i++ )
+    {
+        std::vector<wxPoint> anchors;
+
+        SetCurrentSheet( sheetList[i] );
+        GetCurrentSheet().UpdateAllScreenReferences();
+
+        auto screen = GetCurrentSheet().LastScreen();
+
+        for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
+        {
+            if( item->Type() == SCH_COMPONENT_T )
+            {
+                auto cmp = static_cast<SCH_COMPONENT*>( item );
+                auto xform = cmp->GetTransform();
+
+                for( auto pin : cmp->GetPins() )
+                {
+                    auto pos = cmp->GetPosition() + xform.TransformCoordinate( pin.GetPosition() );
+                    if ( screen->IsJunctionNeeded( pos ) )
+                    {
+                        AddJunction( pos );
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h
index 10f01c61b..126f3e1fe 100644
--- a/eeschema/sch_edit_frame.h
+++ b/eeschema/sch_edit_frame.h
@@ -1026,6 +1026,8 @@ public:
 
     const BOX2I GetDocumentExtents() const override;
 
+    void FixupJunctions();
+
     DECLARE_EVENT_TABLE()
 };
 
-- 
2.17.1