KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_constraint_adapter.h File Reference
#include <deque>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <vector>
#include <kiid.h>
#include <math/vector2d.h>
#include <constraints/pcb_constraint.h>

Go to the source code of this file.

Classes

struct  CONSTRAINT_DIAGNOSIS
 The outcome of a constraint solve, in plain data so callers need not know planegcs. More...
 
struct  BOARD_CONSTRAINT_DIAGNOSTICS
 Board-wide diagnostics for the constraint overlay and info bar. More...
 
class  BOARD_CONSTRAINT_ADAPTER
 Translates KiCad board geometry to and from the planegcs solver (issue #2329). More...
 
struct  BOARD_CONSTRAINT_ADAPTER::SHAPE_VARS
 Per-shape indices into m_params. More...
 
struct  BOARD_CONSTRAINT_ADAPTER::ANCHOR_PARAMS
 Param indices of an anchor coordinates a rect corner aliases mixed start end params so y is not always x plus 1. More...
 
struct  CLUSTER_DIAGNOSIS
 One cluster's diagnosis, the unit DiagnoseBoardConstraints assembles the board-wide result from and BOARD_CONSTRAINT_DIAGNOSER caches state applies to every shape and dimension in the cluster. More...
 
class  BOARD_CONSTRAINT_DIAGNOSER
 An incremental DiagnoseBoardConstraints for the interactive edit path. More...
 
struct  BOARD_CONSTRAINT_DIAGNOSER::CACHE_ENTRY
 

Namespaces

namespace  GCS
 

Enumerations

enum class  CONSTRAINT_STATE { UNCONSTRAINED , UNDER_CONSTRAINED , WELL_CONSTRAINED , OVER_CONSTRAINED }
 The constraint state of a shape, for the diagnostics overlay tint. More...
 

Functions

CONSTRAINT_DIAGNOSIS SolveCluster (BOARD *aBoard, const CONSTRAINT_MEMBER &aDragged, const VECTOR2I &aCursor, std::vector< PCB_SHAPE * > *aModified=nullptr, const std::function< void(BOARD_ITEM *)> &aBeforeModify={}, bool aIncludeDragged=false, bool aStabilize=false, const std::set< KIID > &aEdited={}, const std::optional< std::pair< CONSTRAINT_MEMBER, VECTOR2I > > &aCoDragged=std::nullopt)
 Gather the cluster of shapes transitively constrained with the dragged shape, solve with the dragged anchor pinned to the cursor, and apply the result.
 
CONSTRAINT_DIAGNOSIS ApplyConstraintImmediately (BOARD *aBoard, const PCB_CONSTRAINT *aConstraint, std::vector< PCB_SHAPE * > *aModified=nullptr, const std::function< void(BOARD_ITEM *)> &aBeforeModify={})
 Solve a just-created constraint's cluster so the geometry snaps to satisfy it (SolidWorks-style), pinning the constraint's first member at its current position and letting the solver move the rest.
 
bool BoardHasConstraints (BOARD *aBoard)
 True if the board or any of its footprints carries at least one geometric constraint.
 
bool ConstraintItemIsLocked (const BOARD_ITEM *aItem)
 True when the solver must treat aItem as immovable, either locked itself or living inside a locked footprint.
 
void ReSolveShapeClusters (BOARD *aBoard, const std::vector< PCB_SHAPE * > &aShapes, std::vector< PCB_SHAPE * > *aModified=nullptr, const std::function< void(BOARD_ITEM *)> &aBeforeModify={})
 Re-solve the clusters of shapes edited outside the solver, e.g.
 
void ReSolveShapeClustersHoldingEdited (BOARD *aBoard, const std::vector< PCB_SHAPE * > &aEditedShapes, std::vector< PCB_SHAPE * > *aModified=nullptr, const std::function< void(BOARD_ITEM *)> &aBeforeModify={})
 Re-solve clusters whose new geometry is authoritative holding every edited shape fully fixed so only constrained neighbors move a failed solve leaves that cluster untouched.
 
void ReSolveAfterShapeResize (BOARD *aBoard, PCB_SHAPE *aShape, std::vector< PCB_SHAPE * > *aModified=nullptr, const std::function< void(BOARD_ITEM *)> &aBeforeModify={})
 Re-solve after a resize, e.g. a circle radius edit. Holds aShape fixed so its neighbors adjust.
 
BOARD_CONSTRAINT_DIAGNOSTICS DiagnoseBoardConstraints (BOARD *aBoard)
 Diagnose every constraint cluster on the board (validate only – geometry is not changed) and return the per-shape state plus the conflicting/redundant constraints and total free DOF, for the diagnostics overlay and info bar.
 

Enumeration Type Documentation

◆ CONSTRAINT_STATE

enum class CONSTRAINT_STATE
strong

The constraint state of a shape, for the diagnostics overlay tint.

Enumerator
UNCONSTRAINED 

Not referenced by any constraint.

UNDER_CONSTRAINED 

In a cluster with remaining free degrees of freedom.

WELL_CONSTRAINED 

In a fully-determined cluster (zero free DOF).

OVER_CONSTRAINED 

In a cluster the solver reports as conflicting.

Definition at line 66 of file board_constraint_adapter.h.

Function Documentation

◆ ApplyConstraintImmediately()

CONSTRAINT_DIAGNOSIS ApplyConstraintImmediately ( BOARD * aBoard,
const PCB_CONSTRAINT * aConstraint,
std::vector< PCB_SHAPE * > * aModified = nullptr,
const std::function< void(BOARD_ITEM *)> & aBeforeModify = {} )

Solve a just-created constraint's cluster so the geometry snaps to satisfy it (SolidWorks-style), pinning the constraint's first member at its current position and letting the solver move the rest.

aConstraint must already be on the board. A failed solve leaves geometry untouched.

Parameters
aModified[out] the shapes the solve moved, for the caller to stage in a commit.
aBeforeModifyif set, invoked with each moved shape and each reference constraint whose value changed, just before it changes, so the caller can stage it in the commit.

Definition at line 2329 of file board_constraint_adapter.cpp.

References ConstraintAnchorPosition(), ConstraintShapeAnchors(), PCB_CONSTRAINT::GetMembers(), pin, BOARD::ResolveItem(), SolveCluster(), and WHOLE.

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), EditConstraintValue(), DIALOG_DIMENSION_PROPERTIES::resolveDrivingConstraint(), CONSTRAINT_EDIT_TOOL::solveAddedConstraint(), and PCB_PROPERTIES_PANEL::valueChanged().

◆ BoardHasConstraints()

bool BoardHasConstraints ( BOARD * aBoard)

True if the board or any of its footprints carries at least one geometric constraint.

Cheap, short-circuiting guard so an unconstrained board pays nothing on every edit.

Definition at line 74 of file board_constraint_adapter.cpp.

References BOARD::Constraints(), and BOARD::Footprints().

Referenced by CONSTRAINT_EDIT_TOOL::DiagnoseAfterMove(), EDIT_TOOL::doMoveSelection(), EDIT_TOOL::Move(), CONSTRAINT_EDIT_TOOL::onHoverMotion(), CONSTRAINT_EDIT_TOOL::SolveAfterEdit(), and CONSTRAINT_EDIT_TOOL::SolveAfterMove().

◆ ConstraintItemIsLocked()

bool ConstraintItemIsLocked ( const BOARD_ITEM * aItem)

True when the solver must treat aItem as immovable, either locked itself or living inside a locked footprint.

BOARD_ITEM::IsLocked() alone misses the parent-footprint case because most edit tools filter footprint children out before their lock checks run, while the solver collects footprint graphics directly.

Definition at line 88 of file board_constraint_adapter.cpp.

References FPHOLDER, BOARD_ITEM::GetBoard(), BOARD::GetBoardUse(), BOARD_ITEM::GetParentFootprint(), BOARD_ITEM::IsLocked(), and FOOTPRINT::IsLocked().

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOARD_CONSTRAINT_ADAPTER::Build(), hashShape(), BOARD_CONSTRAINT_ADAPTER::holdFreeArcRadii(), BOARD_CONSTRAINT_ADAPTER::holdFreeSegmentLengths(), BOARD_CONSTRAINT_ADAPTER::holdPolygonVertices(), and BOARD_CONSTRAINT_ADAPTER::pinUneditedShapes().

◆ DiagnoseBoardConstraints()

◆ ReSolveAfterShapeResize()

void ReSolveAfterShapeResize ( BOARD * aBoard,
PCB_SHAPE * aShape,
std::vector< PCB_SHAPE * > * aModified = nullptr,
const std::function< void(BOARD_ITEM *)> & aBeforeModify = {} )

◆ ReSolveShapeClusters()

void ReSolveShapeClusters ( BOARD * aBoard,
const std::vector< PCB_SHAPE * > & aShapes,
std::vector< PCB_SHAPE * > * aModified = nullptr,
const std::function< void(BOARD_ITEM *)> & aBeforeModify = {} )

◆ ReSolveShapeClustersHoldingEdited()

void ReSolveShapeClustersHoldingEdited ( BOARD * aBoard,
const std::vector< PCB_SHAPE * > & aEditedShapes,
std::vector< PCB_SHAPE * > * aModified = nullptr,
const std::function< void(BOARD_ITEM *)> & aBeforeModify = {} )

◆ SolveCluster()

CONSTRAINT_DIAGNOSIS SolveCluster ( BOARD * aBoard,
const CONSTRAINT_MEMBER & aDragged,
const VECTOR2I & aCursor,
std::vector< PCB_SHAPE * > * aModified = nullptr,
const std::function< void(BOARD_ITEM *)> & aBeforeModify = {},
bool aIncludeDragged = false,
bool aStabilize = false,
const std::set< KIID > & aEdited = {},
const std::optional< std::pair< CONSTRAINT_MEMBER, VECTOR2I > > & aCoDragged = std::nullopt )

Gather the cluster of shapes transitively constrained with the dragged shape, solve with the dragged anchor pinned to the cursor, and apply the result.

Kept free-standing so the interactive drag hook stays thin and this logic is headless-testable.

Parameters
aBoardthe board (or footprint-holder board) owning the constraints.
aDraggedthe anchor being dragged.
aCursorthe cursor target, in IU.
aModified[out] the neighbor shapes whose geometry the solve changed (excludes the dragged shape itself), for the caller to stage in a commit.
aBeforeModifyif set, invoked with each item (neighbor shape or reference constraint whose measured value changed) immediately before it is modified, so the caller can BOARD_COMMIT::Modify it for a clean undo. The dragged shape is excluded unless aIncludeDragged, since the caller stages it itself.
aIncludeDraggedif true, the dragged shape is also reported in aModified and passed to aBeforeModify – for callers (e.g. solve-on-create) that pin where the shape already is and do not stage it themselves.
aEditedother shapes in this cluster edited alongside the dragged one left free instead of stay-pinned back
aCoDraggedsecond anchor moved by the same handle with its own target for a polygon edge drag pinned at the same weight as the primary
Returns
the diagnosis; .solved is false if the cluster could not be built or did not converge.

Definition at line 2267 of file board_constraint_adapter.cpp.

References BOARD_CONSTRAINT_ADAPTER::Apply(), BOARD_CONSTRAINT_ADAPTER::ApplyReferenceValues(), BOARD_CONSTRAINT_ADAPTER::Build(), buildShapeConstraintMap(), collectAllConstraints(), collectConstraintCluster(), BOARD_CONSTRAINT_ADAPTER::Diagnose(), CONSTRAINT_MEMBER::m_item, resolveClusterDimensions(), resolveClusterShapes(), BOARD::ResolveItem(), BOARD_CONSTRAINT_ADAPTER::Solve(), and CONSTRAINT_DIAGNOSIS::solved.

Referenced by ApplyConstraintImmediately(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), BOOST_FIXTURE_TEST_CASE(), ReSolveShapeClusters(), and PCB_POINT_EDITOR::updateItem().