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>
24#include <wx/log.h>
25
26const wxChar* const traceApi = wxT( "KICAD_API" );
27
28
30{
31
32KICOMMON_API std::optional<KICAD_T> TypeNameFromAny( const google::protobuf::Any& aMessage )
33{
34 static const std::map<std::string, KICAD_T> s_types = {
35 { "type.googleapis.com/kiapi.board.types.Track", PCB_TRACE_T },
36 { "type.googleapis.com/kiapi.board.types.Arc", PCB_ARC_T },
37 { "type.googleapis.com/kiapi.board.types.Via", PCB_VIA_T },
38 { "type.googleapis.com/kiapi.board.types.BoardText", PCB_TEXT_T },
39 { "type.googleapis.com/kiapi.board.types.BoardTextBox", PCB_TEXTBOX_T },
40 { "type.googleapis.com/kiapi.board.types.BoardGraphicShape", PCB_SHAPE_T },
41 { "type.googleapis.com/kiapi.board.types.Pad", PCB_PAD_T },
42 { "type.googleapis.com/kiapi.board.types.Zone", PCB_ZONE_T },
43 { "type.googleapis.com/kiapi.board.types.Dimension", PCB_DIMENSION_T },
44 { "type.googleapis.com/kiapi.board.types.ReferenceImage", PCB_REFERENCE_IMAGE_T },
45 { "type.googleapis.com/kiapi.board.types.Group", PCB_GROUP_T },
46 { "type.googleapis.com/kiapi.board.types.Field", PCB_FIELD_T },
47 { "type.googleapis.com/kiapi.board.types.FootprintInstance", PCB_FOOTPRINT_T },
48 };
49
50 auto it = s_types.find( aMessage.type_url() );
51
52 if( it != s_types.end() )
53 return it->second;
54
55 wxLogTrace( traceApi, wxString::Format( wxS( "Any message type %s is not known" ),
56 aMessage.type_url() ) );
57
58 return std::nullopt;
59}
60
61
62KICOMMON_API LIB_ID LibIdFromProto( const types::LibraryIdentifier& aId )
63{
64 return LIB_ID( aId.library_nickname(), aId.entry_name() );
65}
66
67
68KICOMMON_API types::LibraryIdentifier LibIdToProto( const LIB_ID& aId )
69{
70 types::LibraryIdentifier msg;
71 msg.set_library_nickname( aId.GetLibNickname() );
72 msg.set_entry_name( aId.GetLibItemName() );
73 return msg;
74}
75
76
77KICOMMON_API void PackVector2( types::Vector2& aOutput, const VECTOR2I& aInput )
78{
79 aOutput.set_x_nm( aInput.x );
80 aOutput.set_y_nm( aInput.y );
81}
82
83
84KICOMMON_API VECTOR2I UnpackVector2( const types::Vector2& aInput )
85{
86 return VECTOR2I( aInput.x_nm(), aInput.y_nm() );
87}
88
89
90KICOMMON_API void PackVector3D( types::Vector3D& aOutput, const VECTOR3D& aInput )
91{
92 aOutput.set_x_nm( aInput.x );
93 aOutput.set_y_nm( aInput.y );
94 aOutput.set_z_nm( aInput.z );
95}
96
97
98KICOMMON_API VECTOR3D UnpackVector3D( const types::Vector3D& aInput )
99{
100 return VECTOR3D( aInput.x_nm(), aInput.y_nm(), aInput.z_nm() );
101}
102
103
104KICOMMON_API void PackBox2( types::Box2& aOutput, const BOX2I& aInput )
105{
106 PackVector2( *aOutput.mutable_position(), aInput.GetOrigin() );
107 PackVector2( *aOutput.mutable_size(), aInput.GetSize() );
108}
109
110
111KICOMMON_API BOX2I UnpackBox2( const types::Box2& aInput )
112{
113 return BOX2I( UnpackVector2( aInput.position() ), UnpackVector2( aInput.size() ) );
114}
115
116
117KICOMMON_API void PackPolyLine( types::PolyLine& aOutput, const SHAPE_LINE_CHAIN& aSlc )
118{
119 for( int vertex = 0; vertex < aSlc.PointCount(); vertex = aSlc.NextShape( vertex ) )
120 {
121 if( vertex < 0 )
122 break;
123
124 types::PolyLineNode* node = aOutput.mutable_nodes()->Add();
125
126 if( aSlc.IsPtOnArc( vertex ) )
127 {
128 const SHAPE_ARC& arc = aSlc.Arc( aSlc.ArcIndex( vertex ) );
129 node->mutable_arc()->mutable_start()->set_x_nm( arc.GetP0().x );
130 node->mutable_arc()->mutable_start()->set_y_nm( arc.GetP0().y );
131 node->mutable_arc()->mutable_mid()->set_x_nm( arc.GetArcMid().x );
132 node->mutable_arc()->mutable_mid()->set_y_nm( arc.GetArcMid().y );
133 node->mutable_arc()->mutable_end()->set_x_nm( arc.GetP1().x );
134 node->mutable_arc()->mutable_end()->set_y_nm( arc.GetP1().y );
135 }
136 else
137 {
138 node->mutable_point()->set_x_nm( aSlc.CPoint( vertex ).x );
139 node->mutable_point()->set_y_nm( aSlc.CPoint( vertex ).y );
140 }
141 }
142
143 aOutput.set_closed( aSlc.IsClosed() );
144}
145
146
147KICOMMON_API SHAPE_LINE_CHAIN UnpackPolyLine( const types::PolyLine& aInput )
148{
150
151 for( const types::PolyLineNode& node : aInput.nodes() )
152 {
153 if( node.has_point() )
154 {
155 slc.Append( VECTOR2I( node.point().x_nm(), node.point().y_nm() ) );
156 }
157 else if( node.has_arc() )
158 {
159 slc.Append( SHAPE_ARC( VECTOR2I( node.arc().start().x_nm(), node.arc().start().y_nm() ),
160 VECTOR2I( node.arc().mid().x_nm(), node.arc().mid().y_nm() ),
161 VECTOR2I( node.arc().end().x_nm(), node.arc().end().y_nm() ),
162 0 /* don't care about width here */ ) );
163 }
164 }
165
166 slc.SetClosed( aInput.closed() );
167
168 return slc;
169}
170
171
172KICOMMON_API void PackPolySet( types::PolySet& aOutput, const SHAPE_POLY_SET& aInput )
173{
174 for( int idx = 0; idx < aInput.OutlineCount(); ++idx )
175 {
176 const SHAPE_POLY_SET::POLYGON& poly = aInput.Polygon( idx );
177
178 if( poly.empty() )
179 continue;
180
181 types::PolygonWithHoles* polyMsg = aOutput.mutable_polygons()->Add();
182 PackPolyLine( *polyMsg->mutable_outline(), poly.front() );
183
184 if( poly.size() > 1 )
185 {
186 for( size_t hole = 1; hole < poly.size(); ++hole )
187 {
188 types::PolyLine* pl = polyMsg->mutable_holes()->Add();
189 PackPolyLine( *pl, poly[hole] );
190 }
191 }
192 }
193}
194
195
196KICOMMON_API SHAPE_POLY_SET UnpackPolySet( const types::PolySet& aInput )
197{
198 SHAPE_POLY_SET sps;
199
200 for( const types::PolygonWithHoles& polygonWithHoles : aInput.polygons() )
201 {
203
204 polygon.emplace_back( UnpackPolyLine( polygonWithHoles.outline() ) );
205
206 for( const types::PolyLine& holeMsg : polygonWithHoles.holes() )
207 polygon.emplace_back( UnpackPolyLine( holeMsg ) );
208
209 sps.AddPolygon( polygon );
210 }
211
212 return sps;
213}
214
215} // namespace kiapi::common
BOX2< VECTOR2I > BOX2I
Definition: box2.h:922
constexpr const Vec & GetOrigin() const
Definition: box2.h:210
constexpr const SizeVec & GetSize() const
Definition: box2.h:206
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:116
const VECTOR2I & GetP1() const
Definition: shape_arc.h:115
const VECTOR2I & GetP0() const
Definition: shape_arc.h:114
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.
Represent a set of closed polygons.
POLYGON & Polygon(int aIndex)
Return the aIndex-th subpolygon in the set.
int AddPolygon(const POLYGON &apolygon)
Adds a polygon to the set.
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
int OutlineCount() const
Return the number of outlines in the set.
T y
Definition: vector3.h:64
T z
Definition: vector3.h:65
T x
Definition: vector3.h:63
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition: api_utils.cpp:26
#define KICOMMON_API
Definition: kicommon.h:28
KICOMMON_API SHAPE_LINE_CHAIN UnpackPolyLine(const types::PolyLine &aInput)
Definition: api_utils.cpp:147
KICOMMON_API BOX2I UnpackBox2(const types::Box2 &aInput)
Definition: api_utils.cpp:111
KICOMMON_API void PackBox2(types::Box2 &aOutput, const BOX2I &aInput)
Definition: api_utils.cpp:104
KICOMMON_API std::optional< KICAD_T > TypeNameFromAny(const google::protobuf::Any &aMessage)
Definition: api_utils.cpp:32
KICOMMON_API VECTOR3D UnpackVector3D(const types::Vector3D &aInput)
Definition: api_utils.cpp:98
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput)
Definition: api_utils.cpp:84
KICOMMON_API SHAPE_POLY_SET UnpackPolySet(const types::PolySet &aInput)
Definition: api_utils.cpp:196
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput)
Definition: api_utils.cpp:77
KICOMMON_API LIB_ID LibIdFromProto(const types::LibraryIdentifier &aId)
Definition: api_utils.cpp:62
KICOMMON_API types::LibraryIdentifier LibIdToProto(const LIB_ID &aId)
Definition: api_utils.cpp:68
KICOMMON_API void PackPolyLine(types::PolyLine &aOutput, const SHAPE_LINE_CHAIN &aSlc)
Definition: api_utils.cpp:117
KICOMMON_API void PackVector3D(types::Vector3D &aOutput, const VECTOR3D &aInput)
Definition: api_utils.cpp:90
KICOMMON_API void PackPolySet(types::PolySet &aOutput, const SHAPE_POLY_SET &aInput)
Definition: api_utils.cpp:172
@ 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< int32_t > VECTOR2I
Definition: vector2d.h:691
VECTOR3< double > VECTOR3D
Definition: vector3.h:214