7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-07 21:55:32 +00:00

API: Add ExpandTextVariables

This commit is contained in:
Jon Evans 2024-11-28 19:21:18 -05:00
parent 7168364445
commit 8ff3ef5fd5
6 changed files with 79 additions and 0 deletions

View File

@ -21,6 +21,7 @@ syntax = "proto3";
package kiapi.common.commands;
import "common/types/base_types.proto";
import "common/types/project_settings.proto";
@ -32,3 +33,17 @@ message NetClassesResponse
{
repeated kiapi.common.project.NetClass net_classes = 1;
}
message ExpandTextVariables
{
kiapi.common.types.DocumentSpecifier document = 1;
repeated string text = 2;
}
message ExpandTextVariablesResponse
{
repeated string text = 1;
}

View File

@ -89,6 +89,7 @@ enum DocumentType
DOCTYPE_PCB = 3;
DOCTYPE_FOOTPRINT = 4;
DOCTYPE_DRAWING_SHEET = 5;
DOCTYPE_PROJECT = 6;
}
/**

View File

@ -47,6 +47,8 @@ API_HANDLER_COMMON::API_HANDLER_COMMON() :
registerHandler<GetTextExtents, types::Box2>( &API_HANDLER_COMMON::handleGetTextExtents );
registerHandler<GetTextAsShapes, GetTextAsShapesResponse>(
&API_HANDLER_COMMON::handleGetTextAsShapes );
registerHandler<ExpandTextVariables, ExpandTextVariablesResponse>(
&API_HANDLER_COMMON::handleExpandTextVariables );
}
@ -197,3 +199,27 @@ HANDLER_RESULT<GetTextAsShapesResponse> API_HANDLER_COMMON::handleGetTextAsShape
return reply;
}
HANDLER_RESULT<ExpandTextVariablesResponse> API_HANDLER_COMMON::handleExpandTextVariables(
ExpandTextVariables& aMsg, const HANDLER_CONTEXT& aCtx )
{
if( !aMsg.has_document() || aMsg.document().type() != DocumentType::DOCTYPE_PROJECT )
{
ApiResponseStatus e;
e.set_status( ApiStatusCode::AS_UNHANDLED );
// No error message, this is a flag that the server should try a different handler
return tl::unexpected( e );
}
ExpandTextVariablesResponse reply;
PROJECT& project = Pgm().GetSettingsManager().Prj();
for( const std::string& textMsg : aMsg.text() )
{
wxString result = ExpandTextVars( wxString::FromUTF8( textMsg ), &project );
reply.add_text( result.ToUTF8() );
}
return reply;
}

View File

@ -51,6 +51,9 @@ private:
HANDLER_RESULT<commands::GetTextAsShapesResponse>
handleGetTextAsShapes( commands::GetTextAsShapes& aMsg, const HANDLER_CONTEXT& aCtx );
HANDLER_RESULT<commands::ExpandTextVariablesResponse>
handleExpandTextVariables( commands::ExpandTextVariables& aMsg, const HANDLER_CONTEXT& aCtx );
};
#endif //KICAD_API_HANDLER_COMMON_H

View File

@ -69,6 +69,8 @@ API_HANDLER_PCB::API_HANDLER_PCB( PCB_EDIT_FRAME* aFrame ) :
&API_HANDLER_PCB::handleGetPadShapeAsPolygon );
registerHandler<GetTitleBlockInfo, types::TitleBlockInfo>(
&API_HANDLER_PCB::handleGetTitleBlockInfo );
registerHandler<ExpandTextVariables, ExpandTextVariablesResponse>(
&API_HANDLER_PCB::handleExpandTextVariables );
registerHandler<InteractiveMoveItems, Empty>( &API_HANDLER_PCB::handleInteractiveMoveItems );
registerHandler<GetNets, NetsResponse>( &API_HANDLER_PCB::handleGetNets );
@ -694,6 +696,34 @@ HANDLER_RESULT<types::TitleBlockInfo> API_HANDLER_PCB::handleGetTitleBlockInfo(
}
HANDLER_RESULT<ExpandTextVariablesResponse> API_HANDLER_PCB::handleExpandTextVariables(
ExpandTextVariables& aMsg, const HANDLER_CONTEXT& aCtx )
{
HANDLER_RESULT<bool> documentValidation = validateDocument( aMsg.document() );
if( !documentValidation )
return tl::unexpected( documentValidation.error() );
ExpandTextVariablesResponse reply;
BOARD* board = frame()->GetBoard();
std::function<bool( wxString* )> textResolver =
[&]( wxString* token ) -> bool
{
// Handles m_board->GetTitleBlock() *and* m_board->GetProject()
return board->ResolveTextVar( token, 0 );
};
for( const std::string& textMsg : aMsg.text() )
{
wxString text = ExpandTextVars( wxString::FromUTF8( textMsg ), &textResolver );
reply.add_text( text.ToUTF8() );
}
return reply;
}
HANDLER_RESULT<Empty> API_HANDLER_PCB::handleInteractiveMoveItems( InteractiveMoveItems& aMsg,
const HANDLER_CONTEXT& aCtx )
{

View File

@ -27,6 +27,7 @@
#include <api/board/board_commands.pb.h>
#include <api/board/board_types.pb.h>
#include <api/common/commands/editor_commands.pb.h>
#include <api/common/commands/project_commands.pb.h>
#include <kiid.h>
#include <properties/property_mgr.h>
@ -81,6 +82,9 @@ private:
HANDLER_RESULT<types::TitleBlockInfo> handleGetTitleBlockInfo( commands::GetTitleBlockInfo& aMsg,
const HANDLER_CONTEXT& aCtx );
HANDLER_RESULT<commands::ExpandTextVariablesResponse>
handleExpandTextVariables( commands::ExpandTextVariables& aMsg, const HANDLER_CONTEXT& aCtx );
HANDLER_RESULT<Empty> handleInteractiveMoveItems( InteractiveMoveItems& aMsg,
const HANDLER_CONTEXT& aCtx );