KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_3d_body.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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <pcb_3d_body.h>
21
22#include <api/api_enums.h>
23#include <api/api_utils.h>
24#include <api/board/board_types.pb.h>
25#include <google/protobuf/any.pb.h>
26#include <i18n_utility.h>
27#include <properties/property.h>
29#include <board.h>
30#include <footprint.h>
31
32
37
38
39void PCB_3D_BODY::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
40{
41 if( m_extruded.m_layer == UNDEFINED_LAYER )
42 return;
43
44 if( BOARD* board = GetBoard() )
45 m_extruded.m_layer = board->FlipLayer( m_extruded.m_layer );
46 else
47 m_extruded.m_layer = ::FlipLayer( m_extruded.m_layer );
48}
49
50
51void PCB_3D_BODY::CopyFrom( const BOARD_ITEM* aOther )
52{
53 wxCHECK( aOther && aOther->Type() == PCB_3D_BODY_T, /* void */ );
54 *this = *static_cast<const PCB_3D_BODY*>( aOther );
55}
56
57
58double PCB_3D_BODY::Similarity( const BOARD_ITEM& aOther ) const
59{
60 if( aOther.Type() != Type() )
61 return 0.0;
62
63 const PCB_3D_BODY& other = static_cast<const PCB_3D_BODY&>( aOther );
64
65 if( m_kind != other.m_kind )
66 return 0.0;
67
68 return m_extruded == other.m_extruded ? 1.0 : 0.5;
69}
70
71
72bool PCB_3D_BODY::operator==( const PCB_3D_BODY& aOther ) const
73{
74 return m_kind == aOther.m_kind && m_extruded == aOther.m_extruded;
75}
76
77
78bool PCB_3D_BODY::operator==( const BOARD_ITEM& aOther ) const
79{
80 if( aOther.Type() != Type() )
81 return false;
82
83 return *this == static_cast<const PCB_3D_BODY&>( aOther );
84}
85
86
88{
89 assert( aImage->Type() == PCB_3D_BODY_T );
90
91 std::swap( *( (PCB_3D_BODY*) this ), *( (PCB_3D_BODY*) aImage ) );
92}
93
94
95void PCB_3D_BODY::Serialize( kiapi::board::types::Body3D& aOutput ) const
96{
97 using namespace kiapi::board;
98
99 aOutput.mutable_id()->set_value( m_Uuid.AsStdString() );
100
101 if( FOOTPRINT* parent = GetParentFootprint() )
102 aOutput.mutable_parent()->set_value( parent->m_Uuid.AsStdString() );
103 else if( const BOARD* board = GetBoard() )
104 aOutput.mutable_parent()->set_value( board->m_Uuid.AsStdString() );
105
106 aOutput.set_kind( types::Body3DKind::BDK_EXTRUDED );
107
108 kiapi::common::PackVector3D( *aOutput.mutable_scale(), m_extruded.m_scale );
109 kiapi::common::PackVector3D( *aOutput.mutable_rotation(), m_extruded.m_rotation );
110 kiapi::common::PackVector3D( *aOutput.mutable_offset(), m_extruded.m_offset );
111 aOutput.set_visible( m_extruded.m_show );
112
113 types::ExtrudedBody* extruded = aOutput.mutable_extruded();
114 kiapi::common::PackDistance( *extruded->mutable_height(), m_extruded.m_height );
115 kiapi::common::PackDistance( *extruded->mutable_standoff(), m_extruded.m_standoff );
116 extruded->set_outline_layer( ToProtoEnum<PCB_LAYER_ID, types::BoardLayer>( m_extruded.m_layer ) );
117
118 switch( m_extruded.m_material )
119 {
120 case EXTRUSION_MATERIAL::PLASTIC: extruded->set_material( types::EM_PLASTIC ); break;
121 case EXTRUSION_MATERIAL::MATTE: extruded->set_material( types::EM_MATTE ); break;
122 case EXTRUSION_MATERIAL::METAL: extruded->set_material( types::EM_METAL ); break;
123 case EXTRUSION_MATERIAL::COPPER: extruded->set_material( types::EM_COPPER ); break;
124 }
125
127 kiapi::common::PackColor( *extruded->mutable_color(), m_extruded.m_color );
128}
129
130
131void PCB_3D_BODY::Serialize( google::protobuf::Any& aContainer ) const
132{
133 kiapi::board::types::Body3D msg;
134 Serialize( msg );
135 aContainer.PackFrom( msg );
136}
137
138
139bool PCB_3D_BODY::Deserialize( const kiapi::board::types::Body3D& aInput )
140{
141 using namespace kiapi::board;
142
143 if( aInput.kind() != types::BDK_EXTRUDED || !aInput.has_extruded() )
144 return false;
145
146 SetUuidDirect( KIID( aInput.id().value() ) );
147
148 m_extruded.m_scale = kiapi::common::UnpackVector3D( aInput.scale() );
149 m_extruded.m_rotation = kiapi::common::UnpackVector3D( aInput.rotation() );
150 m_extruded.m_offset = kiapi::common::UnpackVector3D( aInput.offset() );
151 m_extruded.m_show = aInput.visible();
152
153 const types::ExtrudedBody& extruded = aInput.extruded();
154 m_extruded.m_height = kiapi::common::UnpackDistance( extruded.height() );
155 m_extruded.m_standoff = kiapi::common::UnpackDistance( extruded.standoff() );
156 m_extruded.m_layer = FromProtoEnum<PCB_LAYER_ID, types::BoardLayer>( extruded.outline_layer() );
157
158 switch( extruded.material() )
159 {
160 default:
161 case types::EM_UNKNOWN: m_extruded.m_material = EXTRUSION_MATERIAL::PLASTIC; break;
162 case types::EM_PLASTIC: m_extruded.m_material = EXTRUSION_MATERIAL::PLASTIC; break;
163 case types::EM_MATTE: m_extruded.m_material = EXTRUSION_MATERIAL::MATTE; break;
164 case types::EM_METAL: m_extruded.m_material = EXTRUSION_MATERIAL::METAL; break;
165 case types::EM_COPPER: m_extruded.m_material = EXTRUSION_MATERIAL::COPPER; break;
166 }
167
168 if( extruded.has_color() )
169 m_extruded.m_color = kiapi::common::UnpackColor( extruded.color() );
170 else
172
173 return true;
174}
175
176
177bool PCB_3D_BODY::Deserialize( const google::protobuf::Any& aContainer )
178{
179 kiapi::board::types::Body3D msg;
180
181 if( !aContainer.UnpackTo( &msg ) )
182 return false;
183
184 return Deserialize( msg );
185}
186
187
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:55
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:85
BOARD_ITEM(BOARD_ITEM *aParent, KICAD_T idtype, PCB_LAYER_ID aLayer=F_Cu)
Definition board_item.h:87
void SetUuidDirect(const KIID &aUuid)
Raw UUID assignment.
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
FOOTPRINT * GetParentFootprint() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
Definition kiid.h:46
A 3D body attached to a footprint (or, later, placed on a board).
Definition pcb_3d_body.h:93
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
bool operator==(const PCB_3D_BODY &aOther) const
PCB_3D_BODY(BOARD_ITEM *aParent=nullptr)
double Similarity(const BOARD_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
void CopyFrom(const BOARD_ITEM *aOther) override
EXTRUDED_3D_BODY m_extruded
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
void swapData(BOARD_ITEM *aImage) override
Provide class metadata.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
static PROPERTY_MANAGER & Instance()
Some functions to handle hotkeys in KiCad.
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayerId, int aCopperLayersCount)
Definition layer_id.cpp:181
@ UNDEFINED_LAYER
Definition layer_ids.h:57
FLIP_DIRECTION
Definition mirror.h:23
KICOMMON_API void PackColor(types::Color &aOutput, const KIGFX::COLOR4D &aInput)
KICOMMON_API int UnpackDistance(const types::Distance &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API VECTOR3D UnpackVector3D(const types::Vector3D &aInput)
KICOMMON_API KIGFX::COLOR4D UnpackColor(const types::Color &aInput)
KICOMMON_API void PackDistance(types::Distance &aOutput, int aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector3D(types::Vector3D &aOutput, const VECTOR3D &aInput)
static struct PCB_3D_BODY_DESC _PCB_3D_BODY_DESC
#define TYPE_HASH(x)
Macro to generate unique identifier for a type.
Definition property.h:74
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
@ PCB_3D_BODY_T
class PCB_3D_BODY, a 3D body (extruded or model) on a footprint
Definition typeinfo.h:241
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708