30#include <google/protobuf/empty.pb.h>
40using namespace kiapi::common::commands;
41using namespace kiapi::common::types;
42using google::protobuf::Empty;
72 GetVersionResponse reply;
74 reply.mutable_version()->set_full_version(
GetBuildVersion().ToStdString() );
77 reply.mutable_version()->set_major( std::get<0>( version ) );
78 reply.mutable_version()->set_minor( std::get<1>( version ) );
79 reply.mutable_version()->set_patch( std::get<2>( version ) );
88 wxFileName fn( wxEmptyString, wxString::FromUTF8( aCtx.
Request.binary_name() ) );
90 fn.SetExt( wxT(
"exe" ) );
95 reply.set_path(
path.ToUTF8() );
103 NetClassesResponse reply;
105 std::shared_ptr<NET_SETTINGS>& netSettings =
108 google::protobuf::Any
any;
110 netSettings->GetDefaultNetclass()->Serialize(
any );
111 any.UnpackTo( reply.add_net_classes() );
113 for(
const auto& netClass : netSettings->GetNetclasses() | std::views::values )
115 netClass->Serialize(
any );
116 any.UnpackTo( reply.add_net_classes() );
126 std::shared_ptr<NET_SETTINGS>& netSettings =
129 if( aCtx.
Request.merge_mode() == MapMergeMode::MMM_REPLACE )
130 netSettings->ClearNetclasses();
132 auto netClasses = netSettings->GetNetclasses();
133 google::protobuf::Any
any;
135 for(
const auto& ncProto : aCtx.
Request.net_classes() )
137 any.PackFrom( ncProto );
138 wxString
name = wxString::FromUTF8( ncProto.name() );
140 if(
name == wxT(
"Default" ) )
142 netSettings->GetDefaultNetclass()->Deserialize(
any );
146 if( !netClasses.contains(
name ) )
147 netClasses.insert( {
name, std::make_shared<NETCLASS>(
name,
false ) } );
149 netClasses[
name]->Deserialize(
any );
153 netSettings->SetNetclasses( netClasses );
169 google::protobuf::Any
any;
175 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
176 e.set_error_message(
"Could not decode text in GetTextExtents message" );
177 return tl::unexpected( e );
180 types::Box2 response;
188 response.mutable_position()->set_x_nm( bbox.
GetPosition().
x );
189 response.mutable_position()->set_y_nm( bbox.
GetPosition().
y );
190 response.mutable_size()->set_x_nm( bbox.
GetSize().
x );
191 response.mutable_size()->set_y_nm( bbox.
GetSize().
y );
200 GetTextAsShapesResponse reply;
202 for(
const TextOrTextBox& textMsg : aCtx.
Request.text() )
205 const Text* textPtr = &textMsg.text();
207 if( textMsg.has_textbox() )
209 dummyText.set_text( textMsg.textbox().text() );
210 dummyText.mutable_attributes()->CopyFrom( textMsg.textbox().attributes() );
211 textPtr = &dummyText;
215 google::protobuf::Any
any;
216 any.PackFrom( *textPtr );
221 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
222 e.set_error_message(
"Could not decode text in GetTextAsShapes message" );
223 return tl::unexpected( e );
226 std::shared_ptr<SHAPE_COMPOUND> shapes =
text.GetEffectiveTextShape(
false );
228 TextWithShapes* entry = reply.add_text_with_shapes();
229 entry->mutable_text()->CopyFrom( textMsg );
231 for(
SHAPE* subshape : shapes->Shapes() )
235 GraphicShape* shapeMsg = entry->mutable_shapes()->add_shapes();
236 any.UnpackTo( shapeMsg );
239 if( textMsg.has_textbox() )
241 GraphicShape* border = entry->mutable_shapes()->add_shapes();
242 int width = textMsg.textbox().attributes().stroke_width().value_nm();
243 border->mutable_attributes()->mutable_stroke()->mutable_width()->set_value_nm( width );
248 PackVector2( *border->mutable_segment()->mutable_start(), tl );
252 border = entry->mutable_shapes()->add_shapes();
253 border->mutable_attributes()->mutable_stroke()->mutable_width()->set_value_nm( width );
255 PackVector2( *border->mutable_segment()->mutable_end(), br );
258 border = entry->mutable_shapes()->add_shapes();
259 border->mutable_attributes()->mutable_stroke()->mutable_width()->set_value_nm( width );
260 PackVector2( *border->mutable_segment()->mutable_start(), br );
264 border = entry->mutable_shapes()->add_shapes();
265 border->mutable_attributes()->mutable_stroke()->mutable_width()->set_value_nm( width );
267 PackVector2( *border->mutable_segment()->mutable_end(), tl );
278 if( !aCtx.
Request.has_document() || aCtx.
Request.document().type() != DOCTYPE_PROJECT )
281 e.set_status( ApiStatusCode::AS_UNHANDLED );
283 return tl::unexpected( e );
286 ExpandTextVariablesResponse reply;
289 for(
const std::string& textMsg : aCtx.
Request.text() )
292 reply.add_text(
result.ToUTF8() );
302 wxString identifier = wxString::FromUTF8( aCtx.
Request.identifier() );
304 if( identifier.IsEmpty() )
307 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
308 e.set_error_message(
"plugin identifier is missing" );
309 return tl::unexpected( e );
315 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
316 e.set_error_message(
"plugin identifier is invalid" );
317 return tl::unexpected( e );
321 path.AppendDir(
"plugins" );
326 path.AppendDir( identifier );
328 StringResponse reply;
329 reply.set_response(
path.GetPath() );
337 if( !aCtx.
Request.has_document() || aCtx.
Request.document().type() != DOCTYPE_PROJECT )
340 e.set_status( ApiStatusCode::AS_UNHANDLED );
342 return tl::unexpected( e );
350 e.set_status( ApiStatusCode::AS_NOT_READY );
351 e.set_error_message(
"no valid project is loaded, cannot get text variables" );
352 return tl::unexpected( e );
355 const std::map<wxString, wxString>& vars =
project.GetTextVars();
357 project::TextVariables reply;
358 auto map = reply.mutable_variables();
360 for(
const auto& [key, value] : vars )
361 ( *map )[ std::string( key.ToUTF8() ) ] = value.ToUTF8();
370 if( !aCtx.
Request.has_document() || aCtx.
Request.document().type() != DOCTYPE_PROJECT )
373 e.set_status( ApiStatusCode::AS_UNHANDLED );
375 return tl::unexpected( e );
383 e.set_status( ApiStatusCode::AS_NOT_READY );
384 e.set_error_message(
"no valid project is loaded, cannot set text variables" );
385 return tl::unexpected( e );
388 const project::TextVariables& newVars = aCtx.
Request.variables();
389 std::map<wxString, wxString>& vars =
project.GetTextVars();
391 if( aCtx.
Request.merge_mode() == MapMergeMode::MMM_REPLACE )
394 for(
const auto& [key, value] : newVars.variables() )
395 vars[wxString::FromUTF8( key )] = wxString::FromUTF8( value );
tl::expected< T, ApiResponseStatus > HANDLER_RESULT
constexpr EDA_IU_SCALE pcbIUScale
const std::tuple< int, int, int > & GetMajorMinorPatchTuple()
Get the build version numbers as a tuple.
wxString GetBuildVersion()
Get the full KiCad version string.
HANDLER_RESULT< Empty > handlePing(const HANDLER_CONTEXT< commands::Ping > &aCtx)
HANDLER_RESULT< types::Box2 > handleGetTextExtents(const HANDLER_CONTEXT< commands::GetTextExtents > &aCtx)
HANDLER_RESULT< commands::GetTextAsShapesResponse > handleGetTextAsShapes(const HANDLER_CONTEXT< commands::GetTextAsShapes > &aCtx)
HANDLER_RESULT< commands::GetVersionResponse > handleGetVersion(const HANDLER_CONTEXT< commands::GetVersion > &aCtx)
HANDLER_RESULT< commands::PathResponse > handleGetKiCadBinaryPath(const HANDLER_CONTEXT< commands::GetKiCadBinaryPath > &aCtx)
HANDLER_RESULT< commands::NetClassesResponse > handleGetNetClasses(const HANDLER_CONTEXT< commands::GetNetClasses > &aCtx)
HANDLER_RESULT< Empty > handleSetNetClasses(const HANDLER_CONTEXT< commands::SetNetClasses > &aCtx)
HANDLER_RESULT< commands::StringResponse > handleGetPluginSettingsPath(const HANDLER_CONTEXT< commands::GetPluginSettingsPath > &aCtx)
HANDLER_RESULT< commands::ExpandTextVariablesResponse > handleExpandTextVariables(const HANDLER_CONTEXT< commands::ExpandTextVariables > &aCtx)
HANDLER_RESULT< Empty > handleSetTextVariables(const HANDLER_CONTEXT< commands::SetTextVariables > &aCtx)
HANDLER_RESULT< project::TextVariables > handleGetTextVariables(const HANDLER_CONTEXT< commands::GetTextVariables > &aCtx)
void registerHandler(HANDLER_RESULT< ResponseType >(HandlerType::*aHandler)(const HANDLER_CONTEXT< RequestType > &))
Registers an API command handler for the given message types.
static bool IsValidIdentifier(const wxString &aIdentifier)
constexpr const Vec & GetPosition() const
const BOX2< Vec > GetBoundingBoxRotated(const VECTOR2I &aRotCenter, const EDA_ANGLE &aAngle) const
Useful to calculate bounding box of rotated items, when rotation is not cardinal.
constexpr const SizeVec & GetSize() const
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
virtual SETTINGS_MANAGER & GetSettingsManager() const
std::shared_ptr< NET_SETTINGS > m_NetSettings
Net settings for this project (owned here)
Container for project specific data.
virtual PROJECT_FILE & GetProjectFile() const
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Save a loaded project.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
An abstract shape on 2D plane.
A type-safe container of any type.
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput)
PGM_BASE & Pgm()
The global program "get" accessor.
RequestMessageType Request
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I