KiCad PCB EDA Suite
Loading...
Searching...
No Matches
3d_placeholder_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 The 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 <footprint.h>
27#include <pad.h>
28#include <board_item.h>
29#include <layer_ids.h>
30#include <geometry/eda_angle.h>
31
32
34{
35 BOX2I localBox;
36 bool hasLocalBounds = false;
37
38 for( PAD* pad : aFootprint->Pads() )
39 {
40 VECTOR2I padPos = pad->GetFPRelativePosition();
41 VECTOR2I padSize = pad->GetSize( PADSTACK::ALL_LAYERS );
42
43 BOX2I padBox;
44 padBox.SetOrigin( padPos - padSize / 2 );
45 padBox.SetSize( padSize );
46
47 if( !hasLocalBounds )
48 {
49 localBox = padBox;
50 hasLocalBounds = true;
51 }
52 else
53 {
54 localBox.Merge( padBox );
55 }
56 }
57
58 if( !hasLocalBounds )
59 {
60 VECTOR2I fpPos = aFootprint->GetPosition();
61 EDA_ANGLE fpAngle = aFootprint->GetOrientation();
62
63 for( BOARD_ITEM* item : aFootprint->GraphicalItems() )
64 {
65 PCB_LAYER_ID layer = item->GetLayer();
66
67 if( layer == F_Fab || layer == B_Fab || layer == F_CrtYd || layer == B_CrtYd || layer == F_SilkS
68 || layer == B_SilkS )
69 {
70 BOX2I itemBox = item->GetBoundingBox();
71
72 VECTOR2I corners[4] = { itemBox.GetOrigin() - fpPos,
73 VECTOR2I( itemBox.GetRight(), itemBox.GetTop() ) - fpPos,
74 VECTOR2I( itemBox.GetRight(), itemBox.GetBottom() ) - fpPos,
75 VECTOR2I( itemBox.GetLeft(), itemBox.GetBottom() ) - fpPos };
76
77 BOX2I localItemBox;
78
79 for( int ci = 0; ci < 4; ++ci )
80 {
81 RotatePoint( corners[ci], -fpAngle );
82
83 if( ci == 0 )
84 {
85 localItemBox.SetOrigin( corners[ci] );
86 localItemBox.SetSize( VECTOR2I( 0, 0 ) );
87 }
88 else
89 {
90 localItemBox.Merge( BOX2I( corners[ci], VECTOR2I( 0, 0 ) ) );
91 }
92 }
93
94 if( !hasLocalBounds )
95 {
96 localBox = localItemBox;
97 hasLocalBounds = true;
98 }
99 else
100 {
101 localBox.Merge( localItemBox );
102 }
103 }
104 }
105 }
106
107 if( !hasLocalBounds )
108 {
109 BOX2I fpBox = aFootprint->GetBoundingBox( false );
110 int size = std::min( fpBox.GetWidth(), fpBox.GetHeight() );
111 localBox.SetOrigin( VECTOR2I( -size / 2, -size / 2 ) );
112 localBox.SetSize( VECTOR2I( size, size ) );
113 }
114
115 return localBox;
116}
BOX2I CalcPlaceholderLocalBox(const FOOTPRINT *aFootprint)
Calculate a local space bounding box for a placeholder 3D model.
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:237
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:658
constexpr void SetSize(const SizeVec &size)
Definition box2.h:248
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr coord_type GetLeft() const
Definition box2.h:228
constexpr const Vec & GetOrigin() const
Definition box2.h:210
constexpr coord_type GetRight() const
Definition box2.h:217
constexpr coord_type GetTop() const
Definition box2.h:229
constexpr coord_type GetBottom() const
Definition box2.h:222
EDA_ANGLE GetOrientation() const
Definition footprint.h:350
std::deque< PAD * > & Pads()
Definition footprint.h:326
VECTOR2I GetPosition() const override
Definition footprint.h:347
DRAWINGS & GraphicalItems()
Definition footprint.h:329
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
Definition pad.h:55
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ F_CrtYd
Definition layer_ids.h:116
@ F_Fab
Definition layer_ids.h:119
@ F_SilkS
Definition layer_ids.h:100
@ B_CrtYd
Definition layer_ids.h:115
@ B_SilkS
Definition layer_ids.h:101
@ B_Fab
Definition layer_ids.h:118
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687