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:
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{
57 if( m_drcEngine->IsErrorLimitExceeded( DRCE_TRACK_ANGLE ) )
58 {
59 REPORT_AUX( wxT( "Track angle violations ignored. Tests not run." ) );
60 return true; // continue with other tests
61 }
62
63 if( !m_drcEngine->HasRulesForConstraintType( TRACK_ANGLE_CONSTRAINT ) )
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 [&]( const int ind ) -> bool
74 {
75 if( m_drcEngine->IsErrorLimitExceeded( DRCE_TRACK_ANGLE ) )
76 {
77 return false;
78 }
79
80 PCB_TRACK* item = m_drcEngine->GetBoard()->Tracks()[ind];
81
82 if( item->Type() != PCB_TRACE_T )
83 {
84 return true;
85 }
86
87 SEG segment = SEG( item->GetStart(), item->GetEnd() );
88
89 std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_drcEngine->GetBoard()->GetConnectivity();
90
91 for( PCB_TRACK* other : connectivity->GetConnectedTracks( item ) )
92 {
93 if( other->Type() != PCB_TRACE_T )
94 {
95 continue;
96 }
97
98 SEG other_segment = SEG( other->GetStart(), other->GetEnd() );
99
100 OPT_VECTOR2I intersection_opt = segment.Intersect( other_segment );
101
102 if( !intersection_opt.has_value() )
103 {
104 continue;
105 }
106
107 VECTOR2I p0 = intersection_opt.value();
108
109 if( m_drcEngine->GetBoard()->GetPad( p0, { item->GetLayer() } ) )
110 {
111 continue;
112 }
113
114 auto constraint = m_drcEngine->EvalRules( TRACK_ANGLE_CONSTRAINT, item, other,
115 item->GetLayer() );
116
117 VECTOR2D direction = VECTOR2D( item->GetEnd() - item->GetStart() ).Resize( 1 );
118 VECTOR2D other_direction = VECTOR2D( other->GetEnd() - other->GetStart() ).Resize( 1 );
120 bool angle_below_90 = false;
121
122 if( segment.B == p0 )
123 {
124 direction *= -1;
125 }
126 else if( segment.A != p0 )
127 {
128 angle_below_90 = true;
129 }
130
131 if( other_segment.B == p0 )
132 {
133 other_direction *= -1;
134 }
135 else if( other_segment.A != p0 )
136 {
137 angle_below_90 = true;
138 }
139
140 actual = EDA_ANGLE::Arccos( direction.Dot( other_direction ) );
141 if( angle_below_90 && actual > ANGLE_90 )
142 {
143 actual -= ANGLE_90;
144 }
145
146 bool fail_min = false;
147 bool fail_max = false;
148 EDA_ANGLE constraintAngle = ANGLE_0;
149
150 if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE )
151 {
152 if( constraint.Value().HasMin() && actual.AsDegrees() < constraint.Value().Min() )
153 {
154 fail_min = true;
155 constraintAngle = EDA_ANGLE( constraint.Value().Min(), DEGREES_T );
156 }
157
158 if( constraint.Value().HasMax() && actual.AsDegrees() > constraint.Value().Max() )
159 {
160 fail_max = true;
161 constraintAngle = EDA_ANGLE( constraint.Value().Max(), DEGREES_T );
162 }
163 }
164
165 if( fail_min || fail_max )
166 {
167 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_ANGLE );
168 wxString constraintName = constraint.GetName();
169 wxString msg;
170
171 if( fail_min )
172 {
173 msg = formatMsg( _( "(%s min angle %s; actual %s)" ),
174 constraintName,
175 constraintAngle,
176 actual );
177 }
178 else
179 {
180 msg = formatMsg( _( "(%s max angle %s; actual %s)" ),
181 constraintName,
182 constraintAngle,
183 actual );
184 }
185
186 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
187 drcItem->SetItems( item , other );
188 drcItem->SetViolatingRule( constraint.GetParentRule() );
189
190 reportViolation( drcItem, p0, item->GetLayer() );
191 }
192 }
193
194 return true;
195 };
196
197 const int progressDelta = 250;
198 int ii = 0;
199
201 auto futures = tp.submit_loop( 0, m_drcEngine->GetBoard()->Tracks().size(), checkTrackAngle );
202
203 for( auto& ret : futures )
204 {
205 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
206
207 while( status != std::future_status::ready )
208 {
209 reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), progressDelta );
210 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
211 }
212 }
213
214 return !m_drcEngine->IsCancelled();
215}
216
217
218namespace detail
219{
221}
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:232
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:381
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).
virtual bool reportPhase(const wxString &aStageName)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer, const std::vector< PCB_SHAPE > &aShapes={})
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:218
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
const VECTOR2I & GetStart() const
Definition pcb_track.h:152
const VECTOR2I & GetEnd() const
Definition pcb_track.h:149
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:437
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:411
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
@ 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.
static thread_pool * tp
BS::thread_pool< 0 > 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< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694