KiCad PCB EDA Suite
board_construction_utils.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) 2019 KiCad Developers, see AUTHORS.txt for contributors.
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 
25 
26 #include <fp_shape.h>
27 #include <footprint.h>
28 #include <geometry/seg.h>
29 #include <math/vector2d.h>
30 
31 
32 namespace KI_TEST
33 {
34 
35 void DrawSegment( FOOTPRINT& aFootprint, const SEG& aSeg, int aWidth, PCB_LAYER_ID aLayer )
36 {
37  auto seg = std::make_unique<FP_SHAPE>( &aFootprint, S_SEGMENT );
38 
39  seg->SetStart0( (wxPoint) aSeg.A );
40  seg->SetEnd0( (wxPoint) aSeg.B );
41 
42  seg->SetWidth( aWidth );
43  seg->SetLayer( aLayer );
44 
45  aFootprint.Add( seg.release() );
46 }
47 
48 
49 void DrawPolyline( FOOTPRINT& aFootprint, const std::vector<VECTOR2I>& aPts, int aWidth,
50  PCB_LAYER_ID aLayer )
51 {
52  for( unsigned i = 0; i < aPts.size() - 1; ++i )
53  {
54  DrawSegment( aFootprint, { aPts[i], aPts[i + 1] }, aWidth, aLayer );
55  }
56 }
57 
58 
59 void DrawArc( FOOTPRINT& aFootprint, const VECTOR2I& aCentre, const VECTOR2I& aStart,
60  double aAngle, int aWidth, PCB_LAYER_ID aLayer )
61 {
62  auto seg = std::make_unique<FP_SHAPE>( &aFootprint, S_ARC );
63 
64  seg->SetStart0( (wxPoint) aCentre );
65  seg->SetEnd0( (wxPoint) aStart );
66  seg->SetAngle( aAngle * 10 );
67 
68  seg->SetWidth( aWidth );
69  seg->SetLayer( aLayer );
70 
71  aFootprint.Add( seg.release() );
72 }
73 
74 
75 void DrawRect( FOOTPRINT& aFootprint, const VECTOR2I& aPos, const VECTOR2I& aSize, int aRadius,
76  int aWidth, PCB_LAYER_ID aLayer )
77 {
78  const auto x_r = aPos.x + aSize.x / 2;
79  const auto x_l = aPos.x - aSize.x / 2;
80  const auto y_t = aPos.y + aSize.y / 2;
81  const auto y_b = aPos.y - aSize.y / 2;
82 
83  const auto xin_r = x_r - aRadius;
84  const auto xin_l = x_l + aRadius;
85  const auto yin_t = y_t - aRadius;
86  const auto yin_b = y_b + aRadius;
87 
88  // If non-zero (could be zero if it's a stadium shape)
89  if( xin_l != xin_r )
90  {
91  DrawSegment( aFootprint, { { xin_l, y_t }, { xin_r, y_t } }, aWidth, aLayer );
92  DrawSegment( aFootprint, { { xin_l, y_b }, { xin_r, y_b } }, aWidth, aLayer );
93  }
94 
95  if( yin_b != yin_t )
96  {
97  DrawSegment( aFootprint, { { x_l, yin_b }, { x_l, yin_t } }, aWidth, aLayer );
98  DrawSegment( aFootprint, { { x_r, yin_b }, { x_r, yin_t } }, aWidth, aLayer );
99  }
100 
101  if( aRadius > 0 )
102  {
103  DrawArc( aFootprint, { xin_r, yin_t }, { x_r, yin_t }, 90, aWidth, aLayer );
104  DrawArc( aFootprint, { xin_l, yin_t }, { xin_l, y_t }, 90, aWidth, aLayer );
105  DrawArc( aFootprint, { xin_l, yin_b }, { x_l, yin_b }, 90, aWidth, aLayer );
106  DrawArc( aFootprint, { xin_r, yin_b }, { xin_r, y_b }, 90, aWidth, aLayer );
107  }
108 }
109 
110 } // namespace KI_TEST
void DrawSegment(FOOTPRINT &aFootprint, const SEG &aSeg, int aWidth, PCB_LAYER_ID aLayer)
Draw a segment in the given footprint.
usual segment : line with rounded ends
Definition: board_item.h:50
Arcs (with rounded ends)
Definition: board_item.h:52
Construction utilities for PCB tests.
void DrawPolyline(FOOTPRINT &aFootprint, const std::vector< VECTOR2I > &aPts, int aWidth, PCB_LAYER_ID aLayer)
Draw a polyline - a set of linked segments.
PCB_LAYER_ID
A quick note on layer IDs:
void DrawRect(FOOTPRINT &aFootprint, const VECTOR2I &aPos, const VECTOR2I &aSize, int aRadius, int aWidth, PCB_LAYER_ID aLayer)
Draw a rectangle on a footprint.
Definition: seg.h:39
void DrawArc(FOOTPRINT &aFootprint, const VECTOR2I &aCentre, const VECTOR2I &aStart, double aAngle, int aWidth, PCB_LAYER_ID aLayer)
Draw an arc on a footprint.
VECTOR2I A
Definition: seg.h:47
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT) override
Definition: footprint.cpp:445
VECTOR2I B
Definition: seg.h:48