KiCad PCB EDA Suite
drc_marker_factory.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) 2010 Dick Hollenbeck, dick@softplc.com
5  * Copyright (C) 2004-2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
6  * Copyright (C) 2018-2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include "drc_marker_factory.h"
27 
28 #include <board_design_settings.h>
29 #include <class_board_item.h>
30 #include <class_edge_mod.h>
31 #include <class_marker_pcb.h>
32 #include <class_pad.h>
33 #include <class_pcb_text.h>
34 #include <class_text_mod.h>
35 #include <class_track.h>
36 #include <class_zone.h>
37 #include <common.h>
38 #include <tools/drc.h>
39 #include <fctsys.h>
41 #include <pcb_edit_frame.h>
42 #include <pcbnew.h>
43 
44 
45 const int EPSILON = Mils2iu( 5 );
46 
47 
49 {
51 }
52 
53 
55 {
56  m_units_provider = aUnitsProvider;
57 }
58 
59 
61 {
62  m_units_provider = [=]() { return aUnits; };
63 }
64 
65 
67  TRACK* aTrack, ZONE_CONTAINER* aConflictZone, int aErrorCode ) const
68 {
69  SHAPE_POLY_SET* conflictOutline;
70 
71  if( aConflictZone->IsFilled() )
72  conflictOutline = const_cast<SHAPE_POLY_SET*>( &aConflictZone->GetFilledPolysList() );
73  else
74  conflictOutline = aConflictZone->Outline();
75 
76  wxPoint markerPos;
77  wxPoint pt1 = aTrack->GetPosition();
78  wxPoint pt2 = aTrack->GetEnd();
79 
80  // If the mid-point is in the zone, then that's a fine place for the marker
81  if( conflictOutline->Distance( ( pt1 + pt2 ) / 2 ) == 0 )
82  markerPos = ( pt1 + pt2 ) / 2;
83 
84  // Otherwise do a binary search for a "good enough" marker location
85  else
86  {
87  while( GetLineLength( pt1, pt2 ) > EPSILON )
88  {
89  if( conflictOutline->Distance( pt1 ) < conflictOutline->Distance( pt2 ) )
90  pt2 = ( pt1 + pt2 ) / 2;
91  else
92  pt1 = ( pt1 + pt2 ) / 2;
93  }
94 
95  // Once we're within EPSILON pt1 and pt2 are "equivalent"
96  markerPos = pt1;
97  }
98 
99  return new MARKER_PCB( getCurrentUnits(), aErrorCode, markerPos, aTrack, aTrack->GetPosition(),
100  aConflictZone, aConflictZone->GetPosition() );
101 }
102 
103 
105  TRACK* aTrack, BOARD_ITEM* aConflitItem, const SEG& aConflictSeg, int aErrorCode ) const
106 {
107  wxPoint markerPos;
108  wxPoint pt1 = aTrack->GetPosition();
109  wxPoint pt2 = aTrack->GetEnd();
110 
111  // Do a binary search along the track for a "good enough" marker location
112  while( GetLineLength( pt1, pt2 ) > EPSILON )
113  {
114  if( aConflictSeg.Distance( pt1 ) < aConflictSeg.Distance( pt2 ) )
115  pt2 = ( pt1 + pt2 ) / 2;
116  else
117  pt1 = ( pt1 + pt2 ) / 2;
118  }
119 
120  // Once we're within EPSILON pt1 and pt2 are "equivalent"
121  markerPos = pt1;
122 
123  return new MARKER_PCB( getCurrentUnits(), aErrorCode, markerPos, aTrack, aTrack->GetPosition(),
124  aConflitItem, aConflitItem->GetPosition() );
125 }
126 
127 
129  D_PAD* aPad, BOARD_ITEM* aConflictItem, int aErrorCode ) const
130 {
131  return new MARKER_PCB( getCurrentUnits(), aErrorCode, aPad->GetPosition(), aPad,
132  aPad->GetPosition(), aConflictItem, aConflictItem->GetPosition() );
133 }
134 
135 
137  const wxPoint& aPos, BOARD_ITEM* aItem, int aErrorCode ) const
138 {
139  return new MARKER_PCB(
140  getCurrentUnits(), aErrorCode, aPos, aItem, aPos, nullptr, wxPoint() );
141 }
142 
143 
145  const wxPoint& aPos, BOARD_ITEM* aItem, BOARD_ITEM* bItem, int aErrorCode ) const
146 {
147  return new MARKER_PCB( getCurrentUnits(), aErrorCode, aPos, aItem, aItem->GetPosition(), bItem,
148  bItem->GetPosition() );
149 }
150 
151 
152 MARKER_PCB* DRC_MARKER_FACTORY::NewMarker( int aErrorCode, const wxString& aMessage ) const
153 {
154  MARKER_PCB* marker = new MARKER_PCB( aErrorCode, wxPoint(), aMessage, wxPoint() );
155 
156  marker->SetShowNoCoordinate();
157 
158  return marker;
159 }
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:57
bool IsFilled() const
Definition: class_zone.h:164
int Distance(const SEG &aSeg) const
Function Distance()
Definition: seg.h:199
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Function GetLineLength returns the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:202
TEXTE_PCB class definition.
UNITS_PROVIDER m_units_provider
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
void SetShowNoCoordinate()
Definition: marker_base.h:232
SHAPE_POLY_SET * Outline()
Definition: class_zone.h:237
void SetUnits(EDA_UNITS_T aUnits)
Set the units provider to a function returning a constant value.
const wxPoint GetPosition() const override
Definition: class_zone.cpp:184
Definitions for tracks, vias and zones.
const int EPSILON
Markers used to show a drc problem on boards.
Footprint text class description.
const wxPoint GetPosition() const override
Definition: class_track.h:100
Class SHAPE_POLY_SET.
MARKER_PCB * NewMarker(TRACK *aTrack, BOARD_ITEM *aConflitItem, const SEG &aConflictSeg, int aErrorCode) const
Creates a marker on a track, via or pad.
a few functions useful in geometry calculations.
Pad object description.
virtual const wxPoint GetPosition() const =0
Definition: seg.h:36
int Distance(VECTOR2I aPoint)
Function DistanceToPolygon computes the minimum distance between aPoint and all the polygons in the s...
The common library.
const wxPoint & GetEnd() const
Definition: class_track.h:106
std::function< EDA_UNITS_T()> UNITS_PROVIDER
const wxPoint GetPosition() const override
Definition: class_pad.h:225
void SetUnitsProvider(UNITS_PROVIDER aUnitsProvider)
Set the provider for the units (this will be called for each new marker constructed)
EDGE_MODULE class definition.
EDA_UNITS_T getCurrentUnits() const
EDA_UNITS_T
Definition: common.h:133
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:543