KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <pcb_shape.h>
27#include <footprint.h>
28#include <geometry/seg.h>
29#include <math/vector2d.h>
30
31
32namespace KI_TEST
33{
34
35void DrawSegment( FOOTPRINT& aFootprint, const SEG& aSeg, int aWidth, PCB_LAYER_ID aLayer )
36{
37 std::unique_ptr<PCB_SHAPE> seg = std::make_unique<PCB_SHAPE>( &aFootprint, SHAPE_T::SEGMENT );
38
39 seg->SetStart( aSeg.A );
40 seg->SetEnd( aSeg.B );
41
42 seg->SetStroke( STROKE_PARAMS( aWidth, LINE_STYLE::SOLID ) );
43 seg->SetLayer( aLayer );
44
45 seg->Rotate( { 0, 0 }, aFootprint.GetOrientation() );
46 seg->Move( aFootprint.GetPosition() );
47
48 aFootprint.Add( seg.release() );
49}
50
51
52void DrawPolyline( FOOTPRINT& aFootprint, const std::vector<VECTOR2I>& aPts, int aWidth,
53 PCB_LAYER_ID aLayer )
54{
55 for( unsigned i = 0; i < aPts.size() - 1; ++i )
56 {
57 DrawSegment( aFootprint, { aPts[i], aPts[i + 1] }, aWidth, aLayer );
58 }
59}
60
61
62void DrawArc( FOOTPRINT& aFootprint, const VECTOR2I& aCentre, const VECTOR2I& aStart,
63 const EDA_ANGLE& aAngle, int aWidth, PCB_LAYER_ID aLayer )
64{
65 std::unique_ptr<PCB_SHAPE> arc = std::make_unique<PCB_SHAPE>( &aFootprint, SHAPE_T::ARC );
66
67 arc->SetCenter( aCentre );
68 arc->SetStart( aStart );
69 arc->SetArcAngleAndEnd( aAngle );
70
71 arc->SetStroke( STROKE_PARAMS( aWidth, LINE_STYLE::SOLID ) );
72 arc->SetLayer( aLayer );
73
74 arc->Rotate( { 0, 0 }, aFootprint.GetOrientation() );
75 arc->Move( aFootprint.GetPosition() );
76
77 aFootprint.Add( arc.release() );
78}
79
80
81void DrawRect( FOOTPRINT& aFootprint, const VECTOR2I& aPos, const VECTOR2I& aSize, int aRadius,
82 int aWidth, PCB_LAYER_ID aLayer )
83{
84 const auto x_r = aPos.x + aSize.x / 2;
85 const auto x_l = aPos.x - aSize.x / 2;
86 const auto y_t = aPos.y + aSize.y / 2;
87 const auto y_b = aPos.y - aSize.y / 2;
88
89 const auto xin_r = x_r - aRadius;
90 const auto xin_l = x_l + aRadius;
91 const auto yin_t = y_t - aRadius;
92 const auto yin_b = y_b + aRadius;
93
94 // If non-zero (could be zero if it's a stadium shape)
95 if( xin_l != xin_r )
96 {
97 DrawSegment( aFootprint, { { xin_l, y_t }, { xin_r, y_t } }, aWidth, aLayer );
98 DrawSegment( aFootprint, { { xin_l, y_b }, { xin_r, y_b } }, aWidth, aLayer );
99 }
100
101 if( yin_b != yin_t )
102 {
103 DrawSegment( aFootprint, { { x_l, yin_b }, { x_l, yin_t } }, aWidth, aLayer );
104 DrawSegment( aFootprint, { { x_r, yin_b }, { x_r, yin_t } }, aWidth, aLayer );
105 }
106
107 if( aRadius > 0 )
108 {
109 DrawArc( aFootprint, { xin_r, yin_t }, { x_r, yin_t }, ANGLE_90, aWidth, aLayer );
110 DrawArc( aFootprint, { xin_l, yin_t }, { xin_l, y_t }, ANGLE_90, aWidth, aLayer );
111 DrawArc( aFootprint, { xin_l, yin_b }, { x_l, yin_b }, ANGLE_90, aWidth, aLayer );
112 DrawArc( aFootprint, { xin_r, yin_b }, { xin_r, y_b }, ANGLE_90, aWidth, aLayer );
113 }
114}
115
116} // namespace KI_TEST
Construction utilities for PCB tests.
EDA_ANGLE GetOrientation() const
Definition: footprint.h:212
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: footprint.cpp:969
VECTOR2I GetPosition() const override
Definition: footprint.h:209
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
Simple container to manage line stroke parameters.
Definition: stroke_params.h:81
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:437
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
void DrawArc(FOOTPRINT &aFootprint, const VECTOR2I &aCentre, const VECTOR2I &aStart, const EDA_ANGLE &aAngle, int aWidth, PCB_LAYER_ID aLayer)
Draw an arc on a footprint.
void DrawSegment(FOOTPRINT &aFootprint, const SEG &aSeg, int aWidth, PCB_LAYER_ID aLayer)
Draw a segment in the given footprint.
void DrawRect(FOOTPRINT &aFootprint, const VECTOR2I &aPos, const VECTOR2I &aSize, int aRadius, int aWidth, PCB_LAYER_ID aLayer)
Draw a rectangle on a footprint.
void DrawPolyline(FOOTPRINT &aFootprint, const std::vector< VECTOR2I > &aPts, int aWidth, PCB_LAYER_ID aLayer)
Draw a polyline - a set of linked segments.