From 653c7b78d70f41e4318687ae47ce8b06e96a3d23 Mon Sep 17 00:00:00 2001
From: Jeff Young <jeff@rokeby.ie>
Date: Sun, 12 Dec 2021 21:37:06 +0000
Subject: [PATCH] Move NC pin logic so we set it before we need it.

Also fixes drawing the dangling symbol in the Pin Properties dialog
and when printing.

Fixes https://gitlab.com/kicad/code/kicad/issues/9962
---
 eeschema/dialogs/dialog_pin_properties.cpp |  4 ++--
 eeschema/lib_pin.cpp                       | 10 ++++++----
 eeschema/sch_painter.cpp                   |  2 --
 eeschema/sch_pin.h                         |  9 ++++++++-
 4 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/eeschema/dialogs/dialog_pin_properties.cpp b/eeschema/dialogs/dialog_pin_properties.cpp
index 2cab71856e..5aa9122387 100644
--- a/eeschema/dialogs/dialog_pin_properties.cpp
+++ b/eeschema/dialogs/dialog_pin_properties.cpp
@@ -377,8 +377,8 @@ void DIALOG_PIN_PROPERTIES::OnPaintShowPanel( wxPaintEvent& event )
     double   yscale = (double) dc_size.y / bBox.GetHeight();
     double   scale = std::min( xscale, yscale );
 
-    // Give a 10% margin and limit to no more than 100% zoom
-    scale = std::min( scale * 0.9, 1.0 );
+    // Give a 7% margin (each side) and limit to no more than 100% zoom
+    scale = std::min( scale * 0.85, 1.0 );
     dc.SetUserScale( scale, scale );
     GRResetPenAndBrush( &dc );
 
diff --git a/eeschema/lib_pin.cpp b/eeschema/lib_pin.cpp
index 3472ec6783..50be185118 100644
--- a/eeschema/lib_pin.cpp
+++ b/eeschema/lib_pin.cpp
@@ -195,9 +195,9 @@ void LIB_PIN::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, v
                      const TRANSFORM& aTransform )
 {
     LIB_SYMBOL_OPTIONS* opts = (LIB_SYMBOL_OPTIONS*) aData;
-    bool               drawHiddenFields = opts ? opts->draw_hidden_fields : false;
-    bool               showPinType = opts ? opts->show_elec_type : false;
-    bool               show_connect_point = opts ? opts->show_connect_point : false;
+    bool                drawHiddenFields   = opts ? opts->draw_hidden_fields : false;
+    bool                showPinType        = opts ? opts->show_elec_type     : false;
+    bool                show_connect_point = opts ? opts->show_connect_point : false;
 
     LIB_SYMBOL* part = GetParent();
 
@@ -217,7 +217,9 @@ void LIB_PIN::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset, v
         if( showPinType )
             printPinElectricalTypeName( aSettings, pos1, orient );
 
-        if( show_connect_point )
+        if( show_connect_point
+                && m_type != ELECTRICAL_PINTYPE::PT_NC
+                && m_type != ELECTRICAL_PINTYPE::PT_NIC )
         {
             wxDC* DC = aSettings->GetPrintDC();
             COLOR4D color = aSettings->GetLayerColor( IsVisible() ? LAYER_PIN : LAYER_HIDDEN );
diff --git a/eeschema/sch_painter.cpp b/eeschema/sch_painter.cpp
index 9d56d053ac..d1e4e53e1d 100644
--- a/eeschema/sch_painter.cpp
+++ b/eeschema/sch_painter.cpp
@@ -842,8 +842,6 @@ void SCH_PAINTER::draw( LIB_PIN *aPin, int aLayer )
                          pos + VECTOR2D(  1,  1 ) * TARGET_PIN_RADIUS );
         m_gal->DrawLine( pos + VECTOR2D(  1, -1 ) * TARGET_PIN_RADIUS ,
                          pos + VECTOR2D( -1,  1 ) * TARGET_PIN_RADIUS );
-
-        aPin->ClearFlags( IS_DANGLING ); // PIN_NC pin type is always not connected and dangling.
     }
     else
     {
diff --git a/eeschema/sch_pin.h b/eeschema/sch_pin.h
index 5b217a642d..544ee0ce63 100644
--- a/eeschema/sch_pin.h
+++ b/eeschema/sch_pin.h
@@ -88,7 +88,14 @@ public:
 
     bool IsConnectable() const override { return true; }
 
-    bool IsDangling() const override { return m_isDangling; }
+    bool IsDangling() const override
+    {
+        if( GetType() == ELECTRICAL_PINTYPE::PT_NC || GetType() == ELECTRICAL_PINTYPE::PT_NIC )
+            return false;
+
+        return m_isDangling;
+    }
+
     void SetIsDangling( bool isDangling ) { m_isDangling = isDangling; }
 
     bool IsPointClickableAnchor( const wxPoint& aPos ) const override