KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_constraint.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef PCB_CONSTRAINT_H_
21#define PCB_CONSTRAINT_H_
22
23#include <optional>
24#include <vector>
25
26#include <board_item.h>
27
28
59
60
79
80
86{
89 int m_index = -1;
90
96
97 CONSTRAINT_MEMBER( const KIID& aItem, CONSTRAINT_ANCHOR aAnchor, int aIndex = -1 ) :
98 m_item( aItem ),
99 m_anchor( aAnchor ),
100 m_index( aIndex )
101 {
102 }
103
104 bool operator==( const CONSTRAINT_MEMBER& aOther ) const = default;
105};
106
107
109const char* ConstraintTypeToken( PCB_CONSTRAINT_TYPE aType );
110
112PCB_CONSTRAINT_TYPE ConstraintTypeFromToken( const wxString& aToken );
113
115const char* ConstraintAnchorToken( CONSTRAINT_ANCHOR aAnchor );
116
118CONSTRAINT_ANCHOR ConstraintAnchorFromToken( const wxString& aToken );
119
122wxString ConstraintAnchorLabel( CONSTRAINT_ANCHOR aAnchor );
123
126
129
132
133enum class BITMAPS : unsigned int;
134
137
138enum class EDA_UNITS : int;
139
140
152{
153public:
154 PCB_CONSTRAINT( BOARD_ITEM* aParent = nullptr );
156
157 static inline bool ClassOf( const EDA_ITEM* aItem )
158 {
159 return aItem && PCB_CONSTRAINT_T == aItem->Type();
160 }
161
162 wxString GetClass() const override { return wxT( "PCB_CONSTRAINT" ); }
163
166
167 const std::vector<CONSTRAINT_MEMBER>& GetMembers() const { return m_members; }
168 std::vector<CONSTRAINT_MEMBER>& Members() { return m_members; }
169
171 int aIndex = -1 )
172 {
173 m_members.emplace_back( aItem, aAnchor, aIndex );
174 }
175
176 bool HasValue() const { return m_value.has_value(); }
177 std::optional<double> GetValue() const { return m_value; }
178 void SetValue( std::optional<double> aValue ) { m_value = aValue; }
179
181 bool IsDriving() const { return m_driving; }
182 void SetDriving( bool aDriving ) { m_driving = aDriving; }
183
184 void Serialize( google::protobuf::Any& aContainer ) const override;
185 bool Deserialize( const google::protobuf::Any& aContainer ) override;
186
187 EDA_ITEM* Clone() const override;
188
189 double Similarity( const BOARD_ITEM& aOther ) const override;
190 bool operator==( const PCB_CONSTRAINT& aOther ) const;
191 bool operator==( const BOARD_ITEM& aOther ) const override;
192
193 // A constraint occupies no space and is never on a layer.
194 VECTOR2I GetPosition() const override { return VECTOR2I(); }
195 void SetPosition( const VECTOR2I& ) override {}
196
198 bool IsLayerAgnostic() const override { return true; }
199
200 const BOX2I GetBoundingBox() const override { return BOX2I(); }
201
202 bool HitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const override { return false; }
203 bool HitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const override
204 {
205 return false;
206 }
207
208 std::vector<int> ViewGetLayers() const override { return {}; }
209
210 // A constraint has no geometry, so the spatial transforms are no-ops (the base
211 // implementations wxFAIL_MSG). Its members move with their own host items.
212 void Move( const VECTOR2I& aMoveVector ) override {}
213 void Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle ) override {}
214 void Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection ) override {}
215 void Mirror( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection ) override {}
216
218 std::shared_ptr<SHAPE> GetEffectiveShape( PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
219 FLASHING aFlash = FLASHING::DEFAULT ) const override;
220
221 wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override;
222 BITMAPS GetMenuImage() const override;
223
224 void RemapKIIDs( const std::map<KIID, KIID>& aIdMap ) override;
225
226#if defined( DEBUG )
227 void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
228#endif
229
230protected:
231 void swapData( BOARD_ITEM* aImage ) override;
232
233private:
235 std::vector<CONSTRAINT_MEMBER> m_members;
236 std::optional<double> m_value;
238};
239
240
248wxString ConstraintDisplayLabel( const PCB_CONSTRAINT& aConstraint, EDA_UNITS aUnits );
249
250
257wxString ConstraintMemberLabel( BOARD_ITEM* aItem, const CONSTRAINT_MEMBER& aMember,
258 UNITS_PROVIDER* aUnitsProvider );
259
260
267bool ConstraintsAreDuplicate( const PCB_CONSTRAINT& aA, const PCB_CONSTRAINT& aB );
268
269#endif // PCB_CONSTRAINT_H_
BITMAPS
A list of all bitmap identifiers.
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:85
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
Definition kiid.h:46
A geometric constraint between board items (issue #2329).
void swapData(BOARD_ITEM *aImage) override
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
const std::vector< CONSTRAINT_MEMBER > & GetMembers() const
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool HitTest(const BOX2I &aRect, bool aContained, int aAccuracy=0) const override
Test if aRect intersects this item.
void Mirror(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Mirror this object relative to a given horizontal axis the layer is not changed.
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
std::optional< double > GetValue() const
PCB_CONSTRAINT(BOARD_ITEM *aParent=nullptr)
BITMAPS GetMenuImage() const override
Return a pointer to an image to be used in menus.
bool IsDriving() const
A driving constraint forces its value; a reference (non-driving) one only measures it.
bool IsLayerAgnostic() const override
Check if this item's layer set must not be confined to a board's enabled layers.
PCB_CONSTRAINT_TYPE GetConstraintType() const
void AddMember(const KIID &aItem, CONSTRAINT_ANCHOR aAnchor=CONSTRAINT_ANCHOR::WHOLE, int aIndex=-1)
double Similarity(const BOARD_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
bool operator==(const PCB_CONSTRAINT &aOther) const
wxString GetClass() const override
Return the class name.
std::optional< double > m_value
PCB_CONSTRAINT_TYPE m_type
bool HasValue() const
void SetValue(std::optional< double > aValue)
VECTOR2I GetPosition() const override
void SetConstraintType(PCB_CONSTRAINT_TYPE aType)
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
void RemapKIIDs(const std::map< KIID, KIID > &aIdMap) override
Remap KIIDs this item stores to reference other items (e.g.
void SetDriving(bool aDriving)
std::vector< CONSTRAINT_MEMBER > m_members
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
std::vector< CONSTRAINT_MEMBER > & Members()
static bool ClassOf(const EDA_ITEM *aItem)
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
void SetPosition(const VECTOR2I &) override
void Move(const VECTOR2I &aMoveVector) override
Move this object.
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const override
Empty shape so any generic GetItemSet()/RunOnChildren consumer stays safe.
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
EDA_UNITS
Definition eda_units.h:44
KIID niluuid(0)
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition layer_ids.h:180
@ DEFAULT
Flashing follows connectivity.
Definition layer_ids.h:181
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
FLIP_DIRECTION
Definition mirror.h:23
bool ConstraintsAreDuplicate(const PCB_CONSTRAINT &aA, const PCB_CONSTRAINT &aB)
True if two constraints express the same relation, meaning the same type and the same members compare...
wxString ConstraintTypeGlyph(PCB_CONSTRAINT_TYPE aType)
Compact glyph for a constraint type (e.g. "//", "=") for on-canvas badges.
PCB_CONSTRAINT_TYPE ConstraintTypeFromToken(const wxString &aToken)
Parse a constraint-type token; returns UNDEFINED for an unknown token.
BITMAPS ConstraintTypeBitmap(PCB_CONSTRAINT_TYPE aType)
Badge icon for a constraint type; INVALID_BITMAP for UNDEFINED.
bool ConstraintValueIsLength(PCB_CONSTRAINT_TYPE aType)
True if this type's value is a length in IU (serialized in mm); false for an angle in degrees.
CONSTRAINT_ANCHOR
Which feature of a referenced board item participates in a constraint.
@ MID
Midpoint of a segment or arc.
@ RADIUS
The radius (scalar feature) of an arc or circle.
@ WHOLE
The item as a whole (a segment as a line, a circle).
@ START
First endpoint of a segment or arc.
wxString ConstraintTypeLabel(PCB_CONSTRAINT_TYPE aType)
Human-readable name of a constraint type (e.g. "Parallel"), for menus and lists.
wxString ConstraintMemberLabel(BOARD_ITEM *aItem, const CONSTRAINT_MEMBER &aMember, UNITS_PROVIDER *aUnitsProvider)
Label for one constrained item in a list combining the item description with its anchored feature suc...
CONSTRAINT_ANCHOR ConstraintAnchorFromToken(const wxString &aToken)
Parse an anchor token; returns WHOLE for an unknown token.
const char * ConstraintTypeToken(PCB_CONSTRAINT_TYPE aType)
Stable file-format token for a constraint type (e.g. "parallel"). Used by serialization.
const char * ConstraintAnchorToken(CONSTRAINT_ANCHOR aAnchor)
Stable file-format token for a member anchor (e.g. "start").
PCB_CONSTRAINT_TYPE
The geometric relationship a PCB_CONSTRAINT enforces between its members.
@ CONCENTRIC
Two arcs/circles share a center.
@ SYMMETRIC
Two points are mirror images about an axis.
@ FIXED_POSITION
A point is locked at its current location.
@ VERTICAL
A segment (or two points) is vertical.
@ 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.
@ MIDPOINT
A point is the midpoint of a segment.
@ 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.
wxString ConstraintAnchorLabel(CONSTRAINT_ANCHOR aAnchor)
Translated name of the feature an anchor references (e.g.
wxString ConstraintDisplayLabel(const PCB_CONSTRAINT &aConstraint, EDA_UNITS aUnits)
Display label for a constraint in lists and menus.
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_MEMBER(const KIID &aItem, CONSTRAINT_ANCHOR aAnchor, int aIndex=-1)
CONSTRAINT_ANCHOR m_anchor
Which feature of that item participates.
bool operator==(const CONSTRAINT_MEMBER &aOther) const =default
int m_index
Vertex ordinal; only meaningful for the VERTEX anchor.
@ PCB_CONSTRAINT_T
class PCB_CONSTRAINT, a geometric constraint between board items
Definition typeinfo.h:238
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683