KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_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) 2023 Jon Evans <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <magic_enum.hpp>
22#include <api/api_utils.h>
23
25{
26
27std::optional<KICAD_T> TypeNameFromAny( const google::protobuf::Any& aMessage )
28{
29 static const std::map<std::string, KICAD_T> s_types = {
30 { "type.googleapis.com/kiapi.board.types.Track", PCB_TRACE_T },
31 { "type.googleapis.com/kiapi.board.types.Arc", PCB_ARC_T },
32 { "type.googleapis.com/kiapi.board.types.Via", PCB_VIA_T },
33 { "type.googleapis.com/kiapi.board.types.Text", PCB_TEXT_T },
34 { "type.googleapis.com/kiapi.board.types.TextBox", PCB_TEXTBOX_T },
35 { "type.googleapis.com/kiapi.board.types.GraphicShape", PCB_SHAPE_T },
36 { "type.googleapis.com/kiapi.board.types.Pad", PCB_PAD_T },
37 { "type.googleapis.com/kiapi.board.types.Zone", PCB_ZONE_T },
38 { "type.googleapis.com/kiapi.board.types.Dimension", PCB_DIMENSION_T },
39 { "type.googleapis.com/kiapi.board.types.ReferenceImage", PCB_REFERENCE_IMAGE_T },
40 { "type.googleapis.com/kiapi.board.types.Group", PCB_GROUP_T },
41 { "type.googleapis.com/kiapi.board.types.Field", PCB_FIELD_T },
42 { "type.googleapis.com/kiapi.board.types.FootprintInstance", PCB_FOOTPRINT_T },
43 };
44
45 auto it = s_types.find( aMessage.type_url() );
46
47 if( it != s_types.end() )
48 return it->second;
49
50 return std::nullopt;
51}
52
53
54LIB_ID LibIdFromProto( const types::LibraryIdentifier& aId )
55{
56 return LIB_ID( aId.library_nickname(), aId.entry_name() );
57}
58
59
60types::LibraryIdentifier LibIdToProto( const LIB_ID& aId )
61{
62 types::LibraryIdentifier msg;
63 msg.set_library_nickname( aId.GetLibNickname() );
64 msg.set_entry_name( aId.GetLibItemName() );
65 return msg;
66}
67
68
69void PackVector2( kiapi::common::types::Vector2& aOutput, const VECTOR2I aInput )
70{
71 aOutput.set_x_nm( aInput.x );
72 aOutput.set_y_nm( aInput.y );
73}
74
75VECTOR2I UnpackVector2( const types::Vector2& aInput )
76{
77 return VECTOR2I( aInput.x_nm(), aInput.y_nm() );
78}
79
80
81void PackPolyLine( kiapi::common::types::PolyLine& aOutput, const SHAPE_LINE_CHAIN& aSlc )
82{
83 for( int vertex = 0; vertex < aSlc.PointCount(); vertex = aSlc.NextShape( vertex ) )
84 {
85 if( vertex < 0 )
86 break;
87
88 kiapi::common::types::PolyLineNode* node = aOutput.mutable_nodes()->Add();
89
90 if( aSlc.IsPtOnArc( vertex ) )
91 {
92 const SHAPE_ARC& arc = aSlc.Arc( aSlc.ArcIndex( vertex ) );
93 node->mutable_arc()->mutable_start()->set_x_nm( arc.GetP0().x );
94 node->mutable_arc()->mutable_start()->set_y_nm( arc.GetP0().y );
95 node->mutable_arc()->mutable_mid()->set_x_nm( arc.GetArcMid().x );
96 node->mutable_arc()->mutable_mid()->set_y_nm( arc.GetArcMid().y );
97 node->mutable_arc()->mutable_end()->set_x_nm( arc.GetP1().x );
98 node->mutable_arc()->mutable_end()->set_y_nm( arc.GetP1().y );
99 }
100 else
101 {
102 node->mutable_point()->set_x_nm( aSlc.CPoint( vertex ).x );
103 node->mutable_point()->set_y_nm( aSlc.CPoint( vertex ).y );
104 }
105 }
106
107 aOutput.set_closed( aSlc.IsClosed() );
108}
109
110
111SHAPE_LINE_CHAIN UnpackPolyLine( const kiapi::common::types::PolyLine& aInput )
112{
114
115 for( const kiapi::common::types::PolyLineNode& node : aInput.nodes() )
116 {
117 if( node.has_point() )
118 {
119 slc.Append( VECTOR2I( node.point().x_nm(), node.point().y_nm() ) );
120 }
121 else if( node.has_arc() )
122 {
123 slc.Append( SHAPE_ARC( VECTOR2I( node.arc().start().x_nm(), node.arc().start().y_nm() ),
124 VECTOR2I( node.arc().mid().x_nm(), node.arc().mid().y_nm() ),
125 VECTOR2I( node.arc().end().x_nm(), node.arc().end().y_nm() ),
126 0 /* don't care about width here */ ) );
127 }
128 }
129
130 slc.SetClosed( aInput.closed() );
131
132 return slc;
133}
134
135} // namespace kiapi::common
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
const VECTOR2I & GetArcMid() const
Definition: shape_arc.h:115
const VECTOR2I & GetP1() const
Definition: shape_arc.h:114
const VECTOR2I & GetP0() const
Definition: shape_arc.h:113
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
bool IsPtOnArc(size_t aPtIndex) const
const SHAPE_ARC & Arc(size_t aArc) const
bool IsClosed() const override
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
int PointCount() const
Return the number of points (vertices) in this line chain.
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
int NextShape(int aPointIndex) const
Return the vertex index of the next shape in the chain, or -1 if aPointIndex is the last shape.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
void PackVector2(kiapi::common::types::Vector2 &aOutput, const VECTOR2I aInput)
Definition: api_utils.cpp:69
std::optional< KICAD_T > TypeNameFromAny(const google::protobuf::Any &aMessage)
Definition: api_utils.cpp:27
types::LibraryIdentifier LibIdToProto(const LIB_ID &aId)
Definition: api_utils.cpp:60
VECTOR2I UnpackVector2(const types::Vector2 &aInput)
Definition: api_utils.cpp:75
void PackPolyLine(kiapi::common::types::PolyLine &aOutput, const SHAPE_LINE_CHAIN &aSlc)
Definition: api_utils.cpp:81
SHAPE_LINE_CHAIN UnpackPolyLine(const kiapi::common::types::PolyLine &aInput)
Definition: api_utils.cpp:111
LIB_ID LibIdFromProto(const types::LibraryIdentifier &aId)
Definition: api_utils.cpp:54
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:107
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:100
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588