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 (C) 2004-2023 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 <core/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
48 {}
49
50 virtual bool Run() override;
51
52 virtual const wxString GetName() const override
53 {
54 return wxT( "angle" );
55 };
56
57 virtual const wxString GetDescription() const override
58 {
59 return wxT( "Tests track angles" );
60 }
61};
62
63
65{
67 {
68 reportAux( wxT( "Track angle violations ignored. Tests not run." ) );
69 return true; // continue with other tests
70 }
71
73 {
74 reportAux( wxT( "No track angle constraints found. Tests not run." ) );
75 return true; // continue with other tests
76 }
77
78 if( !reportPhase( _( "Checking track angles..." ) ) )
79 return false; // DRC cancelled
80
81 auto checkTrackAngle =
82 [&]( PCB_TRACK* item ) -> bool
83 {
85 {
86 return false;
87 }
88
89 if( item->Type() != PCB_TRACE_T )
90 {
91 return true;
92 }
93
94 SEG segment = SEG( item->GetStart(), item->GetEnd() );
95
96 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_drcEngine->GetBoard()->GetConnectivity();
97
98 for( PCB_TRACK* other : connectivity->GetConnectedTracks( item ) )
99 {
100 if( other->Type() != PCB_TRACE_T )
101 {
102 continue;
103 }
104
105 SEG other_segment = SEG( other->GetStart(), other->GetEnd() );
106
107 OPT_VECTOR2I intersection_opt = segment.Intersect( other_segment );
108
109 if( !intersection_opt.has_value() )
110 {
111 continue;
112 }
113
114 VECTOR2I p0 = intersection_opt.value();
115
116 if( m_drcEngine->GetBoard()->GetPad( p0, { item->GetLayer() } ) )
117 {
118 continue;
119 }
120
121 auto constraint = m_drcEngine->EvalRules( TRACK_ANGLE_CONSTRAINT, item, other,
122 item->GetLayer() );
123
124 VECTOR2D direction = VECTOR2D( item->GetEnd() - item->GetStart() ).Resize( 1 );
125 VECTOR2D other_direction = VECTOR2D( other->GetEnd() - other->GetStart() ).Resize( 1 );
126 EDA_ANGLE actual;
127 bool angle_below_90 = false;
128
129 if( segment.B == p0 )
130 {
131 direction *= -1;
132 }
133 else if( segment.A != p0 )
134 {
135 angle_below_90 = true;
136 }
137
138 if( other_segment.B == p0 )
139 {
140 other_direction *= -1;
141 }
142 else if( other_segment.A != p0 )
143 {
144 angle_below_90 = true;
145 }
146
147 actual = EDA_ANGLE::Arccos( direction.Dot( other_direction ) );
148 if( angle_below_90 && actual > 90 )
149 {
150 actual -= 90;
151 }
152
153 bool fail_min = false;
154 bool fail_max = false;
155 EDA_ANGLE constraintAngle = 0;
156
157 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
158 {
159 if( constraint.Value().HasMin() && actual.AsDegrees() < constraint.Value().Min() )
160 {
161 fail_min = true;
162 constraintAngle = EDA_ANGLE( constraint.Value().Min(), DEGREES_T );
163 }
164
165 if( constraint.Value().HasMax() && actual.AsDegrees() > constraint.Value().Max() )
166 {
167 fail_max = true;
168 constraintAngle = EDA_ANGLE( constraint.Value().Max(), DEGREES_T );
169 }
170 }
171
172 if( fail_min || fail_max )
173 {
174 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_ANGLE );
175 wxString constraintName = constraint.GetName();
176 wxString msg;
177
178 if( fail_min )
179 {
180 msg = formatMsg( _( "(%s min angle %s; actual %s)" ),
181 constraintName,
182 constraintAngle,
183 actual );
184 }
185 else
186 {
187 msg = formatMsg( _( "(%s max angle %s; actual %s)" ),
188 constraintName,
189 constraintAngle,
190 actual );
191 }
192
193 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
194 drcItem->SetItems( item , other );
195 drcItem->SetViolatingRule( constraint.GetParentRule() );
196
197 reportViolation( drcItem, p0, item->GetLayer() );
198 }
199 }
200
201 return true;
202 };
203
204 const int progressDelta = 250;
205 int ii = 0;
206
208 std::vector<std::future<bool>> returns;
209
210 returns.reserve( m_drcEngine->GetBoard()->Tracks().size() );
211
212 for( PCB_TRACK* item : m_drcEngine->GetBoard()->Tracks() )
213 {
214 returns.emplace_back( tp.submit( checkTrackAngle, item ) );
215 }
216
217 for( std::future<bool>& ret : returns )
218 {
219 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
220
221 while( status != std::future_status::ready )
222 {
223 reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), progressDelta );
224 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
225 }
226 }
227
229
230 return !m_drcEngine->IsCancelled();
231}
232
233
234namespace detail
235{
237}
const TRACKS & Tracks() const
Definition: board.h:329
PAD * GetPad(const VECTOR2I &aPosition, LSET aLayerMask) const
Find a pad aPosition on aLayer.
Definition: board.cpp:2115
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:475
BOARD * GetBoard() const
Definition: drc_engine.h:99
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:679
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:372
virtual const wxString GetName() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual const wxString GetDescription() const override
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual)
virtual bool reportPhase(const wxString &aStageName)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
DRC_ENGINE * m_drcEngine
void reportAux(const wxString &aMsg)
virtual void reportRuleStatistics()
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
double AsDegrees() const
Definition: eda_angle.h:113
static EDA_ANGLE Arccos(double x)
Definition: eda_angle.h:210
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:254
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition: vector2d.h:550
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:78
#define _(s)
@ 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
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690