KiCad PCB EDA Suite
Loading...
Searching...
No Matches
legacy_item_order.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 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8#pragma once
9
10#include <optional>
11
12#include <board_item.h>
14#include <pcb_shape.h>
15#include <pcb_text.h>
16
18{
19
23inline std::optional<bool> orderPoints( const VECTOR2I& aFirst, const VECTOR2I& aSecond )
24{
25 if( aFirst.x != aSecond.x )
26 return aFirst.x < aSecond.x;
27
28 if( aFirst.y != aSecond.y )
29 return aFirst.y < aSecond.y;
30
31 return std::nullopt;
32}
33
34
35inline std::optional<bool> orderFpShapes( const PCB_SHAPE* aFirst, const PCB_SHAPE* aSecond )
36{
37 if( aFirst->GetShape() != aSecond->GetShape() )
38 return aFirst->GetShape() < aSecond->GetShape();
39
40 // Start and end have no meaning for a polygon, so they cannot order one.
41 if( aFirst->GetShape() != SHAPE_T::POLY )
42 {
43 if( std::optional<bool> cmp = orderPoints( aFirst->GetStart(), aSecond->GetStart() ) )
44 return cmp;
45
46 if( std::optional<bool> cmp = orderPoints( aFirst->GetEnd(), aSecond->GetEnd() ) )
47 return cmp;
48 }
49
50 if( aFirst->GetShape() == SHAPE_T::ARC )
51 {
52 if( std::optional<bool> cmp = orderPoints( aFirst->GetCenter(), aSecond->GetCenter() ) )
53 return cmp;
54 }
55 else if( aFirst->GetShape() == SHAPE_T::BEZIER )
56 {
57 if( std::optional<bool> cmp = orderPoints( aFirst->GetBezierC1(), aSecond->GetBezierC1() ) )
58 return cmp;
59
60 if( std::optional<bool> cmp = orderPoints( aFirst->GetBezierC2(), aSecond->GetBezierC2() ) )
61 return cmp;
62 }
63 else if( aFirst->GetShape() == SHAPE_T::POLY )
64 {
65 const SHAPE_POLY_SET& polyA = aFirst->GetPolyShape();
66 const SHAPE_POLY_SET& polyB = aSecond->GetPolyShape();
67
68 if( polyA.TotalVertices() != polyB.TotalVertices() )
69 return polyA.TotalVertices() < polyB.TotalVertices();
70
71 for( int ii = 0; ii < polyA.TotalVertices(); ++ii )
72 {
73 if( std::optional<bool> cmp = orderPoints( polyA.CVertex( ii ), polyB.CVertex( ii ) ) )
74 return cmp;
75 }
76 }
77
78 if( aFirst->GetWidth() != aSecond->GetWidth() )
79 return aFirst->GetWidth() < aSecond->GetWidth();
80
81 return std::nullopt;
82}
83
84
85inline std::optional<bool> orderFpTexts( const PCB_TEXT* aFirst, const PCB_TEXT* aSecond )
86{
87 if( std::optional<bool> cmp = orderPoints( aFirst->GetPosition(), aSecond->GetPosition() ) )
88 return cmp;
89
90 if( aFirst->GetTextAngle() != aSecond->GetTextAngle() )
91 return aFirst->GetTextAngle() < aSecond->GetTextAngle();
92
93 if( std::optional<bool> cmp = orderPoints( aFirst->GetTextSize(), aSecond->GetTextSize() ) )
94 return cmp;
95
96 if( aFirst->GetTextThickness() != aSecond->GetTextThickness() )
97 return aFirst->GetTextThickness() < aSecond->GetTextThickness();
98
99 if( aFirst->IsBold() != aSecond->IsBold() )
100 return aFirst->IsBold() < aSecond->IsBold();
101
102 if( aFirst->IsItalic() != aSecond->IsItalic() )
103 return aFirst->IsItalic() < aSecond->IsItalic();
104
105 if( aFirst->IsMirrored() != aSecond->IsMirrored() )
106 return aFirst->IsMirrored() < aSecond->IsMirrored();
107
108 if( aFirst->GetLineSpacing() != aSecond->GetLineSpacing() )
109 return aFirst->GetLineSpacing() < aSecond->GetLineSpacing();
110
111 if( aFirst->GetText() != aSecond->GetText() )
112 return aFirst->GetText().Cmp( aSecond->GetText() ) < 0;
113
114 return std::nullopt;
115}
116
117
120template <bool OrderTextByPosition>
122{
123 bool operator()( const BOARD_ITEM* aFirst, const BOARD_ITEM* aSecond ) const
124 {
125 if( aFirst->Type() != aSecond->Type() )
126 return aFirst->Type() < aSecond->Type();
127
128 if( aFirst->GetLayer() != aSecond->GetLayer() )
129 return aFirst->GetLayer() < aSecond->GetLayer();
130
131 if( aFirst->Type() == PCB_SHAPE_T )
132 {
133 if( std::optional<bool> cmp = orderFpShapes( static_cast<const PCB_SHAPE*>( aFirst ),
134 static_cast<const PCB_SHAPE*>( aSecond ) ) )
135 {
136 return *cmp;
137 }
138 }
139 else if( OrderTextByPosition && aFirst->Type() == PCB_TEXT_T )
140 {
141 if( std::optional<bool> cmp = orderFpTexts( static_cast<const PCB_TEXT*>( aFirst ),
142 static_cast<const PCB_TEXT*>( aSecond ) ) )
143 {
144 return *cmp;
145 }
146 }
147
148 if( aFirst->m_Uuid != aSecond->m_Uuid )
149 return aFirst->m_Uuid < aSecond->m_Uuid;
150
151 return aFirst < aSecond;
152 }
153};
154
158
159} // namespace KICAD_FORMAT::LEGACY
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
const VECTOR2I & GetBezierC2() const
Definition eda_shape.h:368
SHAPE_POLY_SET & GetPolyShape()
SHAPE_T GetShape() const
Definition eda_shape.h:175
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:325
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:275
const VECTOR2I & GetBezierC1() const
Definition eda_shape.h:365
bool IsItalic() const
Definition eda_text.h:200
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
double GetLineSpacing() const
Definition eda_text.h:298
bool IsMirrored() const
Definition eda_text.h:229
bool IsBold() const
Definition eda_text.h:215
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pcb_shape.h:78
int GetWidth() const override
EDA_ANGLE GetTextAngle() const override
Definition pcb_text.cpp:560
VECTOR2I GetPosition() const override
Definition pcb_text.h:100
int GetTextThickness() const override
Definition pcb_text.cpp:497
VECTOR2I GetTextSize() const override
Definition pcb_text.cpp:470
Represent a set of closed polygons.
int TotalVertices() const
Return total number of vertices stored in the set.
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Return the index-th vertex in a given hole outline within a given outline.
FP_DRAWING_ORDER< true > FP_DRAWING_ORDER_V10
std::optional< bool > orderFpShapes(const PCB_SHAPE *aFirst, const PCB_SHAPE *aSecond)
std::optional< bool > orderFpTexts(const PCB_TEXT *aFirst, const PCB_TEXT *aSecond)
FP_DRAWING_ORDER< false > FP_DRAWING_ORDER_V9
KiCad 9 ordered only shapes by geometry. KiCad 10 also ordered text by position.
std::optional< bool > orderPoints(const VECTOR2I &aFirst, const VECTOR2I &aSecond)
The released comparators ordered footprint graphics in board coordinates.
Every branch falls through to the uuid.
bool operator()(const BOARD_ITEM *aFirst, const BOARD_ITEM *aSecond) const
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:84
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708