From ebef24b6b06537f51a5dd4ae5c27b314d3faa488 Mon Sep 17 00:00:00 2001 From: John Beard Date: Sat, 7 Sep 2024 19:57:40 +0100 Subject: [PATCH] Debug: graphical snap anchor debug mode When working near snap anchors, which come and go rapidly, it's often useful to see what snaps have been calculated. Add an advanced config to show these (EnableSnapAnchorsDebug) on an overlay layer. With more polish this could be a hotkey or something. --- common/CMakeLists.txt | 1 + common/advanced_config.cpp | 6 ++ common/preview_items/anchor_debug.cpp | 129 +++++++++++++++++++++++++ common/tool/grid_helper.cpp | 22 ++++- eeschema/tools/ee_grid_helper.cpp | 10 ++ include/advanced_config.h | 8 ++ include/preview_items/anchor_debug.h | 83 ++++++++++++++++ include/preview_items/snap_indicator.h | 5 +- include/tool/grid_helper.h | 11 +++ pcbnew/tools/pcb_grid_helper.cpp | 11 ++- 10 files changed, 279 insertions(+), 7 deletions(-) create mode 100644 common/preview_items/anchor_debug.cpp create mode 100644 include/preview_items/anchor_debug.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index b5cd95bea4..d2ef5f4e57 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -454,6 +454,7 @@ set( COMMON_DRAWING_SHEET_SRCS ) set( COMMON_PREVIEW_ITEMS_SRCS + preview_items/anchor_debug.cpp preview_items/arc_assistant.cpp preview_items/arc_geom_manager.cpp preview_items/centreline_rect_item.cpp diff --git a/common/advanced_config.cpp b/common/advanced_config.cpp index fb0ca1c84c..c8764d2737 100644 --- a/common/advanced_config.cpp +++ b/common/advanced_config.cpp @@ -118,6 +118,7 @@ static const wxChar MaxFileSystemWatchers[] = wxT( "MaxFileSystemWatchers" ); static const wxChar MinorSchematicGraphSize[] = wxT( "MinorSchematicGraphSize" ); static const wxChar ResolveTextRecursionDepth[] = wxT( "ResolveTextRecursionDepth" ); static const wxChar EnableExtensionSnaps[] = wxT( "EnableExtensionSnaps" ); +static const wxChar EnableSnapAnchorsDebug[] = wxT( "EnableSnapAnchorsDebug" ); } // namespace KEYS @@ -282,6 +283,7 @@ ADVANCED_CFG::ADVANCED_CFG() m_ResolveTextRecursionDepth = 3; m_EnableExtensionSnaps = false; + m_EnableSnapAnchorsDebug = false; loadFromConfigFile(); } @@ -530,6 +532,10 @@ void ADVANCED_CFG::loadSettings( wxConfigBase& aCfg ) &m_EnableExtensionSnaps, m_EnableExtensionSnaps ) ); + configParams.push_back( new PARAM_CFG_BOOL( true, AC_KEYS::EnableSnapAnchorsDebug, + &m_EnableSnapAnchorsDebug, + m_EnableSnapAnchorsDebug ) ); + // Special case for trace mask setting...we just grab them and set them immediately // Because we even use wxLogTrace inside of advanced config wxString traceMasks; diff --git a/common/preview_items/anchor_debug.cpp b/common/preview_items/anchor_debug.cpp new file mode 100644 index 0000000000..a6433b11f4 --- /dev/null +++ b/common/preview_items/anchor_debug.cpp @@ -0,0 +1,129 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "preview_items/anchor_debug.h" + +#include +#include +#include + +using namespace KIGFX; + +ANCHOR_DEBUG::ANCHOR_DEBUG() : + EDA_ITEM( nullptr, NOT_USED ) // Never added to a BOARD/SCHEMATIC so it needs no type +{ +} + + +ANCHOR_DEBUG* ANCHOR_DEBUG::Clone() const +{ + return new ANCHOR_DEBUG(); +} + + +const BOX2I ANCHOR_DEBUG::ViewBBox() const +{ + // We could be a bit more careful here, but also we need to + // know the world scale to cover everything exactly, and there + // is only one of these. + BOX2I bbox; + bbox.SetMaximum(); + return bbox; +} + +void ANCHOR_DEBUG::ViewGetLayers( int aLayers[], int& aCount ) const +{ + aLayers[0] = LAYER_GP_OVERLAY; + aCount = 1; +} + +void ANCHOR_DEBUG::ClearAnchors() +{ + m_nearest.reset(); + m_anchors.clear(); +} + +void ANCHOR_DEBUG::AddAnchor( const VECTOR2I& aAnchor ) +{ + m_anchors[aAnchor]++; +} + +void ANCHOR_DEBUG::SetNearest( const OPT_VECTOR2I& aNearest ) +{ + m_nearest = aNearest; +} + +void ANCHOR_DEBUG::ViewDraw( int, VIEW* aView ) const +{ + GAL& gal = *aView->GetGAL(); + RENDER_SETTINGS& settings = *aView->GetPainter()->GetSettings(); + + const COLOR4D textColor = settings.GetLayerColor( LAYER_AUX_ITEMS ); + + const BOX2D viewportD = aView->GetViewport(); + const BOX2I viewport( KiROUND( viewportD.GetPosition() ), KiROUND( viewportD.GetSize() ) ); + + gal.SetIsFill( false ); + gal.SetIsStroke( true ); + gal.SetLineWidth( 1 ); + + const int markerRad = aView->ToWorld( 3 ); + const int markerTextHeight = aView->ToWorld( 6 ); + const int markerTextGap = aView->ToWorld( 3 ); + const int summaryTextHeight = aView->ToWorld( 10 ); + const VECTOR2I textOffset = { markerRad + markerTextGap, 0 }; + + TEXT_ATTRIBUTES attributes; + attributes.m_Halign = GR_TEXT_H_ALIGN_LEFT; + attributes.m_Size = VECTOR2I( markerTextHeight, markerTextHeight ); + + const KIFONT::METRICS& fontMetrics = KIFONT::METRICS::Default(); + const KIFONT::FONT& font = *KIFONT::FONT::GetFont(); + + size_t total = 0; + for( const auto& [anchor, count] : m_anchors ) + { + if( m_nearest && *m_nearest == anchor ) + gal.SetStrokeColor( RED ); + else + gal.SetStrokeColor( YELLOW ); + + gal.DrawCircle( anchor, markerRad ); + + const std::string countStr = std::to_string( count ); + font.Draw( &gal, countStr, anchor + textOffset, attributes, fontMetrics ); + + total += count; + } + + gal.SetStrokeColor( textColor ); + + const int boundaryMargin = aView->ToWorld( 20 ); + VECTOR2I fontPos{ viewport.GetLeft(), viewport.GetTop() }; + fontPos += VECTOR2I{ boundaryMargin, boundaryMargin }; + + attributes.m_Size = VECTOR2I{ summaryTextHeight, summaryTextHeight }; + + wxString totalStr = wxString::Format( "Current snap anchors: %lu", total ); + font.Draw( &gal, totalStr, fontPos, attributes, fontMetrics ); +} diff --git a/common/tool/grid_helper.cpp b/common/tool/grid_helper.cpp index 5e6b916074..f75dac3887 100644 --- a/common/tool/grid_helper.cpp +++ b/common/tool/grid_helper.cpp @@ -25,6 +25,7 @@ #include +#include #include #include #include // for KiROUND @@ -73,8 +74,25 @@ GRID_HELPER::GRID_HELPER( TOOL_MANAGER* aToolMgr, int aConstructionLayer ) : GRID_HELPER::~GRID_HELPER() { - KIGFX::VIEW* view = m_toolMgr->GetView(); - view->Remove( &m_constructionGeomPreview ); + KIGFX::VIEW& view = *m_toolMgr->GetView(); + view.Remove( &m_constructionGeomPreview ); + + if( m_anchorDebug ) + view.Remove( m_anchorDebug.get() ); +} + + +KIGFX::ANCHOR_DEBUG* GRID_HELPER::enableAndGetAnchorDebug() +{ + static bool permitted = ADVANCED_CFG::GetCfg().m_EnableSnapAnchorsDebug; + if( permitted && !m_anchorDebug ) + { + KIGFX::VIEW& view = *m_toolMgr->GetView(); + m_anchorDebug = std::make_unique(); + view.Add( m_anchorDebug.get() ); + view.SetVisible( m_anchorDebug.get(), true ); + } + return m_anchorDebug.get(); } diff --git a/eeschema/tools/ee_grid_helper.cpp b/eeschema/tools/ee_grid_helper.cpp index 03ab968dc0..a761c48569 100644 --- a/eeschema/tools/ee_grid_helper.cpp +++ b/eeschema/tools/ee_grid_helper.cpp @@ -160,6 +160,16 @@ VECTOR2I EE_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, GRID_HELPER_GR ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aGrid ); VECTOR2I nearestGrid = Align( aOrigin, aGrid ); + if( KIGFX::ANCHOR_DEBUG* ad = enableAndGetAnchorDebug(); ad ) + { + ad->ClearAnchors(); + for( const ANCHOR& a : m_anchors ) + ad->AddAnchor( a.pos ); + + ad->SetNearest( nearest ? OPT_VECTOR2I( nearest->pos ) : std::nullopt ); + m_toolMgr->GetView()->Update( ad, KIGFX::GEOMETRY ); + } + showConstructionGeometry( m_enableSnap ); std::optional snapLineOrigin = getConstructionManager().GetSnapLineOrigin(); diff --git a/include/advanced_config.h b/include/advanced_config.h index 974ab8ac26..7e14047825 100644 --- a/include/advanced_config.h +++ b/include/advanced_config.h @@ -652,6 +652,14 @@ public: */ bool m_EnableExtensionSnaps; + /** + * Enable snap anchors debug visualization. + * + * Setting name: "EnableSnapAnchorsDebug" + * Default value: false + */ + bool m_EnableSnapAnchorsDebug; + ///@} private: diff --git a/include/preview_items/anchor_debug.h b/include/preview_items/anchor_debug.h new file mode 100644 index 0000000000..05b36dd103 --- /dev/null +++ b/include/preview_items/anchor_debug.h @@ -0,0 +1,83 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma once + +#include +#include +#include + +#include + +#include +#include // OPT_VECTOR2I + +namespace KIGFX +{ + +/** + * View item to draw debug items for anchors. + * + * This can be handy when verifying how many/where anchors are being placed. + */ +class ANCHOR_DEBUG : public EDA_ITEM +{ +public: + ANCHOR_DEBUG(); + + ANCHOR_DEBUG* Clone() const override; + + void ViewGetLayers( int aLayers[], int& aCount ) const override; + + const BOX2I ViewBBox() const override; + + void ViewDraw( int aLayer, VIEW* aView ) const override; + + /** + * Get class name. + * + * @return string "ANCHOR_DEBUG" + */ + wxString GetClass() const override { return wxT( "ANCHOR_DEBUG" ); } + + void ClearAnchors(); + + /** + * Add an anchor at the given position. + */ + void AddAnchor( const VECTOR2I& aAnchor ); + + /** + * Set the nearest anchor to the given position. + * + * Pass an empty optional to clear the nearest anchor. + */ + void SetNearest( const OPT_VECTOR2I& aNearest ); + +private: + // Store the anchors by location and count + std::map m_anchors; + OPT_VECTOR2I m_nearest; +}; + +} // namespace KIGFX diff --git a/include/preview_items/snap_indicator.h b/include/preview_items/snap_indicator.h index a0a3859c2d..176d7c1941 100644 --- a/include/preview_items/snap_indicator.h +++ b/include/preview_items/snap_indicator.h @@ -21,8 +21,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __SNAP_INDICATOR_H -#define __SNAP_INDICATOR_H +#pragma once #include #include @@ -72,5 +71,3 @@ private: }; } // namespace KIGFX - -#endif diff --git a/include/tool/grid_helper.h b/include/tool/grid_helper.h index 66a4b4d2be..290b2e09a8 100644 --- a/include/tool/grid_helper.h +++ b/include/tool/grid_helper.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -206,6 +207,13 @@ protected: void updateSnapPoint( const TYPED_POINT2I& aPoint ); + /** + * Enable the anchor debug if permitted and return it + * + * Returns nullptr if not permitted by the advancd config + */ + KIGFX::ANCHOR_DEBUG* enableAndGetAnchorDebug(); + std::vector m_anchors; TOOL_MANAGER* m_toolMgr; @@ -232,6 +240,9 @@ private: // and deals with updating the construction helper as well as keeping // track of what geometry is "active" for construction purposes. CONSTRUCTION_MANAGER m_constructionManager; + + /// @brief VIEW_ITEM for visualising anchor points, if enabled + std::unique_ptr m_anchorDebug; }; #endif diff --git a/pcbnew/tools/pcb_grid_helper.cpp b/pcbnew/tools/pcb_grid_helper.cpp index 2ad8fbd94f..07bf2ae7a2 100644 --- a/pcbnew/tools/pcb_grid_helper.cpp +++ b/pcbnew/tools/pcb_grid_helper.cpp @@ -181,7 +181,6 @@ void PCB_GRID_HELPER::AddConstructionItems( std::vector aItems, boo return; } - // For all the elements that get drawn construction geometry, // add something suitable to the construction helper. // This can be nothing. @@ -519,6 +518,16 @@ VECTOR2I PCB_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& a ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE ); VECTOR2I nearestGrid = Align( aOrigin, aGrid ); + if( KIGFX::ANCHOR_DEBUG* ad = enableAndGetAnchorDebug(); ad ) + { + ad->ClearAnchors(); + for( const ANCHOR& anchor : m_anchors ) + ad->AddAnchor( anchor.pos ); + + ad->SetNearest( nearest ? OPT_VECTOR2I{ nearest->pos } : std::nullopt ); + m_toolMgr->GetView()->Update( ad, KIGFX::GEOMETRY ); + } + // The distance to the nearest snap point, if any std::optional snapDist; if( nearest )