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 The 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#include <api/schematic/schematic_types.pb.h>
25#include <kiid.h>
26#include <project.h>
27#include <wx/log.h>
28
29const wxChar* const traceApi = wxT( "KICAD_API" );
30
31
33{
34
35KICOMMON_API std::optional<KICAD_T> TypeNameFromAny( const google::protobuf::Any& aMessage )
36{
37 if( aMessage.type_url() == "type.googleapis.com/kiapi.schematic.types.BusEntry" )
38 {
39 kiapi::schematic::types::BusEntry entry;
40
41 if( !aMessage.UnpackTo( &entry ) )
42 return std::nullopt;
43
44 switch( entry.type() )
45 {
46 case kiapi::schematic::types::BET_WIRE_TO_BUS:
48
49 case kiapi::schematic::types::BET_BUS_TO_BUS:
51
52 default:
53 return std::nullopt;
54 }
55 }
56
57 static const std::map<std::string, KICAD_T> s_types = {
58 { "type.googleapis.com/kiapi.board.types.Track", PCB_TRACE_T },
59 { "type.googleapis.com/kiapi.board.types.Arc", PCB_ARC_T },
60 { "type.googleapis.com/kiapi.board.types.Via", PCB_VIA_T },
61 { "type.googleapis.com/kiapi.board.types.BoardText", PCB_TEXT_T },
62 { "type.googleapis.com/kiapi.board.types.BoardTextBox", PCB_TEXTBOX_T },
63 { "type.googleapis.com/kiapi.board.types.BoardGraphicShape", PCB_SHAPE_T },
64 { "type.googleapis.com/kiapi.board.types.Barcode", PCB_BARCODE_T },
65 { "type.googleapis.com/kiapi.board.types.Pad", PCB_PAD_T },
66 { "type.googleapis.com/kiapi.board.types.Zone", PCB_ZONE_T },
67 { "type.googleapis.com/kiapi.board.types.Dimension", PCB_DIMENSION_T },
68 { "type.googleapis.com/kiapi.board.types.ReferenceImage", PCB_REFERENCE_IMAGE_T },
69 { "type.googleapis.com/kiapi.board.types.Group", PCB_GROUP_T },
70 { "type.googleapis.com/kiapi.board.types.Field", PCB_FIELD_T },
71 { "type.googleapis.com/kiapi.board.types.FootprintInstance", PCB_FOOTPRINT_T },
72 { "type.googleapis.com/kiapi.schematic.types.Junction", SCH_JUNCTION_T },
73 { "type.googleapis.com/kiapi.schematic.types.NoConnectMarker", SCH_NO_CONNECT_T },
74 { "type.googleapis.com/kiapi.schematic.types.BusEntry", SCH_BUS_WIRE_ENTRY_T },
75 { "type.googleapis.com/kiapi.schematic.types.SchematicLine", SCH_LINE_T },
76 { "type.googleapis.com/kiapi.schematic.types.SchematicGraphicShape", SCH_SHAPE_T },
77 { "type.googleapis.com/kiapi.schematic.types.SchematicImage", SCH_BITMAP_T },
78 { "type.googleapis.com/kiapi.schematic.types.SchematicTextBox", SCH_TEXTBOX_T },
79 { "type.googleapis.com/kiapi.schematic.types.SchematicText", SCH_TEXT_T },
80 { "type.googleapis.com/kiapi.schematic.types.Table", SCH_TABLE_T },
81 { "type.googleapis.com/kiapi.schematic.types.LocalLabel", SCH_LABEL_T },
82 { "type.googleapis.com/kiapi.schematic.types.GlobalLabel", SCH_GLOBAL_LABEL_T },
83 { "type.googleapis.com/kiapi.schematic.types.HierarchicalLabel", SCH_HIER_LABEL_T },
84 { "type.googleapis.com/kiapi.schematic.types.DirectiveLabel", SCH_DIRECTIVE_LABEL_T },
85 { "type.googleapis.com/kiapi.schematic.types.Group", SCH_GROUP_T },
86 { "type.googleapis.com/kiapi.schematic.types.SheetSymbol", SCH_SHEET_T },
87 { "type.googleapis.com/kiapi.schematic.types.SchematicSymbolInstance", SCH_SYMBOL_T },
88 { "type.googleapis.com/kiapi.schematic.types.SchematicPin", SCH_PIN_T },
89 };
90
91 auto it = s_types.find( aMessage.type_url() );
92
93 if( it != s_types.end() )
94 return it->second;
95
96 wxLogTrace( traceApi, wxString::Format( wxS( "Any message type %s is not known" ),
97 aMessage.type_url() ) );
98
99 return std::nullopt;
100}
101
102
103KICOMMON_API LIB_ID UnpackLibId( const types::LibraryIdentifier& aId )
104{
105 return LIB_ID( aId.library_nickname(), aId.entry_name() );
106}
107
108
109KICOMMON_API void PackLibId( types::LibraryIdentifier* aOutput, const LIB_ID& aId )
110{
111 aOutput->set_library_nickname( aId.GetLibNickname() );
112 aOutput->set_entry_name( aId.GetLibItemName() );
113}
114
115
116KICOMMON_API void PackVector2( types::Vector2& aOutput, const VECTOR2I& aInput, const EDA_IU_SCALE& aScale )
117{
118 aOutput.set_x_nm( aScale.IUToNm( aInput.x ) );
119 aOutput.set_y_nm( aScale.IUToNm( aInput.y ) );
120}
121
122
123KICOMMON_API VECTOR2I UnpackVector2( const types::Vector2& aInput, const EDA_IU_SCALE& aScale )
124{
125 return VECTOR2I( aScale.NmToIU( aInput.x_nm() ), aScale.NmToIU( aInput.y_nm() ) );
126}
127
128
129KICOMMON_API void PackVector3D( types::Vector3D& aOutput, const VECTOR3D& aInput )
130{
131 aOutput.set_x_nm( aInput.x );
132 aOutput.set_y_nm( aInput.y );
133 aOutput.set_z_nm( aInput.z );
134}
135
136
137KICOMMON_API VECTOR3D UnpackVector3D( const types::Vector3D& aInput )
138{
139 return VECTOR3D( aInput.x_nm(), aInput.y_nm(), aInput.z_nm() );
140}
141
142
143KICOMMON_API void PackBox2( types::Box2& aOutput, const BOX2I& aInput, const EDA_IU_SCALE& aScale )
144{
145 PackVector2( *aOutput.mutable_position(), aInput.GetOrigin(), aScale );
146 PackVector2( *aOutput.mutable_size(), aInput.GetSize(), aScale );
147}
148
149
150KICOMMON_API BOX2I UnpackBox2( const types::Box2& aInput, const EDA_IU_SCALE& aScale )
151{
152 return BOX2I( UnpackVector2( aInput.position(), aScale ), UnpackVector2( aInput.size(), aScale ) );
153}
154
155
156KICOMMON_API void PackDistance( types::Distance& aOutput, int aInput, const EDA_IU_SCALE& aScale )
157{
158 aOutput.set_value_nm( aScale.IUToNm( aInput ) );
159}
160
161
162KICOMMON_API int UnpackDistance( const types::Distance& aInput, const EDA_IU_SCALE& aScale )
163{
164 return aScale.NmToIU( aInput.value_nm() );
165}
166
167
168KICOMMON_API void PackPolyLine( types::PolyLine& aOutput, const SHAPE_LINE_CHAIN& aSlc, const EDA_IU_SCALE& aScale )
169{
170 for( int vertex = 0; vertex < aSlc.PointCount(); vertex = aSlc.NextShape( vertex ) )
171 {
172 if( vertex < 0 )
173 break;
174
175 if( aSlc.IsArcStart( vertex ) )
176 {
177 types::PolyLineNode* node = aOutput.mutable_nodes()->Add();
178 const SHAPE_ARC& arc = aSlc.Arc( aSlc.ArcIndex( vertex ) );
179 PackVector2( *node->mutable_arc()->mutable_start(), arc.GetP0(), aScale );
180 PackVector2( *node->mutable_arc()->mutable_mid(), arc.GetArcMid(), aScale );
181 PackVector2( *node->mutable_arc()->mutable_end(), arc.GetP1(), aScale );
182 }
183 else if( !aSlc.IsPtOnArc( vertex ) )
184 {
185 types::PolyLineNode* node = aOutput.mutable_nodes()->Add();
186 PackVector2( *node->mutable_point(), aSlc.CPoint( vertex ), aScale );
187 }
188 }
189
190 aOutput.set_closed( aSlc.IsClosed() );
191}
192
193
194KICOMMON_API SHAPE_LINE_CHAIN UnpackPolyLine( const types::PolyLine& aInput, const EDA_IU_SCALE& aScale )
195{
197
198 for( const types::PolyLineNode& node : aInput.nodes() )
199 {
200 if( node.has_point() )
201 {
202 slc.Append( UnpackVector2( node.point(), aScale ) );
203 }
204 else if( node.has_arc() )
205 {
206 slc.Append( SHAPE_ARC( UnpackVector2( node.arc().start(), aScale ),
207 UnpackVector2( node.arc().mid(), aScale ),
208 UnpackVector2( node.arc().end(), aScale ),
209 0 ) );
210 }
211 }
212
213 slc.SetClosed( aInput.closed() );
214
215 return slc;
216}
217
218
219KICOMMON_API void PackPolySet( types::PolySet& aOutput, const SHAPE_POLY_SET& aInput, const EDA_IU_SCALE& aScale )
220{
221 for( int idx = 0; idx < aInput.OutlineCount(); ++idx )
222 {
223 const SHAPE_POLY_SET::POLYGON& poly = aInput.Polygon( idx );
224
225 if( poly.empty() )
226 continue;
227
228 types::PolygonWithHoles* polyMsg = aOutput.mutable_polygons()->Add();
229 PackPolyLine( *polyMsg->mutable_outline(), poly.front(), aScale );
230
231 if( poly.size() > 1 )
232 {
233 for( size_t hole = 1; hole < poly.size(); ++hole )
234 {
235 types::PolyLine* pl = polyMsg->mutable_holes()->Add();
236 PackPolyLine( *pl, poly[hole], aScale );
237 }
238 }
239 }
240}
241
242
243KICOMMON_API SHAPE_POLY_SET UnpackPolySet( const types::PolySet& aInput, const EDA_IU_SCALE& aScale )
244{
245 SHAPE_POLY_SET sps;
246
247 for( const types::PolygonWithHoles& polygonWithHoles : aInput.polygons() )
248 {
250
251 polygon.emplace_back( UnpackPolyLine( polygonWithHoles.outline(), aScale ) );
252
253 for( const types::PolyLine& holeMsg : polygonWithHoles.holes() )
254 polygon.emplace_back( UnpackPolyLine( holeMsg, aScale ) );
255
256 sps.AddPolygon( polygon );
257 }
258
259 return sps;
260}
261
262
263KICOMMON_API void PackColor( types::Color& aOutput, const KIGFX::COLOR4D& aInput )
264{
265 aOutput.set_r( aInput.r );
266 aOutput.set_g( aInput.g );
267 aOutput.set_b( aInput.b );
268 aOutput.set_a( aInput.a );
269}
270
271
272KICOMMON_API KIGFX::COLOR4D UnpackColor( const types::Color& aInput )
273{
274 double r = std::clamp( aInput.r(), 0.0, 1.0 );
275 double g = std::clamp( aInput.g(), 0.0, 1.0 );
276 double b = std::clamp( aInput.b(), 0.0, 1.0 );
277 double a = std::clamp( aInput.a(), 0.0, 1.0 );
278
279 return KIGFX::COLOR4D( r, g, b, a );
280}
281
282KICOMMON_API void PackSheetPath( types::SheetPath& aOutput, const KIID_PATH& aInput )
283{
284 aOutput.clear_path();
285
286 for( const KIID& entry : aInput )
287 aOutput.add_path()->set_value( entry.AsStdString() );
288}
289
290KICOMMON_API KIID_PATH UnpackSheetPath( const types::SheetPath& aInput )
291{
293
294 for( const types::KIID& sheet : aInput.path() )
295 output.push_back( KIID( sheet.value() ) );
296
297 return output;
298}
299
300KICOMMON_API void PackProject( types::ProjectSpecifier& aOutput, const PROJECT& aInput )
301{
302 aOutput.set_name( aInput.GetProjectName().ToUTF8() );
303 aOutput.set_path( aInput.GetProjectPath().ToUTF8() );
304}
305
306} // 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 color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
double r
Red component.
Definition color4d.h:393
double g
Green component.
Definition color4d.h:394
double a
Alpha component.
Definition color4d.h:396
double b
Blue component.
Definition color4d.h:395
Definition kiid.h:48
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
Container for project specific data.
Definition project.h:66
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:187
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition project.cpp:199
const VECTOR2I & GetArcMid() const
Definition shape_arc.h:120
const VECTOR2I & GetP1() const
Definition shape_arc.h:119
const VECTOR2I & GetP0() const
Definition shape_arc.h:118
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.
bool IsArcStart(size_t aIndex) const
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.
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition api_utils.cpp:29
#define KICOMMON_API
Definition kicommon.h:27
KICOMMON_API void PackProject(types::ProjectSpecifier &aOutput, const PROJECT &aInput)
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 KIID_PATH UnpackSheetPath(const types::SheetPath &aInput)
KICOMMON_API std::optional< KICAD_T > TypeNameFromAny(const google::protobuf::Any &aMessage)
Definition api_utils.cpp:35
KICOMMON_API void PackPolySet(types::PolySet &aOutput, const SHAPE_POLY_SET &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 VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackBox2(types::Box2 &aOutput, const BOX2I &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackDistance(types::Distance &aOutput, int aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API BOX2I UnpackBox2(const types::Box2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API SHAPE_LINE_CHAIN UnpackPolyLine(const types::PolyLine &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackSheetPath(types::SheetPath &aOutput, const KIID_PATH &aInput)
KICOMMON_API void PackLibId(types::LibraryIdentifier *aOutput, const LIB_ID &aId)
KICOMMON_API LIB_ID UnpackLibId(const types::LibraryIdentifier &aId)
KICOMMON_API void PackPolyLine(types::PolyLine &aOutput, const SHAPE_LINE_CHAIN &aSlc, const EDA_IU_SCALE &aScale)
KICOMMON_API SHAPE_POLY_SET UnpackPolySet(const types::PolySet &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector3D(types::Vector3D &aOutput, const VECTOR3D &aInput)
constexpr int NmToIU(int64_t nm) const
Definition base_units.h:117
constexpr int64_t IUToNm(int iu) const
Definition base_units.h:112
nlohmann::json output
@ SCH_GROUP_T
Definition typeinfo.h:174
@ SCH_TABLE_T
Definition typeinfo.h:166
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:85
@ SCH_LINE_T
Definition typeinfo.h:164
@ SCH_NO_CONNECT_T
Definition typeinfo.h:161
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ SCH_SYMBOL_T
Definition typeinfo.h:173
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:108
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:90
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:105
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:89
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:172
@ SCH_LABEL_T
Definition typeinfo.h:168
@ SCH_SHEET_T
Definition typeinfo.h:176
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:86
@ SCH_SHAPE_T
Definition typeinfo.h:150
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:87
@ SCH_HIER_LABEL_T
Definition typeinfo.h:170
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:163
@ PCB_BARCODE_T
class PCB_BARCODE, a barcode (graphic item)
Definition typeinfo.h:98
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:83
@ SCH_TEXT_T
Definition typeinfo.h:152
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:84
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:162
@ SCH_BITMAP_T
Definition typeinfo.h:165
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:95
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:97
@ SCH_TEXTBOX_T
Definition typeinfo.h:153
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:93
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:169
@ SCH_JUNCTION_T
Definition typeinfo.h:160
@ SCH_PIN_T
Definition typeinfo.h:154
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR3< double > VECTOR3D
Definition vector3.h:230