API: Use net names, not codes, and allow creating new nets

Fixes https://gitlab.com/kicad/code/kicad/-/issues/21027


(cherry picked from commit 0ff77bd33292b80ab9f5d686c5e7d9ce06be715f)

Co-authored-by: Jon Evans <jon@craftyjon.com>
This commit is contained in:
Jon Evans 2025-05-30 22:13:44 -04:00
parent cbdfc279ff
commit 7e24210b1e
6 changed files with 51 additions and 18 deletions

View File

@ -32,6 +32,7 @@
#include <string_utils.h>
#include <i18n_utility.h>
#include <netinfo.h>
#include <api/board/board_types.pb.h>
using namespace std::placeholders;
@ -43,6 +44,32 @@ BOARD_CONNECTED_ITEM::BOARD_CONNECTED_ITEM( BOARD_ITEM* aParent, KICAD_T idtype
}
void BOARD_CONNECTED_ITEM::UnpackNet( const kiapi::board::types::Net& aProto )
{
if( BOARD* board = GetBoard() )
{
wxString name = wxString::FromUTF8( aProto.name() );
if( NETINFO_ITEM* net = board->FindNet( name ) )
{
m_netinfo = net;
}
else
{
NETINFO_ITEM* newnet = new NETINFO_ITEM( board, name, 0 );
board->Add( newnet );
m_netinfo = newnet;
}
}
}
void BOARD_CONNECTED_ITEM::PackNet( kiapi::board::types::Net* aProto ) const
{
aProto->set_name( GetNetname().ToUTF8() );
}
bool BOARD_CONNECTED_ITEM::SetNetCode( int aNetCode, bool aNoAssert )
{
if( !IsOnCopperLayer() )

View File

@ -33,6 +33,11 @@ class NETCLASS;
class NETINFO_ITEM;
class PAD;
namespace kiapi::board::types
{
class Net;
}
/**
* A base class derived from #BOARD_ITEM for items that can be connected and have a net, a
* netname, a clearance ...
@ -87,6 +92,13 @@ public:
m_netinfo = aNetInfo;
}
/**
* Assigns a net to this item from an API message. If the net does not yet exist, it will be created.
* @param aProto is the protobuf message to unpack a net from
*/
void UnpackNet( const kiapi::board::types::Net& aProto );
void PackNet( kiapi::board::types::Net* aProto ) const;
/**
* @return the net code.
*/

View File

@ -159,8 +159,7 @@ void PAD::Serialize( google::protobuf::Any &aContainer ) const
kiapi::common::PackVector2( *pad.mutable_position(), GetPosition() );
pad.set_locked( IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
: kiapi::common::types::LockedState::LS_UNLOCKED );
pad.mutable_net()->mutable_code()->set_value( GetNetCode() );
pad.mutable_net()->set_name( GetNetname() );
PackNet( pad.mutable_net() );
pad.set_number( GetNumber().ToUTF8() );
pad.set_type( ToProtoEnum<PAD_ATTRIB, PadType>( GetAttribute() ) );
@ -184,7 +183,7 @@ bool PAD::Deserialize( const google::protobuf::Any &aContainer )
const_cast<KIID&>( m_Uuid ) = KIID( pad.id().value() );
SetPosition( kiapi::common::UnpackVector2( pad.position() ) );
SetNetCode( pad.net().code().value() );
UnpackNet( pad.net() );
SetLocked( pad.locked() == kiapi::common::types::LockedState::LS_LOCKED );
SetAttribute( FromProtoEnum<PAD_ATTRIB>( pad.type() ) );
SetNumber( wxString::FromUTF8( pad.number() ) );

View File

@ -83,8 +83,7 @@ void PCB_SHAPE::Serialize( google::protobuf::Any &aContainer ) const
BoardGraphicShape msg;
msg.set_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( GetLayer() ) );
msg.mutable_net()->mutable_code()->set_value( GetNetCode() );
msg.mutable_net()->set_name( GetNetname() );
PackNet( msg.mutable_net() );
msg.mutable_id()->set_value( m_Uuid.AsStdString() );
msg.set_locked( IsLocked() ? types::LockedState::LS_LOCKED : types::LockedState::LS_UNLOCKED );
@ -123,7 +122,7 @@ bool PCB_SHAPE::Deserialize( const google::protobuf::Any &aContainer )
const_cast<KIID&>( m_Uuid ) = KIID( msg.id().value() );
SetLocked( msg.locked() == types::LS_LOCKED );
SetLayer( FromProtoEnum<PCB_LAYER_ID, BoardLayer>( msg.layer() ) );
SetNetCode( msg.net().code().value() );
UnpackNet( msg.net() );
google::protobuf::Any any;
any.PackFrom( msg.shape() );

View File

@ -410,8 +410,7 @@ void PCB_TRACK::Serialize( google::protobuf::Any &aContainer ) const
track.set_layer( ToProtoEnum<PCB_LAYER_ID, kiapi::board::types::BoardLayer>( GetLayer() ) );
track.set_locked( IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
: kiapi::common::types::LockedState::LS_UNLOCKED );
track.mutable_net()->mutable_code()->set_value( GetNetCode() );
track.mutable_net()->set_name( GetNetname() );
PackNet( track.mutable_net() );
// TODO m_hasSolderMask and m_solderMaskMargin
aContainer.PackFrom( track );
@ -430,7 +429,7 @@ bool PCB_TRACK::Deserialize( const google::protobuf::Any &aContainer )
SetEnd( VECTOR2I( track.end().x_nm(), track.end().y_nm() ) );
SetWidth( track.width().value_nm() );
SetLayer( FromProtoEnum<PCB_LAYER_ID, kiapi::board::types::BoardLayer>( track.layer() ) );
SetNetCode( track.net().code().value() );
UnpackNet( track.net() );
SetLocked( track.locked() == kiapi::common::types::LockedState::LS_LOCKED );
// TODO m_hasSolderMask and m_solderMaskMargin
@ -453,8 +452,7 @@ void PCB_ARC::Serialize( google::protobuf::Any &aContainer ) const
arc.set_layer( ToProtoEnum<PCB_LAYER_ID, kiapi::board::types::BoardLayer>( GetLayer() ) );
arc.set_locked( IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
: kiapi::common::types::LockedState::LS_UNLOCKED );
arc.mutable_net()->mutable_code()->set_value( GetNetCode() );
arc.mutable_net()->set_name( GetNetname() );
PackNet( arc.mutable_net() );
// TODO m_hasSolderMask and m_solderMaskMargin
aContainer.PackFrom( arc );
@ -474,7 +472,7 @@ bool PCB_ARC::Deserialize( const google::protobuf::Any &aContainer )
SetEnd( VECTOR2I( arc.end().x_nm(), arc.end().y_nm() ) );
SetWidth( arc.width().value_nm() );
SetLayer( FromProtoEnum<PCB_LAYER_ID, kiapi::board::types::BoardLayer>( arc.layer() ) );
SetNetCode( arc.net().code().value() );
UnpackNet( arc.net() );
SetLocked( arc.locked() == kiapi::common::types::LockedState::LS_LOCKED );
// TODO m_hasSolderMask and m_solderMaskMargin
@ -503,8 +501,7 @@ void PCB_VIA::Serialize( google::protobuf::Any &aContainer ) const
via.set_type( ToProtoEnum<VIATYPE, kiapi::board::types::ViaType>( GetViaType() ) );
via.set_locked( IsLocked() ? kiapi::common::types::LockedState::LS_LOCKED
: kiapi::common::types::LockedState::LS_UNLOCKED );
via.mutable_net()->mutable_code()->set_value( GetNetCode() );
via.mutable_net()->set_name( GetNetname() );
PackNet( via.mutable_net() );
aContainer.PackFrom( via );
}
@ -531,7 +528,7 @@ bool PCB_VIA::Deserialize( const google::protobuf::Any &aContainer )
m_padStack.LayerSet().reset();
SetViaType( FromProtoEnum<VIATYPE>( via.type() ) );
SetNetCode( via.net().code().value() );
UnpackNet( via.net() );
SetLocked( via.locked() == kiapi::common::types::LockedState::LS_LOCKED );
return true;

View File

@ -294,8 +294,7 @@ void ZONE::Serialize( google::protobuf::Any& aContainer ) const
case 1: hatch->set_border_mode( types::ZHFBM_USE_HATCH_THICKNESS ); break;
}
cu->mutable_net()->mutable_code()->set_value( GetNetCode() );
cu->mutable_net()->set_name( GetNetname() );
PackNet( cu->mutable_net() );
cu->mutable_teardrop()->set_type(
ToProtoEnum<TEARDROP_TYPE, types::TeardropType>( m_teardropType ) );
}
@ -391,7 +390,7 @@ bool ZONE::Deserialize( const google::protobuf::Any& aContainer )
case types::ZHFBM_USE_HATCH_THICKNESS: m_hatchBorderAlgorithm = 1; break;
}
SetNetCode( cu.net().code().value() );
UnpackNet( cu.net() );
m_teardropType = FromProtoEnum<TEARDROP_TYPE>( cu.teardrop().type() );
for( const auto& properties : zone.layer_properties() )