50#include <fmt/format.h>
51#include <fmt/ranges.h>
71 case SCH_LINE_T:
return std::make_unique<SCH_LINE>();
72 case SCH_SHAPE_T:
return std::make_unique<SCH_SHAPE>();
73 case SCH_BITMAP_T:
return std::make_unique<SCH_BITMAP>();
75 case SCH_TEXT_T:
return std::make_unique<SCH_TEXT>();
76 case SCH_TABLE_T:
return std::make_unique<SCH_TABLE>();
78 case SCH_LABEL_T:
return std::make_unique<SCH_LABEL>();
83 case SCH_FIELD_T:
return std::make_unique<SCH_FIELD>( parentSchItem );
84 case SCH_GROUP_T:
return std::make_unique<SCH_GROUP>();
85 case SCH_SYMBOL_T:
return std::make_unique<SCH_SYMBOL>();
86 case LIB_SYMBOL_T:
return std::make_unique<LIB_SYMBOL>( wxEmptyString );
90 return std::make_unique<SCH_SHEET>(
static_cast<SCH_SCREEN*
>( aContainer ) );
98 return std::make_unique<SCH_SHEET_PIN>(
static_cast<SCH_SHEET*
>( aContainer ) );
104 return std::make_unique<SCH_PIN>(
static_cast<LIB_SYMBOL*
>( aContainer ) );
124 kiapi::schematic::types::PinMapEntry* e = aOutput->add_edits();
125 e->set_pin_number(
edit.m_PinNumber.ToUTF8() );
126 e->set_pad_number(
edit.m_PadNumber.ToUTF8() );
135 override.m_ActiveMapName = wxString::FromUTF8( aInput.active_map_name() );
137 for(
const kiapi::schematic::types::PinMapEntry& e : aInput.edits() )
139 override.m_Edits.push_back( { wxString::FromUTF8( e.pin_number() ), wxString::FromUTF8( e.pad_number() ) } );
154 wxLogTrace(
traceApi,
"error: instance data for symbol %s on %s is missing",
159 google::protobuf::Any
any;
162 if( !
any.UnpackTo( aOutput ) )
166 aOutput->mutable_reference_field()->mutable_text()->set_text( instance.
m_Reference.ToUTF8() );
167 aOutput->mutable_unit()->set_unit( instance.
m_Unit );
169 kiapi::schematic::types::SchematicSymbol* def = aOutput->mutable_definition();
171 std::vector<const SCH_PIN*> pins = aInput->
GetPins( &aPath );
173 std::ranges::sort( pins,
181 kiapi::schematic::types::SchematicSymbolChild* item = def->add_items();
182 item->mutable_unit()->set_unit(
pin->GetUnit() );
183 item->mutable_body_style()->set_style(
pin->GetBodyStyle() );
184 item->set_is_private(
pin->IsPrivate() );
185 pin->Serialize( *item->mutable_item() );
191 std::set<wxString> placedNumbers;
194 placedNumbers.insert(
pin->GetNumber() );
205 if( placedNumbers.count( libPin->GetNumber() ) )
208 kiapi::schematic::types::SchematicSymbolChild* item = def->add_items();
209 item->mutable_unit()->set_unit( libPin->GetUnit() );
210 item->mutable_body_style()->set_style( libPin->GetBodyStyle() );
211 item->set_is_private( libPin->IsPrivate() );
212 libPin->Serialize( *item->mutable_item() );
218 if( !
override.IsDefault() )
221 kiapi::schematic::types::SchematicSymbolAttributes* attributes = aOutput->mutable_attributes();
227 attributes->set_do_not_populate( aInput->
GetDNP() );
233 kiapi::schematic::types::SchematicSymbolVariants* variants = aOutput->mutable_variants();
237 kiapi::schematic::types::SchematicSymbolVariant* variant = variants->add_variants();
238 variant->set_name(
name.ToUTF8() );
241 variant->set_description(
schematic->GetVariantDescription(
name ).ToUTF8() );
243 attributes = variant->mutable_attributes();
244 attributes->set_exclude_from_simulation( variantInfo.m_ExcludedFromSim );
245 attributes->set_exclude_from_bill_of_materials( variantInfo.m_ExcludedFromBOM );
246 attributes->set_exclude_from_board( variantInfo.m_ExcludedFromBoard );
247 attributes->set_exclude_from_position_files( variantInfo.m_ExcludedFromPosFiles );
248 attributes->set_do_not_populate( variantInfo.m_DNP );
250 for(
const auto& [key, value] : variantInfo.m_Fields )
251 ( *variant->mutable_fields() )[std::string( key.ToUTF8() )] = value.ToUTF8();
253 if( variantInfo.m_SymbolOverride )
254 PackLibId( variant->mutable_symbol_override(), *variantInfo.m_SymbolOverride );
256 if( !variantInfo.m_PinMapOverride.IsDefault() )
257 PackPinMapOverride( variant->mutable_pin_map_override(), variantInfo.m_PinMapOverride );
272 if( aInput.has_attributes() )
274 const SchematicSymbolAttributes& attrs = aInput.attributes();
280 aOutput->
SetDNP( attrs.do_not_populate() );
295 if( variantName.CmpNoCase( aName ) == 0 )
304template <
typename VariantProto>
310 wxString
name( aName );
318 if( aInput.has_description() )
325 const kiapi::schematic::types::SchematicSymbolVariants& aInput,
330 std::set<wxString> requested;
332 for(
const SchematicSymbolVariant& variantProto : aInput.variants() )
334 wxString
name = wxString::FromUTF8( variantProto.name() );
340 requested.insert(
name );
343 if( variantProto.has_attributes() )
345 const SchematicSymbolAttributes& attrs = variantProto.attributes();
351 aSymbol->
SetDNP( attrs.do_not_populate(), &aPath,
name );
359 for(
const auto& [fieldName, unused] : stored.
m_Variants[
name].m_Fields )
361 if( variantProto.fields().find( std::string( fieldName.ToUTF8() ) ) == variantProto.fields().end() )
366 if( variantProto.has_symbol_override() )
371 if( variantProto.has_pin_map_override() )
376 for(
const auto& [key, value] : variantProto.fields() )
378 wxString fieldName = wxString::FromUTF8( key );
379 wxString fieldValue = wxString::FromUTF8( value );
385 aStatus->set_code( commands::ItemStatusCode::ISC_INVALID_DATA );
386 aStatus->set_error_message(
"symbol reference can not be changed in a variant" );
403 if( !requested.contains(
name ) )
413 const kiapi::schematic::types::SchematicSymbolInstance& aInput,
416 wxString reference = wxString::FromUTF8( aInput.reference_field().text().text() );
417 int unit = aInput.has_unit() ? aInput.unit().unit() : 1;
422 if( !reference.IsEmpty() )
423 aSymbol->
SetRef( &aPath, reference );
441 if( aInput.has_variants() && !
applySymbolVariants( aStatus, aSymbol, aInput.variants(), aPath, aSchematic ) )
451 google::protobuf::Any
any;
454 if( !
any.UnpackTo( aOutput ) )
461 kiapi::schematic::types::SheetVariants* variants = aOutput->mutable_variants();
465 aOutput->set_page_number( instance->m_PageNumber.ToUTF8() );
467 for(
const auto& [
name, variantInfo] : instance->m_Variants )
469 kiapi::schematic::types::SheetVariant* variant = variants->add_variants();
470 variant->set_name(
name.ToUTF8() );
473 variant->set_description(
schematic->GetVariantDescription(
name ).ToUTF8() );
475 variant->set_exclude_from_sim( variantInfo.m_ExcludedFromSim );
476 variant->set_exclude_from_bom( variantInfo.m_ExcludedFromBOM );
477 variant->set_dnp( variantInfo.m_DNP );
479 for(
const auto& [key, value] : variantInfo.m_Fields )
480 ( *variant->mutable_fields() )[std::string( key.ToUTF8() )] = value.ToUTF8();
492 const kiapi::schematic::types::SheetVariants& aInput,
497 std::set<wxString> requested;
499 for(
const SheetVariant& variantProto : aInput.variants() )
501 wxString
name = wxString::FromUTF8( variantProto.name() );
507 requested.insert(
name );
511 if( variantProto.has_exclude_from_sim() )
514 if( variantProto.has_exclude_from_bom() )
517 if( variantProto.has_dnp() )
518 aSheet->
SetDNP( variantProto.dnp(), &aParentPath,
name );
524 std::map<wxString, wxString> storedFields = stored->m_Variants.at(
name ).m_Fields;
526 for(
const auto& [fieldName, unused] : storedFields )
528 if( variantProto.fields().find( std::string( fieldName.ToUTF8() ) ) == variantProto.fields().end() )
533 for(
const auto& [key, value] : variantProto.fields() )
535 wxString fieldName = wxString::FromUTF8( key );
536 wxString fieldValue = wxString::FromUTF8( value );
542 aStatus->set_code( commands::ItemStatusCode::ISC_INVALID_DATA );
543 aStatus->set_error_message(
"sheet name can not be changed in a variant" );
548 aStatus->set_code( commands::ItemStatusCode::ISC_INVALID_DATA );
549 aStatus->set_error_message(
"sheet filename can not be changed in a variant" );
561 std::vector<wxString> obsolete;
563 for(
const auto& [
name, unused] : stored->m_Variants )
565 if( !requested.contains(
name ) )
566 obsolete.push_back(
name );
569 for(
const wxString&
name : obsolete )
578 const kiapi::schematic::types::SheetSymbol& aInput,
581 wxString pageNumber = wxString::FromUTF8( aInput.page_number() );
584 path.push_back( aSheet );
588 path.SetPageNumber( pageNumber.IsEmpty() ?
path.GetPageNumber() : pageNumber );
591 if( aInput.has_variants() && !
applySheetVariants( aStatus, aSheet, aInput.variants(), aParentPath, aSchematic ) )
598tl::expected<bool, ApiResponseStatus>
UnpackSheet(
SCH_SHEET* aOutput,
const kiapi::schematic::types::SheetSymbol& aInput )
600 google::protobuf::Any
any;
601 any.PackFrom( aInput );
606 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
607 e.set_error_message(
"could not unpack SCH_SHEET from SheetSymbol in request" );
608 return tl::unexpected( e );
622tl::expected<SCH_FOCUS_TARGET, ApiResponseStatus>
626 return tl::unexpected(
MakeResponseStatus( AS_BAD_REQUEST,
"no items were given to focus on" ) );
631 std::optional<SCH_SHEET_PATH> sheet;
639 return tl::unexpected(
MakeResponseStatus( AS_BAD_REQUEST,
"the requested sheet path is not valid for "
640 "this schematic" ) );
644 std::optional<BOX2I> bbox;
645 std::vector<std::string> missing;
647 for(
const KIID&
id : aIds )
650 SCH_ITEM* item = aSheetPath ? sheet->ResolveItem(
id ) : aSchematic.
ResolveItem(
id, &itemSheet,
true );
654 missing.push_back(
id.AsStdString() );
662 else if( !aSheetPath && itemSheet != *sheet )
664 return tl::unexpected(
MakeResponseStatus( AS_BAD_REQUEST,
"the items are not all on one sheet; set "
665 "the document's sheet path to choose the "
666 "sheet to focus" ) );
675 if( !missing.empty() )
677 return tl::unexpected(
MakeResponseStatus( AS_BAD_REQUEST, fmt::format(
"the items {} are not in the "
678 "requested document",
679 fmt::join( missing,
", " ) ) ) );
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
static void registerVariant(SCHEMATIC *aSchematic, const wxString &aName, const VariantProto &aInput)
Make the schematic aware of a variant a request named. Accepts either variant message.
static bool applySheetVariants(commands::ItemStatus *aStatus, SCH_SHEET *aSheet, const kiapi::schematic::types::SheetVariants &aInput, const SCH_SHEET_PATH &aParentPath, SCHEMATIC *aSchematic)
Make the set of variant records on one placement of a sheet match aInput, by name.
std::unique_ptr< EDA_ITEM > CreateItemForType(KICAD_T aType, EDA_ITEM *aContainer)
tl::expected< bool, ApiResponseStatus > UnpackSheet(SCH_SHEET *aOutput, const kiapi::schematic::types::SheetSymbol &aInput)
Unpack the every placement data from the input.
bool ApplySymbolInstance(kiapi::common::commands::ItemStatus *aStatus, SCH_SYMBOL *aSymbol, const kiapi::schematic::types::SchematicSymbolInstance &aInput, const SCH_SHEET_PATH &aPath, SCHEMATIC *aSchematic)
Apply placement-specific data to an aSymbol at aPath: reference, unit, and the per-placement attribut...
std::optional< wxString > FindVariantNoCase(const SCHEMATIC *aSchematic, const wxString &aName)
Variant names are stored and compared exactly, but SCHEMATIC::HasVariant matches case-insensitively.
bool PackSheet(kiapi::schematic::types::SheetSymbol *aOutput, const SCH_SHEET *aInput, const SCH_SHEET_PATH &aPath)
bool ApplySheetInstance(commands::ItemStatus *aStatus, SCH_SHEET *aSheet, const kiapi::schematic::types::SheetSymbol &aInput, const SCH_SHEET_PATH &aParentPath, SCHEMATIC *aSchematic)
bool PackSymbol(kiapi::schematic::types::SchematicSymbolInstance *aOutput, const SCH_SYMBOL *aInput, const SCH_SHEET_PATH &aPath)
static bool applySymbolVariants(kiapi::common::commands::ItemStatus *aStatus, SCH_SYMBOL *aSymbol, const kiapi::schematic::types::SchematicSymbolVariants &aInput, const SCH_SHEET_PATH &aPath, SCHEMATIC *aSchematic)
Make the set of variant records on one placement of a symbol match aInput, by name.
PIN_MAP_INSTANCE_OVERRIDE UnpackPinMapOverride(const kiapi::schematic::types::PinMapInstanceOverride &aInput)
void PackPinMapOverride(kiapi::schematic::types::PinMapInstanceOverride *aOutput, const PIN_MAP_INSTANCE_OVERRIDE &aOverride)
Pack/unpack a pin-to-pad map instance override to/from its protobuf form (issue #2282).
tl::expected< SCH_FOCUS_TARGET, ApiResponseStatus > ResolveFocusItems(SCHEMATIC &aSchematic, const std::vector< KIID > &aIds, const std::optional< KIID_PATH > &aSheetPath)
Resolve the items of a FocusOnItems request to the sheet that shows them and their combined bounding ...
bool UnpackSymbol(SCH_SYMBOL *aOutput, const kiapi::schematic::types::SchematicSymbolInstance &aInput)
Unpack the geometry, the library definition, fields, and the default-variant attributes that are shar...
A base class for most all the KiCad significant classes used in schematics and boards.
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
KICAD_T Type() const
Returns the type of object.
wxString AsString() const
Define a library symbol object.
Holds all the data relating to one schematic.
void AddVariant(const wxString &aVariantName)
SCH_ITEM * ResolveItem(const KIID &aID, SCH_SHEET_PATH *aPathOut=nullptr, bool aAllowNullptrReturn=false) const
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
bool HasHierarchy() const
Check if the hierarchy has been built.
void SetVariantDescription(const wxString &aVariantName, const wxString &aDescription)
Set the description for a variant.
const std::set< wxString > & GetVariantNames() const
Return the set of variant names (without the default placeholder).
Base class for any item which can be embedded within the SCHEMATIC container class,...
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
virtual void SetUnit(int aUnit)
std::optional< SCH_SHEET_PATH > GetSheetPathByKIIDPath(const KIID_PATH &aPath, bool aIncludeLastSheet=true) const
Finds a SCH_SHEET_PATH that matches the provided KIID_PATH.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false, bool aEscapeSheetNames=false) const
Return the sheet path in a human readable form made from the sheet names.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
void ClearVariantField(const KIID_PATH &aPath, const wxString &aVariantName, const wxString &aFieldName)
void SetFieldText(SCH_FIELD *aField, const wxString &aFieldName, const wxString &aFieldText, const SCH_SHEET_PATH *aPath, const wxString &aVariantName)
void SetExcludedFromSim(bool aExcludeFromSim, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from simulation flag.
const SCH_SHEET_INSTANCE * GetInstance(const KIID_PATH &aParentPath) const
Return this sheet's placement record under aParentPath, or nullptr if it has none.
void SetDNP(bool aDNP, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
void SetExcludedFromBOM(bool aExcludeFromBOM, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from schematic bill of materials flag.
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
void DeleteVariant(const KIID_PATH &aPath, const wxString &aVariantName)
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool GetExcludedFromSim(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
void SetFieldText(SCH_FIELD *aField, const wxString &aFieldName, const wxString &aFieldText, const SCH_SHEET_PATH *aPath, const wxString &aVariantName)
virtual void SetDNP(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool ClearVariantSymbolOverride(const SCH_SHEET_PATH &aPath, const wxString &aVariantName)
Remove the alternate-symbol override for a given variant and sheet instance.
void SetVariantSymbolOverride(const SCH_SHEET_PATH &aPath, const wxString &aVariantName, const LIB_ID &aLibId)
Set the alternate-symbol override for a given variant and sheet instance.
void SetExcludedFromSim(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from simulation flag.
PIN_MAP_INSTANCE_OVERRIDE GetPinMapOverride(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
bool GetExcludedFromBOM(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
void SetPinMapOverride(const PIN_MAP_INSTANCE_OVERRIDE &aOverride, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString)
Set the per-instance pin-to-pad map override (issue #2282).
void ClearVariantField(const KIID_PATH &aPath, const wxString &aVariantName, const wxString &aFieldName)
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
void AddHierarchicalReference(const KIID_PATH &aPath, const wxString &aRef, int aUnit)
Add a full hierarchical reference to this symbol.
void SetExcludedFromPosFiles(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
bool GetExcludedFromPosFiles(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
bool GetInstance(SCH_SYMBOL_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
void SetExcludedFromBOM(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from schematic bill of materials flag.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
void DeleteVariant(const KIID_PATH &aPath, const wxString &aVariantName)
virtual bool GetDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Set or clear the 'Do Not Populate' flag.
void SetExcludedFromBoard(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
A type-safe container of any type.
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
KICOMMON_API ApiResponseStatus MakeResponseStatus(ApiStatusCode aCode, const std::string &aMessage)
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)
Class to handle a set of SCH_ITEMs.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
One symbol-pin to footprint-pad mapping inside a PIN_MAP.
Per-instance override of the active pin map and a sparse delta on top.
std::vector< PIN_MAP_ENTRY > m_Edits
PIN_MAP_OVERRIDE_MODE m_Mode
A simple container for sheet instance information.
std::map< wxString, SCH_SHEET_VARIANT > m_Variants
A list of sheet variants.
A simple container for schematic symbol instance information.
std::map< wxString, SCH_SYMBOL_VARIANT > m_Variants
A list of symbol variants.
@ REFERENCE
Field Reference of part, i.e. "IC21".
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.