KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_track_angle.cpp
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.
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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <thread_pool.h>
25#include "geometry/eda_angle.h"
26#include <numbers>
27#include <pcb_track.h>
28#include <drc/drc_engine.h>
29#include <drc/drc_item.h>
30#include <drc/drc_rule.h>
33
34
35/*
36 Track angle test. Checks the angle between two connected track segments.
37 Errors generated:
38 - DRCE_TRACK_ANGLE
39*/
40
42{
43public:
45 {}
46
47 virtual ~DRC_TEST_PROVIDER_TRACK_ANGLE() = default;
48
49 virtual bool Run() override;
50
51 virtual const wxString GetName() const override { return wxT( "angle" ); };
52};
53
54
56{
58 {
59 REPORT_AUX( wxT( "Track angle violations ignored. Tests not run." ) );
60 return true; // continue with other tests
61 }
62
64 {
65 REPORT_AUX( wxT( "No track angle constraints found. Tests not run." ) );
66 return true; // continue with other tests
67 }
68
69 if( !reportPhase( _( "Checking track angles..." ) ) )
70 return false; // DRC cancelled
71
72 auto checkTrackAngle =
73 [&]( PCB_TRACK* item ) -> bool
74 {
76 {
77 return false;
78 }
79
80 if( item->Type() != PCB_TRACE_T )
81 {
82 return true;
83 }
84
85 SEG segment = SEG( item->GetStart(), item->GetEnd() );
86
87 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_drcEngine->GetBoard()->GetConnectivity();
88
89 for( PCB_TRACK* other : connectivity->GetConnectedTracks( item ) )
90 {
91 if( other->Type() != PCB_TRACE_T )
92 {
93 continue;
94 }
95
96 SEG other_segment = SEG( other->GetStart(), other->GetEnd() );
97
98 OPT_VECTOR2I intersection_opt = segment.Intersect( other_segment );
99
100 if( !intersection_opt.has_value() )
101 {
102 continue;
103 }
104
105 VECTOR2I p0 = intersection_opt.value();
106
107 if( m_drcEngine->GetBoard()->GetPad( p0, { item->GetLayer() } ) )
108 {
109 continue;
110 }
111
112 auto constraint = m_drcEngine->EvalRules( TRACK_ANGLE_CONSTRAINT, item, other,
113 item->GetLayer() );
114
115 VECTOR2D direction = VECTOR2D( item->GetEnd() - item->GetStart() ).Resize( 1 );
116 VECTOR2D other_direction = VECTOR2D( other->GetEnd() - other->GetStart() ).Resize( 1 );
118 bool angle_below_90 = false;
119
120 if( segment.B == p0 )
121 {
122 direction *= -1;
123 }
124 else if( segment.A != p0 )
125 {
126 angle_below_90 = true;
127 }
128
129 if( other_segment.B == p0 )
130 {
131 other_direction *= -1;
132 }
133 else if( other_segment.A != p0 )
134 {
135 angle_below_90 = true;
136 }
137
138 actual = EDA_ANGLE::Arccos( direction.Dot( other_direction ) );
139 if( angle_below_90 && actual > ANGLE_90 )
140 {
141 actual -= ANGLE_90;
142 }
143
144 bool fail_min = false;
145 bool fail_max = false;
146 EDA_ANGLE constraintAngle = ANGLE_0;
147
148 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
149 {
150 if( constraint.Value().HasMin() && actual.AsDegrees() < constraint.Value().Min() )
151 {
152 fail_min = true;
153 constraintAngle = EDA_ANGLE( constraint.Value().Min(), DEGREES_T );
154 }
155
156 if( constraint.Value().HasMax() && actual.AsDegrees() > constraint.Value().Max() )
157 {
158 fail_max = true;
159 constraintAngle = EDA_ANGLE( constraint.Value().Max(), DEGREES_T );
160 }
161 }
162
163 if( fail_min || fail_max )
164 {
165 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_ANGLE );
166 wxString constraintName = constraint.GetName();
167 wxString msg;
168
169 if( fail_min )
170 {
171 msg = formatMsg( _( "(%s min angle %s; actual %s)" ),
172 constraintName,
173 constraintAngle,
174 actual );
175 }
176 else
177 {
178 msg = formatMsg( _( "(%s max angle %s; actual %s)" ),
179 constraintName,
180 constraintAngle,
181 actual );
182 }
183
184 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
185 drcItem->SetItems( item , other );
186 drcItem->SetViolatingRule( constraint.GetParentRule() );
187
188 reportViolation( drcItem, p0, item->GetLayer() );
189 }
190 }
191
192 return true;
193 };
194
195 const int progressDelta = 250;
196 int ii = 0;
197
199 std::vector<std::future<bool>> returns;
200
201 returns.reserve( m_drcEngine->GetBoard()->Tracks().size() );
202
203 for( PCB_TRACK* item : m_drcEngine->GetBoard()->Tracks() )
204 {
205 returns.emplace_back( tp.submit( checkTrackAngle, item ) );
206 }
207
208 for( std::future<bool>& ret : returns )
209 {
210 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
211
212 while( status != std::future_status::ready )
213 {
214 reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), progressDelta );
215 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
216 }
217 }
218
219 return !m_drcEngine->IsCancelled();
220}
221
222
223namespace detail
224{
226}
PAD * GetPad(const VECTOR2I &aPosition, const LSET &aLayerMask) const
Find a pad aPosition on aLayer.
Definition: board.cpp:2280
const TRACKS & Tracks() const
Definition: board.h:356
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:520
BOARD * GetBoard() const
Definition: drc_engine.h:95
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
bool IsErrorLimitExceeded(int error_code)
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:706
bool IsCancelled() const
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:393
virtual ~DRC_TEST_PROVIDER_TRACK_ANGLE()=default
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
virtual bool reportPhase(const wxString &aStageName)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
DRC_ENGINE * m_drcEngine
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
static EDA_ANGLE Arccos(double x)
Definition: eda_angle.h:215
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition: seg.cpp:259
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:554
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
@ DRCE_TRACK_ANGLE
Definition: drc_item.h:56
@ TRACK_ANGLE_CONSTRAINT
Definition: drc_rule.h:81
#define REPORT_AUX(s)
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:406
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:408
@ DEGREES_T
Definition: eda_angle.h:31
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
int actual
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:30
static thread_pool * tp
Definition: thread_pool.cpp:28
BS::thread_pool thread_pool
Definition: thread_pool.h:31
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
VECTOR2< double > VECTOR2D
Definition: vector2d.h:694