73bool allSegments(
const std::vector<BOARD_ITEM*>& aItems )
75 return std::ranges::all_of( aItems, isSegment );
80bool allRadial(
const std::vector<BOARD_ITEM*>& aItems )
82 return std::ranges::all_of( aItems, isCircleOrArc );
87bool allCentered(
const std::vector<BOARD_ITEM*>& aItems )
89 return std::ranges::all_of( aItems,
92 return isCircleOrArc( aItem ) || isEllipseKind( aItem );
105 SEG::ecoord best = ( aEnds[0] - bEnds[0] ).SquaredEuclideanNorm();
107 for(
int i = 0; i < 2; ++i )
109 for(
int j = 0; j < 2; ++j )
111 SEG::ecoord dist = ( aEnds[i] - bEnds[j] ).SquaredEuclideanNorm();
125 return SEG( aEnds[
vA], aEnds[1 -
vA] ).
Angle(
SEG( bEnds[vB], bEnds[1 - vB] ) );
131 const std::vector<BOARD_ITEM*>& aItems )
134 auto makeWhole = [&]()
136 std::unique_ptr<PCB_CONSTRAINT> c = std::make_unique<PCB_CONSTRAINT>( aParent, aType );
151 if( aItems.size() != 2 || !allSegments( aItems ) )
160 if( aItems.size() != 1 || !isSegment( aItems[0] ) )
168 if( aItems.size() != 1 || !isSegment( aItems[0] ) )
173 std::unique_ptr<PCB_CONSTRAINT> c = makeWhole();
174 c->SetValue( ( seg->
GetEnd() - seg->
GetStart() ).EuclideanNorm() );
180 if( aItems.size() != 2 || !allCentered( aItems ) )
188 if( aItems.size() != 2 || !allRadial( aItems ) )
196 if( aItems.size() != 2 || !allSegments( aItems ) )
207 std::unique_ptr<PCB_CONSTRAINT> c = makeWhole();
215 if( aItems.size() != 1 || !isCircleOrArc( aItems[0] ) )
218 std::unique_ptr<PCB_CONSTRAINT> c = makeWhole();
219 c->SetValue(
static_cast<const PCB_SHAPE*
>( aItems[0] )->GetRadius() );
225 if( aItems.size() != 1 || !isArc( aItems[0] ) )
228 std::unique_ptr<PCB_CONSTRAINT> c = makeWhole();
229 c->SetValue(
static_cast<const PCB_SHAPE*
>( aItems[0] )->GetArcAngle().AsDegrees() );
235 if( aItems.size() != 2 )
243 return isCircleOrArc( aItem ) || isEllipseKind( aItem );
246 bool lineCurve = ( isSegment( a ) && isCurve( b ) ) || ( isSegment( b ) && isCurve( a ) );
247 bool curveCurve = isCircleOrArc( a ) && isCircleOrArc( b );
249 if( !lineCurve && !curveCurve )
265 std::vector<CONSTRAINT_ANCHOR_POINT> anchors;
292 VECTOR2I tl( std::min( s.
x, e.
x ), std::min( s.
y, e.
y ) );
293 VECTOR2I br( std::max( s.
x, e.
x ), std::max( s.
y, e.
y ) );
309 for(
int i = 0; i < outline.
PointCount(); ++i )
310 anchors.push_back( { CONSTRAINT_ANCHOR::VERTEX, outline.CPoint( i ), i } );
339 if( !aShape || aIndex < 0 )
347 if( aIndex >= (
int) anchors.size() )
350 return anchors[aIndex];
355 const VECTOR2I& aPos,
double aMaxDist )
357 double best = aMaxDist;
358 std::optional<CONSTRAINT_MEMBER>
result;
364 double dist = ( a.pos - aPos ).EuclideanNorm();
380 std::vector<PCB_SHAPE*> shapes;
388 shapes.push_back( shape );
393 for(
BOARD_ITEM* item : footprint->GraphicalItems() )
396 shapes.push_back( shape );
406 std::vector<BOARD_ITEM*> items;
412 [&](
const auto& aContainer )
417 items.push_back( item );
424 collect( footprint->GraphicalItems() );
432 const std::vector<CONSTRAINT_MEMBER>& aExclude )
434 double best = aMaxDist;
435 std::optional<CONSTRAINT_MEMBER>
result;
447 double dist = ( a.pos - aPos ).EuclideanNorm();
461std::vector<DIMENSION_ENDPOINT_BINDING>
463 const std::optional<VECTOR2I>& aEnd,
double aMaxDist )
465 std::vector<DIMENSION_ENDPOINT_BINDING> bindings;
472 using ANCHOR_PAIR = std::pair<CONSTRAINT_MEMBER, CONSTRAINT_MEMBER>;
474 auto bestPairOn = [&](
BOARD_ITEM* aItem ) -> std::optional<std::pair<ANCHOR_PAIR, double>>
480 const size_t none = anchors.size();
481 size_t bestEnd = none;
482 size_t secondEnd = none;
483 std::vector<double> dEnd( anchors.size(), 0.0 );
485 for(
size_t j = 0; j < anchors.size(); ++j )
487 dEnd[j] = ( anchors[j].pos - *aEnd ).EuclideanNorm();
489 if( dEnd[j] > aMaxDist )
492 if( bestEnd == none || dEnd[j] < dEnd[bestEnd] )
497 else if( secondEnd == none || dEnd[j] < dEnd[secondEnd] )
503 if( bestEnd == none )
506 std::optional<std::pair<ANCHOR_PAIR, double>> best;
508 for(
size_t i = 0; i < anchors.size(); ++i )
510 double dStart = ( anchors[i].pos - aStart ).EuclideanNorm();
512 if( dStart > aMaxDist )
515 size_t j = ( i == bestEnd ) ? secondEnd : bestEnd;
520 double sum = dStart + dEnd[j];
522 if( !best || sum < best->second )
524 best = std::make_pair(
528 anchors[j].index ) },
540 std::optional<ANCHOR_PAIR> bestPair;
541 double bestSum = 0.0;
545 if( item->m_Uuid == aDimension )
548 auto pair = bestPairOn( item );
553 if( !bestPair || pair->second < bestSum )
555 bestSum = pair->second;
556 bestPair = pair->first;
576 exclude.push_back( *startTarget );
612 std::vector<CONSTRAINT_ANCHOR_POINT> anchors;
617 switch( aItem->
Type() )
653 const std::map<PCB_CONSTRAINT_TYPE, double>& aRemembered )
655 auto it = aRemembered.find( aType );
657 return it != aRemembered.end() ? it->second : aMeasured;
662 const VECTOR2I& aPos,
int aMaxDist )
664 auto it = std::ranges::find_if( aCandidates,
667 return aShape && aShape->
HitTest( aPos, aMaxDist );
670 return it == aCandidates.end() ? std::nullopt : std::optional<KIID>( ( *it )->m_Uuid );
681 std::optional<KIID> best;
682 double bestErr = 0.0;
686 if( shape->m_Uuid == aDimension || !isCircleOrArc( shape ) )
691 std::optional<VECTOR2I> centerPos;
702 double centerErr = ( *centerPos - aCenter ).EuclideanNorm();
704 if( centerErr > aMaxDist )
707 double rimErr =
std::abs( ( aRim - *centerPos ).EuclideanNorm() - shape->GetRadius() );
709 if( rimErr > aMaxDist )
717 double err = centerErr + rimErr;
719 if( !best || err < bestErr )
722 best = shape->m_Uuid;
732 if( !aBoard || !aDimension )
738 auto anyConstraint = [&](
const auto& aMatch )
740 if( std::ranges::any_of( aBoard->
Constraints(), aMatch ) )
745 return std::ranges::any_of( aBoard->
Footprints(),
747 { return std::ranges::any_of( aFootprint->Constraints(), aMatch ); } );
754 auto rimOnItem = [&](
const KIID& aItem )
756 return anyConstraint(
764 const std::vector<CONSTRAINT_MEMBER>& members = aConstraint->
GetMembers();
766 return members.size() == 2 && members[0] == endMember
771 return anyConstraint(
777 const std::vector<CONSTRAINT_MEMBER>& members = aConstraint->
GetMembers();
779 if( members.size() != 2 )
785 if( members[0] == startMember )
786 target = &members[1];
787 else if( members[1] == startMember )
788 target = &members[0];
795 return item && isCircleOrArc( item ) && rimOnItem( target->
m_item );
803 return anyConstraint(
809 const std::vector<CONSTRAINT_MEMBER>& members = aConstraint->
GetMembers();
812 if( members.size() != 2 || members[0].m_item == members[1].m_item )
815 if( members[0] == aMember )
818 return members[1] == aMember
823 return hasCoincident( startMember ) && hasCoincident( endMember );
832 switch( aDimension->
Type() )
847 if( !aBoard || !aDimension )
858 const std::vector<CONSTRAINT_MEMBER>& members = aConstraint->GetMembers();
860 return members.size() == 2
861 && ( ( members[0] == startMember && members[1] == endMember )
862 || ( members[0] == endMember && members[1] == startMember ) );
867 auto it = std::ranges::find_if( aList, matches );
868 return it != aList.end() ? *it :
nullptr;
896 return existing && existing->
IsDriving();
904 if( lengthConstraint && lengthConstraint->
IsDriving() )
915 std::optional<int> aDrivingLengthIU,
916 const std::optional<wxString>& aOverrideText,
917 const std::function<
void(
BOARD_ITEM* )>& aBeforeModify,
918 const std::function<
void(
BOARD_ITEM* )>& aStageAdd,
919 const std::function<
void(
BOARD_ITEM* )>& aBeforeRemove )
930 if( !aDrivingLengthIU || *aDrivingLengthIU <= 0 || !
DimensionCanDrive( aBoard, aDimension ) )
933 aBeforeModify( aDimension );
939 aBeforeModify( existing );
940 existing->
SetValue( *aDrivingLengthIU );
952 constraint->SetValue( *aDrivingLengthIU );
953 constraint->SetDriving(
true );
956 aStageAdd( constraint.release() );
960 aBeforeModify( aDimension );
970 aBeforeRemove( existing );
977 const std::function<
void(
BOARD_ITEM* )>& aBeforeModify,
978 const std::function<
void(
BOARD_ITEM* )>& aBeforeRemove )
980 if( !aBoard || aDelta == 0 )
983 auto remapIn = [&](
const CONSTRAINTS& aConstraints )
995 if( aDelta < 0 && member.m_index == aChangedIndex )
997 else if( member.m_index >= aChangedIndex )
1006 aBeforeRemove( constraint );
1013 aBeforeModify( constraint );
1018 && member.m_index >= aChangedIndex )
1020 member.m_index += aDelta;
1029 remapIn( footprint->Constraints() );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
FOOTPRINT * GetParentFootprint() const
Information pertinent to a Pcbnew printed circuit board.
const FOOTPRINTS & Footprints() const
const CONSTRAINTS & Constraints() const
Geometric constraints (#2329) owned by this board.
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
const DRAWINGS & Drawings() const
KICAD_T Type() const
Returns the type of object.
SHAPE_POLY_SET & GetPolyShape()
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
A geometric constraint between board items (issue #2329).
const std::vector< CONSTRAINT_MEMBER > & GetMembers() const
bool IsDriving() const
A driving constraint forces its value; a reference (non-driving) one only measures it.
PCB_CONSTRAINT_TYPE GetConstraintType() const
void SetValue(std::optional< double > aValue)
void SetDriving(bool aDriving)
void Update()
Update the dimension's cached text and geometry.
void SetOverrideTextEnabled(bool aOverride)
void SetOverrideText(const wxString &aValue)
bool GetOverrideTextEnabled() const
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
VECTOR2I::extended_type ecoord
EDA_ANGLE Angle(const SEG &aOther) const
Determine the smallest angle between two segments.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
Represent a set of closed polygons.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
DIM_VALUE_MODE DimensionValueMode(BOARD *aBoard, const PCB_DIMENSION_BASE *aDimension)
The value mode aDimension is in, derived from state: a self driving length means Driving,...
void RemapPolygonVertexMembers(BOARD *aBoard, const KIID &aPoly, int aChangedIndex, int aDelta, const std::function< void(BOARD_ITEM *)> &aBeforeModify, const std::function< void(BOARD_ITEM *)> &aBeforeRemove)
Repoint persisted VERTEX constraint members after an outline edit of polygon aPoly inserts or removes...
std::vector< PCB_SHAPE * > CollectConstraintShapes(BOARD *aBoard)
Every PCB_SHAPE on the board (drawings plus footprint graphics) – the candidates constraints can refe...
double InitialConstraintValue(PCB_CONSTRAINT_TYPE aType, double aMeasured, const std::map< PCB_CONSTRAINT_TYPE, double > &aRemembered)
Value a freshly authored constraint dialog should open with.
EDA_ANGLE MeasureCornerAngle(const SEG &aA, const SEG &aB)
The corner angle between two segments, in the closed range [0, 180] degrees.
bool DimensionHasValueMode(const PCB_DIMENSION_BASE *aDimension)
True for dimension types with a measured value aligned orthogonal or radial offering the Driven Drivi...
PCB_CONSTRAINT * FindDimensionLengthConstraint(BOARD *aBoard, const PCB_DIMENSION_BASE *aDimension)
Self FIXED_LENGTH constraint whose members are exactly aDimension START and END or nullptr the drivin...
std::vector< CONSTRAINT_ANCHOR_POINT > ConstraintItemAnchors(const BOARD_ITEM *aItem)
The constraint anchors an item exposes.
std::optional< KIID > NearestConstrainedShape(const std::vector< PCB_SHAPE * > &aCandidates, const VECTOR2I &aPos, int aMaxDist)
The candidate shape whose outline aPos hits within aMaxDist, or std::nullopt.
std::optional< CONSTRAINT_ANCHOR_POINT > ConstraintShapeVertex(const PCB_SHAPE *aShape, int aIndex)
VERTEX anchor at ordinal aIndex of a rectangle or eligible polygon or std::nullopt if the shape has n...
std::unique_ptr< PCB_CONSTRAINT > BuildConstraintFromItems(BOARD_ITEM *aParent, PCB_CONSTRAINT_TYPE aType, const std::vector< BOARD_ITEM * > &aItems)
Build a constraint of aType from a set of selected board items, or nullptr if the selection does not ...
std::vector< CONSTRAINT_ANCHOR_POINT > ConstraintShapeAnchors(const PCB_SHAPE *aShape)
Enumerate a shape constraint anchors with positions segment and arc endpoints arc centre circle centr...
std::optional< KIID > SelectRadialDimensionTarget(BOARD *aBoard, const KIID &aDimension, const VECTOR2I &aCenter, const VECTOR2I &aRim, double aMaxDist)
Single circle or arc a radial dimension binds to or std::nullopt.
std::vector< BOARD_ITEM * > CollectConstrainableItems(BOARD *aBoard)
Every constrainable item on the board – shapes plus dimensions – for board-wide anchor picking.
std::optional< VECTOR2I > ConstraintAnchorPosition(BOARD *aBoard, const CONSTRAINT_MEMBER &aMember)
Current location of a constraint member's anchor (its shape's START/END/CENTER, or a dimension's feat...
std::optional< CONSTRAINT_MEMBER > NearestConstraintAnchor(BOARD *aBoard, const VECTOR2I &aPos, double aMaxDist, const std::vector< CONSTRAINT_MEMBER > &aExclude)
Find the constrainable-item anchor (a shape's segment/arc endpoint or centre, or a dimension's featur...
bool DimensionCanDrive(BOARD *aBoard, const PCB_DIMENSION_BASE *aDimension)
True when Driving mode may be offered for aDimension needs both endpoints bound via DimensionEndpoint...
bool ConstraintPolygonIsModelable(const PCB_SHAPE *aShape)
True when polygon has one non empty hole free arc free outline making it solver eligible Shared by ad...
std::vector< DIMENSION_ENDPOINT_BINDING > SelectDimensionEndpointBindings(BOARD *aBoard, const KIID &aDimension, const VECTOR2I &aStart, const std::optional< VECTOR2I > &aEnd, double aMaxDist)
Choose the coincident bindings a freshly drawn dimension endpoints should take so it tracks the geome...
BOARD_ITEM * ResolveConstrainableItem(BOARD *aBoard, const KIID &aId)
The board item a constraint may reference: a PCB_SHAPE or a dimension, or nullptr for anything else (...
std::optional< CONSTRAINT_MEMBER > NearestAnchorAmong(const std::vector< PCB_SHAPE * > &aShapes, const VECTOR2I &aPos, double aMaxDist)
The {shape, anchor} member nearest aPos within aMaxDist among aShapes, or std::nullopt.
bool DimensionEndpointsBound(BOARD *aBoard, const PCB_DIMENSION_BASE *aDimension)
True when both of aDimension measured endpoints are bound to anchors that still resolve a coincident ...
PCB_CONSTRAINT * SetDimensionValueMode(BOARD *aBoard, PCB_DIMENSION_BASE *aDimension, DIM_VALUE_MODE aMode, std::optional< int > aDrivingLengthIU, const std::optional< wxString > &aOverrideText, const std::function< void(BOARD_ITEM *)> &aBeforeModify, const std::function< void(BOARD_ITEM *)> &aStageAdd, const std::function< void(BOARD_ITEM *)> &aBeforeRemove)
Apply a value mode transition to aDimension Driving creates or updates the driving length with aDrivi...
DIM_VALUE_MODE
Mode a value bearing dimension value is in Driven mirrors measured geometry Driving forces geometry t...
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
@ VERTEX
An indexed rectangle corner or polygon outline vertex; pairs with CONSTRAINT_MEMBER::m_index.
@ WHOLE
The item as a whole (a segment as a line, a circle).
@ START
First endpoint of a segment or arc.
@ END
Second endpoint of a segment or arc.
@ CENTER
Center of an arc or circle.
PCB_CONSTRAINT_TYPE
The geometric relationship a PCB_CONSTRAINT enforces between its members.
@ CONCENTRIC
Two arcs/circles share a center.
@ VERTICAL
A segment (or two points) is vertical.
@ TANGENT
A line and a curve, or two curves, touch tangentially.
@ COINCIDENT
Two points are made to coincide.
@ PERPENDICULAR
Two segments are perpendicular.
@ FIXED_RADIUS
An arc/circle has a driving radius value.
@ HORIZONTAL
A segment (or two points) is horizontal.
@ EQUAL_RADIUS
Two arcs/circles have equal radius.
@ POINT_ON_LINE
A point lies on a segment's supporting line.
@ FIXED_LENGTH
A segment has a driving length value.
@ ANGULAR_DIMENSION
An angle between members (driving or reference).
@ COLLINEAR
Two segments lie on the same line.
@ ARC_ANGLE
An arc has a driving or reference swept-angle value.
@ PARALLEL
Two segments are parallel.
@ EQUAL_LENGTH
Two segments have equal length.
std::deque< PCB_CONSTRAINT * > CONSTRAINTS
A selectable feature of a shape (a segment endpoint, arc centre, ...) and its location.
One participant in a constraint: a referenced board item plus the feature of that item that participa...
KIID m_item
Referenced board item, usually a PCB_SHAPE.
CONSTRAINT_ANCHOR m_anchor
Which feature of that item participates.
int m_index
Vertex ordinal; only meaningful for the VERTEX anchor.
wxString result
Test unit parsing edge cases and error handling.
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
VECTOR2< int32_t > VECTOR2I