Implement PCB_GROUP serialization and deserialization methods
This commit is contained in:
@@ -841,7 +841,9 @@ message ReferenceImage
|
||||
|
||||
message Group
|
||||
{
|
||||
// TODO
|
||||
kiapi.common.types.KIID id = 1;
|
||||
string name = 2;
|
||||
repeated kiapi.common.types.KIID items = 3;
|
||||
}
|
||||
|
||||
message FieldId
|
||||
|
||||
@@ -222,6 +222,14 @@ message GetItems
|
||||
repeated kiapi.common.types.KiCadObjectType types = 2;
|
||||
}
|
||||
|
||||
message GetItemsById
|
||||
{
|
||||
// Specifies which document to query, which fields to return, etc.
|
||||
kiapi.common.types.ItemHeader header = 1;
|
||||
|
||||
repeated kiapi.common.types.KIID items = 2;
|
||||
}
|
||||
|
||||
message GetItemsResponse
|
||||
{
|
||||
// Specifies which document was modified, which fields are included in items, etc.
|
||||
|
||||
@@ -64,6 +64,7 @@ API_HANDLER_PCB::API_HANDLER_PCB( PCB_EDIT_FRAME* aFrame ) :
|
||||
registerHandler<RevertDocument, Empty>( &API_HANDLER_PCB::handleRevertDocument );
|
||||
|
||||
registerHandler<GetItems, GetItemsResponse>( &API_HANDLER_PCB::handleGetItems );
|
||||
registerHandler<GetItemsById, GetItemsResponse>( &API_HANDLER_PCB::handleGetItemsById );
|
||||
|
||||
registerHandler<GetSelection, SelectionResponse>( &API_HANDLER_PCB::handleGetSelection );
|
||||
registerHandler<ClearSelection, Empty>( &API_HANDLER_PCB::handleClearSelection );
|
||||
@@ -484,7 +485,9 @@ HANDLER_RESULT<ItemRequestStatus> API_HANDLER_PCB::handleCreateUpdateItemsIntern
|
||||
// doesn't currently know what to do with a footprint that has had its children
|
||||
// replaced with other children; which results in things like the view not having its
|
||||
// cached geometry for footprint children updated when you move a footprint around.
|
||||
if( boardItem->Type() == PCB_FOOTPRINT_T )
|
||||
// And also, groups are special because they can contain any item type, so we
|
||||
// can't use CopyFrom on them either.
|
||||
if( boardItem->Type() == PCB_FOOTPRINT_T || boardItem->Type() == PCB_GROUP_T )
|
||||
{
|
||||
commit->Remove( boardItem );
|
||||
item->Serialize( newItem );
|
||||
@@ -615,6 +618,16 @@ HANDLER_RESULT<GetItemsResponse> API_HANDLER_PCB::handleGetItems(
|
||||
break;
|
||||
}
|
||||
|
||||
case PCB_GROUP_T:
|
||||
{
|
||||
handledAnything = true;
|
||||
|
||||
std::copy( board->Groups().begin(), board->Groups().end(),
|
||||
std::back_inserter( items ) );
|
||||
|
||||
typesInserted.insert( PCB_GROUP_T );
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -643,6 +656,47 @@ HANDLER_RESULT<GetItemsResponse> API_HANDLER_PCB::handleGetItems(
|
||||
}
|
||||
|
||||
|
||||
HANDLER_RESULT<GetItemsResponse> API_HANDLER_PCB::handleGetItemsById(
|
||||
const HANDLER_CONTEXT<GetItemsById>& aCtx )
|
||||
{
|
||||
if( std::optional<ApiResponseStatus> busy = checkForBusy() )
|
||||
return tl::unexpected( *busy );
|
||||
|
||||
if( !validateItemHeaderDocument( aCtx.Request.header() ) )
|
||||
{
|
||||
ApiResponseStatus e;
|
||||
e.set_status( ApiStatusCode::AS_UNHANDLED );
|
||||
return tl::unexpected( e );
|
||||
}
|
||||
|
||||
GetItemsResponse response;
|
||||
|
||||
std::vector<BOARD_ITEM*> items;
|
||||
for( const kiapi::common::types::KIID& id : aCtx.Request.items() )
|
||||
{
|
||||
if( std::optional<BOARD_ITEM*> item = getItemById( KIID( id.value() ) ) )
|
||||
items.emplace_back( *item );
|
||||
}
|
||||
|
||||
if( items.empty() )
|
||||
{
|
||||
ApiResponseStatus e;
|
||||
e.set_status( ApiStatusCode::AS_BAD_REQUEST );
|
||||
e.set_error_message( "none of the requested IDs were found or valid" );
|
||||
return tl::unexpected( e );
|
||||
}
|
||||
|
||||
for( const BOARD_ITEM* item : items )
|
||||
{
|
||||
google::protobuf::Any itemBuf;
|
||||
item->Serialize( itemBuf );
|
||||
response.mutable_items()->Add( std::move( itemBuf ) );
|
||||
}
|
||||
|
||||
response.set_status( ItemRequestStatus::IRS_OK );
|
||||
return response;
|
||||
}
|
||||
|
||||
void API_HANDLER_PCB::deleteItemsInternal( std::map<KIID, ItemDeletionStatus>& aItemsToDelete,
|
||||
const std::string& aClientName )
|
||||
{
|
||||
|
||||
@@ -74,6 +74,9 @@ private:
|
||||
HANDLER_RESULT<commands::GetItemsResponse> handleGetItems(
|
||||
const HANDLER_CONTEXT<commands::GetItems>& aCtx );
|
||||
|
||||
HANDLER_RESULT<commands::GetItemsResponse> handleGetItemsById(
|
||||
const HANDLER_CONTEXT<commands::GetItemsById>& aCtx );
|
||||
|
||||
HANDLER_RESULT<commands::SelectionResponse> handleGetSelection(
|
||||
const HANDLER_CONTEXT<commands::GetSelection>& aCtx );
|
||||
|
||||
|
||||
@@ -32,6 +32,11 @@
|
||||
#include <confirm.h>
|
||||
#include <widgets/msgpanel.h>
|
||||
#include <view/view.h>
|
||||
#include <api/api_enums.h>
|
||||
#include <api/api_utils.h>
|
||||
#include <api/api_pcb_utils.h>
|
||||
#include <api/board/board_types.pb.h>
|
||||
#include <google/protobuf/any.pb.h>
|
||||
|
||||
#include <wx/debug.h>
|
||||
|
||||
@@ -46,6 +51,53 @@ PCB_GROUP::PCB_GROUP( BOARD_ITEM* aParent, KICAD_T idtype, PCB_LAYER_ID aLayer )
|
||||
{
|
||||
}
|
||||
|
||||
void PCB_GROUP::Serialize( google::protobuf::Any &aContainer ) const
|
||||
{
|
||||
using namespace kiapi::board::types;
|
||||
Group group;
|
||||
|
||||
group.mutable_id()->set_value( m_Uuid.AsStdString() );
|
||||
group.set_name( GetName().ToUTF8() );
|
||||
|
||||
for( EDA_ITEM* item : GetItems() )
|
||||
{
|
||||
kiapi::common::types::KIID* itemId = group.add_items();
|
||||
itemId->set_value( item->m_Uuid.AsStdString() );
|
||||
}
|
||||
|
||||
aContainer.PackFrom( group );
|
||||
}
|
||||
|
||||
|
||||
bool PCB_GROUP::Deserialize( const google::protobuf::Any &aContainer )
|
||||
{
|
||||
kiapi::board::types::Group group;
|
||||
|
||||
if( !aContainer.UnpackTo( &group ) )
|
||||
return false;
|
||||
|
||||
const_cast<KIID&>( m_Uuid ) = KIID( group.id().value() );
|
||||
SetName( wxString( group.name().c_str(), wxConvUTF8 ) );
|
||||
|
||||
|
||||
BOARD* board = GetBoard();
|
||||
if( !board )
|
||||
return false;
|
||||
|
||||
for ( const kiapi::common::types::KIID& itemId : group.items() )
|
||||
{
|
||||
|
||||
KIID id( itemId.value() );
|
||||
BOARD_ITEM* item = board->ResolveItem( id, true );
|
||||
|
||||
if( item )
|
||||
{
|
||||
AddItem( item );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unordered_set<BOARD_ITEM*> PCB_GROUP::GetBoardItems() const
|
||||
{
|
||||
|
||||
@@ -54,6 +54,10 @@ class PCB_GROUP : public BOARD_ITEM, public EDA_GROUP
|
||||
public:
|
||||
PCB_GROUP( BOARD_ITEM* aParent );
|
||||
|
||||
void Serialize( google::protobuf::Any &aContainer ) const override;
|
||||
bool Deserialize( const google::protobuf::Any &aContainer ) override;
|
||||
|
||||
|
||||
EDA_ITEM* AsEdaItem() override { return this; }
|
||||
|
||||
static inline bool ClassOf( const EDA_ITEM* aItem )
|
||||
|
||||
Reference in New Issue
Block a user