2021-01-16 23:17:32 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2025-01-01 13:30:11 -08:00
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2021-01-16 23:17:32 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GRID_HELPER_H
|
|
|
|
#define GRID_HELPER_H
|
|
|
|
|
|
|
|
#include <vector>
|
2024-08-08 23:55:48 +08:00
|
|
|
|
|
|
|
#include <geometry/point_types.h>
|
2021-01-16 23:17:32 +00:00
|
|
|
#include <math/vector2d.h>
|
2024-09-07 19:57:40 +01:00
|
|
|
#include <preview_items/anchor_debug.h>
|
2024-08-08 23:55:48 +08:00
|
|
|
#include <preview_items/snap_indicator.h>
|
2024-09-06 00:00:00 +01:00
|
|
|
#include <preview_items/construction_geom.h>
|
|
|
|
#include <tool/construction_manager.h>
|
2024-08-08 23:55:48 +08:00
|
|
|
#include <tool/selection.h>
|
2021-01-16 23:17:32 +00:00
|
|
|
#include <origin_viewitem.h>
|
|
|
|
|
2025-08-05 16:10:17 -07:00
|
|
|
class TOOL_MANAGER; // Forward declaration to avoid hard dependency in tests
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
class EDA_ITEM;
|
|
|
|
|
2023-08-23 13:05:08 -04:00
|
|
|
enum GRID_HELPER_GRIDS : int
|
|
|
|
{
|
|
|
|
// When the item doesn't match an override, use the current user grid
|
|
|
|
GRID_CURRENT,
|
|
|
|
|
|
|
|
GRID_CONNECTABLE,
|
|
|
|
GRID_WIRES,
|
2023-08-24 10:13:59 -04:00
|
|
|
GRID_VIAS,
|
2023-08-23 13:05:08 -04:00
|
|
|
GRID_TEXT,
|
|
|
|
GRID_GRAPHICS
|
|
|
|
};
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
class GRID_HELPER
|
|
|
|
{
|
2025-08-05 16:10:17 -07:00
|
|
|
friend void TEST_CLEAR_ANCHORS( GRID_HELPER& helper );
|
2021-01-16 23:17:32 +00:00
|
|
|
public:
|
2025-08-05 16:10:17 -07:00
|
|
|
GRID_HELPER();
|
2024-09-06 00:00:00 +01:00
|
|
|
GRID_HELPER( TOOL_MANAGER* aToolMgr, int aConstructionLayer );
|
2021-02-02 09:40:04 -08:00
|
|
|
virtual ~GRID_HELPER();
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
VECTOR2I GetGrid() const;
|
2023-08-11 17:58:33 +02:00
|
|
|
VECTOR2D GetVisibleGrid() const;
|
2021-01-16 23:17:32 +00:00
|
|
|
VECTOR2I GetOrigin() const;
|
|
|
|
|
2025-08-05 16:10:17 -07:00
|
|
|
// Manual setters used when no TOOL_MANAGER/View is available (e.g. in tests)
|
|
|
|
void SetGridSize( const VECTOR2D& aGrid ) { m_manualGrid = aGrid; }
|
|
|
|
void SetVisibleGridSize( const VECTOR2D& aGrid ) { m_manualVisibleGrid = aGrid; }
|
|
|
|
void SetOrigin( const VECTOR2I& aOrigin ) { m_manualOrigin = aOrigin; }
|
|
|
|
void SetGridSnapping( bool aEnable ) { m_manualGridSnapping = aEnable; }
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
|
|
|
|
|
2023-08-23 13:05:08 -04:00
|
|
|
virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
|
|
|
|
{
|
2025-08-31 07:20:39 -07:00
|
|
|
return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
|
2023-08-23 13:05:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
|
|
|
|
{
|
2025-08-31 07:20:39 -07:00
|
|
|
return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
|
2023-08-23 13:05:08 -04:00
|
|
|
}
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
|
2025-08-31 07:20:39 -07:00
|
|
|
virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
|
|
|
|
const VECTOR2D& aOffset ) const;
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
|
2025-08-31 07:20:39 -07:00
|
|
|
VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
|
|
|
|
const VECTOR2D& aOffset ) const;
|
2021-01-16 23:17:32 +00:00
|
|
|
|
2023-08-23 13:05:08 -04:00
|
|
|
/**
|
|
|
|
* Gets the coarsest grid that applies to a selecion of items.
|
|
|
|
*/
|
|
|
|
virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
|
|
|
|
|
|
|
|
/**
|
2025-01-04 09:21:11 -05:00
|
|
|
* Get the coarsest grid that applies to an item.
|
2023-08-23 13:05:08 -04:00
|
|
|
*/
|
|
|
|
virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
|
|
|
|
|
|
|
|
/**
|
2025-01-04 09:21:11 -05:00
|
|
|
* Return the size of the specified grid.
|
2023-08-23 13:05:08 -04:00
|
|
|
*/
|
|
|
|
virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
void SetSkipPoint( const VECTOR2I& aPoint )
|
|
|
|
{
|
|
|
|
m_skipPoint = aPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-01-04 09:21:11 -05:00
|
|
|
* Clear the skip point by setting it to an unreachable position, thereby preventing matching.
|
2021-01-16 23:17:32 +00:00
|
|
|
*/
|
|
|
|
void ClearSkipPoint()
|
|
|
|
{
|
|
|
|
m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
|
2021-01-18 22:31:48 +00:00
|
|
|
bool GetSnap() const { return m_enableSnap; }
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
|
2021-01-18 22:31:48 +00:00
|
|
|
bool GetUseGrid() const { return m_enableGrid; }
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
|
|
|
|
|
2021-02-02 09:40:04 -08:00
|
|
|
void SetMask( int aMask ) { m_maskTypes = aMask; }
|
|
|
|
void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
|
|
|
|
void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
|
|
|
|
|
2024-08-11 18:59:26 -04:00
|
|
|
std::optional<VECTOR2I> GetSnappedPoint() const;
|
|
|
|
|
2024-08-08 23:55:48 +08:00
|
|
|
enum ANCHOR_FLAGS
|
|
|
|
{
|
2021-01-16 23:17:32 +00:00
|
|
|
CORNER = 1,
|
|
|
|
OUTLINE = 2,
|
|
|
|
SNAPPABLE = 4,
|
|
|
|
ORIGIN = 8,
|
|
|
|
VERTICAL = 16,
|
2021-02-02 09:40:04 -08:00
|
|
|
HORIZONTAL = 32,
|
2025-01-04 09:21:11 -05:00
|
|
|
|
2024-09-06 00:00:00 +01:00
|
|
|
// This anchor comes from 'constructed' geometry (e.g. an intersection
|
|
|
|
// with something else), and not from some intrinsic point of an item
|
|
|
|
// (e.g. an endpoint)
|
|
|
|
CONSTRUCTED = 64,
|
|
|
|
ALL = CORNER | OUTLINE | SNAPPABLE | ORIGIN | VERTICAL | HORIZONTAL | CONSTRUCTED
|
2021-01-16 23:17:32 +00:00
|
|
|
};
|
|
|
|
|
2021-02-02 09:40:04 -08:00
|
|
|
protected:
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
struct ANCHOR
|
|
|
|
{
|
2024-08-08 23:55:48 +08:00
|
|
|
/**
|
2025-01-04 09:21:11 -05:00
|
|
|
* @param aPos The position of the anchor.
|
2024-08-08 23:55:48 +08:00
|
|
|
* @param aFlags The flags for the anchor - this is a bitfield of ANCHOR_FLAGS,
|
|
|
|
* specifying the type of anchor (which may be used to filter out
|
2025-01-04 09:21:11 -05:00
|
|
|
* unwanted anchors per the settings).
|
|
|
|
* @param aPointTypes The point types that this anchor represents in geometric terms.
|
|
|
|
* @param aItem The item to which the anchor belongs.
|
2024-08-08 23:55:48 +08:00
|
|
|
*/
|
2024-09-06 00:00:00 +01:00
|
|
|
ANCHOR( const VECTOR2I& aPos, int aFlags, int aPointTypes, std::vector<EDA_ITEM*> aItems ) :
|
|
|
|
pos( aPos ), flags( aFlags ), pointTypes( aPointTypes ),
|
|
|
|
items( std::move( aItems ) )
|
2024-08-08 23:55:48 +08:00
|
|
|
{
|
|
|
|
}
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
VECTOR2I pos;
|
|
|
|
int flags;
|
2024-08-08 23:55:48 +08:00
|
|
|
int pointTypes;
|
2025-01-04 09:21:11 -05:00
|
|
|
|
|
|
|
/// Items that are associated with this anchor (can be more than one, e.g. for an
|
|
|
|
/// intersection).
|
2024-09-06 00:00:00 +01:00
|
|
|
std::vector<EDA_ITEM*> items;
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
double Distance( const VECTOR2I& aP ) const
|
|
|
|
{
|
2024-04-03 00:39:55 +03:00
|
|
|
return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
|
2021-01-16 23:17:32 +00:00
|
|
|
}
|
2024-09-06 00:00:00 +01:00
|
|
|
|
|
|
|
bool InvolvesItem( const EDA_ITEM& aItem ) const
|
|
|
|
{
|
|
|
|
return std::find( items.begin(), items.end(), &aItem ) != items.end();
|
|
|
|
}
|
2021-01-16 23:17:32 +00:00
|
|
|
};
|
|
|
|
|
2024-08-08 23:55:48 +08:00
|
|
|
void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem,
|
|
|
|
int aPointTypes = POINT_TYPE::PT_NONE )
|
2024-09-06 00:00:00 +01:00
|
|
|
{
|
|
|
|
addAnchor( aPos, aFlags, std::vector<EDA_ITEM*>{ aItem }, aPointTypes );
|
|
|
|
}
|
|
|
|
|
|
|
|
void addAnchor( const VECTOR2I& aPos, int aFlags, std::vector<EDA_ITEM*> aItems,
|
|
|
|
int aPointTypes )
|
2021-01-16 23:17:32 +00:00
|
|
|
{
|
2021-02-02 09:40:04 -08:00
|
|
|
if( ( aFlags & m_maskTypes ) == aFlags )
|
2024-09-06 00:00:00 +01:00
|
|
|
m_anchors.emplace_back( ANCHOR( aPos, aFlags, aPointTypes, std::move( aItems ) ) );
|
2021-01-16 23:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clearAnchors()
|
|
|
|
{
|
|
|
|
m_anchors.clear();
|
|
|
|
}
|
|
|
|
|
2021-02-10 01:01:36 +01:00
|
|
|
/**
|
|
|
|
* Check whether it is possible to use the grid -- this depends both on local grid helper
|
|
|
|
* settings and global (tool manager) KiCad settings.
|
|
|
|
*/
|
2023-09-18 19:52:27 -04:00
|
|
|
bool canUseGrid() const;
|
2021-02-10 01:01:36 +01:00
|
|
|
|
2025-08-31 07:20:39 -07:00
|
|
|
VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
|
|
|
|
const VECTOR2I& aOffset ) const;
|
2023-07-23 13:51:42 -04:00
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
protected:
|
2024-09-06 00:00:00 +01:00
|
|
|
void showConstructionGeometry( bool aShow );
|
|
|
|
|
2024-09-24 23:09:47 +01:00
|
|
|
SNAP_MANAGER& getSnapManager() { return m_snapManager; }
|
2024-09-06 00:00:00 +01:00
|
|
|
|
|
|
|
void updateSnapPoint( const TYPED_POINT2I& aPoint );
|
|
|
|
|
2024-09-07 19:57:40 +01:00
|
|
|
/**
|
|
|
|
* Enable the anchor debug if permitted and return it
|
|
|
|
*
|
|
|
|
* Returns nullptr if not permitted by the advancd config
|
|
|
|
*/
|
|
|
|
KIGFX::ANCHOR_DEBUG* enableAndGetAnchorDebug();
|
|
|
|
|
2023-08-26 13:29:24 +01:00
|
|
|
std::vector<ANCHOR> m_anchors;
|
|
|
|
|
|
|
|
TOOL_MANAGER* m_toolMgr;
|
|
|
|
std::optional<VECTOR2I> m_auxAxis;
|
|
|
|
|
|
|
|
int m_maskTypes; // Mask of allowed snap types
|
|
|
|
|
|
|
|
bool m_enableSnap; // Allow snapping to other items on the layers
|
|
|
|
bool m_enableGrid; // If true, allow snapping to grid
|
|
|
|
bool m_enableSnapLine; // Allow drawing lines from snap points
|
2024-08-08 14:28:58 -07:00
|
|
|
std::optional<ANCHOR> m_snapItem; // Pointer to the currently snapped item in m_anchors
|
2023-08-26 13:29:24 +01:00
|
|
|
// (NULL if not snapped)
|
|
|
|
VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
|
|
|
|
// source point
|
2024-08-08 23:55:48 +08:00
|
|
|
KIGFX::SNAP_INDICATOR m_viewSnapPoint;
|
2023-08-26 13:29:24 +01:00
|
|
|
KIGFX::ORIGIN_VIEWITEM m_viewAxis;
|
2024-09-06 00:00:00 +01:00
|
|
|
|
2025-08-05 16:10:17 -07:00
|
|
|
// Manual grid parameters used when no TOOL_MANAGER is provided
|
|
|
|
VECTOR2D m_manualGrid;
|
|
|
|
VECTOR2D m_manualVisibleGrid;
|
|
|
|
VECTOR2I m_manualOrigin;
|
|
|
|
bool m_manualGridSnapping;
|
|
|
|
|
2024-09-06 00:00:00 +01:00
|
|
|
private:
|
2025-01-04 09:21:11 -05:00
|
|
|
/// Show construction geometry (if any) on the canvas.
|
2024-09-06 00:00:00 +01:00
|
|
|
KIGFX::CONSTRUCTION_GEOM m_constructionGeomPreview;
|
|
|
|
|
2025-01-04 09:21:11 -05:00
|
|
|
/// Manage the construction geometry, snap lines, reference points, etc.
|
2024-09-24 23:09:47 +01:00
|
|
|
SNAP_MANAGER m_snapManager;
|
2024-09-07 19:57:40 +01:00
|
|
|
|
2025-01-04 09:21:11 -05:00
|
|
|
/// #VIEW_ITEM for visualising anchor points, if enabled.
|
2024-09-07 19:57:40 +01:00
|
|
|
std::unique_ptr<KIGFX::ANCHOR_DEBUG> m_anchorDebug;
|
2021-01-16 23:17:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|